Browse Source

fix(qms): 解决质检模板导入和数据库更新问题

- 在QcMapper.xml中添加object_id空值判断逻辑,兼容历史数据处理
- 新增getCellBigDecimalValue方法用于安全解析Excel单元格数值
- 优化Excel导入逻辑,添加空行检测和模板名称验证
- 统一使用getCellStringValue方法处理Excel单元格读取
- 完善检验类型验证,对无效类型抛出明确错误信息
- 修复抽样方案字段解析问题,支持数值和文本格式数据
- 优化检验项目读取逻辑,避免越界访问和格式错误
- 添加数据校验增强,提供更准确的错误提示信息
- 修复自动提交流程中的条件判断问题
master
qiankanghui 3 months ago
parent
commit
7db19383e2
  1. 132
      src/main/java/com/xujie/modules/qms/service/impl/QcServiceImpl.java
  2. 7
      src/main/resources/mapper/qms/QcMapper.xml

132
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()) {

7
src/main/resources/mapper/qms/QcMapper.xml

@ -3137,7 +3137,12 @@
<!-- 兼容历史数据:若 order_id 为空,退回 item_no + object_id 组合定位 -->
<otherwise>
and item_no = #{itemNo}
and object_id = #{objectId}
<if test="objectId != null and objectId != ''">
and object_id = #{objectId}
</if>
<if test="objectId == null or objectId == ''">
and object_id is null
</if>
</otherwise>
</choose>
</update>

Loading…
Cancel
Save