zhangherong
2025-04-02 5ba069a84b332b22e628393f54e45bead79d7c15
art: 设备管理-保养标准-周保标准导入功能
已添加1个文件
已修改2个文件
146 ■■■■■ 文件已修改
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java 117 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/dto/WeekMaintenanceStandardImport.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/entity/EamMaintenanceStandardDetail.java 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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);
        }
    }
}
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/dto/WeekMaintenanceStandardImport.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,21 @@
package org.jeecg.modules.eam.dto;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
import java.io.Serializable;
@Data
public class WeekMaintenanceStandardImport implements Serializable {
    @Excel(name = "序号", width = 15)
    private Integer itemCode;
    @Excel(name = "保养大项目", width = 15)
    private String itemName;
    @Excel(name = "子项目", width = 15)
    private String subItemName;
    @Excel(name = "部位/类型", width = 15)
    private String itemPart;
    @Excel(name = "保养要求", width = 15)
    private String itemDemand;
}
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/entity/EamMaintenanceStandardDetail.java
@@ -9,6 +9,7 @@
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.jeecg.modules.eam.dto.MaintenanceStandardImport;
import org.jeecg.modules.eam.dto.WeekMaintenanceStandardImport;
import java.io.Serializable;
import java.util.Date;
@@ -68,4 +69,11 @@
        this.itemName = dto.getItemName() + " " + dto.getItemPart() + " " + dto.getItemContent();
        this.itemDemand = dto.getItemDemand();
    }
    public EamMaintenanceStandardDetail(WeekMaintenanceStandardImport dto) {
        this.itemCode = dto.getItemCode();
        this.itemName = dto.getItemName();
        this.subItemName = dto.getSubItemName() + " " + (dto.getItemPart() == null ? "" : dto.getItemPart());
        this.itemDemand = dto.getItemDemand();
    }
}