From beaed6d077e7c3e9abfad68acb8c587835b5a406 Mon Sep 17 00:00:00 2001
From: baoshiwei <baoshiwei@shlanbao.cn>
Date: 星期六, 12 四月 2025 16:17:16 +0800
Subject: [PATCH] feat(eims): 添加点检计划和保养计划的导入功能

---
 eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/service/impl/EimsMaintPlanServiceImpl.java |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 172 insertions(+), 3 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 937832b..f1e2647 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;
 
     /**
      * 鏌ヨ淇濆吇璁″垝
@@ -194,4 +202,165 @@
     }
 
 
+    public String importData(MultipartFile is, boolean updateSupport) throws IOException {
+    int successNum = 0;
+    int failureNum = 0;
+    StringBuilder successMsg = new StringBuilder();
+    StringBuilder failureMsg = new StringBuilder();
+
+
+        // 淇濆吇椤圭洰鍒楄〃
+        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)
+            .orElseThrow(() -> new ServiceException("瀵煎叆澶辫触锛屾棤娉曡鍙栧浐瀹氳祫浜х紪鍙�"));
+
+
+
+        // 璇诲彇淇濆吇璁″垝骞翠唤
+        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) throw new ServiceException("瀵煎叆澶辫触锛岃澶囨湭鎵惧埌璇峰湪璁惧鍙板笎涓坊鍔�");
+
+        // 鏈堜唤瀛楁澶勭悊浼樺寲
+        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++;
+                failureMsg.append(failureNum).append("銆佸懆鏈熷瓧娈典负绌�<br>");
+                continue;
+            }
+
+            try {
+                if (period.length() > 1) {
+                    maintPlanBo.setMaintCycle(Long.parseLong(period.substring(0, period.length() - 1)));
+                    String substring = period.substring(period.length() - 1);
+                    // 杞崲鍛ㄦ湡鍗曚綅锛孧杞崲涓�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;
+                        default:
+                    }
+                    maintPlanBo.setMaintCycleUnit(substring);
+                } else {
+                    maintPlanBo.setMaintCycle(Long.parseLong(period));
+                    maintPlanBo.setMaintCycleUnit("");
+                }
+            } catch (NumberFormatException e) {
+                failureNum++;
+                failureMsg.append(failureNum).append("銆佹棤鏁堢殑鍛ㄦ湡鏍煎紡:").append(period).append("<br>");
+                continue;
+            }
+
+            // 鏈堜唤鍒ゆ柇浼樺寲
+            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"));
+                        break;
+                    }
+                } catch (Exception e) {
+                    // 鍙嶅皠寮傚父澶勭悊
+                    failureNum++;
+                    failureMsg.append(failureNum).append("銆佹湀浠藉瓧娈佃闂紓甯�<br>");
+                    continue;
+                }
+            }
+
+            if (maintPlanBo.getMaintFirstTime() != null) {
+                Date firstTime = maintPlanBo.getMaintFirstTime();
+                Date nextTime = calcNextTime(firstTime, maintPlanBo.getMaintCycle().intValue(), 1);
+                maintPlanBo.setMaintNextTime(nextTime);
+            }
+
+            if (!insertByBo(maintPlanBo)) {
+                failureNum++;
+                failureMsg.append(failureNum).append("銆佽澶囷細").append(eimsEquVo.getEquName()).append("锛屽鍏ュけ璐�<br>");
+            } else {
+                successNum++;
+                successMsg.append("<br/>").append(successNum).append("銆佽澶囷細").append(eimsEquVo.getEquName()).append("锛屽鍏ユ垚鍔�");
+            }
+        }
+
+    if (failureNum > 0) {
+        failureMsg.insert(0, "寰堟姳姝夛紝瀵煎叆澶辫触锛佸叡 " + failureNum + " 鏉℃暟鎹牸寮忎笉姝g‘锛岄敊璇涓嬶細");
+        return failureMsg.toString();
+    } else {
+        successMsg.insert(0, "鎭枩鎮紝鏁版嵁宸插叏閮ㄥ鍏ユ垚鍔燂紒鍏� " + successNum + " 鏉★紝鏁版嵁濡備笅锛�");
+        return successMsg.toString();
+    }
+}
+
+
+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