xiangpei
2024-09-09 f5e0ae6c3e2d4b7367389315bf24596446280430
ycl-server/src/main/java/com/ycl/platform/service/impl/ReportServiceImpl.java
@@ -2,27 +2,33 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.entity.*;
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.domain.vo.YwPointVO;
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.ReportAuditingRecordService;
import com.ycl.platform.service.ReportErrorTypeService;
import com.ycl.platform.service.ReportService;
import com.ycl.system.Result;
import com.ycl.system.entity.SysDictData;
import com.ycl.system.page.PageUtil;
import com.ycl.system.service.ISysDictTypeService;
import com.ycl.utils.DateUtils;
import com.ycl.utils.SecurityUtils;
import com.ycl.utils.StringUtils;
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;
@@ -30,11 +36,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -50,6 +54,9 @@
    private final YwUnitMapper unitMapper;
    private final YwPointMapper ywpointMapper;
    private final YwPeopleMapper peopleMapper;
    private final ReportErrorTypeService reportErrorTypeService;
    private final ISysDictTypeService dictTypeService;
    private final ReportAuditingRecordService reportAuditingRecordService;
    /**
     * 添加
@@ -57,26 +64,47 @@
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    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());
        Integer unitId = SecurityUtils.getUnitId();
        if (Objects.isNull(unitId)) {
            throw new RuntimeException("非运维单位暂时无法报备");
        }
        form.setUnitId(unitId);
        if ("事前报备".equals(form.getReportType())) {
            // 如果是事前报备需要检验是否已经生成下发工单
            YwPointVO point = ywpointMapper.countNotFinishedWorkOrderByGb(form.getPointId());
            if (Objects.nonNull(point)) {
                throw new RuntimeException(String.format("点位【%s】已经存在下发工单,无法事前报备", point.getPointName()));
            }
        }
        YwPoint point = new LambdaQueryChainWrapper<>(ywpointMapper)
                .eq(YwPoint::getSerialNumber, form.getPointId())
                .one();
        if (Objects.isNull(point)) {
            throw new RuntimeException("点位不存在");
        }
        Report entity = ReportForm.getEntityByForm(form, null);
        entity.setSerialNumber(form.getPointId());
        entity.setErrorType(String.join(",", form.getErrorType()));
        entity.setSerialNumber(point.getSerialNumber());
        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("添加失败");
        entity.setIdentify(IdUtils.randomNO(now));
        baseMapper.insert(entity);
        List<ReportErrorType> errorTypeList = form.getErrorTypeList().stream().map(item -> {
            ReportErrorType reportErrorType = new ReportErrorType();
            reportErrorType.setErrorType(item);
            reportErrorType.setDeleted(0);
            reportErrorType.setCreateTime(now);
            reportErrorType.setReportId(entity.getId());
            return reportErrorType;
        }).collect(Collectors.toList());
        reportErrorTypeService.saveBatch(errorTypeList);
        return Result.ok("报备成功");
    }
    @Override
@@ -90,33 +118,60 @@
    @SneakyThrows
    @Transactional
    public Result importData(ReportForm form) {
        if (Objects.isNull(form.getImportPointId())) {
            throw new RuntimeException("点位不能为空");
        Integer unitId = SecurityUtils.getUnitId();
        if (Objects.isNull(unitId)) {
            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());
        form.setUnitId(unitId);
        form.setBeginCreateTime(DateUtils.parseDate(form.getBeginCreateTimeStr()));
        form.setEndCreateTime(DateUtils.parseDate(form.getEndCreateTimeStr()));
        // 读取excel数据
        ExcelUtil<ReportImportDTO> excelUtil = new ExcelUtil<>(ReportImportDTO.class);
        List<ReportImportDTO> list = excelUtil.importExcel(form.getImportPointId().getInputStream());
        Date now = DateUtils.getNowDate();
        // 批量插入
        ArrayList<Report> reports = new ArrayList<>();
        long l = System.currentTimeMillis();
        list.forEach( item -> {
        String pid = IdUtils.randomNO(now);
        Integer success = 0;
        for (ReportImportDTO item : list) {
            if ("事前报备".equals(form.getReportType())) {
                // 如果是事前报备需要检验是否已经生成下发工单
                YwPointVO point = ywpointMapper.countNotFinishedWorkOrderByGb(item.getSerialNumber());
                if (Objects.nonNull(point)) {
                    throw new RuntimeException(String.format("点位【%s】已经存在下发工单,无法事前报备", point.getPointName()));
                }
            }
            YwPoint point = new LambdaQueryChainWrapper<>(ywpointMapper)
                    .eq(YwPoint::getSerialNumber, item.getSerialNumber())
                    .one();
            if (Objects.isNull(point)) {
                continue;
            }
            Report entity = ReportForm.getEntityByForm(form, null);
            entity.setImportBatchNumber(String.valueOf(l));
            entity.setImportBatchNumber(pid);
            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("导入成功");
            entity.setIdentify(IdUtils.randomNO(now));
            entity.setCreateTime(now);
            entity.setUpdateTime(now);
            baseMapper.insert(entity);
            List<ReportErrorType> errorTypeList = form.getErrorTypeList().stream().map(err -> {
                ReportErrorType reportErrorType = new ReportErrorType();
                reportErrorType.setErrorType(err);
                reportErrorType.setDeleted(0);
                reportErrorType.setCreateTime(now);
                reportErrorType.setReportId(entity.getId());
                return reportErrorType;
            }).collect(Collectors.toList());
            reportErrorTypeService.saveBatch(errorTypeList);
            success++;
        }
        return Result.error("添加失败");
        return Result.ok("报备点位数:" + list.size() + ",导入成功数:" + success);
    }
    @Override
    public Result getTogether(String pid) {
        List<Report> list = baseMapper.getTogether(pid);
        return Result.ok().data(list);
    }
    /**
@@ -125,6 +180,7 @@
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(ReportForm form) {
        Report entity = baseMapper.selectById(form.getId());
@@ -132,21 +188,36 @@
        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);
        if ("事前报备".equals(form.getReportType())) {
            // 如果是事前报备需要检验是否已经生成下发工单
            YwPointVO point = ywpointMapper.countNotFinishedWorkOrderByGb(form.getPointId());
            if (Objects.nonNull(point)) {
                throw new RuntimeException(String.format("点位【%s】已经存在下发工单,无法事前报备", point.getPointName()));
            }
        }
        YwPoint point = new LambdaQueryChainWrapper<>(ywpointMapper)
                .eq(YwPoint::getSerialNumber, form.getPointId())
                .one();
        if (Objects.isNull(point)) {
            throw new RuntimeException("点位不存在");
        }
        BeanUtils.copyProperties(form, entity);
        entity.setSerialNumber(form.getPointId());
        // 重置为待审核
        entity.setStatus(0);
        baseMapper.updateById(entity);
        new LambdaUpdateChainWrapper<>(reportErrorTypeService.getBaseMapper())
                .eq(ReportErrorType::getReportId, form.getId())
                .remove();
        List<ReportErrorType> errorTypeList = form.getErrorTypeList().stream().map(err -> {
            ReportErrorType reportErrorType = new ReportErrorType();
            reportErrorType.setErrorType(err);
            reportErrorType.setDeleted(0);
            reportErrorType.setCreateTime(now);
            reportErrorType.setReportId(entity.getId());
            return reportErrorType;
        }).collect(Collectors.toList());
        reportErrorTypeService.saveBatch(errorTypeList);
        return Result.ok("修改成功");
    }
@@ -184,8 +255,23 @@
    @Override
    public Result page(ReportQuery query) {
        IPage<ReportVO> page = PageUtil.getPage(query, ReportVO.class);
        query.setUnitId(SecurityUtils.getUnitId());
        baseMapper.page(page, query);
        page.getRecords().forEach(item -> item.setErrorType(ErrorTypeEnum.getEnumValue(item.getErrorType())));
        List<SysDictData> errorTypeList = dictTypeService.selectDictDataByType("report_error_type");
        Map<String, String> dictMap = errorTypeList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        page.getRecords().stream().forEach(item -> {
            if (StringUtils.hasText(item.getErrorType())) {
                item.setErrorTypeList(List.of(item.getErrorType().split(",")));
                StringBuilder sb = new StringBuilder();
                item.getErrorTypeList().stream().forEach(err -> {
                    String s = dictMap.get(err);
                    if (org.springframework.util.StringUtils.hasText(s)) {
                        sb.append(s).append("、");
                    }
                });
                item.setErrorType(sb.substring(0, sb.length() - 1));
            }
        });
        return Result.ok().data(page).total(page.getTotal());
    }
@@ -196,10 +282,16 @@
     */
    @Override
    public Result detail(String id) {
        Report entity = baseMapper.selectById(id);
        Assert.notNull(entity, "记录不存在");
        ReportVO vo = ReportVO.getVoByEntity(entity, null);
        vo.setPointId(vo.getSerialNumber());
        List<ReportErrorType> errList = new LambdaQueryChainWrapper<>(reportErrorTypeService.getBaseMapper())
                .select(ReportErrorType::getErrorType)
                .eq(ReportErrorType::getReportId, id)
                .list();
        List<String> list = errList.stream().map(ReportErrorType::getErrorType).collect(Collectors.toList());
        vo.setErrorTypeList(list);
        return Result.ok().data(vo);
    }
