From 99b53c056b309b935c8eb23a0ddf244bd4f4df96 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期日, 01 九月 2024 21:54:32 +0800
Subject: [PATCH] 报备功能优化

---
 ycl-server/src/main/java/com/ycl/platform/service/impl/ReportServiceImpl.java |  177 ++++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 134 insertions(+), 43 deletions(-)

diff --git a/ycl-server/src/main/java/com/ycl/platform/service/impl/ReportServiceImpl.java b/ycl-server/src/main/java/com/ycl/platform/service/impl/ReportServiceImpl.java
index 2ccf8e2..7ad031e 100644
--- a/ycl-server/src/main/java/com/ycl/platform/service/impl/ReportServiceImpl.java
+++ b/ycl-server/src/main/java/com/ycl/platform/service/impl/ReportServiceImpl.java
@@ -2,27 +2,32 @@
 
 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.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;
@@ -31,9 +36,9 @@
 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.Map;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
@@ -50,6 +55,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,6 +65,7 @@
      * @return
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Result add(ReportForm form) {
         if (Objects.isNull(form.getPointId())) {
             throw new RuntimeException("鐐逛綅涓嶈兘涓虹┖");
@@ -65,18 +74,37 @@
         YwPeople people = new LambdaQueryChainWrapper<>(peopleMapper).eq(YwPeople::getUserId, userId).one();
         form.setPeopleId(people.getId());
         form.setUnitId(people.getBelongUnit());
+        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("娣诲姞澶辫触");
+        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,9 +118,6 @@
     @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());
@@ -101,22 +126,43 @@
         ExcelUtil<ReportImportDTO> excelUtil = new ExcelUtil<>(ReportImportDTO.class);
         List<ReportImportDTO> list = excelUtil.importExcel(form.getImportPointId().getInputStream());
         // 鎵归噺鎻掑叆
-        ArrayList<Report> reports = new ArrayList<>();
-        long l = System.currentTimeMillis();
-        list.forEach( item -> {
+        String pid = IdUtils.randomNO();
+        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, form.getPointId())
+                    .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("鎴愬姛瀵煎叆" + list.size() + "鏉℃暟鎹�");
+            Date now = DateUtils.getNowDate();
+            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);
     }
 
     /**
@@ -125,6 +171,7 @@
      * @return
      */
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Result update(ReportForm form) {
 
         Report entity = baseMapper.selectById(form.getId());
@@ -132,21 +179,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()) {
-            // 濡傛灉鏄湭瀹℃牳閫氳繃锛岃繘琛屼慨鏀癸紝閭d箞鐩存帴鏂板锛堜负浜嗕繚瀛樺鏍歌褰曪級
-            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("淇敼鎴愬姛");
     }
 
@@ -185,7 +247,21 @@
     public Result page(ReportQuery query) {
         IPage<ReportVO> page = PageUtil.getPage(query, ReportVO.class);
         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 +272,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);
     }
 
@@ -224,7 +306,6 @@
         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();
@@ -238,6 +319,7 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public Result auditing(ReportAuditingForm form) {
         Report report = baseMapper.selectById(form.getId());
         if (Objects.isNull(report)) {
@@ -248,9 +330,18 @@
         } else {
             report.setStatus(2);
         }
-        report.setAuditOpinion(form.getAuditOpinion());
-        report.setAuditingTime(new Date());
+        Date now = new Date();
+        report.setUpdateTime(now);
         baseMapper.updateById(report);
+
+        // 娣诲姞涓�鏉″鏍歌褰�
+        ReportAuditingRecord reportAuditingRecord = new ReportAuditingRecord();
+        reportAuditingRecord.setReportId(form.getId());
+        reportAuditingRecord.setDeleted(0);
+        reportAuditingRecord.setCreateTime(now);
+        reportAuditingRecord.setResultRemark(form.getAuditOpinion());
+        reportAuditingRecord.setResult(form.getAuditingResult());
+        reportAuditingRecordService.save(reportAuditingRecord);
         return Result.ok("鎿嶄綔鎴愬姛");
     }
 }

--
Gitblit v1.8.0