From 5fac1dbc81262639bb7bfac11e2d7b56795f6d44 Mon Sep 17 00:00:00 2001
From: baoshiwei <baoshiwei@shlanbao.cn>
Date: 星期四, 29 五月 2025 11:39:22 +0800
Subject: [PATCH] feat(eims): 优化保养计划导入功能

---
 eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java |  247 ++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 204 insertions(+), 43 deletions(-)

diff --git a/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java b/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java
index 8cbc9af..1343ecc 100644
--- a/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java
+++ b/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java
@@ -1,8 +1,8 @@
 package org.dromara.eims.service.impl;
 
+import com.alibaba.excel.EasyExcel;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import lombok.SneakyThrows;
-import org.dromara.common.core.constant.DictConstants;
+import org.dromara.common.core.exception.ServiceException;
 import org.dromara.common.core.utils.DateUtils;
 import org.dromara.common.core.utils.MapstructUtils;
 import org.dromara.common.core.utils.StringUtils;
@@ -12,7 +12,12 @@
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import lombok.RequiredArgsConstructor;
-import org.dromara.eims.domain.vo.EimsRepairResVo;
+import org.dromara.eims.domain.EimsEqu;
+import org.dromara.eims.domain.vo.EimsEquVo;
+import org.dromara.eims.domain.vo.MaintCheckItemVo;
+import org.dromara.eims.listener.EasyExcelCellListener;
+import org.dromara.eims.listener.MaintCheckItemImportListener;
+import org.dromara.eims.mapper.EimsEquMapper;
 import org.dromara.system.domain.SysDept;
 import org.dromara.system.domain.vo.SysDeptVo;
 import org.dromara.system.mapper.SysDeptMapper;
@@ -22,7 +27,9 @@
 import org.dromara.eims.domain.EimsMaintPlan;
 import org.dromara.eims.mapper.EimsMaintPlanMapper;
 import org.dromara.eims.service.IEimsMaintPlanService;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.IOException;
 import java.util.*;
 
 /**
@@ -37,6 +44,7 @@
 
     private final EimsMaintPlanMapper baseMapper;
     private final SysDeptMapper sysDeptMapper;
+    private final EimsEquMapper equMapper;
 
     /**
      * 鏌ヨ淇濆吇璁″垝
@@ -91,6 +99,8 @@
         lqw.eq(bo.getMaintUser() != null, EimsMaintPlan::getMaintUser, bo.getMaintUser());
         lqw.eq(bo.getMaintDept() != null, EimsMaintPlan::getMaintDept, bo.getMaintDept());
         lqw.eq(bo.getStatus() != null, EimsMaintPlan::getStatus, bo.getStatus());
+        // 鎸夊垱寤烘椂闂村�掑簭
+        lqw.orderByDesc(EimsMaintPlan::getCreateTime);
         return lqw;
     }
 
@@ -106,6 +116,7 @@
         qw.eq(bo.getMaintUser() != null, "mp.maint_user", bo.getMaintUser());
         qw.in(bo.getMaintDept() != null, "mp.maint_dept", getAllDescendantIds(bo.getMaintDept()));
         qw.eq(bo.getStatus() != null, "mp.status", bo.getStatus());
+        qw.orderByDesc("mp.create_time");
         return qw;
     }
 
@@ -144,7 +155,7 @@
      */
     @Override
     public Boolean insertByBo(EimsMaintPlanBo bo) {
-        setMaintNextTime(bo);
+        //setMaintNextTime(bo);
         EimsMaintPlan add = MapstructUtils.convert(bo, EimsMaintPlan.class);
         validEntityBeforeSave(add);
         boolean flag = baseMapper.insert(add) > 0;
@@ -154,46 +165,7 @@
         return flag;
     }
 
