From 6ab1565a1a86623af925719935d605e875d98af4 Mon Sep 17 00:00:00 2001
From: 吴羡 <16976746+Xian-TT3@user.noreply.gitee.com>
Date: 星期五, 23 十月 2026 18:08:56 +0800
Subject: [PATCH] 设备核查计划:设备档案变更即时登记计划明细,提前核查复用待核查明细并回写档案,已完成记录不可覆盖;新增附件字段、明细固定排序与删除保护;人员查询新增包含admin的类型;检验保存/提交按页面模板校验,复核驳回按检验室删除复核人

---
 cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceExaminePlanServiceImpl.java |  156 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 143 insertions(+), 13 deletions(-)

diff --git a/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceExaminePlanServiceImpl.java b/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceExaminePlanServiceImpl.java
index 7ef252c..a8b3ace 100644
--- a/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceExaminePlanServiceImpl.java
+++ b/cnas-device/src/main/java/com/ruoyi/device/service/impl/DeviceExaminePlanServiceImpl.java
@@ -22,10 +22,14 @@
 import com.ruoyi.device.excel.upload.DeviceExaminePlanUpload;
 import com.ruoyi.device.mapper.DeviceExaminePlanDetailsMapper;
 import com.ruoyi.device.mapper.DeviceExaminePlanMapper;
+import com.ruoyi.device.mapper.DeviceExamineRecordContrastMapper;
+import com.ruoyi.device.mapper.DeviceExamineRecordMapper;
 import com.ruoyi.device.mapper.DeviceMapper;
 import com.ruoyi.device.pojo.Device;
 import com.ruoyi.device.pojo.DeviceExaminePlan;
 import com.ruoyi.device.pojo.DeviceExaminePlanDetails;
+import com.ruoyi.device.pojo.DeviceExamineRecord;
+import com.ruoyi.device.pojo.DeviceExamineRecordContrast;
 import com.ruoyi.device.service.DeviceExaminePlanDetailsService;
 import com.ruoyi.device.service.DeviceExaminePlanService;
 import com.ruoyi.framework.exception.ErrorException;
@@ -49,6 +53,7 @@
 import java.time.LocalDateTime;
 import java.time.YearMonth;
 import java.time.format.DateTimeFormatter;
+import java.time.temporal.ChronoUnit;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -77,6 +82,10 @@
     private DeviceMapper deviceMapper;
     @Resource
     private UserMapper userMapper;
+    @Resource
+    private DeviceExamineRecordMapper deviceExamineRecordMapper;
+    @Resource
+    private DeviceExamineRecordContrastMapper deviceExamineRecordContrastMapper;
 
     /**
      * 鏂板璁惧鏍告煡璁″垝
@@ -381,15 +390,15 @@
     }
 
     /**
-     * 璁惧妗f鍙樻洿鍚庯紝鎶婅澶囧悓姝ュ埌瀵瑰簲骞翠唤鐨勮澶囨牳鏌ヨ鍒掓槑缁�
+     * 璁惧妗f鍙樻洿鍚庯紝绔嬪嵆鎶婅澶囩櫥璁板埌涓嬫鏍告煡鏃ユ湡鎵�鍦ㄥ勾浠界殑璁惧鏍告煡璁″垝鏄庣粏
      */
     @Override
     public void syncDeviceExamineDetail(Device device) {
-        LocalDate remind = calcExamineRemindDate(device);
-        if (remind == null || remind.isAfter(LocalDate.now())) {
+        LocalDate due = calcExamineDueDate(device);
+        if (due == null) {
             return;
         }
-        DeviceExaminePlan plan = getOrCreatePlanEntity(String.valueOf(remind.getYear()));
+        DeviceExaminePlan plan = getOrCreatePlanEntity(String.valueOf(due.getYear()));
         fillExamineDetailIfAbsent(plan, device);
     }
 
@@ -406,6 +415,7 @@
         if (device == null) {
             return;
         }
+        // 鎻愬墠鏍告煡鏃惰鍒掓湀浠藉彲鑳借繕娌″埌锛屽彇瀹為檯瀹屾垚褰撳ぉ锛涘惁鍒欐寜鏈疆璁″垝鏍告煡鏈堜唤
         LocalDate checkDate = parseCheckDate(detail.getCheckTime());
         if (checkDate.isAfter(LocalDate.now())) {
             checkDate = LocalDate.now();
@@ -423,6 +433,8 @@
             }
         }
         deviceMapper.updateById(device);
