From 92bd9ed78ad1da026b81fcd8e88445b6834bba76 Mon Sep 17 00:00:00 2001
From: zhangherong <571457620@qq.com>
Date: 星期二, 01 四月 2025 17:12:11 +0800
Subject: [PATCH] art: 设备管理-保养标准-点检导入功能

---
 lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java |  157 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 153 insertions(+), 4 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 8f3ffef..d153584 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
@@ -7,22 +7,45 @@
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.jeecg.common.api.vo.FileUploadResult;
 import org.jeecg.common.api.vo.Result;
 import org.jeecg.common.aspect.annotation.AutoLog;
+import org.jeecg.common.exception.JeecgBootException;
 import org.jeecg.common.system.base.controller.JeecgController;
 import org.jeecg.common.system.query.QueryGenerator;
+import org.jeecg.common.util.FileUtil;
 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.entity.EamEquipment;
 import org.jeecg.modules.eam.entity.EamMaintenanceStandard;
+import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail;
 import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest;
+import org.jeecg.modules.eam.service.IEamEquipmentService;
 import org.jeecg.modules.eam.service.IEamMaintenanceStandardService;
 import org.jeecg.modules.system.service.ISysBusinessCodeRuleService;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.entity.ImportParams;
+import org.jeecgframework.poi.util.PoiPublicUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
 import org.springframework.web.servlet.ModelAndView;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.util.Arrays;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @Description: 淇濆吇鏍囧噯
@@ -39,6 +62,8 @@
     private IEamMaintenanceStandardService eamMaintenanceStandardService;
     @Autowired
     private ISysBusinessCodeRuleService businessCodeRuleService;
+    @Autowired
+    private IEamEquipmentService eamEquipmentService;
 
     /**
      * 鍒嗛〉鍒楄〃鏌ヨ
@@ -168,9 +193,133 @@
      * @param response
      * @return
      */
-    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
-    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
-        return super.importExcel(request, response, EamMaintenanceStandard.class);
+    @RequestMapping(value = "/inspectionImportExcel", method = RequestMethod.POST)
+    public Result<?> inspectionImportExcel(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 {
+                //璇诲彇璁惧缂栧彿锛屽浘鐗囩瓑
+                readExcel(file, standardRequest);
+                EamEquipment equipment = eamEquipmentService.selectByEquipmentCode(standardRequest.getEquipmentCode());
+                if(equipment == null) {
+                    log.error("璁惧涓嶅瓨鍦細{}", standardRequest.getEquipmentCode());
+                    continue;
+                }
+                standardRequest.setStandardName(standardRequest.getEquipmentName() + "鐐规鏍囧噯");
+                standardRequest.setMaintenanceCategory(MaintenanceCategoryEnum.POINT_INSPECTION.name());
+                standardRequest.setEquipmentId(equipment.getId());
+                //璇诲彇淇濆吇鏄庣粏鍐呭
+                List<MaintenanceStandardImport> list = ExcelImportUtil.importExcel(file.getInputStream(), MaintenanceStandardImport.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
+     */
+    private void readExcel(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(15);
+            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(11);
+            if (DateUtil.isCellDateFormatted(initialDate)) {
+                request.setInitialDate(initialDate.getDateCellValue());
+            } else {
+                request.setInitialDate(new Date());
+            }
+            //璁惧鍚嶇О
+            Cell equipmentName = row.getCell(13);
+            request.setEquipmentName(equipmentName.getStringCellValue());
+
+            //绗簩琛岃鍙�
+            row = sheet.getRow(1);
+            //淇濆吇鍛ㄦ湡
+            Cell period = row.getCell(11);
+            if (CellType.NUMERIC.equals(period.getCellType())) {
+                request.setMaintenancePeriod((int) period.getNumericCellValue());
+            } else {
+                //榛樿鐐规鍛ㄦ湡 1
+                request.setMaintenancePeriod(1);
+            }
+            //鏂囦欢缂栫爜
+            Cell fileCode = row.getCell(13);
+            request.setFileCode(fileCode.getStringCellValue());
+
+            Map<String, PictureData> pictures;
+            if (isXSSFWorkbook) {
+                pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(0), (XSSFWorkbook) book);
+            } else {
+                pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(0), (HSSFWorkbook) book);
+            }
+
+            if (CollectionUtil.isNotEmpty(pictures)) {
+                //鍙細瀛樺湪涓�寮犲浘鐗�
+                PictureData pictureData = pictures.get(pictures.keySet().iterator().next());
+                byte[] data = pictureData.getData();
+                String fileName = request.getEquipmentCode() + "[" + request.getFileCode() + "]" + "." + pictureData.suggestFileExtension();
+                FileUploadResult fileUploadResult = FileUtil.uploadFile(data, fileName);
+                if(fileUploadResult != null) {
+                    List<FileUploadResult> fileList = request.getFileList();
+                    if(fileList == null) {
+                        fileList = new ArrayList<FileUploadResult>();
+                    }
+                    fileList.add(fileUploadResult);
+                    request.setFileList(fileList);
+                }
+            }
+        } catch (Exception e) {
+            log.error("璇诲彇Excel淇℃伅澶辫触锛歿}", e.getMessage(), e);
+        }
+    }
 }

--
Gitblit v1.9.3