From d2f07163fc4074f335986dd51eedbdc7be9e19ea Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期四, 30 七月 2026 11:52:47 +0800
Subject: [PATCH] fix: 线路巡检调整

---
 src/main/java/com/ruoyi/safe/service/impl/SafeLineInspectionHazardServiceImpl.java |  160 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 154 insertions(+), 6 deletions(-)

diff --git a/src/main/java/com/ruoyi/safe/service/impl/SafeLineInspectionHazardServiceImpl.java b/src/main/java/com/ruoyi/safe/service/impl/SafeLineInspectionHazardServiceImpl.java
index 4b53b5c..a2d03e7 100644
--- a/src/main/java/com/ruoyi/safe/service/impl/SafeLineInspectionHazardServiceImpl.java
+++ b/src/main/java/com/ruoyi/safe/service/impl/SafeLineInspectionHazardServiceImpl.java
@@ -2,16 +2,27 @@
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.basic.dto.StorageBlobVO;
+import com.ruoyi.basic.enums.ApplicationTypeEnum;
+import com.ruoyi.basic.enums.RecordTypeEnum;
+import com.ruoyi.basic.utils.FileUtil;
 import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.mapper.SysUserMapper;
 import com.ruoyi.safe.mapper.SafeLineInspectionHazardMapper;
+import com.ruoyi.safe.mapper.SafeLineInspectionMapper;
+import com.ruoyi.safe.pojo.SafeHidden;
+import com.ruoyi.safe.pojo.SafeLineInspection;
 import com.ruoyi.safe.pojo.SafeLineInspectionHazard;
+import com.ruoyi.safe.service.SafeHiddenService;
 import com.ruoyi.safe.service.SafeLineInspectionHazardService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.Serializable;
+import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.util.*;
 import java.util.stream.Collectors;
@@ -29,17 +40,37 @@
 
     @Autowired
     private SysUserMapper sysUserMapper;
+    @Autowired
+    private SafeHiddenService safeHiddenService;
+    @Autowired
+    private SafeLineInspectionMapper safeLineInspectionMapper;
+    @Autowired
+    private FileUtil fileUtil;
 
     @Override
     public IPage<SafeLineInspectionHazard> pageSafeLineInspectionHazard(Page page, SafeLineInspectionHazard safeLineInspectionHazard) {
         IPage<SafeLineInspectionHazard> result = this.lambdaQuery()
                 .eq(safeLineInspectionHazard.getInspectionId() != null, SafeLineInspectionHazard::getInspectionId, safeLineInspectionHazard.getInspectionId())
-                .like(safeLineInspectionHazard.getHazardCode() != null, SafeLineInspectionHazard::getHazardCode, safeLineInspectionHazard.getHazardCode())
-                .eq(safeLineInspectionHazard.getStatus() != null, SafeLineInspectionHazard::getStatus, safeLineInspectionHazard.getStatus())
+                .like(StringUtils.isNotBlank(safeLineInspectionHazard.getHazardCode()), SafeLineInspectionHazard::getHazardCode, safeLineInspectionHazard.getHazardCode())
+                .like(StringUtils.isNotBlank(safeLineInspectionHazard.getHazardDesc()), SafeLineInspectionHazard::getHazardDesc, safeLineInspectionHazard.getHazardDesc())
+                .eq(StringUtils.isNotBlank(safeLineInspectionHazard.getStatus()), SafeLineInspectionHazard::getStatus, safeLineInspectionHazard.getStatus())
                 .orderByDesc(SafeLineInspectionHazard::getCreateTime)
                 .page(page);
         fillRectifyUserName(result.getRecords());
+        fillInspectionInfo(result.getRecords());
+        fillRectifyAttachments(result.getRecords());
         return result;
+    }
+
+    @Override
+    public SafeLineInspectionHazard getById(Serializable id) {
+        SafeLineInspectionHazard record = super.getById(id);
+        if (record != null) {
+            fillRectifyUserName(Collections.singletonList(record));
+            fillInspectionInfo(Collections.singletonList(record));
+            fillRectifyAttachments(Collections.singletonList(record));
+        }
+        return record;
     }
 
     @Override
@@ -49,6 +80,8 @@
                 .orderByDesc(SafeLineInspectionHazard::getCreateTime)
                 .list();
         fillRectifyUserName(records);
+        fillInspectionInfo(records);
+        fillRectifyAttachments(records);
         return records;
     }
 