+        // 鏈疆鏍告煡瀹屾垚鍚庣珛鍗崇櫥璁颁笅涓�杞紝鍘嗗彶鏄庣粏鍜岃褰曚繚鎸佷笉鍔�
+        syncDeviceExamineDetail(device);
     }
 
     /**
@@ -445,7 +457,7 @@
     }
 
     /**
-     * 鎵弿鎵�鏈夎澶囷紝鎶婂凡鍒扮偣锛堜笅娆℃牳鏌ユ棩鏈熷墠1涓湀璧凤紝鍚凡杩囨湡锛夌殑璁惧琛ヨ繘鏈勾搴﹁鍒�
+     * 鎵弿鎵�鏈夎澶囷紝鎶婃湰骞村害鍙婂線骞撮�炬湡鏈牳鏌ョ殑璁惧琛ヨ繘璁″垝
      */
     private void ensureExamineDetails(DeviceExaminePlan plan) {
         List<Device> devices = deviceMapper.selectList(null);
@@ -455,9 +467,9 @@
     }
 
     /**
-     * 璁惧鎻愰啋璧风偣锛氫笅娆℃牳鏌ユ棩鏈熷墠1涓湀锛堟病濉笅娆℃棩鏈熸椂鐢ㄦ渶杩戞棩鏈�+鏍告煡鍛ㄦ湡鍏滃簳锛�
+     * 璁惧鏈疆搴旀牳鏌ユ棩鏈燂紙娌″~涓嬫鏃ユ湡鏃剁敤鏈�杩戞棩鏈�+鏍告煡鍛ㄦ湡鍏滃簳锛�
      */
