From 5ba069a84b332b22e628393f54e45bead79d7c15 Mon Sep 17 00:00:00 2001
From: zhangherong <571457620@qq.com>
Date: 星期三, 02 四月 2025 11:42:34 +0800
Subject: [PATCH] art: 设备管理-保养标准-周保标准导入功能

---
 lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java |  117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 115 insertions(+), 2 deletions(-)

diff --git a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java
index d153584..b65ab9a 100644
--- a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java
+++ b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java
@@ -23,6 +23,7 @@
 import org.jeecg.modules.eam.constant.BusinessCodeConst;
 import org.jeecg.modules.eam.constant.MaintenanceCategoryEnum;
 import org.jeecg.modules.eam.dto.MaintenanceStandardImport;
+import org.jeecg.modules.eam.dto.WeekMaintenanceStandardImport;
 import org.jeecg.modules.eam.entity.EamEquipment;
 import org.jeecg.modules.eam.entity.EamMaintenanceStandard;
 import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail;
@@ -42,8 +43,6 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -245,6 +244,64 @@
     }
 
     /**
+     * 閫氳繃excel瀵煎叆鏁版嵁
+     *
+     * @param request
+     * @param response
+     * @return
+     */
+    @RequestMapping(value = "/weekMaintenanceImportExcel", method = RequestMethod.POST)
+    public Result<?> weekMaintenanceImportExcel(HttpServletRequest request, HttpServletResponse response) {
+        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
+        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
+            // 鑾峰彇涓婁紶鏂囦欢瀵硅薄
+            MultipartFile file = entity.getValue();
+            ImportParams params = new ImportParams();
+            params.setTitleRows(2);
+            params.setHeadRows(1);
+            params.setSheetNum(1);
+            params.setNeedSave(true);
+            EamMaintenanceStandardRequest standardRequest = new EamMaintenanceStandardRequest();
+            try {
+                //璇诲彇璁惧缂栧彿锛屽浘鐗囩瓑
+                readWeekExcel(file, standardRequest);
+                EamEquipment equipment = eamEquipmentService.selectByEquipmentCode(standardRequest.getEquipmentCode());
+                if(equipment == null) {
+                    log.error("璁惧涓嶅瓨鍦細{}", standardRequest.getEquipmentCode());
+                    continue;
+                }
+                standardRequest.setStandardName(standardRequest.getEquipmentName() + "鍛ㄤ繚鏍囧噯");
+                standardRequest.setMaintenanceCategory(MaintenanceCategoryEnum.WEEK_MAINTENANCE.name());
+                standardRequest.setEquipmentId(equipment.getId());
+                //璇诲彇淇濆吇鏄庣粏鍐呭
+                List<WeekMaintenanceStandardImport> list = ExcelImportUtil.importExcel(file.getInputStream(), WeekMaintenanceStandardImport.class, params);
+                //鏄庣粏椤�
+                List<EamMaintenanceStandardDetail> tableList = list.stream().map(EamMaintenanceStandardDetail::new).collect(Collectors.toList());
+                standardRequest.setTableDetailList(tableList);
+                String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE);
+                standardRequest.setStandardCode(codeSeq);
+                boolean b = eamMaintenanceStandardService.addMaintenanceStandard(standardRequest);
+                if (!b) {
+                    log.error("淇濆瓨澶辫触锛� {}", standardRequest.getEquipmentCode());
+                }
+            } catch (Exception e) {
+                //update-begin-author:taoyan date:20211124 for: 瀵煎叆鏁版嵁閲嶅澧炲姞鎻愮ず
+                String msg = e.getMessage();
+                log.error("鏂囦欢 {} 澶勭悊寮傚父锛� {}", file.getOriginalFilename(), msg, e);
+                //update-end-author:taoyan date:20211124 for: 瀵煎叆鏁版嵁閲嶅澧炲姞鎻愮ず
+            } finally {
+                try {
+                    file.getInputStream().close();
+                } catch (IOException e) {
+                    log.error(e.getMessage(), e);
+                }
+            }
+        }
+        return Result.ok("鏂囦欢瀵煎叆瀹屾垚锛�");
+    }
+
+    /**
      * 璇诲彇Excel 绗竴琛岋紝 绗簩琛岀殑淇℃伅锛屽寘鎷浘鐗囦俊鎭�
      * @param file
      * @param request
@@ -322,4 +379,60 @@
             log.error("璇诲彇Excel淇℃伅澶辫触锛歿}", e.getMessage(), e);
         }
     }
+
+    /**
+     * 璇诲彇Excel 绗竴琛岋紝 绗簩琛岀殑淇℃伅
+     * @param file
+     * @param request
+     */
+    public void readWeekExcel(MultipartFile file, EamMaintenanceStandardRequest request) {
+        Workbook book = null;
+        boolean isXSSFWorkbook = false;
+        try {
+            book = WorkbookFactory.create(file.getInputStream());
+            if (book instanceof XSSFWorkbook) {
+                isXSSFWorkbook = true;
+            }
+
+            Sheet sheet = book.getSheetAt(0);
+            //绗竴琛岃鍙�
+            Row row = sheet.getRow(0);
+            //璁惧缂栫爜
+            Cell equipmentCode = row.getCell(10);
+            if(CellType.NUMERIC.equals(equipmentCode.getCellType())) {
+                request.setEquipmentCode(String.valueOf((int) equipmentCode.getNumericCellValue()));
+            }else if(CellType.STRING.equals(equipmentCode.getCellType())) {
+                request.setEquipmentCode(equipmentCode.getStringCellValue());
+            }
+            if (StringUtils.isBlank(request.getEquipmentCode())) {
+                throw new JeecgBootException("Excel銆� " + file.getOriginalFilename() + "銆戞病鏈夎鍙栧埌璁惧缂栧彿锛屽鍏ュけ璐ワ紒");
+            }
+            //鍒濆鏃ユ湡
+            Cell initialDate = row.getCell(6);
+            if (DateUtil.isCellDateFormatted(initialDate)) {
+                request.setInitialDate(initialDate.getDateCellValue());
+            } else {
+                request.setInitialDate(new Date());
+            }
+            //璁惧鍚嶇О
+            Cell equipmentName = row.getCell(8);
+            request.setEquipmentName(equipmentName.getStringCellValue());
+
+            //绗簩琛岃鍙�
+            row = sheet.getRow(1);
+            //淇濆吇鍛ㄦ湡
+            Cell period = row.getCell(6);
+            if (CellType.NUMERIC.equals(period.getCellType())) {
+                request.setMaintenancePeriod((int) period.getNumericCellValue());
+            } else {
+                //榛樿鐐规鍛ㄦ湡 1
+                request.setMaintenancePeriod(1);
+            }
+            //鏂囦欢缂栫爜
+            Cell fileCode = row.getCell(8);
+            request.setFileCode(fileCode.getStringCellValue());
+        } catch (Exception e) {
+            log.error("璇诲彇Excel淇℃伅澶辫触锛歿}", e.getMessage(), e);
+        }
+    }
 }

--
Gitblit v1.9.3