检验项目设置-检验类型功能完成

This commit is contained in:
2025-12-22 11:45:16 +08:00
parent c1074fc4fb
commit ae5f1c795b
2 changed files with 21 additions and 32 deletions

View File

@@ -24,14 +24,6 @@ import java.util.List;
@AllArgsConstructor
public class InspectionTypeController extends BaseController {
// 辅助方法:将字节数组转换为十六进制字符串
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02X ", b));
}
return sb.toString().trim();
}
private final IInspectionTypeService inspectionTypeService;
private final TransactionTemplate transactionTemplate;
@@ -43,7 +35,14 @@ public class InspectionTypeController extends BaseController {
public AjaxResult list(InspectionType inspectionType) {
// 使用Wrapper构建查询条件确保order字段被正确处理
QueryWrapper<InspectionType> queryWrapper = new QueryWrapper<>(inspectionType);
// 默认只查询有效记录(前端也有过滤逻辑,这里是为了减少数据传输量)
if (inspectionType.getValidFlag() == null) {
queryWrapper.eq("valid_flag", 1);
}
List<InspectionType> list = inspectionTypeService.list(queryWrapper);
log.debug("查询检验类型列表:条件={}, 返回记录数={}", inspectionType, list.size());
return AjaxResult.success(list);
}
@@ -72,23 +71,15 @@ public class InspectionTypeController extends BaseController {
QueryWrapper<InspectionType> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("code", inspectionType.getCode());
// 输出调试信息
System.out.println("检查编码唯一性code=" + inspectionType.getCode() + ", 长度=" + inspectionType.getCode().length());
System.out.println("code的十六进制表示" + bytesToHex(inspectionType.getCode().getBytes()));
// 直接查询具体记录,而不仅仅是计数
// 查询是否存在相同编码的记录
List<InspectionType> existingRecords = inspectionTypeService.list(queryWrapper);
System.out.println("数据库中存在记录数" + existingRecords.size());
for (InspectionType record : existingRecords) {
System.out.println("已存在记录id=" + record.getId() + ", code=" + record.getCode() + ", code长度=" + record.getCode().length() + ", code十六进制=" + bytesToHex(record.getCode().getBytes()));
}
log.debug("检查编码唯一性code={}, 数据库中存在记录数={}", inspectionType.getCode(), existingRecords.size());
if (!existingRecords.isEmpty()) {
return AjaxResult.error("检验类型编码已存在");
}
// 保存前再次验证
System.out.println("准备保存数据:" + inspectionType);
try {
// 使用事务确保一致性
@@ -102,11 +93,11 @@ public class InspectionTypeController extends BaseController {
}
boolean result = inspectionTypeService.save(inspectionType);
System.out.println("保存结果:" + result);
log.info("新增检验类型成功code={}, name={}", inspectionType.getCode(), inspectionType.getName());
return toAjax(result);
});
} catch (Exception e) {
System.out.println("保存失败,错误信息:" + e.getMessage());
log.error("新增检验类型失败code={}, 错误信息:{}", inspectionType.getCode(), e.getMessage(), e);
// 捕获唯一性约束冲突异常
if (e.getMessage().contains("uk_inspection_type_code") ||
@@ -139,15 +130,20 @@ public class InspectionTypeController extends BaseController {
try {
boolean result = inspectionTypeService.updateById(inspectionType);
if (result) {
log.info("修改检验类型成功id={}, code={}, name={}", inspectionType.getId(), inspectionType.getCode(), inspectionType.getName());
}
return toAjax(result);
} catch (Exception e) {
log.error("修改检验类型失败id={}, code={}, 错误信息:{}", inspectionType.getId(), inspectionType.getCode(), e.getMessage(), e);
// 捕获唯一性约束冲突异常
if (e.getMessage().contains("uk_inspection_type_code") ||
e.getMessage().contains("duplicate key value") ||
if (e.getMessage().contains("uk_inspection_type_code") ||
e.getMessage().contains("duplicate key value") ||
e.getMessage().contains("检验类型编码已存在")) {
return AjaxResult.error("检验类型编码已存在");
}
return AjaxResult.error("更新失败:" + e.getMessage());
}
}
@@ -180,11 +176,4 @@ public class InspectionTypeController extends BaseController {
return AjaxResult.error("删除失败: " + e.getMessage());
}
}
// 测试删除接口,直接返回成功
@DeleteMapping("/test-delete/{id}")
public AjaxResult testDelete(@PathVariable Long id) {
log.info("测试删除接口ID: {}", id);
return AjaxResult.success("测试删除成功");
}
}