| | |
| | | import com.ruoyi.inspect.pojo.*; |
| | | import com.ruoyi.inspect.service.*; |
| | | import com.ruoyi.inspect.util.HackLoopTableRenderPolicy; |
| | | import com.ruoyi.inspect.util.PreserveReportPicturePlaceholderHandler; |
| | | import com.ruoyi.inspect.vo.InsOrderPlanTaskSwitchVo; |
| | | import com.ruoyi.inspect.vo.InsOrderPlanVO; |
| | | import com.ruoyi.inspect.vo.InsSampleUserVO; |
| | |
| | | InsOrder order = insOrderMapper.selectOne(Wrappers.<InsOrder>lambdaQuery() |
| | | .eq(InsOrder::getId, orderId) |
| | | .last("FOR UPDATE")); |
| | | InsReport existingReport = insReportMapper.selectOne(Wrappers.<InsReport>lambdaQuery() |
| | | .eq(InsReport::getInsOrderId, orderId)); |
| | | if (existingReport != null && existingReport.getReportType() != null |
| | | && !Objects.equals(existingReport.getReportType(), reportType)) { |
| | | throw new ErrorException("å䏿£éªè®¢ååªè½ä½¿ç¨ä¸ç§æ¥åç±»å"); |
| | | } |
| | | boolean isInspectionReport = Objects.equals(INSPECTION_REPORT, reportType); |
| | | registerInsResults = isInspectionReport && Boolean.TRUE.equals(registerInsResults); |
| | | // 1. 夿æ¯å¦æéå¤ç¼å·, æéå¤ç¼å·åæé |
| | |
| | | } |
| | | } |
| | | } |
| | | // æ°çæ£æµæ¥åç´æ¥ä½¿ç¨åºå®ååç»æè¡ï¼ä¸åæé æ§çå¯åå TableRenderDataã |
| | | // æ£éªæ¥å仿²¿ç¨ä¸æ¹åæçå¤§è¡¨æ ¼é»è¾ã |
| | | if (!showResult) { |
| | | return; |
| | | } |
| | | // æ¶æ ·æ¥æ |
| | | List<RowRenderData> rows = new ArrayList<>(); |
| | | List<TextRenderData> text = new ArrayList<>(); |
| | |
| | | for (String s : standardMethod) { |
| | | standardMethod2.append("ã").append(s); |
| | | } |
| | | standardMethod2.replace(0, 1, ""); |
| | | if (standardMethod2.length() > 0) { |
| | | standardMethod2.deleteCharAt(0); |
| | | } |
| | | tables.forEach(table -> { |
| | | table.put("tableSize", tables.size() + 1); |
| | | }); |
| | |
| | | // æ½æ ·æ¥æ |
| | | // æåæ¯æ½æ ·æ¶é´ |
| | | LocalDateTime sendTime = insOrder.getSendTime(); |
| | | if (insOrder.getTypeSource() != null && insOrder.getTypeSource().equals(1)) { |
| | | IfsInventoryQuantity reportInventory = null; |
| | | if (insOrder.getIfsInventoryId() != null) { |
| | | reportInventory = ifsInventoryQuantityMapper.selectById(insOrder.getIfsInventoryId()); |
| | | } |
| | | if (insOrder.getTypeSource() != null && insOrder.getTypeSource().equals(1) && reportInventory != null) { |
| | | // åæææ¶æ ·æ¥ææ¯æ¥æ£æ¥æ |
| | | IfsInventoryQuantity ifsInventoryQuantity = ifsInventoryQuantityMapper.selectById(insOrder.getIfsInventoryId()); |
| | | sendTime = ifsInventoryQuantity.getDeclareDate(); |
| | | sendTime = reportInventory.getDeclareDate(); |
| | | } |
| | | |
| | | // æ£éªæ¶é´ æ½æ ·æ¶é´-æäº¤æ¶é´ |
| | |
| | | List<Map<String, String>> finalDeviceList = deviceList; |
| | | String finalModelStr = modelStr; |
| | | |
| | | InputStream inputStream = this.getClass().getResourceAsStream(showResult ? "/static/report-template.docx" : "/static/detection-report-template.docx"); |
| | | List<Map<String, String>> detectionResultRows = buildDetectionResultRows(samples); |
| | | String productName = defaultReportText(insOrder.getSampleView(), insOrder.getSample()); |
| | | String customerName = defaultReportText(custom == null ? null : custom.getCompany(), insOrder.getCompany()); |
| | | String detectionCategory = defaultReportText(finalOrderType, "å§ææ£æµ"); |
| | | String sampleCode = samples.stream() |
| | | .map(SampleProductDto::getSampleCode) |
| | | .filter(StringUtils::isNotBlank) |
| | | .distinct() |
| | | .collect(Collectors.joining("ã")); |
| | | String productAndModel = productName; |
| | | if (StringUtils.isNotBlank(finalModelStr) && !Objects.equals(productName, finalModelStr)) { |
| | | productAndModel = productName + " " + finalModelStr; |
| | | } |
| | | String standardMethodText = "null".equals(standardMethod2.toString()) ? "" : standardMethod2.toString(); |
| | | String productionDate = "/"; |
| | | String batchNo = "/"; |
| | | if (reportInventory != null) { |
| | | if (reportInventory.getProductDate() != null) { |
| | | productionDate = reportInventory.getProductDate().format(DateTimeFormatter.ofPattern("yyyyå¹´MMæddæ¥")); |
| | | } |
| | | batchNo = defaultReportText(reportInventory.getUpdateBatchNo(), reportInventory.getLotBatchNo()); |
| | | } |
| | | boolean containsOrganicChlorine = detectionResultRows.stream() |
| | | .map(row -> row.get("itemName")) |
| | | .filter(Objects::nonNull) |
| | | .anyMatch(name -> name.contains("ææºæ°¯")); |
| | | String resultRemark = containsOrganicChlorine |
| | | ? "å¾®åºä»æ³ï¼ææºæ°¯å«éå°äº0.005%æ¶ï¼ä»¥æªæ£åºæ¥åºã" |
| | | : defaultReportText(insOrder.getRemark(), "/"); |
| | | |
| | | // æ£æµæ¥ååæ£éªæ¥åç»ä¸ä½¿ç¨æ°çæ¥åæ¨¡æ¿ |
| | | String templateName = "/static/detection-report-template-v2.docx"; |
| | | InputStream inputStream = this.getClass().getResourceAsStream(templateName); |
| | | if (inputStream == null) { |
| | | throw new RuntimeException("æ¾ä¸å°æ¥å模æ¿: " + templateName); |
| | | } |
| | | Configure configure = Configure.builder() |
| | | .bind("deviceList", new HackLoopTableRenderPolicy()) |
| | | .bind("r", new HackLoopTableRenderPolicy(true)) |
| | | .setValidErrorHandler(new PreserveReportPicturePlaceholderHandler()) |
| | | .useSpringEL(true) |
| | | .build(); |
| | | |
| | | String finalProductAndModel = productAndModel; |
| | | String finalProductName = productName; |
| | | String finalCustomerName = customerName; |
| | | String finalDetectionCategory = detectionCategory; |
| | | String finalSampleCode = defaultReportText(sampleCode, "/"); |
| | | String finalStandardMethodText = defaultReportText(standardMethodText, "/"); |
| | | String finalProductionDate = productionDate; |
| | | String finalBatchNo = batchNo; |
| | | String finalResultRemark = resultRemark; |
| | | String equipmentText = buildEquipmentText(finalDeviceList); |
| | | String issueDate = now.format(DateTimeFormatter.ofPattern("yyyyå¹´MMæddæ¥")); |
| | | XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render( |
| | | new HashMap<String, Object>() {{ |
| | | put("order", insOrder); |
| | |
| | | put("sampleSize", insOrder.getTestQuantity()); |
| | | put("tables", tables); |
| | | put("tableSize", tables.size() + 1); |
| | | put("standardMethod", (standardMethod2.toString().equals("null") ? "" : standardMethod2)); |
| | | put("standardMethod", finalStandardMethodText); |
| | | put("deviceList", finalDeviceList); |
| | | put("twoCode", null); |
| | | put("models", finalModelStr); |
| | |
| | | put("resultCh", finalResultCh); |
| | | put("resultEn", finalResultEn); |
| | | put("sampleStatus", sampleStatus); |
| | | // æ°çæ£æµæ¥å模æ¿ï¼åç
§ãææºæ°¯æ£æµæ¥åãï¼ä½¿ç¨çæå¹³å段ã |
| | | put("no", insReport.getCode()); |
| | | put("cover", finalProductAndModel); |
| | | put("product", finalProductName); |
| | | put("model", defaultReportText(finalModelStr, "/")); |
| | | put("customer", finalCustomerName); |
| | | put("category", finalDetectionCategory); |
| | | put("date", issueDate); |
| | | put("maker", defaultReportText(insOrder.getProduction(), "/")); |
| | | put("env", finalEnvironment); |
| | | put("qty", defaultReportText(insOrder.getTestQuantity(), "/")); |
| | | put("receive", finalSendTime.format(DateTimeFormatter.ofPattern("yyyyå¹´MMæddæ¥"))); |
| | | put("sampleNo", finalSampleCode); |
| | | put("appearance", defaultReportText(sampleStatus, "/")); |
| | | put("sender", defaultReportText(insOrder.getPrepareUser(), "/")); |
| | | put("produced", finalProductionDate); |
| | | put("batch", finalBatchNo); |
| | | put("phone", defaultReportText(insOrder.getPhone(), "/")); |
| | | put("testDate", insTime); |
| | | put("entrustNo", defaultReportText(insOrder.getEntrustCode(), "/")); |
| | | put("sampling", "/"); |
| | | put("standard", finalStandardMethodText); |
| | | put("equipment", equipmentText); |
| | | put("conclusion", "æ ·åæå§ææ¹è¦æ±è¿è¡æ£æµï¼æ°æ®è§æ£æµç»æã"); |
| | | put("remark", "æ¬æ¥åæ£æµç»æä»
坹彿¥ææ¥æ¶æ ·åè´è´£ã"); |
| | | put("r", detectionResultRows); |
| | | put("resultNote", finalResultRemark); |
| | | }}); |
| | | try { |
| | | // ä¿®æ¹æ¢è¡ååå¹¶é®é¢ |
| | | updaeMerge(template.getXWPFDocument(), false); |
| | | String name = insReport.getCode().replace("/", "") + (showResult ? ".docx" : "-C.docx"); |
| | | Files.createDirectories(Paths.get(wordUrl)); |
| | | template.writeAndClose(Files.newOutputStream(Paths.get(wordUrl, name))); |
| | |
| | | } |
| | | |
| | | /** |
| | | * æ°çæ£æµæ¥åçç»æè¡¨åºå®ä¸ºååï¼åºå·ãæ£æµé¡¹ç®ãåä½ãæ£æµç»æã |
| | | */ |
| | | private static List<Map<String, String>> buildDetectionResultRows(List<SampleProductDto> samples) { |
| | | List<Map<String, String>> rows = new ArrayList<>(); |
| | | if (CollectionUtils.isEmpty(samples)) { |
| | | return rows; |
| | | } |
| | | |
| | | boolean multipleSamples = samples.size() > 1; |
| | | int sequence = 1; |
| | | for (SampleProductDto sample : samples) { |
| | | if (CollectionUtils.isEmpty(sample.getInsProduct())) { |
| | | continue; |
| | | } |
| | | for (InsProduct product : sample.getInsProduct()) { |
| | | // ç»å®é¡¹ç®ä»
åä¸è®¡ç®ï¼ä¸åºåºç°å¨æ¥åä¸ã |
| | | if (product == null || Objects.equals(product.getIsBinding(), 1)) { |
| | | continue; |
| | | } |
| | | |
| | | String itemName = defaultReportText( |
| | | product.getInspectionItemSubclass(), |
| | | defaultReportText(product.getInspectionItem(), product.getInspectionItemClass())); |
| | | if (multipleSamples && StringUtils.isNotBlank(sample.getSampleCode())) { |
| | | itemName = sample.getSampleCode() + " - " + itemName; |
| | | } |
| | | |
| | | Map<String, String> row = new LinkedHashMap<>(); |
| | | row.put("seq", String.valueOf(sequence++)); |
| | | row.put("itemName", itemName); |
| | | row.put("unit", defaultReportText(product.getUnit(), "/")); |
| | | row.put("result", defaultReportText(product.getLastValue(), "/")); |
| | | rows.add(row); |
| | | } |
| | | } |
| | | return rows; |
| | | } |
| | | |
| | | private static String buildEquipmentText(List<Map<String, String>> deviceList) { |
| | | if (CollectionUtils.isEmpty(deviceList)) { |
| | | return "/"; |
| | | } |
| | | Set<String> lines = new LinkedHashSet<>(); |
| | | int sequence = 1; |
| | | for (Map<String, String> device : deviceList) { |
| | | String name = getMapText(device, "deviceName", "device_name"); |
| | | String managementNumber = getMapText(device, "managementNumber", "management_number"); |
| | | String line = sequence++ + ". " + defaultReportText(name, "/"); |
| | | if (StringUtils.isNotBlank(managementNumber)) { |
| | | line += "ï¼" + managementNumber; |
| | | } |
| | | lines.add(line); |
| | | } |
| | | return String.join("\n", lines); |
| | | } |
| | | |
| | | private static String getMapText(Map<String, String> map, String... keys) { |
| | | if (map == null) { |
| | | return null; |
| | | } |
| | | for (String key : keys) { |
| | | String value = map.get(key); |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return value; |
| | | } |
| | | for (Map.Entry<String, String> entry : map.entrySet()) { |
| | | if (entry.getKey() != null && entry.getKey().equalsIgnoreCase(key) |
| | | && StringUtils.isNotBlank(entry.getValue())) { |
| | | return entry.getValue(); |
| | | } |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private static String defaultReportText(String value, String fallback) { |
| | | if (StringUtils.isNotBlank(value)) { |
| | | return value; |
| | | } |
| | | return StringUtils.isNotBlank(fallback) ? fallback : "/"; |
| | | } |
| | | |
| | | /** |
| | | * 计ç®è¡¨æ ¼å宽度 |
| | | * @param max æå¤§æ£éªä¸ªæ° |
| | | * @return |