@@ -62,12 +95,65 @@
 
     @Override
     public boolean updateById(SafeLineInspectionHazard entity) {
+        SafeLineInspectionHazard oldRecord = entity.getId() == null ? null : this.getById(entity.getId());
+        if (oldRecord == null) {
+            throw new IllegalArgumentException("鏁存敼璁板綍涓嶅瓨鍦�");
+        }
+        Integer safeHiddenId = entity.getSafeHiddenId() != null
+                ? entity.getSafeHiddenId()
+                : oldRecord.getSafeHiddenId();
         if ("宸叉暣鏀�".equals(entity.getStatus())) {
-            Long currentUserId = SecurityUtils.getUserId();
-            entity.setRectifyUserId(currentUserId.intValue());
+            validateRectify(entity, oldRecord);
+            entity.setRectifyUserId(oldRecord.getRectifyUserId());
             entity.setRectifyTime(LocalDateTime.now());
         }
-        return super.updateById(entity);
+        boolean updated = super.updateById(entity);
+        if (updated && "宸叉暣鏀�".equals(entity.getStatus())) {
+            syncRectifyAttachments(entity);
+        }
+        if (updated && "宸叉暣鏀�".equals(entity.getStatus()) && safeHiddenId != null) {
+            SafeHidden hidden = new SafeHidden();
+            hidden.setId(safeHiddenId);
+            hidden.setRectifyMeasures(entity.getRectifyDesc());
+            hidden.setRectifyActualTime(LocalDate.now());
+            safeHiddenService.updateById(hidden);
+        }
+        if (updated && "宸叉暣鏀�".equals(entity.getStatus())) {
+            Integer inspectionId = entity.getInspectionId() != null
+                    ? entity.getInspectionId()
+                    : oldRecord.getInspectionId();
+            completeInspectionIfAllRectified(inspectionId);
+        }
+        return updated;
+    }
+
+    private void validateRectify(SafeLineInspectionHazard entity, SafeLineInspectionHazard oldRecord) {
+        if ("宸叉暣鏀�".equals(oldRecord.getStatus())) {
+            throw new IllegalArgumentException("璇ラ殣鎮e凡鏁存敼锛岃鍕块噸澶嶆搷浣�");
+        }
+        Long currentUserId = SecurityUtils.getUserId();
+        if (oldRecord.getRectifyUserId() == null || currentUserId == null
+                || !oldRecord.getRectifyUserId().equals(currentUserId.intValue())) {
+            throw new IllegalArgumentException("鍙湁鏁存敼璐d换浜哄彲浠ヨ繘琛屾暣鏀�");
+        }
+        if (StringUtils.isBlank(entity.getRectifyDesc())) {
+            throw new IllegalArgumentException("璇疯緭鍏ユ暣鏀硅鏄�");
+        }
+        if (entity.getStorageBlobDTOs() == null || entity.getStorageBlobDTOs().isEmpty()) {
+            throw new IllegalArgumentException("璇蜂笂浼犳暣鏀瑰悗鐓х墖");
+        }
+    }
+
+    private void syncRectifyAttachments(SafeLineInspectionHazard entity) {
+        if (entity.getId() == null || entity.getStorageBlobDTOs() == null) {
+            return;
+        }
+        fileUtil.saveStorageAttachment(
+                ApplicationTypeEnum.IMAGE,
+                RecordTypeEnum.SAFE_LINE_INSPECTION_HAZARD,
+                entity.getId().longValue(),
+                entity.getStorageBlobDTOs()
+        );
     }
 
     private String generateHazardCode() {
@@ -96,4 +182,66 @@
             }
         });
     }
+
+    private void fillInspectionInfo(List<SafeLineInspectionHazard> records) {
+        if (records == null || records.isEmpty()) {
+            return;
+        }
+        List<Integer> inspectionIds = records.stream()
+                .map(SafeLineInspectionHazard::getInspectionId)
+                .filter(Objects::nonNull)
+                .distinct()
+                .collect(Collectors.toList());
+        if (inspectionIds.isEmpty()) {
+            return;
+        }
+        Map<Integer, SafeLineInspection> inspectionMap = safeLineInspectionMapper.selectBatchIds(inspectionIds).stream()
+                .collect(Collectors.toMap(SafeLineInspection::getId, item -> item, (v1, v2) -> v1));
+        records.forEach(record -> {
+            SafeLineInspection inspection = inspectionMap.get(record.getInspectionId());
+            if (inspection != null) {
+                record.setInspectionCode(inspection.getInspectionCode());
+                record.setInspectionName(inspection.getInspectionName());
+                record.setLineName(inspection.getLineName());
+            }
+        });
+    }
+
+    private void fillRectifyAttachments(List<SafeLineInspectionHazard> records) {
+        if (records == null || records.isEmpty()) {
+            return;
+        }
+        records.forEach(record -> {
+            if (record.getId() == null) {
+                return;
+            }
+            List<StorageBlobVO> attachments = fileUtil.getStorageBlobVOsByRecordTypeAndRecordId(
+                    RecordTypeEnum.SAFE_LINE_INSPECTION_HAZARD,
+                    record.getId().longValue()
+            );
+            record.setStorageBlobVOs(attachments == null ? Collections.emptyList() : attachments);
+        });
+    }
+
+    private void completeInspectionIfAllRectified(Integer inspectionId) {
+        if (inspectionId == null) {
+            return;
+        }
+        long unrectifiedCount = this.lambdaQuery()
+                .eq(SafeLineInspectionHazard::getInspectionId, inspectionId)
+                .ne(SafeLineInspectionHazard::getStatus, "宸叉暣鏀�")
+                .count();
+        if (unrectifiedCount > 0) {
+            return;
+        }
+        SafeLineInspection inspection = safeLineInspectionMapper.selectById(inspectionId);
+        if (inspection == null || "宸插畬鎴�".equals(inspection.getStatus())) {
+            return;
+        }
+        Long currentUserId = SecurityUtils.getUserId();
+        inspection.setStatus("宸插畬鎴�");
+        inspection.setUpdateTime(LocalDateTime.now());
+        inspection.setUpdateUser(currentUserId == null ? null : currentUserId.intValue());
+        safeLineInspectionMapper.updateById(inspection);
+    }
 }

--
Gitblit v1.9.3