-    private LocalDate calcExamineRemindDate(Device device) {
+    private LocalDate calcExamineDueDate(Device device) {
         LocalDateTime base = device.getNextExamineDate();
         if (base == null && device.getLastExamineDate() != null && StringUtils.isNotBlank(device.getExamineCycle())) {
             try {
@@ -465,21 +477,21 @@
             } catch (NumberFormatException ignored) {
             }
         }
-        return base == null ? null : base.toLocalDate().minusMonths(1);
+        return base == null ? null : base.toLocalDate();
     }
 
     /**
-     * 宸插埌鐐逛笖璁″垝涓皻鏃犺璁惧鏈疆鏄庣粏鏃惰嚜鍔ㄨˉ涓�鏉�
+     * 璁″垝涓皻鏃犺璁惧鏈疆鏄庣粏鏃惰嚜鍔ㄨˉ涓�鏉★紱鏍告煡鏃堕棿鍙栧簲鏍告煡鏈堜唤
      */
     private void fillExamineDetailIfAbsent(DeviceExaminePlan plan, Device device) {
-        LocalDate remind = calcExamineRemindDate(device);
-        if (remind == null || remind.isAfter(LocalDate.now())) {
+        LocalDate due = calcExamineDueDate(device);
+        if (due == null) {
             return;
         }
-        if (plan.getPlanYear() != null && String.valueOf(remind.getYear()).compareTo(plan.getPlanYear()) > 0) {
+        if (plan.getPlanYear() != null && String.valueOf(due.getYear()).compareTo(plan.getPlanYear()) > 0) {
             return;
         }
-        String checkTime = remind.format(DateTimeFormatter.ofPattern("yyyy-MM"));
+        String checkTime = due.format(DateTimeFormatter.ofPattern("yyyy-MM"));
         Long exist = deviceExaminePlanDetailsMapper.selectCount(Wrappers.<DeviceExaminePlanDetails>lambdaQuery()
                 .eq(DeviceExaminePlanDetails::getPlanId, plan.getPlanId())
                 .eq(DeviceExaminePlanDetails::getDeviceId, device.getId())
@@ -515,4 +527,122 @@
         }
         return LocalDate.now();
     }
+
+    /**
+     * 涓烘寚瀹氳澶囨壘涓�鏉″彲褰曞叆鏍告煡璁板綍鐨勮鍒掓槑缁嗭紱娌℃湁鍙敤鏄庣粏鏃跺湪褰撳墠骞村害璁″垝鏂板鏈疆鏄庣粏
+     */
+    @Override
+    public DeviceExaminePlanDetails resolveExaminePlanDetail(Integer deviceId, String checkTime) {
+        if (deviceId == null) {
+            throw new ErrorException("璇烽�夋嫨鏍告煡璁惧");
+        }
+        Device device = deviceMapper.selectById(deviceId);
+        if (device == null) {
+            throw new ErrorException("鏍告煡璁惧涓嶅瓨鍦�");
+        }
+        String checkMonth = normalizeCheckMonth(checkTime);
+        DeviceExaminePlan plan = getOrCreatePlanEntity(checkMonth.substring(0, 4));
+        List<DeviceExaminePlanDetails> details = deviceExaminePlanDetailsMapper.selectList(
+                Wrappers.<DeviceExaminePlanDetails>lambdaQuery()
+                        .eq(DeviceExaminePlanDetails::getDeviceId, deviceId)
+                        .orderByAsc(DeviceExaminePlanDetails::getCheckTime));
+        DeviceExaminePlanDetails nearest = null;
+        long nearestDistance = Long.MAX_VALUE;
+        for (DeviceExaminePlanDetails detail : details) {
+            // 宸叉湁鏍告煡璁板綍鐨勬槑缁嗕唬琛ㄥ凡瀹屾垚鐨勪竴杞紝鍚庣画鏍告煡姘歌繙涓嶅鐢ㄣ�佷笉瑕嗙洊
+            Long recordCount = deviceExamineRecordMapper.selectCount(Wrappers.<DeviceExamineRecord>lambdaQuery()
+                    .eq(DeviceExamineRecord::getPlanDetailsId, detail.getPlanDetailsId()));
+            if (recordCount != null && recordCount > 0) {
+                continue;
+            }
+            if (detail.getPlanId() != null && detail.getPlanId().equals(plan.getPlanId())) {
+                return detail;
+            }
+            long distance = monthDistance(detail.getCheckTime(), checkMonth);
+            if (distance < nearestDistance) {
+                nearest = detail;
+                nearestDistance = distance;
+            }
+        }
+        // 鎻愬墠鏍告煡鏃朵紭鍏堝鐢ㄧ郴缁熺櫥璁般�佸皻鏈牳鏌ョ殑杩欎竴杞紙鍙兘鍦ㄥ埆鐨勫勾浠借鍒掗噷锛�
+        if (nearest != null) {
+            return nearest;
+        }
+        DeviceExaminePlanDetails detail = new DeviceExaminePlanDetails();
+        detail.setPlanId(plan.getPlanId());
+        detail.setDeviceId(device.getId());
+        String deviceName = device.getDeviceName() == null ? "" : device.getDeviceName();
+        if (StringUtils.isNotBlank(device.getSpecificationModel())) {
+            deviceName = deviceName + " " + device.getSpecificationModel();
+        }
+        detail.setDeviceName(deviceName);
+        detail.setDeviceNumber(device.getManagementNumber());
+        detail.setCheckTime(checkMonth);
+        deviceExaminePlanDetailsMapper.insert(detail);
+        return detail;
+    }
+
+    /**
+     * 瑙勮寖鏍告煡鏈堜唤涓� yyyy-MM锛岄潪娉曟垨涓虹┖鏃跺彇褰撳墠鏈堜唤
+     */
+    private String normalizeCheckMonth(String checkTime) {
+        if (StringUtils.isNotBlank(checkTime)) {
+            String normalized = checkTime.trim().replace('.', '-').replace('/', '-');
+            if (normalized.length() >= 7) {
+                try {
+                    return YearMonth.parse(normalized.substring(0, 7)).toString();
+                } catch (Exception ignored) {
+                }
+            }
+        }
+        return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
+    }
+
+    /**
+     * 涓や釜鏈堜唤涔嬮棿鐩稿樊鐨勬湀鏁�
+     */
+    private long monthDistance(String fromCheckTime, String targetMonth) {
+        try {
+            YearMonth from = YearMonth.parse(normalizeCheckMonth(fromCheckTime));
+            YearMonth target = YearMonth.parse(targetMonth);
+            return Math.abs(ChronoUnit.MONTHS.between(from, target));
+        } catch (Exception e) {
+            return Long.MAX_VALUE;
+        }
+    }
+
+    /**
+     * 淇敼璁″垝鏄庣粏锛涙牳鏌ユ湀浠借法骞存椂锛屾妸鏄庣粏褰掑埌鏍告煡鏈堜唤鎵�灞炲勾浠界殑璁″垝涓�
+     */
+    @Override
+    public boolean updateExaminePlanDetail(DeviceExaminePlanDetails detail) {
+        if (detail == null || detail.getPlanDetailsId() == null) {
+            throw new ErrorException("缂哄皯璁惧鏍告煡璁″垝鏄庣粏id");
+        }
+        if (StringUtils.isNotBlank(detail.getCheckTime())) {
+            String checkMonth = normalizeCheckMonth(detail.getCheckTime());
+            detail.setCheckTime(checkMonth);
+            DeviceExaminePlan plan = getOrCreatePlanEntity(checkMonth.substring(0, 4));
+            detail.setPlanId(plan.getPlanId());
+        }
+        return deviceExaminePlanDetailsMapper.updateById(detail) > 0;
+    }
+
+    /**
+     * 鍒犻櫎璁″垝鏄庣粏锛涘凡鏈夋牳鏌�/姣斿璁板綍鐨勪笉鍏佽鍒犻櫎锛屼繚璇佸巻鍙茬暀妗�
+     */
+    @Override
+    public boolean removeExaminePlanDetail(Integer planDetailsId) {
+        if (planDetailsId == null) {
+            throw new ErrorException("缂哄皯璁惧鏍告煡璁″垝鏄庣粏id");
+        }
+        Long recordCount = deviceExamineRecordMapper.selectCount(Wrappers.<DeviceExamineRecord>lambdaQuery()
+                .eq(DeviceExamineRecord::getPlanDetailsId, planDetailsId));
+        Long contrastCount = deviceExamineRecordContrastMapper.selectCount(Wrappers.<DeviceExamineRecordContrast>lambdaQuery()
+                .eq(DeviceExamineRecordContrast::getPlanDetailsId, planDetailsId));
+        if ((recordCount != null && recordCount > 0) || (contrastCount != null && contrastCount > 0)) {
+            throw new ErrorException("璇ヨ鍒掑凡瀛樺湪鏍告煡璁板綍锛屼负淇濈暀鍘嗗彶璁板綍涓嶅厑璁稿垹闄�");
+        }
+        return deviceExaminePlanDetailsService.removeById(planDetailsId);
+    }
 }

--
Gitblit v1.9.3