-    /**
-     * 璁$畻淇濆吇璁″垝涓嬫鎵ц鏃堕棿
-     */
-    @SneakyThrows
-    private void setMaintNextTime(EimsMaintPlanBo bo) {
-        Date maintFirstTime = bo.getMaintFirstTime();
-        Date maintLastTime = bo.getMaintLastTime();
-        //涓嬫淇濆吇鏃堕棿璁$畻瑙勫垯 0-鎸夊浐瀹氬懆鏈�  1-鎸変笂娆′繚鍏绘椂闂�
-        Date nextDate = (maintLastTime != null && bo.getMaintRule().equals(DictConstants.MAINT_TIME_RULE_DETAIL.LAST)) ? maintLastTime :maintFirstTime;
 
-        //棣栨鎵ц鏃堕棿涓虹┖鎶涘嚭寮傚父
-        if (maintFirstTime == null) {
-            throw new Exception("棣栨鎵ц鏃堕棿涓嶈兘涓虹┖!");
-        }
-        //鍛ㄦ湡
-        Long maintCycle = bo.getMaintCycle();
-        //鍗曚綅 1-澶� 2-鍛� 3-鏈� 4-瀛� 5-骞�
-        String maintCycleUnit = bo.getMaintCycleUnit();
-        switch (maintCycleUnit) {
-            case "1":
-                nextDate = DateUtils.addDays(nextDate, maintCycle.intValue());
-                break;
-            case "2":
-                nextDate = DateUtils.addWeeks(nextDate, maintCycle.intValue());
-                break;
-            case "3":
-                nextDate = DateUtils.addMonths(nextDate, maintCycle.intValue());
-                break;
-            case "4":
-                nextDate = DateUtils.addMonths(nextDate, maintCycle.intValue() * 3);
-                break;
-            case "5":
-                nextDate = DateUtils.addYears(nextDate, maintCycle.intValue());
-                break;
-
-        }
-        bo.setMaintNextTime(nextDate);
-
-
-    }
 
     /**
      * 淇敼淇濆吇璁″垝
@@ -232,4 +204,193 @@
     }
 
 
+    public String importData(MultipartFile is, boolean updateSupport) {
+        try {
+            int successNum = 0;
+            int failureNum = 0;
+
+            // 淇濆吇椤圭洰鍒楄〃
+            MaintCheckItemImportListener checkItemImportListener = new MaintCheckItemImportListener(updateSupport);
+            EasyExcel.read(is.getInputStream(), MaintCheckItemVo.class, checkItemImportListener).headRowNumber(4).sheet().doRead();
+            List<MaintCheckItemVo> successList = checkItemImportListener.getSuccessList();
+
+            // 璇诲彇鍥哄畾璧勪骇缂栧彿
+            EasyExcelCellListener readListener = new EasyExcelCellListener(3, 1);
+            EasyExcel.read(is.getInputStream(), readListener).headRowNumber(0).sheet().doReadSync();
+            String assetNo = Optional.ofNullable(readListener.getCellValue())
+                .map(value -> {
+                    int colonIndex = Math.max(value.indexOf(":"), value.indexOf("锛�")); // 鍚堝苟鍐掑彿澶勭悊
+                    return colonIndex != -1 ? value.substring(colonIndex + 1) : value;
+                })
+                .map(String::trim)
+                .orElse("");
+
+            if (assetNo.isEmpty()) {
+                return is.getOriginalFilename() + " 瀵煎叆澶辫触锛屾棤娉曡鍙栧浐瀹氳祫浜х紪鍙�";
+            }
+
+            // 璇诲彇淇濆吇璁″垝骞翠唤
+            EasyExcelCellListener readYearListener = new EasyExcelCellListener(2, 3);
+            EasyExcel.read(is.getInputStream(), readYearListener).headRowNumber(0).sheet().doReadSync();
+            String yearStr = readYearListener.getCellValue();
+            String year = yearStr.replaceAll("[^\\d]", ""); // 鍘婚櫎闈炴暟瀛楀瓧绗�
+            year = (year.length() == 4) ? year : DateUtils.getDate().substring(0,4);
+
+            QueryWrapper<EimsEqu> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("asset_no", assetNo);
+            EimsEquVo eimsEquVo = equMapper.selectVoOne(queryWrapper);
+            if (eimsEquVo == null) {
+                return is.getOriginalFilename() + " 璁惧鏈壘鍒帮紝璇峰厛鍦ㄨ澶囧彴甯愪腑娣诲姞";
+            }
+
+            // 鏈堜唤瀛楁澶勭悊浼樺寲
+            String[] monthFields = {"january","february","march","april","may","june",
+                                  "july","august","september","october","november","december"};
+
+            for (MaintCheckItemVo itemVo : successList) {
+                if ("鎵ц浜虹鍚�".equals(itemVo.getItemName())) break;
+
+                EimsMaintPlanBo maintPlanBo = new EimsMaintPlanBo();
+                maintPlanBo.setEquId(eimsEquVo.getEquId());
+                maintPlanBo.setMaintName(itemVo.getItemName());
+                maintPlanBo.setStatus("0");
+                maintPlanBo.setMaintType("1");
+                maintPlanBo.setMaintRule("0");
+
+                // 娣诲姞period鏍¢獙
+                String period = itemVo.getPeriod();
+                if (StringUtils.isBlank(period)) {
+                    failureNum++;
+                    continue;
+                }
+
+                try {
+                    if (period.length() > 1) {
+                        String num = period.replaceAll("[^\\d]", "");
+                        maintPlanBo.setMaintCycle(Long.parseLong(num));
+                        String substring = period.replace(num, "");
+                        // 杞崲鍛ㄦ湡鍗曚綅锛孧杞崲涓�3锛孌杞崲涓�1锛孻杞崲涓�5锛學杞崲涓�2锛孮杞崲涓�4
+                        switch (substring) {
+                            case "M":
+                                substring = "3";
+                                break;
+                            case "D":
+                                substring = "1";
+                                break;
+                            case "Y":
+                                substring = "5";
+                                break;
+                            case "W":
+                                substring = "2";
+                                break;
+                            case "Q":
+                                substring = "4";
+                                break;
+                            case "涓湀":
+                                substring = "3";
+                                break;
+                            case "澶�":
+                                substring = "1";
+                                break;
+                            case "骞�":
+                                substring = "5";
+                                break;
+                            case "鍛�":
+                                substring = "2";
+                                break;
+                            case "瀛e害":
+                                substring = "4";
+                                break;
+                            default:
+                        }
+                        maintPlanBo.setMaintCycleUnit(substring);
+                    } else {
+                        maintPlanBo.setMaintCycle(Long.parseLong(period));
+                        maintPlanBo.setMaintCycleUnit("");
+                    }
+                } catch (NumberFormatException e) {
+                    failureNum++;
+                    continue;
+                }
+
+                // 鏈堜唤鍒ゆ柇浼樺寲
+                boolean monthFound = false;
+                for (int i = 0; i < monthFields.length; i++) {
+                    try {
+                        String monthValue = (String) MaintCheckItemVo.class
+                            .getMethod("get" + StringUtils.capitalize(monthFields[i]))
+                            .invoke(itemVo);
+
+                        if (StringUtils.isNotBlank(monthValue)) {
+                            String month = String.format("%02d", i+1); // 淇濊瘉涓や綅鏈堜唤
+                            maintPlanBo.setMaintFirstTime(DateUtils.parseDate(year + "-" + month + "-01"));
+                            monthFound = true;
+                            break;
+                        }
+                    } catch (Exception e) {
+                        // 鍙嶅皠寮傚父澶勭悊锛岀户缁笅涓�涓湀浠�
+                        continue;
+                    }
+                }
+
+                if (!monthFound) {
+                    failureNum++;
+                    continue;
+                }
+
+                if (maintPlanBo.getMaintFirstTime() != null) {
+                    Date firstTime = maintPlanBo.getMaintFirstTime();
+                    Date nextTime = calcNextTime(firstTime, maintPlanBo.getMaintCycle().intValue(), 1);
+                    maintPlanBo.setMaintNextTime(nextTime);
+                }
+
+                // 濡傛灉鍏佽瑕嗙洊锛屽垯鍒犻櫎宸插瓨鍦ㄧ殑鐩稿悓鏉′欢璁板綍
+                if (updateSupport) {
+                    LambdaQueryWrapper<EimsMaintPlan> deleteWrapper = Wrappers.lambdaQuery();
+                    deleteWrapper.eq(EimsMaintPlan::getEquId, maintPlanBo.getEquId())
+                        .eq(EimsMaintPlan::getMaintName, maintPlanBo.getMaintName())
+                        .eq(EimsMaintPlan::getStatus, maintPlanBo.getStatus())
+                        .eq(EimsMaintPlan::getMaintCycle, maintPlanBo.getMaintCycle())
+                        .eq(EimsMaintPlan::getMaintCycleUnit, maintPlanBo.getMaintCycleUnit());
+                    baseMapper.delete(deleteWrapper);
+                }
+
+                if (!insertByBo(maintPlanBo)) {
+                    failureNum++;
+                } else {
+                    successNum++;
+                }
+            }
+
+            return is.getOriginalFilename() + " 瀵煎叆鎴愬姛锛�" + successNum + "鏉★紝瀵煎叆澶辫触锛�" + failureNum + "鏉�";
+        } catch (Exception e) {
+            return is.getOriginalFilename() + " 瀵煎叆寮傚父锛�" + e.getMessage();
+        }
+    }
+
+
+
+private Date calcNextTime(Date firstTime, int intervalMonths, int initialOffset) {
+    if (intervalMonths <= 0) {
+        throw new IllegalArgumentException("Interval months must be positive");
+    }
+    if (firstTime == null) {
+        throw new IllegalArgumentException("First time cannot be null");
+    }
+
+    Date current = new Date();
+    int adjustmentCount = initialOffset;
+
+    Date workingDate = (Date) firstTime.clone();
+    while (workingDate.before(current)) {
+        adjustmentCount++;
+        workingDate = DateUtils.addMonths(workingDate, intervalMonths);
+    }
+
+    return (Date) workingDate.clone();
+}
+
+
+
+
 }

--
Gitblit v1.9.3