| | |
| | | } |
| | | |
| | | |
| | | public String importData(MultipartFile is, boolean updateSupport) throws IOException { |
| | | public String importData(MultipartFile is, boolean updateSupport) { |
| | | try { |
| | | 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); |
| | |
| | | return colonIndex != -1 ? value.substring(colonIndex + 1) : value; |
| | | }) |
| | | .map(String::trim) |
| | | .orElseThrow(() -> new ServiceException("导入失败,无法读取固定资产编号")); |
| | | .orElse(""); |
| | | |
| | | |
| | | if (assetNo.isEmpty()) { |
| | | return is.getOriginalFilename() + " 导入失败,无法读取固定资产编号"; |
| | | } |
| | | |
| | | // 读取保养计划年份 |
| | | EasyExcelCellListener readYearListener = new EasyExcelCellListener(2, 3); |
| | |
| | | QueryWrapper<EimsEqu> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("asset_no", assetNo); |
| | | EimsEquVo eimsEquVo = equMapper.selectVoOne(queryWrapper); |
| | | if (eimsEquVo == null) throw new ServiceException("导入失败,设备未找到请在设备台帐中添加"); |
| | | if (eimsEquVo == null) { |
| | | return is.getOriginalFilename() + " 设备未找到,请先在设备台帐中添加"; |
| | | } |
| | | |
| | | // 月份字段处理优化 |
| | | String[] monthFields = {"january","february","march","april","may","june", |
| | |
| | | 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); |
| | | String num = period.replaceAll("[^\\d]", ""); |
| | | maintPlanBo.setMaintCycle(Long.parseLong(num)); |
| | | String substring = period.replace(num, ""); |
| | | // 转换周期单位,M转换为3,D转换为1,Y转换为5,W转换为2,Q转换为4 |
| | | switch (substring) { |
| | | case "M": |
| | |
| | | case "Q": |
| | | substring = "4"; |
| | | break; |
| | | case "个月": |
| | | substring = "3"; |
| | | break; |
| | | case "天": |
| | | substring = "1"; |
| | | break; |
| | | case "年": |
| | | substring = "5"; |
| | | break; |
| | | case "周": |
| | | substring = "2"; |
| | | break; |
| | | case "季度": |
| | | substring = "4"; |
| | | break; |
| | | default: |
| | | } |
| | | maintPlanBo.setMaintCycleUnit(substring); |
| | |
| | | } |
| | | } catch (NumberFormatException e) { |
| | | failureNum++; |
| | | failureMsg.append(failureNum).append("、无效的周期格式:").append(period).append("<br>"); |
| | | continue; |
| | | } |
| | | |
| | | // 月份判断优化 |
| | | boolean monthFound = false; |
| | | for (int i = 0; i < monthFields.length; i++) { |
| | | try { |
| | | String monthValue = (String) MaintCheckItemVo.class |
| | |
| | | if (StringUtils.isNotBlank(monthValue)) { |
| | | String month = String.format("%02d", i+1); // 保证两位月份 |
| | | maintPlanBo.setMaintFirstTime(DateUtils.parseDate(year + "-" + month + "-01")); |
| | | monthFound = true; |
| | | break; |
| | | } |
| | | } catch (Exception e) { |
| | | // 反射异常处理 |
| | | failureNum++; |
| | | failureMsg.append(failureNum).append("、月份字段访问异常<br>"); |
| | | // 反射异常处理,继续下一个月份 |
| | | continue; |
| | | } |
| | | } |
| | | |
| | | if (!monthFound) { |
| | | failureNum++; |
| | | continue; |
| | | } |
| | | |
| | | if (maintPlanBo.getMaintFirstTime() != null) { |
| | |
| | | |
| | | 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(); |
| | | return is.getOriginalFilename() + " 导入成功:" + successNum + "条,导入失败:" + failureNum + "条"; |
| | | } catch (Exception e) { |
| | | return is.getOriginalFilename() + " 导入异常:" + e.getMessage(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | private Date calcNextTime(Date firstTime, int intervalMonths, int initialOffset) { |
| | | if (intervalMonths <= 0) { |
| | | throw new IllegalArgumentException("Interval months must be positive"); |