| | |
| | | package org.dromara.eims.service.impl; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import lombok.SneakyThrows; |
| | | import org.dromara.common.core.constant.DictConstants; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.DateUtils; |
| | | import org.dromara.common.core.utils.MapstructUtils; |
| | | import org.dromara.common.core.utils.StringUtils; |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.dromara.eims.domain.vo.EimsRepairResVo; |
| | | import org.dromara.eims.domain.EimsEqu; |
| | | import org.dromara.eims.domain.vo.EimsEquVo; |
| | | import org.dromara.eims.domain.vo.MaintCheckItemVo; |
| | | import org.dromara.eims.listener.EasyExcelCellListener; |
| | | import org.dromara.eims.listener.MaintCheckItemImportListener; |
| | | import org.dromara.eims.mapper.EimsEquMapper; |
| | | import org.dromara.system.domain.SysDept; |
| | | import org.dromara.system.domain.vo.SysDeptVo; |
| | | import org.dromara.system.mapper.SysDeptMapper; |
| | |
| | | import org.dromara.eims.domain.EimsMaintPlan; |
| | | import org.dromara.eims.mapper.EimsMaintPlanMapper; |
| | | import org.dromara.eims.service.IEimsMaintPlanService; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | |
| | | private final EimsMaintPlanMapper baseMapper; |
| | | private final SysDeptMapper sysDeptMapper; |
| | | private final EimsEquMapper equMapper; |
| | | |
| | | /** |
| | | * 查询保养计划 |
| | |
| | | } |
| | | |
| | | |
| | | public String importData(MultipartFile is, boolean updateSupport) throws IOException { |
| | | int successNum = 0; |
| | | int failureNum = 0; |
| | | StringBuilder successMsg = new StringBuilder(); |
| | | StringBuilder failureMsg = new StringBuilder(); |
| | | |
| | | |
| | | // 保养项目列表 |
| | | 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) |
| | | .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; |
| | | } |
| | | |
| | | try { |
| | | if (period.length() > 1) { |
| | | maintPlanBo.setMaintCycle(Long.parseLong(period.substring(0, period.length() - 1))); |
| | | String substring = period.substring(period.length() - 1); |
| | | // 转换周期单位,M转换为3,D转换为1,Y转换为5,W转换为2,Q转换为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; |
| | | } |
| | | |
| | | // 月份判断优化 |
| | | 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")); |
| | | break; |
| | | } |
| | | } catch (Exception e) { |
| | | // 反射异常处理 |
| | | failureNum++; |
| | | failureMsg.append(failureNum).append("、月份字段访问异常<br>"); |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | if (maintPlanBo.getMaintFirstTime() != null) { |
| | | Date firstTime = maintPlanBo.getMaintFirstTime(); |
| | | Date nextTime = calcNextTime(firstTime, maintPlanBo.getMaintCycle().intValue(), 1); |
| | | maintPlanBo.setMaintNextTime(nextTime); |
| | | } |
| | | |
| | | 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(",导入成功"); |
| | | } |
| | | } |
| | | |
| | | if (failureNum > 0) { |
| | | failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); |
| | | return failureMsg.toString(); |
| | | } else { |
| | | successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:"); |
| | | return successMsg.toString(); |
| | | } |
| | | } |
| | | |
| | | |
| | | private Date calcNextTime(Date firstTime, int intervalMonths, int initialOffset) { |
| | | if (intervalMonths <= 0) { |
| | | throw new IllegalArgumentException("Interval months must be positive"); |
| | | } |
| | | if (firstTime == null) { |
| | | throw new IllegalArgumentException("First time cannot be null"); |
| | | } |
| | | |
| | | Date current = new Date(); |
| | | int adjustmentCount = initialOffset; |
| | | |
| | | Date workingDate = (Date) firstTime.clone(); |
| | | while (workingDate.before(current)) { |
| | | adjustmentCount++; |
| | | workingDate = DateUtils.addMonths(workingDate, intervalMonths); |
| | | } |
| | | |
| | | return (Date) workingDate.clone(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |