diff --git a/src/main/java/com/glxp/udidl/admin/controller/device/UdiInfoController.java b/src/main/java/com/glxp/udidl/admin/controller/device/UdiInfoController.java index b86f24e..33d50cc 100644 --- a/src/main/java/com/glxp/udidl/admin/controller/device/UdiInfoController.java +++ b/src/main/java/com/glxp/udidl/admin/controller/device/UdiInfoController.java @@ -16,6 +16,7 @@ import com.glxp.udidl.admin.res.PageSimpleResponse; import com.glxp.udidl.admin.res.UdiAppResponse; import com.glxp.udidl.admin.service.dataSync.DeviceSyncService; import com.glxp.udidl.admin.service.inout.ProductInfoService; +import com.glxp.udidl.admin.util.DateUtil; import com.glxp.udidl.admin.util.FilterUdiUtils; import com.glxp.udidl.admin.util.ResultVOUtils; import io.swagger.annotations.ApiOperation; @@ -77,50 +78,16 @@ public class UdiInfoController { } else { if (StrUtil.isNotEmpty(udiEntity.getUdi())) { ProductInfoEntity productInfoEntity = productInfoEntityList.get(0); - if ("是".equals(productInfoEntity.getScbssfbhph()) && StrUtil.isEmpty(udiEntity.getBatchNo())) { - checkSuccess = false; - lostMsg = lostMsg + ",批次号"; - } - if ("是".equals(productInfoEntity.getScbssfbhscrq()) && StrUtil.isEmpty(udiEntity.getProduceDate())) { - checkSuccess = false; - lostMsg = lostMsg + ",生产日期"; - } - - if ("是".equals(productInfoEntity.getScbssfbhsxrq()) && StrUtil.isEmpty(udiEntity.getExpireDate())) { - checkSuccess = false; - lostMsg = lostMsg + ",失效日期"; - } - if (("是".equals(productInfoEntity.getScbssfbhxlh()) && StrUtil.isEmpty(udiEntity.getSerialNo())) - ) { - checkSuccess = false; - lostMsg = lostMsg + ",序列号"; - } - if (!checkSuccess) { - lostMsg = "UDI码格式错误,缺少" + lostMsg.substring(1); - } - if (StrUtil.isNotEmpty(udiEntity.getSerialNo()) && udiEntity.getSerialNo().length() > 20) { - checkSuccess = false; - lostMsg = lostMsg + ",序列号超出20位"; - } - if (StrUtil.isNotEmpty(udiEntity.getBatchNo()) && udiEntity.getBatchNo().length() > 20) { - checkSuccess = false; - lostMsg = lostMsg + ",批次号超出20位"; - } - if (StrUtil.isEmpty(udiEntity.getSerialNo()) && StrUtil.isEmpty(udiEntity.getBatchNo())) { - lostMsg = lostMsg + ",UDI码批次号与序列号不能全为空!"; - checkSuccess = false; - } - if (checkSuccess) { - lostMsg = "校验成功!"; - } else { - lostMsg = lostMsg.substring(1); - } + lostMsg = getErrMsg(productInfoEntity, udiEntity, lostMsg); + //缺少GS1强行添加分隔符进行判断 + processUdiEntity(udiEntity, lostMsg); for (ProductInfoEntity temp : results) { temp.setBatchNo(udiEntity.getBatchNo()); temp.setExpireDate(udiEntity.getExpireDate()); temp.setProduceDate(udiEntity.getProduceDate()); temp.setSerialNo(udiEntity.getSerialNo()); } + lostMsg = getErrMsg(productInfoEntity, udiEntity, "扫码识别格式错误,已进行AI自动校正"); } } UdiAppResponse udiAppResponse = new UdiAppResponse(); @@ -130,6 +97,113 @@ public class UdiInfoController { return ResultVOUtils.success(udiAppResponse); } + + private static final String PRODUCTION_DATE_CODE = "11"; + private static final String EXPIRATION_DATE_CODE = "17"; + private static final String SERIAL_NUMBER_CODE = "21"; + + public void processUdiEntity(UdiEntity udiEntity, String lostMsg) { + if (StrUtil.isNotEmpty(udiEntity.getBatchNo()) && isRelevantMessage(lostMsg)) { + String batchNo = udiEntity.getBatchNo(); // 避免重复调用,提高性能 + if (lostMsg.contains("生产日期")) { + if (processDate(udiEntity, batchNo, PRODUCTION_DATE_CODE)) { + return; + } + } + + if (lostMsg.contains("失效日期")) { + if (processDate(udiEntity, batchNo, EXPIRATION_DATE_CODE)) + return; + } + + if (lostMsg.contains("序列号")) { + if (extractSerialNumber(udiEntity, batchNo, SERIAL_NUMBER_CODE)) + return; + } + } + } + + + private boolean isRelevantMessage(String lostMsg) { + return lostMsg.contains("生产日期") || lostMsg.contains("失效日期") || lostMsg.contains("序列号"); + } + + private boolean processDate(UdiEntity udiEntity, String batchNo, String dateCode) { + int index = batchNo.indexOf(dateCode) + 2; + if (index > 1 && index + 6 <= batchNo.length()) { + String dateStr = batchNo.substring(index, index + 6); + if (DateUtil.isValidDate(dateStr)) { + // 正确处理生产日期和失效日期的赋值 + if (PRODUCTION_DATE_CODE.equals(dateCode)) { + udiEntity.setProduceDate(dateStr); + } else if (EXPIRATION_DATE_CODE.equals(dateCode)) { + udiEntity.setExpireDate(dateStr); // 假设存在此方法,用于设置失效日期 + } + udiEntity.setBatchNo(batchNo.substring(0, index)); + return true; + } + } + return false; + } + + private boolean extractSerialNumber(UdiEntity udiEntity, String batchNo, String serialNumberCode) { + int index = batchNo.indexOf(serialNumberCode)+2; + if (index != -1 && index + 2 <= batchNo.length()) { + // 假设序列号是6位数字,此处可以根据实际需求进行调整 + String serialNumber = batchNo.substring(index); + // 对序列号进行一些合法性校验,如果需要 + // ... + udiEntity.setSerialNo(serialNumber); // 假设存在此方法,用于设置序列号 + udiEntity.setBatchNo(batchNo.substring(0, index-2)); + return true; + } + return false; + } + + public String getErrMsg(ProductInfoEntity productInfoEntity, UdiEntity udiEntity, String lostMsg) { + boolean checkSuccess = true; + if ("是".equals(productInfoEntity.getScbssfbhph()) && StrUtil.isEmpty(udiEntity.getBatchNo())) { + checkSuccess = false; + lostMsg = lostMsg + ",批次号"; + } + if ("是".equals(productInfoEntity.getScbssfbhscrq()) && StrUtil.isEmpty(udiEntity.getProduceDate())) { + checkSuccess = false; + lostMsg = lostMsg + ",生产日期"; + } + + if ("是".equals(productInfoEntity.getScbssfbhsxrq()) && StrUtil.isEmpty(udiEntity.getExpireDate())) { + checkSuccess = false; + lostMsg = lostMsg + ",失效日期"; + } + if (("是".equals(productInfoEntity.getScbssfbhxlh()) && StrUtil.isEmpty(udiEntity.getSerialNo())) + ) { + checkSuccess = false; + lostMsg = lostMsg + ",序列号"; + } + if (!checkSuccess) { + lostMsg = "UDI码格式错误,缺少" + lostMsg.substring(1); + } + if (StrUtil.isNotEmpty(udiEntity.getSerialNo()) && udiEntity.getSerialNo().length() > 20) { + checkSuccess = false; + lostMsg = lostMsg + ",序列号超出20位"; + } + if (StrUtil.isNotEmpty(udiEntity.getBatchNo()) && udiEntity.getBatchNo().length() > 20) { + checkSuccess = false; + lostMsg = lostMsg + ",批次号超出20位"; + } + if (StrUtil.isEmpty(udiEntity.getSerialNo()) && StrUtil.isEmpty(udiEntity.getBatchNo())) { + lostMsg = lostMsg + ",UDI码批次号与序列号不能全为空!"; + checkSuccess = false; + } + if (checkSuccess) { + lostMsg = "校验成功!"; + } else { + lostMsg = lostMsg.substring(1); + } + return lostMsg; + } + + @GetMapping("udi/basic/app/mutilScan") public BaseResponse mutilScan(MutiScanRequest mutiScanRequest) { if (StrUtil.isEmpty(mutiScanRequest.getCode())) { diff --git a/src/main/java/com/glxp/udidl/admin/util/DateUtil.java b/src/main/java/com/glxp/udidl/admin/util/DateUtil.java index d14db2f..2016398 100644 --- a/src/main/java/com/glxp/udidl/admin/util/DateUtil.java +++ b/src/main/java/com/glxp/udidl/admin/util/DateUtil.java @@ -9,6 +9,9 @@ import java.lang.management.ManagementFactory; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -439,4 +442,15 @@ public class DateUtil extends DateUtils { } return 0; } -} \ No newline at end of file + + + public static boolean isValidDate(String dateStr) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyMMdd"); + try { + LocalDate.parse(dateStr, formatter); + return true; // 如果没有抛出异常,说明日期格式正确且有效 + } catch (DateTimeParseException e) { + return false; // 抛出异常,说明日期格式不正确或无效 + } + } +}