zhangherong
2025-04-02 9f76c277b006cff0d289dd5df08c3d12c09b8cc4
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/controller/EamMaintenanceStandardController.java
@@ -16,6 +16,7 @@
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.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
@@ -23,6 +24,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 +44,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;
@@ -80,9 +80,9 @@
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<EamMaintenanceStandard> queryWrapper = QueryGenerator.initQueryWrapper(eamMaintenanceStandard, req.getParameterMap());
//        QueryWrapper<EamMaintenanceStandard> queryWrapper = QueryGenerator.initQueryWrapper(eamMaintenanceStandard, req.getParameterMap());
        Page<EamMaintenanceStandard> page = new Page<EamMaintenanceStandard>(pageNo, pageSize);
        IPage<EamMaintenanceStandard> pageList = eamMaintenanceStandardService.page(page, queryWrapper);
        IPage<EamMaintenanceStandard> pageList = eamMaintenanceStandardService.queryPageList(page, eamMaintenanceStandard);
        return Result.OK(pageList);
    }
@@ -144,7 +144,11 @@
    @ApiOperation(value = "保养标准-通过id删除", notes = "保养标准-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        eamMaintenanceStandardService.removeById(id);
        EamMaintenanceStandard entity = eamMaintenanceStandardService.getById(id);
        if (entity != null) {
            entity.setDelFlag(CommonConstant.DEL_FLAG_1);
            eamMaintenanceStandardService.updateById(entity);
        }
        return Result.OK("删除成功!");
    }
@@ -158,7 +162,14 @@
    @ApiOperation(value = "保养标准-批量删除", notes = "保养标准-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eamMaintenanceStandardService.removeByIds(Arrays.asList(ids.split(",")));
        List<String> list = Arrays.asList(ids.split(","));
        list.forEach(id -> {
            EamMaintenanceStandard entity = eamMaintenanceStandardService.getById(id);
            if (entity != null) {
                entity.setDelFlag(CommonConstant.DEL_FLAG_1);
                eamMaintenanceStandardService.updateById(entity);
            }
        });
        return Result.OK("批量删除成功!");
    }
@@ -219,6 +230,64 @@
                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 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);
@@ -322,4 +391,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);
        }
    }
}