From 8ef2ea1ef685b39c15d194fbd8f6d8046d353656 Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期三, 09 七月 2025 11:32:45 +0800 Subject: [PATCH] 修改word导入 --- lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java | 585 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 569 insertions(+), 16 deletions(-) diff --git a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java index 596714b..06054ef 100644 --- a/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java +++ b/lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EamMaintenanceStandardServiceImpl.java @@ -12,6 +12,7 @@ import com.jeecg.weibo.exception.BusinessException; import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xwpf.usermodel.*; import org.apache.shiro.SecurityUtils; import org.flowable.engine.TaskService; import org.flowable.task.api.Task; @@ -19,17 +20,21 @@ import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.exception.JeecgBootException; import org.jeecg.common.system.vo.LoginUser; -import org.jeecg.modules.eam.base.entity.BaseFactory; -import org.jeecg.modules.eam.base.entity.BaseFactoryUser; -import org.jeecg.modules.eam.base.service.IBaseFactoryService; -import org.jeecg.modules.eam.base.service.IBaseFactoryUserService; +import org.jeecg.modules.system.entity.BaseFactory; +import org.jeecg.modules.system.entity.BaseFactoryUser; +import org.jeecg.modules.system.service.IBaseFactoryService; +import org.jeecg.modules.system.service.IBaseFactoryUserService; import org.jeecg.modules.eam.constant.BusinessCodeConst; +import org.jeecg.modules.eam.constant.EamMaintenanceStandardDetailCategory; import org.jeecg.modules.eam.constant.MaintenanceStandardStatusEnum; import org.jeecg.modules.eam.entity.EamEquipment; +import org.jeecg.modules.eam.entity.EamEquipmentExtend; import org.jeecg.modules.eam.entity.EamMaintenanceStandard; import org.jeecg.modules.eam.entity.EamMaintenanceStandardDetail; import org.jeecg.modules.eam.mapper.EamMaintenanceStandardMapper; import org.jeecg.modules.eam.request.EamMaintenanceStandardRequest; +import org.jeecg.modules.eam.request.ImportException; +import org.jeecg.modules.eam.service.IEamEquipmentExtendService; import org.jeecg.modules.eam.service.IEamEquipmentService; import org.jeecg.modules.eam.service.IEamMaintenanceStandardDetailService; import org.jeecg.modules.eam.service.IEamMaintenanceStandardService; @@ -40,6 +45,7 @@ import org.jeecg.modules.flowable.apithird.service.FlowCommonService; import org.jeecg.modules.flowable.service.IFlowDefinitionService; import org.jeecg.modules.flowable.service.IFlowTaskService; +import org.jeecg.modules.system.service.ISysBusinessCodeRuleService; import org.jeecg.modules.system.service.ISysUserService; import org.jeecg.modules.system.vo.UserSelector; import org.springframework.beans.BeanUtils; @@ -87,6 +93,22 @@ private ISysUserService sysUserService; @Autowired private IEamEquipmentService eamEquipmentService; + @Autowired + private IEamEquipmentExtendService eamEquipmentExtendService; + @Autowired + private ISysBusinessCodeRuleService businessCodeRuleService; + + // 甯搁噺瀹氫箟 + private static final String REPAIR_TITLE = "缁翠慨浜哄憳淇濆吇鍐呭"; + private static final String OPERATOR_TITLE = "鎿嶄綔浜哄憳淇濆吇鍐呭"; + private static final Map<String, EamMaintenanceStandardDetailCategory> SECOND_CATEGORY_MAPPING = new HashMap<>(); + + // 鏋氫妇鏄犲皠鍒濆鍖� + static { + SECOND_CATEGORY_MAPPING.put(REPAIR_TITLE, EamMaintenanceStandardDetailCategory.REPAIRER_MAINTENANCE); + SECOND_CATEGORY_MAPPING.put(OPERATOR_TITLE, EamMaintenanceStandardDetailCategory.OPERATOR_MAINTENANCE); + } + @Override public IPage<EamMaintenanceStandard> queryPageList(Page<EamMaintenanceStandard> page, EamMaintenanceStandard eamMaintenanceStandard) { QueryWrapper<EamMaintenanceStandard> queryWrapper = new QueryWrapper<>(); @@ -107,7 +129,7 @@ if(!CollectionUtils.isEmpty(baseFactoryUserList)){ List<String> factoryIds = baseFactoryUserList.stream().map(BaseFactoryUser::getFactoryId).collect(Collectors.toList()); List<String> factoryCode= baseFactoryService.listByIds(factoryIds).stream().map(BaseFactory::getFactoryCode).collect(Collectors.toList()); - queryWrapper.in("e.factory_code", factoryCode); + queryWrapper.in("e.factory_org_code", factoryCode); } } if(eamMaintenanceStandard != null) { @@ -225,6 +247,7 @@ newEntity.setMaintenanceCategory(standardRequest.getMaintenanceCategory()); newEntity.setMaintenancePeriod(standardRequest.getMaintenancePeriod()); newEntity.setInitialDate(standardRequest.getInitialDate()); + newEntity.setPeriodUnit(standardRequest.getPeriodUnit()); newEntity.setStandardStatus(MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); //鐗堟湰閫掑 //鑾峰彇鏁板瓧 @@ -301,7 +324,7 @@ LambdaQueryWrapper<EamMaintenanceStandard> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(EamMaintenanceStandard::getDelFlag, CommonConstant.DEL_FLAG_0); queryWrapper.eq(EamMaintenanceStandard::getMaintenanceCategory, maintenanceCategory); - queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.WAIT_SUBMIT.name()); + queryWrapper.eq(EamMaintenanceStandard::getStandardStatus, MaintenanceStandardStatusEnum.START.name()); return eamMaintenanceStandardMapper.selectList(queryWrapper); } @@ -347,7 +370,7 @@ FlowMyBusiness flowMyBusiness = businessList.get(0); EamEquipment eamEquipment=eamEquipmentService.getById(maintenanceStandard.getEquipmentId()); BaseFactory baseFactory=baseFactoryService.getOne(new QueryWrapper<BaseFactory>().eq("org_code", eamEquipment.getFactoryOrgCode())); - List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0007); + List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0008); if (!CollectionUtils.isEmpty(userSelectorList)) { List<String> usernameList=userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList()); flowMyBusiness.setTodoUsers(JSON.toJSONString(usernameList)); @@ -427,7 +450,7 @@ EamMaintenanceStandard maintenanceStandard=this.getById(eamMaintenanceStandardVo.getDataId()); EamEquipment eamEquipment=eamEquipmentService.getById(maintenanceStandard.getEquipmentId()); BaseFactory baseFactory=baseFactoryService.getOne(new QueryWrapper<BaseFactory>().eq("org_code", eamEquipment.getFactoryOrgCode())); - List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0008); + List<UserSelector> userSelectorList=sysUserService.selectOperatorFactoryList(eamEquipment.getEquipmentCode(),baseFactory.getId(), BusinessCodeConst.PCR0009); if (!CollectionUtils.isEmpty(userSelectorList)) { List<String> usernameList=userSelectorList.stream().map(UserSelector::getUsername).collect(Collectors.toList()); newflowMyBusiness.setTodoUsers(JSON.toJSONString(usernameList)); @@ -520,7 +543,7 @@ /*娴佺▼涓氬姟浠g爜--------------------------缁撴潫*/ - /*瀵煎叆鏂囦欢--------------------------寮�濮�*/ + /*瀵煎叆鐐规鏂囦欢Excel--------------------------寮�濮�*/ /** * 鐐规琛ㄥ鍏ュ叆鍙� */ @@ -536,7 +559,11 @@ // 1. 鎻愬彇璁惧淇℃伅 EamMaintenanceStandard eamMaintenanceStandard = extractDeviceInfo(sheet); if (eamMaintenanceStandard == null) { - throw new BusinessException("璁惧淇℃伅鎻愬彇澶辫触"); + return Result.error("璁惧淇℃伅鎻愬彇澶辫触"); + }else { + if (eamMaintenanceStandard.getEquipmentId() == null) { + return Result.error("璁惧淇℃伅鎻愬彇澶辫触"); + } } eamMaintenanceStandard.setStandardName(name); @@ -544,9 +571,15 @@ // 2. 鎻愬彇姣忔棩鐐规椤圭洰 List<EamMaintenanceStandardDetail> dailyDetails = extractDailyItems(sheet, eamMaintenanceStandard); + if(dailyDetails.isEmpty()){ + return Result.error("鏈壘鍒版瘡鏃ョ偣妫�椤圭洰"); + } // 3. 鎻愬彇鍛ㄤ繚鍏婚」鐩� List<EamMaintenanceStandardDetail> weeklyDetails = extractWeeklyItems(sheet, eamMaintenanceStandard); + if(weeklyDetails.isEmpty()){ + return Result.error("鏈壘鍒板懆淇濆吇椤圭洰"); + } // 鍚堝苟骞朵繚瀛樻墍鏈夐」鐩� List<EamMaintenanceStandardDetail> allDetails = new ArrayList<>(); @@ -617,8 +650,8 @@ EamMaintenanceStandard standard = new EamMaintenanceStandard(); standard.setEquipmentName(extractField(headerText, "璁惧鍚嶇О[:锛歖\\s*(\\S+)")); - standard.setEquipmentId(extractField(headerText, "璁惧鍨嬪彿[:锛歖\\s*(\\S+)")); - standard.setStandardCode(extractField(headerText, "缁熶竴缂栧彿[:锛歖\\s*(\\S+)")); + standard.setEquipmentCode(extractField(headerText, "缁熶竴缂栧彿[:锛歖\\s*(\\S+)")); + // 鏃ユ湡澶勭悊 String dateStr = extractField(headerText, "鏃ユ湡[:锛歖\\s*(\\S+)"); @@ -633,11 +666,23 @@ } } + // 鍏宠仈璁惧ID + if (StrUtil.isNotEmpty(standard.getEquipmentCode())) { + EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode()); + if (equipments == null) { + return null; + }else { + standard.setEquipmentId(equipments.getId()); + } + } + String codeSeq = businessCodeRuleService.generateBusinessCodeSeq(BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE); + standard.setStandardCode(codeSeq); standard.setMaintenanceCategory("POINT_INSPECTION"); standard.setPeriodUnit("澶�"); standard.setStandardStatus(MaintenanceStandardStatusEnum.START.name()); standard.setStandardVersion("v" + CommonConstant.OPERATE_TYPE_1); standard.setDelFlag(0); + standard.setMaintenancePeriod(1); return standard; } @@ -800,7 +845,7 @@ } /** - * 璇嗗埆鏂拌〃澶村紑濮� + * 璇嗗埆鏂拌〃澶村紑濮� */ private boolean isNewHeaderStart(Row row) { for (int colIdx = 0; colIdx < row.getLastCellNum(); colIdx++) { @@ -837,7 +882,6 @@ */ private String extractField(String text, String regex) { if (StringUtils.isBlank(text)) return ""; - Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(text); return matcher.find() ? matcher.group(1) : ""; @@ -848,7 +892,6 @@ */ private String getCellStringValue(Cell cell) { if (cell == null) return ""; - switch (cell.getCellType()) { case STRING: return cell.getStringCellValue().trim(); @@ -886,5 +929,515 @@ return ""; } } - /*瀵煎叆鏂囦欢--------------------------缁撴潫*/ + /*瀵煎叆鐐规鏂囦欢Excel--------------------------缁撴潫*/ + + /*瀵煎叆浜屼繚涓変繚鏂囦欢Excel--------------------------寮�濮�*/ + /** + * 缁熶竴淇濆吇瑙勮寖瀵煎叆鍏ュ彛 + * @param file 涓婁紶鏂囦欢 + * @param type 淇濆吇绫诲瀷 (SECOND/THIRD) + * @return 瀵煎叆缁撴灉锛堝寘鍚垚鍔�/澶辫触淇℃伅锛� + */ + @Override + @Transactional + public Result<?> importMaintenanceStandard(MultipartFile file, String type) { + String fileName = file.getOriginalFilename(); + + try (XWPFDocument doc = new XWPFDocument(file.getInputStream())) { + // 鍩虹楠岃瘉 + if (doc.getTables().isEmpty()) { + return Result.error(fileName + ": 鏂囨。涓湭鎵惧埌琛ㄦ牸"); + } + + List<XWPFTable> tables = doc.getTables(); + EamMaintenanceStandard standard = null; + boolean firstTableProcessed = false; + List<EamMaintenanceStandardDetail> items = new ArrayList<>(); + String standardId = null; + + // 1. 澶勭悊鎵�鏈夎〃鏍� + for (int i = 0; i < tables.size(); i++) { + XWPFTable table = tables.get(i); + + if (i == 0) { // 绗竴椤佃〃鏍� + // 楠岃瘉璁惧淇℃伅琛ㄦ牸 + if (isWrongDocumentType(table, type)) { + return Result.error(fileName + ": 鏂囨。绫诲瀷涓嶅尮閰� - " + + ("SECOND".equals(type) ? "璇峰鍏ヤ簩绾т繚鍏绘枃妗�" : "璇峰鍏ヤ笁绾т繚鍏绘枃妗�")); + } + + // 鎻愬彇璁惧淇℃伅 + standard = extractDeviceInfo(table); + if (standard == null) { + return Result.error(fileName + ": 璁惧淇℃伅鎻愬彇澶辫触"); + } + + // 閰嶇疆绫诲瀷鐩稿叧鍙傛暟 + configureStandard(standard, type, file); + eamMaintenanceStandardMapper.insert(standard); + standardId = standard.getId(); + + // 鎻愬彇绗竴椤电殑淇濆吇椤圭洰 + if ("SECOND".equals(type)) { + items.addAll(extractSecondMaintenanceItems(table, standardId, true)); + } else if ("THIRD".equals(type)) { + items.addAll(extractThirdMaintenanceItems(table, standardId, true)); + } + + firstTableProcessed = true; + } else if (firstTableProcessed) { // 鍚庣画椤甸潰 + // 鎻愬彇鍚庣画椤甸潰鐨勪繚鍏婚」鐩� + if ("SECOND".equals(type)) { + items.addAll(extractSecondMaintenanceItems(table, standardId, false)); + } else if ("THIRD".equals(type)) { + items.addAll(extractThirdMaintenanceItems(table, standardId, false)); + } + } + } + + // 楠岃瘉璁惧淇℃伅鎻愬彇 + if (standard == null) { + return Result.error(fileName + ": 璁惧淇℃伅鎻愬彇澶辫触"); + } + + // 2. 鍚庡鐞嗭細鏍规嵁涓嶅悓绫诲瀷杩涜澶勭悊 + processItemsAfterExtraction(items, type); + + // 3. 椤圭洰楠岃瘉 + if (items.isEmpty()) { + return Result.error(fileName + ": 鏈彁鍙栧埌浠讳綍淇濆吇椤圭洰"); + } + + // 4. 淇濆瓨椤圭洰 + eamMaintenanceStandardDetailService.saveBatch(items); + + return Result.ok(fileName + ": 瀵煎叆鎴愬姛, 椤圭洰鏁�: " + items.size()); + + } catch (ImportException e) { + return Result.error(e.getMessage()); + } catch (Exception e) { + return Result.error(fileName + ": 绯荤粺閿欒 - " + e.getClass().getSimpleName()); + } + } + + /** + * 鍚庡鐞嗘柟娉曪細鏍规嵁涓嶅悓绫诲瀷杩涜澶勭悊 + */ + private void processItemsAfterExtraction(List<EamMaintenanceStandardDetail> items, String type) { + if ("SECOND".equals(type)) { + // 浜岀骇淇濆吇: 鍒犻櫎娌℃湁搴忓彿鐨勬暟鎹� + items.removeIf(item -> item.getItemCode() == null); + } else { + // 涓夌骇淇濆吇: + // 1. 鍒犻櫎绗竴鏉℃暟鎹紙閫氬父鏄爣棰樿锛� + if (!items.isEmpty()) { + items.remove(0); + } + + // 2. 涓虹己澶遍儴浣嶇殑鏁版嵁濉厖鍓嶄竴鏉$殑淇濆吇閮ㄤ綅 + String lastPart = ""; + for (EamMaintenanceStandardDetail item : items) { + if (item.getItemPart() != null && !item.getItemPart().isEmpty()) { + lastPart = item.getItemPart(); + } else if (!lastPart.isEmpty()) { + item.setItemPart(lastPart); + } + } + } + } + /** + * 鎻愬彇浜岀骇淇濆吇椤圭洰锛堝尯鍒嗙涓�椤靛拰鍚庣画椤甸潰锛� + */ + private List<EamMaintenanceStandardDetail> extractSecondMaintenanceItems( + XWPFTable table, String standardId, boolean isFirstTable) { + + List<EamMaintenanceStandardDetail> items = new ArrayList<>(); + String currentCategory = null; + int startRow = 0; + + // 瀵逛簬绗竴椤佃〃鏍硷紝璺宠繃鍓嶄袱琛岋紙璁惧淇℃伅琛岋級 + if (isFirstTable && table.getNumberOfRows() > 2) { + startRow = 2; // 浠庣涓夎寮�濮嬫槸淇濆吇鍐呭 + } + + for (int i = startRow; i < table.getNumberOfRows(); i++) { + XWPFTableRow row = table.getRow(i); + if (row == null) continue; + + // 妫�鏌ユ槸鍚︽槸鏍囬琛岋紙缁翠慨浜哄憳淇濆吇鍐呭鎴栨搷浣滀汉鍛樹繚鍏诲唴瀹癸級 + String firstCell = getCellText(row.getCell(0)); + // 澶勭悊绌鸿鍚庣殑閮ㄤ綅缁ф壙 + if (firstCell.contains(REPAIR_TITLE)) { + currentCategory = "REPAIRER_MAINTENANCE"; + } else if (firstCell.contains(OPERATOR_TITLE)) { + currentCategory = "OPERATOR_MAINTENANCE"; + } + + // 澶勭悊鏅�氶」鐩 + if (currentCategory != null && isValidItemRow(row)) { + EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail(); + item.setItemCategory(currentCategory); + item.setStandardId(standardId); + + // 鎻愬彇搴忓彿锛堢浜屽垪锛� + if (row.getTableCells().size() > 1) { + String seqText = getCellText(row.getCell(1)); + try { + if (!seqText.equals("搴忓彿")){ + item.setItemCode(Integer.parseInt(seqText.trim())); + } + } catch (NumberFormatException e) { + // 蹇界暐搴忓彿瑙f瀽閿欒 + } + } + + // 鎻愬彇鍐呭锛堢涓夊垪锛� + if (row.getTableCells().size() > 2) { + String seqText = getCellText(row.getCell(2)); + item.setItemName(seqText); + } + + items.add(item); + } + } + return items; + } + + /** + * 鎻愬彇涓夌骇淇濆吇椤圭洰锛堣В鍐宠法椤电┖琛岄棶棰橈級 + */ + private List<EamMaintenanceStandardDetail> extractThirdMaintenanceItems( + XWPFTable table, String standardId, boolean isFirstTable) { + + List<EamMaintenanceStandardDetail> items = new ArrayList<>(); + String currentPart = ""; + int startRow = 0; + + // 瀵逛簬绗竴椤佃〃鏍硷紝璺宠繃鍓嶄袱琛岋紙璁惧淇℃伅琛岋級 + if (isFirstTable && table.getNumberOfRows() > 2) { + startRow = 2; // 浠庣涓夎寮�濮嬫槸淇濆吇鍐呭 + } + + for (int i = startRow; i < table.getNumberOfRows(); i++) { + XWPFTableRow row = table.getRow(i); + if (row == null) continue; // 纭繚琛屽璞′笉涓虹┖ + + // 妫�鏌ユ槸鍚︽槸绌鸿锛堝寘鍚墍鏈夊崟鍏冩牸閮戒负绌虹殑鎯呭喌锛� + if (isRowEmpty(row)) { + // 绌鸿澶勭悊锛氫繚鐣欏綋鍓嶄綅缃絾涓嶅垱寤洪」鐩� + continue; + } + + // 鍒涘缓淇濆吇椤圭洰 + EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail(); + item.setItemCategory("THIRD_MAINTENANCE"); + item.setStandardId(standardId); + int colCount = row.getTableCells().size(); + + // 澶勭悊閮ㄤ綅鍒楋紙绗竴鍒楋級 + if (colCount > 0) { + String firstCell = getCellText(row.getCell(0)).trim(); + + // 鍏抽敭鏀硅繘锛氭纭鐞嗙┖琛屽悗鐨勯儴浣嶇户鎵� + if (!firstCell.isEmpty() && !firstCell.equals("淇濆吇閮ㄤ綅")) { + // 鏇存柊褰撳墠閮ㄤ綅 + currentPart = firstCell; + } + item.setItemPart(currentPart); + } else { + // 濡傛灉娌℃湁鍗曞厓鏍硷紝浣跨敤褰撳墠閮ㄤ綅 + item.setItemPart(currentPart); + } + + // 鏍规嵁鍒楁暟鎻愬彇鍐呭锛堣�冭檻鍚堝苟鍗曞厓鏍兼儏鍐碉級 + List<String> cellContents = new ArrayList<>(); + for (int j = 0; j < colCount; j++) { + XWPFTableCell cell = row.getCell(j); + String text = getCellText(cell).trim(); + + // 鐗规畩澶勭悊锛氱浜岄〉绗竴琛屽彲鑳芥槸绌鸿鍚庣殑鍐呭 + if (j == 0 && !text.isEmpty() && !text.equals(currentPart)) { + // 濡傛灉涓嶆槸閮ㄤ綅鍒楋紝鍒欐坊鍔犱负鍐呭 + cellContents.add(text); + } else if (j > 0) { + // 鍏朵粬鍒椾綔涓哄唴瀹� + cellContents.add(text); + } + } + + // 鏅鸿兘瑙f瀽鍗曞厓鏍煎唴瀹� + if (cellContents.size() >= 2) { + // 榛樿澶勭悊鏂瑰紡锛氭渶鍚庝袱涓綔涓哄唴瀹瑰拰鏍囧噯 + item.setItemName(cellContents.get(cellContents.size() - 2)); + item.setItemDemand(cellContents.get(cellContents.size() - 1)); + } else if (cellContents.size() == 1) { + // 鍗曞垪妯″紡锛氳涓哄唴瀹� + item.setItemName(cellContents.get(0)); + } else if (cellContents.isEmpty() && !isRowEmpty(row)) { + // 鐗规畩澶勭悊锛氳闈炵┖浣嗘病鏈夋彁鍙栧埌鍐呭锛堝彲鑳芥槸澶嶆潅鍚堝苟鍗曞厓鏍硷級 + // 灏濊瘯鎻愬彇鏁磋鏂囨湰浣滀负鍐呭 + StringBuilder content = new StringBuilder(); + for (XWPFTableCell cell : row.getTableCells()) { + content.append(getCellText(cell).trim()).append(" "); + } + item.setItemName(content.toString().trim()); + } + + items.add(item); + } + return items; + } + + /** + * 浼樺寲鍚庣殑绌鸿妫�娴嬶紙瑙e喅璺ㄩ〉绌鸿闂锛� + */ + private boolean isRowEmpty(XWPFTableRow row) { + if (row == null || row.getTableCells().isEmpty()) { + return true; + } + + boolean allCellsEmpty = true; + for (XWPFTableCell cell : row.getTableCells()) { + String text = getCellText(cell).trim(); + // 淇濈暀鍖呭惈鎹㈣绗︾瓑鐨勫崟鍏冩牸浣滀负闈炵┖琛� + if (!text.isEmpty() && !text.replaceAll("\\s+", "").isEmpty()) { + allCellsEmpty = false; + break; + } + } + return allCellsEmpty; + } + + /** + * 鏂囨。绫诲瀷鏍¢獙 - 闃叉浜屼繚浼犲叆涓変繚鎴栧弽涔� + */ + private boolean isWrongDocumentType(XWPFTable table, String requestedType) { + boolean hasRepairTitle = false; + boolean hasOperatorTitle = false; + boolean hasMaintenancePart = false; + + // 鍙鏌ュ墠10琛岋紙閫氬父鏍囬鍦ㄥ墠鍑犺锛� + int maxRows = Math.min(table.getNumberOfRows(), 10); + + for (int i = 0; i < maxRows; i++) { + XWPFTableRow row = table.getRow(i); + if (row == null) continue; + + // 妫�鏌ユ墍鏈夊崟鍏冩牸鍐呭 + for (int j = 0; j < row.getTableCells().size(); j++) { + String cellText = getCellText(row.getCell(j)); + + // 妫�鏌ヤ簩绾т繚鍏荤壒寰� + if (cellText.contains(REPAIR_TITLE)) { + hasRepairTitle = true; + } + if (cellText.contains(OPERATOR_TITLE)) { + hasOperatorTitle = true; + } + + // 妫�鏌ヤ笁绾т繚鍏荤壒寰� + if (cellText.contains("淇濆吇閮ㄤ綅")) { + hasMaintenancePart = true; + } + } + } + + // 閫昏緫鍒ゆ柇 + if ("SECOND".equals(requestedType)) { + // 濡傛灉璇锋眰瀵煎叆浜岀骇淇濆吇锛屼絾鏂囨。涓湁涓夌骇淇濆吇鐗瑰緛 + return !(hasRepairTitle || hasOperatorTitle) || hasMaintenancePart; + } else if ("THIRD".equals(requestedType)) { + // 濡傛灉璇锋眰瀵煎叆涓夌骇淇濆吇锛屼絾鏂囨。涓湁浜岀骇淇濆吇鐗瑰緛 + return !hasMaintenancePart || (hasRepairTitle || hasOperatorTitle); + } + + return false; + } + + /** + * 鎻愬彇璁惧鍩烘湰淇℃伅 + */ + private EamMaintenanceStandard extractDeviceInfo(XWPFTable table) { + if (table.getNumberOfRows() < 2) return null; + + // 鎻愬彇鍓嶄袱琛屾暟鎹� + Map<String, String> row1Data = extractRowData(table.getRow(0)); + Map<String, String> row2Data = extractRowData(table.getRow(1)); + + // 鍒涘缓璁惧鏍囧噯瀵硅薄 + EamMaintenanceStandard standard = new EamMaintenanceStandard(); + standard.setEquipmentText(row1Data.get("璁惧绫诲埆")); + standard.setEquipmentCode(row1Data.get("璁惧缂栧彿")); + standard.setEquipmentName(row2Data.get("璁惧鍚嶇О")); + standard.setEquipmentModel(row2Data.get("璁惧鍨嬪彿")); + + // 鍏宠仈璁惧ID + if (StrUtil.isNotEmpty(standard.getEquipmentCode())) { + EamEquipment equipments = eamEquipmentService.selectByEquipmentCode(standard.getEquipmentCode()); + if (equipments == null) { + return null; + }else { + standard.setEquipmentId(equipments.getId()); + } + } + + return standard; + } + + // 绠�鏄撶増瀛楃涓查潪绌哄垽鏂� + private boolean isNotBlank(String str) { + return str != null && !str.trim().isEmpty(); + } + + /** + * 琛ㄦ牸琛屾暟鎹В鏋� + */ + private Map<String, String> extractRowData(XWPFTableRow row) { + Map<String, String> data = new HashMap<>(); + int cellCount = row.getTableCells().size(); + + try { + // 閿�煎妯″紡 (鏍囩|鍊紎鏍囩|鍊�) + if (cellCount >= 4 && cellCount % 2 == 0) { + for (int i = 0; i < cellCount; i += 2) { + String key = cleanKey(getCellText(row.getCell(i))); + String value = getCellText(row.getCell(i + 1)); + if (!key.isEmpty()) data.put(key, value); + } + } + // 杩炵画鍗曞厓鏍兼ā寮� + else { + for (int i = 0; i < cellCount; i++) { + String text = getCellText(row.getCell(i)); + int colonIndex = text.indexOf('锛�'); + if (colonIndex > 0) { + String key = cleanKey(text.substring(0, colonIndex)); + String value = text.substring(colonIndex + 1); + data.put(key, value); + } + } + } + } catch (Exception e) { + log.error("琛屾暟鎹В鏋愬紓甯�", e); + } + return data; + } + + /** + * 閿悕鏍囧噯鍖栧鐞� + */ + private String cleanKey(String key) { + if (key == null) return ""; + // 绉婚櫎绌烘牸鍜屼腑鏂囧啋鍙� + return key.replaceAll("\\s", "").replace("锛�", ""); + } + + /** + * 閰嶇疆淇濆吇鏍囧噯鍙傛暟 + */ + private void configureStandard(EamMaintenanceStandard standard, String type, MultipartFile file) { + // 鍩虹鍙傛暟 + String filename = file.getOriginalFilename(); + if (filename != null && filename.contains(".")) { + filename = filename.substring(0, filename.lastIndexOf('.')); + } + + standard.setStandardName(filename) + .setInitialDate(new Date()) + .setStandardStatus(MaintenanceStandardStatusEnum.START.name()) + .setStandardVersion("v" + CommonConstant.OPERATE_TYPE_1) + .setDelFlag(0) + .setStandardCode(businessCodeRuleService.generateBusinessCodeSeq( + BusinessCodeConst.MAINTENANCE_STANDARD_CODE_RULE + )); + + // 绫诲瀷鐗瑰畾鍙傛暟 + if ("SECOND".equals(type)) { + standard.setMaintenanceCategory("SECOND_MAINTENANCE") + .setMaintenancePeriod(6) + .setPeriodUnit("鏈�"); + } else { + standard.setMaintenanceCategory("THIRD_MAINTENANCE") + .setPeriodUnit("骞�"); + + // 鑾峰彇涓夌骇淇濆吇鍛ㄦ湡 + if (standard.getEquipmentId() != null) { + EamEquipmentExtend extend = eamEquipmentExtendService.getById(standard.getEquipmentId()); + if (extend != null) { + standard.setMaintenancePeriod(extend.getThirdMaintenancePeriod()); + } + } + } + } + + /** + * 鍒涘缓淇濆吇椤圭洰 + */ + private EamMaintenanceStandardDetail createItem( + EamMaintenanceStandardDetailCategory category, + String content, + String standardId) { + + EamMaintenanceStandardDetail item = new EamMaintenanceStandardDetail(); + item.setItemCategory(category.name()); + item.setStandardId(standardId); + item.setItemName(cleanContent(content)); + return item; + } + + /** + * 鍏煎鐗堝崟鍏冩牸鏂囨湰鎻愬彇 + */ + private String getCellText(XWPFTableCell cell) { + if (cell == null) return ""; + + StringBuilder text = new StringBuilder(); + for (XWPFParagraph para : cell.getParagraphs()) { + if (para != null) { + for (XWPFRun run : para.getRuns()) { + if (run != null) { + String runText = run.getText(0); + if (runText != null) { + text.append(runText); + } + } + } + } + } + return text.toString(); + } + + /** + * 鍐呭娓呯悊 + */ + private String cleanContent(String text) { + if (text == null) return ""; + + // 鏇挎崲鐗规畩绌烘牸鍜屽悎骞惰繛缁┖鏍� + text = text.replace('\u00A0', ' ') + .replace('\u2007', ' ') + .replace('\u202F', ' ') + .replaceAll("\\s+", " "); + + // 瑙勮寖鏍囩偣绗﹀彿 + return text.replace(',', '銆�') + .replace('锛�', '銆�') + .replace(';', '锛�') + .replace('锛�', '锛�') + .replace(':', '锛�') + .replace('锛�', '锛�') + .trim(); + } + + /** + * 楠岃瘉鏈夋晥椤圭洰琛� + */ + private boolean isValidItemRow(XWPFTableRow row) { + return row != null && + row.getTableCells().size() >= 2 && + !getCellText(row.getCell(1)).trim().isEmpty(); + } + + /*瀵煎叆浜屼繚涓変繚鏂囦欢Excel--------------------------缁撴潫*/ + } -- Gitblit v1.9.3