package com.ycl.platform.service.impl; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ycl.platform.domain.dto.ReportImportDTO; import com.ycl.platform.domain.entity.Report; import com.ycl.platform.domain.entity.YwPeople; import com.ycl.platform.domain.form.ReportAuditingForm; import com.ycl.platform.domain.form.ReportForm; import com.ycl.platform.domain.query.ReportQuery; import com.ycl.platform.domain.vo.ReportVO; import com.ycl.platform.mapper.ReportMapper; import com.ycl.platform.mapper.YwPeopleMapper; import com.ycl.platform.mapper.YwPointMapper; import com.ycl.platform.mapper.YwUnitMapper; import com.ycl.platform.service.ReportService; import com.ycl.system.Result; import com.ycl.system.page.PageUtil; import com.ycl.utils.DateUtils; import com.ycl.utils.SecurityUtils; import com.ycl.utils.html.EscapeUtil; import com.ycl.utils.poi.ExcelUtil; import com.ycl.utils.uuid.IdUtils; import enumeration.general.ErrorTypeEnum; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 报备 服务实现类 * * @author xp * @since 2024-03-19 */ @Service @RequiredArgsConstructor public class ReportServiceImpl extends ServiceImpl implements ReportService { private final YwUnitMapper unitMapper; private final YwPointMapper ywpointMapper; private final YwPeopleMapper peopleMapper; /** * 添加 * @param form * @return */ @Override public Result add(ReportForm form) { if (Objects.isNull(form.getPointId())) { throw new RuntimeException("点位不能为空"); } Long userId = SecurityUtils.getUserId(); YwPeople people = new LambdaQueryChainWrapper<>(peopleMapper).eq(YwPeople::getUserId, userId).one(); form.setPeopleId(people.getId()); form.setUnitId(people.getBelongUnit()); Report entity = ReportForm.getEntityByForm(form, null); entity.setSerialNumber(form.getPointId()); entity.setErrorType(String.join(",", form.getErrorType())); entity.setStatus(0); entity.setIdentify(IdUtils.randomNO()); Date now = new Date(); entity.setCreateTime(now); entity.setUpdateTime(now); if(baseMapper.insert(entity) > 0) { return Result.ok("添加成功"); } return Result.error("添加失败"); } @Override @SneakyThrows public void importTemplate(HttpServletResponse response) { ExcelUtil excelUtil = new ExcelUtil<>(ReportImportDTO.class); excelUtil.exportExcel(response, null, "报备导入模板"); } @Override @SneakyThrows @Transactional public Result importData(ReportForm form) { if (Objects.isNull(form.getImportPointId())) { throw new RuntimeException("点位不能为空"); } Long userId = SecurityUtils.getUserId(); YwPeople people = new LambdaQueryChainWrapper<>(peopleMapper).eq(YwPeople::getUserId, userId).one(); form.setPeopleId(people.getId()); form.setUnitId(people.getBelongUnit()); // 读取excel数据 ExcelUtil excelUtil = new ExcelUtil<>(ReportImportDTO.class); List list = excelUtil.importExcel(form.getImportPointId().getInputStream()); // 批量插入 ArrayList reports = new ArrayList<>(); long l = System.currentTimeMillis(); list.forEach( item -> { Report entity = ReportForm.getEntityByForm(form, null); entity.setImportBatchNumber(String.valueOf(l)); entity.setSerialNumber(item.getSerialNumber()); entity.setStatus(0); entity.setIdentify(IdUtils.randomNO()); entity.setCreateTime(DateUtils.getNowDate()); entity.setUpdateTime(DateUtils.getNowDate()); reports.add(entity); }); if (saveBatch(reports)) { return Result.ok("成功导入" + list.size() + "条数据"); } return Result.error("导入失败"); } /** * 修改 * @param form * @return */ @Override public Result update(ReportForm form) { Report entity = baseMapper.selectById(form.getId()); // 为空抛IllegalArgumentException,做全局异常处理 Assert.notNull(entity, "记录不存在"); Date now = new Date(); entity.setUpdateTime(now); if (0 == entity.getStatus()) { // 待审核的直接改 BeanUtils.copyProperties(form, entity); baseMapper.updateById(entity); } else if (2 == entity.getStatus()) { // 如果是未审核通过,进行修改,那么直接新增(为了保存审核记录) Report report = new Report(); BeanUtils.copyProperties(entity, report); BeanUtils.copyProperties(form, report); report.setId(null); report.setStatus(0); report.setAuditingTime(null); report.setAuditOpinion(null); baseMapper.insert(report); } return Result.ok("修改成功"); } /** * 批量删除 * @param ids * @return */ @Override public Result remove(List ids) { if(baseMapper.deleteBatchIds(ids) > 0) { return Result.ok("删除成功"); } return Result.error("删除失败"); } /** * id删除 * @param id * @return */ @Override public Result removeById(String id) { if(baseMapper.deleteById(id) > 0) { return Result.ok("删除成功"); } return Result.error("删除失败"); } /** * 分页查询 * @param query * @return */ @Override public Result page(ReportQuery query) { IPage page = PageUtil.getPage(query, ReportVO.class); baseMapper.page(page, query); page.getRecords().forEach(item -> item.setErrorType(ErrorTypeEnum.getEnumValue(item.getErrorType()))); return Result.ok().data(page).total(page.getTotal()); } /** * 根据id查找 * @param id * @return */ @Override public Result detail(String id) { Report entity = baseMapper.selectById(id); Assert.notNull(entity, "记录不存在"); ReportVO vo = ReportVO.getVoByEntity(entity, null); return Result.ok().data(vo); } /** * 列表 * @return */ @Override public Result all() { List entities = baseMapper.selectList(null); List vos = entities.stream() .map( entity -> ReportVO.getVoByEntity(entity, null) ) .collect(Collectors.toList()); return Result.ok().data(vos); } @Override public List export(ReportQuery query) { IPage page = PageUtil.getPage(query, ReportVO.class); page.setSize(-1); baseMapper.page(page, query); page.getRecords().forEach(item -> { item.setErrorType(ErrorTypeEnum.getEnumValue(item.getErrorType())); item.setReportContent(EscapeUtil.clean(item.getReportContent())); }); return page.getRecords(); } @Override public Result auditingRecord(Integer id) { List reportList = baseMapper.examineRecord(id); return Result.ok().data(reportList); } @Override public Result auditing(ReportAuditingForm form) { Report report = baseMapper.selectById(form.getId()); if (Objects.isNull(report)) { throw new RuntimeException("审核的报备不存在"); } if (form.getAuditingResult()) { report.setStatus(1); } else { report.setStatus(2); } report.setAuditOpinion(form.getAuditOpinion()); report.setAuditingTime(new Date()); baseMapper.updateById(report); return Result.ok("操作成功"); } }