@@ -222,9 +314,35 @@
    public List<ReportVO> export(ReportQuery query) {
        IPage<ReportVO> page = PageUtil.getPage(query, ReportVO.class);
        page.setSize(-1);
        query.setUnitId(SecurityUtils.getUnitId());
        baseMapper.page(page, query);
        List<SysDictData> errorTypeList = dictTypeService.selectDictDataByType("report_error_type");
        Map<String, String> dictMap = errorTypeList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        page.getRecords().forEach(item -> {
            item.setErrorType(ErrorTypeEnum.getEnumValue(item.getErrorType()));
            if (StringUtils.hasText(item.getErrorType())) {
                item.setErrorTypeList(List.of(item.getErrorType().split(",")));
                StringBuilder sb = new StringBuilder();
                item.getErrorTypeList().stream().forEach(err -> {
                    String s = dictMap.get(err);
                    if (org.springframework.util.StringUtils.hasText(s)) {
                        sb.append(s).append("、");
                    }
                });
                item.setErrorType(sb.substring(0, sb.length() - 1));
            }
            // 审核结果
            List<ReportAuditingRecord> records = new LambdaQueryChainWrapper<>(reportAuditingRecordService.getBaseMapper())
                    .eq(ReportAuditingRecord::getReportId, item.getId())
                    .orderByDesc(ReportAuditingRecord::getCreateTime)
                    .last("limit 1")
                    .list();
            if (! CollectionUtils.isEmpty(records)) {
                item.setResultStr(records.get(0).getResult() ? "通过" : "未通过");
                item.setResultRemark(records.get(0).getResultRemark());
                item.setAuditingTime(records.get(0).getCreateTime());
            } else {
                item.setResultStr("审核中");
            }
            item.setReportContent(EscapeUtil.clean(item.getReportContent()));
        });
        return page.getRecords();
@@ -238,19 +356,49 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result auditing(ReportAuditingForm form) {
        Report report = baseMapper.selectById(form.getId());
        if (Objects.isNull(report)) {
            throw new RuntimeException("审核的报备不存在");
        if (CollectionUtils.isEmpty(form.getTogetherList())) {
            form.setTogetherList(new ArrayList<>(2));
        }
        if (form.getAuditingResult()) {
            report.setStatus(1);
        } else {
            report.setStatus(2);
        form.getTogetherList().add(form.getId());
        for (Integer id : form.getTogetherList()) {
            Report report = baseMapper.selectById(id);
            if (Objects.isNull(report)) {
                continue;
            }
            if (form.getAuditingResult()) {
                report.setStatus(1);
            } else {
                report.setStatus(2);
            }
            Date now = new Date();
            report.setUpdateTime(now);
            baseMapper.updateById(report);
            // 添加一条审核记录
            ReportAuditingRecord reportAuditingRecord = new ReportAuditingRecord();
            reportAuditingRecord.setReportId(id);
            reportAuditingRecord.setDeleted(0);
            reportAuditingRecord.setCreateTime(now);
            reportAuditingRecord.setResultRemark(form.getAuditOpinion());
            reportAuditingRecord.setResult(form.getAuditingResult());
            reportAuditingRecordService.save(reportAuditingRecord);
        }
        report.setAuditOpinion(form.getAuditOpinion());
        report.setAuditingTime(new Date());
        baseMapper.updateById(report);
        return Result.ok("操作成功");
    }
    @Override
    public Result getListByGb(String gb) {
        List<ReportVO> list = baseMapper.getListByGb(gb);
        for (ReportVO report : list) {
            List<ReportErrorType> errors = new LambdaQueryChainWrapper<>(reportErrorTypeService.getBaseMapper())
                    .eq(ReportErrorType::getReportId, report.getId())
                    .orderByDesc(ReportErrorType::getCreateTime)
                    .list();
            String err = errors.stream().map(ReportErrorType::getErrorType).collect(Collectors.joining(","));
            report.setErrorType(err);
        }
        return Result.ok().data(list);
    }
}