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 |  281 ++++++++++++++++++++++++----------------------
 eims-ui/apps/web-antd/src/views/eims/maint-plan/maint-plan-import-modal.vue                          |   20 ++-
 eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsMaintPlanController.java    |   14 +-
 3 files changed, 169 insertions(+), 146 deletions(-)

diff --git a/eims-ui/apps/web-antd/src/views/eims/maint-plan/maint-plan-import-modal.vue b/eims-ui/apps/web-antd/src/views/eims/maint-plan/maint-plan-import-modal.vue
index 2943c30..36b348b 100644
--- a/eims-ui/apps/web-antd/src/views/eims/maint-plan/maint-plan-import-modal.vue
+++ b/eims-ui/apps/web-antd/src/views/eims/maint-plan/maint-plan-import-modal.vue
@@ -26,15 +26,21 @@
 async function handleSubmit() {
   try {
     modalApi.modalLoading(true);
-    if (fileList.value.length !== 1) {
+    if (fileList.value.length === 0) {
       handleCancel();
       return;
     }
-    const data = {
-      file: fileList.value[0]!.originFileObj as Blob,
-      updateSupport: unref(checked),
-    };
-    const { code, msg } = await maintPlanImportData(data);
+
+    // 鏋勫缓鍖呭惈澶氫釜鏂囦欢鐨勮姹傛暟鎹紝鏀寔澶氭枃浠朵笂浼�
+    const formData = new FormData();
+    fileList.value.forEach((file) => {
+      const blob = file.originFileObj as Blob;
+      formData.append('files', blob); // 灏嗗涓枃浠朵互鏁扮粍褰㈠紡娣诲姞鍒拌〃鍗曟暟鎹腑
+    });
+    formData.append('updateSupport', unref(checked).toString()); // 娣诲姞 updateSupport 鍙傛暟
+
+    // 璋冪敤鎺ュ彛涓婁紶澶氭枃浠�
+    const { code, msg } = await maintPlanImportData(formData);
     let modal = Modal.success;
     if (code === 200) {
       emit('reload');
@@ -76,7 +82,7 @@
     <UploadDragger
       v-model:file-list="fileList"
       :before-upload="() => false"
-      :max-count="1"
+      :multiple="true"
       :show-upload-list="true"
       accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
     >
diff --git a/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsMaintPlanController.java b/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsMaintPlanController.java
index c00d56b..e9f56e6 100644
--- a/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsMaintPlanController.java
+++ b/eims/ruoyi-modules/lb-eims/src/main/java/org/dromara/eims/controller/EimsMaintPlanController.java
@@ -120,13 +120,13 @@
     @Log(title = "淇濆吇璁″垝", businessType = BusinessType.IMPORT)
     @SaCheckPermission("eims:maintPlan:import")
     @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
-    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
-        //ExcelResult<MaintCheckItemVo> result = ExcelUtil.importExcel(file.getInputStream(), MaintCheckItemVo.class, new MaintCheckItemImportListener(updateSupport));
-
-
-        String res = eimsMaintPlanService.importData(file, updateSupport);
-
-        return R.ok(res);
+    public R<Void> importData(@RequestPart("files") List<MultipartFile> files, boolean updateSupport) throws Exception {
+        StringBuilder successMsg = new StringBuilder();
+        for (MultipartFile file : files) {
+            String res = eimsMaintPlanService.importData(file, updateSupport);
+            successMsg.append(res).append(";");
+        }
+        return R.ok(successMsg.toString());
     }
 
     /**
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 4518514..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
@@ -204,153 +204,170 @@
     }
 
 
-    public String importData(MultipartFile is, boolean updateSupport) throws IOException {
-    int successNum = 0;
-    int failureNum = 0;
-    StringBuilder successMsg = new StringBuilder();
-    StringBuilder failureMsg = new StringBuilder();
+    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();
 
-        // 淇濆吇椤圭洰鍒楄〃
-        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("");
 
-
-        // 璇诲彇鍥哄畾璧勪骇缂栧彿
-        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;
+            if (assetNo.isEmpty()) {
+                return is.getOriginalFilename() + " 瀵煎叆澶辫触锛屾棤娉曡鍙栧浐瀹氳祫浜х紪鍙�";
             }
 
-            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;
+            // 璇诲彇淇濆吇璁″垝骞翠唤
+            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() + " 璁惧鏈壘鍒帮紝璇峰厛鍦ㄨ澶囧彴甯愪腑娣诲姞";
             }
 
-            // 鏈堜唤鍒ゆ柇浼樺寲
-            for (int i = 0; i < monthFields.length; i++) {
-                try {
-                    String monthValue = (String) MaintCheckItemVo.class
-                        .getMethod("get" + StringUtils.capitalize(monthFields[i]))
-                        .invoke(itemVo);
+            // 鏈堜唤瀛楁澶勭悊浼樺寲
+            String[] monthFields = {"january","february","march","april","may","june",
+                                  "july","august","september","october","november","december"};
 
-                    if (StringUtils.isNotBlank(monthValue)) {
-                        String month = String.format("%02d", i+1); // 淇濊瘉涓や綅鏈堜唤
-                        maintPlanBo.setMaintFirstTime(DateUtils.parseDate(year + "-" + month + "-01"));
-                        break;
-                    }
-                } catch (Exception e) {
-                    // 鍙嶅皠寮傚父澶勭悊
+            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) {
+                        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++;
+                }
             }
 
-            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++;
-                failureMsg.append(failureNum).append("銆佽澶囷細").append(eimsEquVo.getEquName()).append("锛屽鍏ュけ璐�<br>");
-            } else {
-                successNum++;
-                successMsg.append("<br/>").append(successNum).append("銆佽澶囷細").append(eimsEquVo.getEquName()).append("锛屽鍏ユ垚鍔�");
-            }
+            return is.getOriginalFilename() + " 瀵煎叆鎴愬姛锛�" + successNum + "鏉★紝瀵煎叆澶辫触锛�" + failureNum + "鏉�";
+        } catch (Exception e) {
+            return is.getOriginalFilename() + " 瀵煎叆寮傚父锛�" + e.getMessage();
         }
-
-    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) {

--
Gitblit v1.9.3