From 7db19383e22a7bca817e79bf34639dd13fe50749 Mon Sep 17 00:00:00 2001 From: qiankanghui Date: Wed, 2 Sep 2026 11:53:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(qms):=20=E8=A7=A3=E5=86=B3=E8=B4=A8?= =?UTF-8?q?=E6=A3=80=E6=A8=A1=E6=9D=BF=E5=AF=BC=E5=85=A5=E5=92=8C=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在QcMapper.xml中添加object_id空值判断逻辑,兼容历史数据处理 - 新增getCellBigDecimalValue方法用于安全解析Excel单元格数值 - 优化Excel导入逻辑,添加空行检测和模板名称验证 - 统一使用getCellStringValue方法处理Excel单元格读取 - 完善检验类型验证,对无效类型抛出明确错误信息 - 修复抽样方案字段解析问题,支持数值和文本格式数据 - 优化检验项目读取逻辑,避免越界访问和格式错误 - 添加数据校验增强,提供更准确的错误提示信息 - 修复自动提交流程中的条件判断问题 --- .../qms/service/impl/QcServiceImpl.java | 132 ++++++++---------- src/main/resources/mapper/qms/QcMapper.xml | 7 +- 2 files changed, 66 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/xujie/modules/qms/service/impl/QcServiceImpl.java b/src/main/java/com/xujie/modules/qms/service/impl/QcServiceImpl.java index 9a6aa0f..30c050b 100644 --- a/src/main/java/com/xujie/modules/qms/service/impl/QcServiceImpl.java +++ b/src/main/java/com/xujie/modules/qms/service/impl/QcServiceImpl.java @@ -423,6 +423,21 @@ public class QcServiceImpl implements QcService { } } + /** + * 读取单元格的数值,空单元格返回 null;文本格式的数字同样兼容解析。 + **/ + private BigDecimal getCellBigDecimalValue(XSSFCell cell) { + String value = getCellStringValue(cell); + if (StringUtils.isBlank(value)) { + return null; + } + try { + return new BigDecimal(value.trim()); + } catch (NumberFormatException e) { + throw new RuntimeException("数值格式无效:" + value); + } + } + // ======================= 质检员信息维护 ======================= /** @@ -857,24 +872,24 @@ public class QcServiceImpl implements QcService { int rows = sheet.getPhysicalNumberOfRows(); // 声明对象 QcTemplateData task = null; - //排除标准数据下的空白行数据 - int nonEmptyRows = 0; - for (int i = 0; i < rows; i++) { - XSSFRow row = sheet.getRow(i); - if (row != null && row.getCell(0) != null && StringUtils.isNotBlank(row.getCell(0).toString())) { - nonEmptyRows++; - } - } // 遍历每一行(从第二行开始) - for (int j = 1; j < nonEmptyRows; j++) { - // 实例化对象 - task = new QcTemplateData(); + for (int j = 1; j < rows; j++) { // 获得该行 XSSFRow row = sheet.getRow(j); + // 模板名称为空视为空行直接跳过,兼容行间/行尾残留的空白行 + String templateName = getCellStringValue(row == null ? null : row.getCell(0)); + if (StringUtils.isBlank(templateName)) { + continue; + } + // 实例化对象 + task = new QcTemplateData(); //截取site和bu QcSiteBuHelper.resolveSiteParam(paraData.getSite(), task::setSite, task::setBuNo); - //获取用户传入的 检验类型描述 - String type = row.getCell(9).getStringCellValue(); + //获取用户传入的 检验类型描述(getCell 对从未写入过的单元格返回 null,统一走工具方法取值) + String type = getCellStringValue(row.getCell(9)); + if (StringUtils.isBlank(type)) { + throw new RuntimeException("第" + (j + 1) + "行检验类型不能为空!"); + } //根据type 设置检验类型编码 和 检验模板编码 if (type.equals("IPQC")){ task.setInspectionTypeNo("101"); @@ -891,6 +906,8 @@ public class QcServiceImpl implements QcService { }else if (type.equals("PQC")){ task.setInspectionTypeNo("108"); task.setTemplateId("PQC-" + qcMapper.getTemplateNo(task)); + } else { + throw new RuntimeException("第" + (j + 1) + "行检验类型无效:" + type + "(只能填写 IPQC/IQC/FAI/FQC/PQC)"); } // task.setInspectionTypeNo(row.getCell(9).getRawValue()); @@ -909,70 +926,37 @@ public class QcServiceImpl implements QcService { // } //判断用户是否输入 抽样方案编码、默认抽样数量和默认抽样比例 其中之一 boolean isFlag = false; - task.setTemplateName(row.getCell(0).getStringCellValue()); -// task.setSamplingProgrammeNo(row.getCell(1).getRawValue()); -// task.setSamplingLevelNo(row.getCell(2).getRawValue()); - if (row.getCell(1)==null){ + task.setTemplateName(templateName.trim()); + String samplingProgrammeNo = getCellStringValue(row.getCell(1)); + if (StringUtils.isBlank(samplingProgrammeNo)){ task.setSamplingProgrammeNo(null); }else { isFlag = true; - task.setSamplingProgrammeNo(row.getCell(1).getRawValue()); + task.setSamplingProgrammeNo(samplingProgrammeNo); } - if (row.getCell(2)==null){ - task.setSamplingLevelNo(null); - }else { - task.setSamplingLevelNo(row.getCell(2).getRawValue()); - } + String samplingLevelNo = getCellStringValue(row.getCell(2)); + task.setSamplingLevelNo(StringUtils.isBlank(samplingLevelNo) ? null : samplingLevelNo); - if (row.getCell(3) == null){ - task.setInspectionCycle(null); - }else { - task.setInspectionCycle(BigDecimal.valueOf(row.getCell(3).getNumericCellValue())); - } - if (row.getCell(4) == null){ - task.setDefaultSamplingQuantity(null); - }else { + task.setInspectionCycle(getCellBigDecimalValue(row.getCell(3))); + BigDecimal defaultSamplingQuantity = getCellBigDecimalValue(row.getCell(4)); + if (defaultSamplingQuantity != null){ isFlag = true; - task.setDefaultSamplingQuantity(BigDecimal.valueOf(row.getCell(4).getNumericCellValue())); } - if (row.getCell(5) == null){ - task.setDefaultSamplingProportion(null); - }else { + task.setDefaultSamplingQuantity(defaultSamplingQuantity); + BigDecimal defaultSamplingProportion = getCellBigDecimalValue(row.getCell(5)); + if (defaultSamplingProportion != null){ isFlag = true; - task.setDefaultSamplingProportion(BigDecimal.valueOf(row.getCell(5).getNumericCellValue())); } + task.setDefaultSamplingProportion(defaultSamplingProportion); if (!isFlag){ - throw new RuntimeException("抽象方案编码、默认抽样数量和默认抽样比例至少填写一个!"); - } - if (row.getCell(6) == null){ - task.setAql(null); - }else { - task.setAql(BigDecimal.valueOf(row.getCell(6).getNumericCellValue())); - } - - if (row.getCell(7) == null){ - task.setAc(null); - }else { - task.setAc(BigDecimal.valueOf(row.getCell(7).getNumericCellValue())); - } - - if (row.getCell(8) == null){ - task.setRe(null); - }else { - task.setRe(BigDecimal.valueOf(row.getCell(8).getNumericCellValue())); - } - if (row.getCell(10) == null){ - task.setTemplateRemark(null); - }else { - task.setTemplateRemark(row.getCell(10).getStringCellValue()); - } -// task.setTemplateVersion(row.getCell(11).getRawValue()); - if (row.getCell(11)==null){ - task.setTemplateVersion(null); - }else { - task.setTemplateVersion(row.getCell(11).getRawValue()); + throw new RuntimeException("第" + (j + 1) + "行抽样方案编码、默认抽样数量和默认抽样比例至少填写一个!"); } + task.setAql(getCellBigDecimalValue(row.getCell(6))); + task.setAc(getCellBigDecimalValue(row.getCell(7))); + task.setRe(getCellBigDecimalValue(row.getCell(8))); + task.setTemplateRemark(getCellStringValue(row.getCell(10))); + task.setTemplateVersion(getCellStringValue(row.getCell(11))); task.setCreateBy(paraData.getCreateBy()); // 查重 QcTemplateData templateData = qcMapper.selectTemplateByDesc(task); @@ -981,17 +965,20 @@ public class QcServiceImpl implements QcService { } // 新增模板 qcMapper.templateSave(task); - // 新增模板中的项目 - int cells = row.getLastCellNum(); - for (int i = 1; i <= cells - 12; i++) { - String itemNo = row.getCell(11 + i).getStringCellValue(); + // 新增模板中的项目(检验项目编码从第13列开始,读到空单元格为止,避免行尾残留格式单元格导致越界读取) + int orderId = 1; + for (int i = 12; ; i++) { + String itemNo = getCellStringValue(row.getCell(i)); + if (StringUtils.isBlank(itemNo)) { + break; + } String itemType = "D"; - QcTemplateData data = qcMapper.selectItem(itemNo,itemType,task.getBuNo()); + QcTemplateData data = qcMapper.selectItem(itemNo.trim(),itemType,task.getBuNo()); if (data== null){ - throw new RuntimeException("工厂为"+task.getSite()+"时,检验项目不存在:"+itemNo); + throw new RuntimeException("第" + (j + 1) + "行,工厂为"+task.getSite()+"时,检验项目不存在:"+itemNo); } data.setTemplateId(task.getTemplateId()); - data.setOrderId(i); + data.setOrderId(orderId++); // 新增 qcMapper.addItemDetails(data); } @@ -3652,6 +3639,7 @@ public class QcServiceImpl implements QcService { // 保存(非应用)时自动审核,无需前端再手动勾选单据点击审核 if (!"1".equals(data.getType())) { autoSubmitIQCRecord(data); + } // // 新增数据采集信息 // if (!data.getSubDetailList().isEmpty()) { diff --git a/src/main/resources/mapper/qms/QcMapper.xml b/src/main/resources/mapper/qms/QcMapper.xml index dc12e68..4d1c536 100644 --- a/src/main/resources/mapper/qms/QcMapper.xml +++ b/src/main/resources/mapper/qms/QcMapper.xml @@ -3137,7 +3137,12 @@ and item_no = #{itemNo} - and object_id = #{objectId} + + and object_id = #{objectId} + + + and object_id is null +