refactor(检验申请): 重构检验申请单生成逻辑,由后端统一处理
- 移除前端生成申请单号的逻辑,改为后端在保存时自动生成 - 申请日期由后端统一处理,前端实时显示当前时间 - 优化金额计算逻辑,确保后端重新计算防止篡改 - 增加废号处理机制,记录生成但保存失败的申请单号 - 简化前端代码,移除不必要的检查逻辑
This commit is contained in:
@@ -19,11 +19,4 @@ public interface IDoctorStationInspectionLabApplyService {
|
||||
* @return 删除结果
|
||||
*/
|
||||
R<?> deleteInspectionLabApply(String applyNo);
|
||||
|
||||
/**
|
||||
* 生成检验申请单号
|
||||
* 规则:LS + YYYYMMDD + 5位流水号(每日从1开始递增)
|
||||
* @return 申请单号
|
||||
*/
|
||||
String generateApplyNo();
|
||||
}
|
||||
|
||||
@@ -94,8 +94,39 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
* 保存检验申请单信息逻辑
|
||||
* 保存检验申请单信息同时根据检验申请单检验项目数据保存检验申请单明细信息
|
||||
*/
|
||||
log.debug("保存检验申请单信息:{}", doctorStationLabApplyDto);
|
||||
log.debug("保存申请单明细信息:{}",doctorStationLabApplyDto.getLabApplyItemList());
|
||||
|
||||
// 申请单号为空或"待生成"时,由后端生成新单号
|
||||
String applyNo = doctorStationLabApplyDto.getApplyNo();
|
||||
boolean isNewApplyNo = false;
|
||||
if (applyNo == null || applyNo.trim().isEmpty() || "待生成".equals(applyNo) || "自动生成".equals(applyNo)) {
|
||||
applyNo = generateApplyNo();
|
||||
isNewApplyNo = true;
|
||||
}
|
||||
// 将生成的单号设置回 DTO
|
||||
doctorStationLabApplyDto.setApplyNo(applyNo);
|
||||
|
||||
try {
|
||||
// 执行保存逻辑
|
||||
doSaveInspectionLabApply(doctorStationLabApplyDto, applyNo);
|
||||
} catch (Exception e) {
|
||||
// 记录废号日志(申请单号已生成但保存失败)
|
||||
if (isNewApplyNo) {
|
||||
log.error("申请单号 {} 因保存失败成为废号,原因:{}", applyNo, e.getMessage());
|
||||
}
|
||||
throw e; // 重新抛出异常,让事务回滚
|
||||
}
|
||||
|
||||
// 返回生成的申请单号
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("applyNo", applyNo);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行保存检验申请单的实际逻辑
|
||||
*/
|
||||
private void doSaveInspectionLabApply(DoctorStationLabApplyDto doctorStationLabApplyDto, String applyNo) {
|
||||
|
||||
//获取当前登陆用户 ID
|
||||
String userId = String.valueOf(SecurityUtils.getLoginUser().getUserId());
|
||||
InspectionLabApply inspectionLabApply = new InspectionLabApply();
|
||||
@@ -108,18 +139,30 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
inspectionLabApply.setOperatorId(userId);
|
||||
inspectionLabApply.setCreateTime(new Date());
|
||||
inspectionLabApply.setDeleteFlag(DelFlag.NO.getCode());
|
||||
// 申请日期使用服务器当前系统时间
|
||||
inspectionLabApply.setApplyTime(new Date());
|
||||
|
||||
log.debug("保存检验申请单信息:{}", inspectionLabApply);
|
||||
inspectionLabApplyService.saveOrUpdate(inspectionLabApply);
|
||||
|
||||
// 金额校验和重算:后端重新计算金额,防止前端篡改
|
||||
java.math.BigDecimal totalAmount = java.math.BigDecimal.ZERO;
|
||||
int index = 0;
|
||||
|
||||
//遍历 doctorStationLabApplyDto.getLabApplyItemList()
|
||||
int index = 0;
|
||||
for (DoctorStationLabApplyItemDto doctorStationLabApplyItemDto : doctorStationLabApplyDto.getLabApplyItemList()) {
|
||||
//将 dto 数据复制到 InspectionLabApplyItem 对象中
|
||||
InspectionLabApplyItem inspectionLabApplyItem = new InspectionLabApplyItem();
|
||||
BeanUtils.copyProperties(doctorStationLabApplyItemDto, inspectionLabApplyItem);
|
||||
|
||||
// 后端重新计算金额:金额 = 单价 × 数量
|
||||
java.math.BigDecimal itemPrice = doctorStationLabApplyItemDto.getItemPrice();
|
||||
java.math.BigDecimal itemQty = doctorStationLabApplyItemDto.getItemQty();
|
||||
if (itemPrice != null && itemQty != null) {
|
||||
java.math.BigDecimal calculatedAmount = itemPrice.multiply(itemQty).setScale(2, java.math.RoundingMode.HALF_UP);
|
||||
inspectionLabApplyItem.setItemAmount(calculatedAmount);
|
||||
totalAmount = totalAmount.add(calculatedAmount);
|
||||
}
|
||||
|
||||
//设置从表申请单明细的申请单号
|
||||
inspectionLabApplyItem.setApplyNo(doctorStationLabApplyDto.getApplyNo());
|
||||
//执行科室代码,取值于检验申请单明细(前端传递的字典值)
|
||||
@@ -131,7 +174,6 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
index++;
|
||||
|
||||
inspectionLabApplyItem.setDeleteFlag(DelFlag.NO.getCode());
|
||||
log.debug("保存申请单明细信息:{}", inspectionLabApplyItem);
|
||||
inspectionLabApplyItemService.saveOrUpdate(inspectionLabApplyItem);
|
||||
|
||||
//创建条码对象
|
||||
@@ -147,8 +189,6 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
barCode.setCreateTime(new Date());
|
||||
barCode.setDeleteFlag(DelFlag.NO.getCode());
|
||||
|
||||
|
||||
log.debug("插入条码数据前,barCode:{}",barCode);
|
||||
inspectionLabBarCodeService.saveOrUpdate(barCode);
|
||||
}
|
||||
|
||||
@@ -195,15 +235,12 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
);
|
||||
if (organization != null) {
|
||||
positionId = organization.getId();
|
||||
} else {
|
||||
log.warn("未找到执行科室代码对应的科室:{}", performDeptCode);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有指定执行科室,使用当前医生所在的科室作为默认执行科室
|
||||
if (positionId == null) {
|
||||
positionId = SecurityUtils.getDeptId();
|
||||
log.debug("检验项目未指定执行科室,使用当前科室:{}", positionId);
|
||||
}
|
||||
|
||||
// 4. 创建医嘱保存对象
|
||||
@@ -282,12 +319,7 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
adviceSaveParam.setAdviceSaveList(adviceSaveList);
|
||||
|
||||
// 调用门诊医嘱保存接口,创建关联的医嘱记录
|
||||
try {
|
||||
iDoctorStationAdviceAppService.saveAdvice(adviceSaveParam, "1"); // "1"表示保存操作
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("创建关联医嘱记录失败", e);
|
||||
}
|
||||
return R.ok();
|
||||
iDoctorStationAdviceAppService.saveAdvice(adviceSaveParam, "1"); // "1"表示保存操作
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -559,8 +591,7 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
* 支持并发安全:使用 Redis 原子递增保证唯一性
|
||||
* @return 申请单号
|
||||
*/
|
||||
@Override
|
||||
public String generateApplyNo() {
|
||||
private String generateApplyNo() {
|
||||
// 获取当前日期
|
||||
LocalDate today = LocalDate.now();
|
||||
String dateStr = today.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
||||
@@ -574,8 +605,8 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
// 使用 Redis 原子递增获取流水号(并发安全)
|
||||
long sequence = redisCache.incr(redisKey, 1);
|
||||
|
||||
// 设置 Redis key 过期时间为 2 天,避免数据积累
|
||||
redisCache.expire(redisKey, 2 * 24 * 60 * 60);
|
||||
// 设置 Redis key 过期时间(每天的 key 按日期独立,隔天不再使用,25小时确保跨午夜场景安全)
|
||||
redisCache.expire(redisKey, 25 * 60 * 60);
|
||||
|
||||
// 格式化流水号为5位,不足前补0
|
||||
String sequenceStr = String.format("%05d", sequence);
|
||||
@@ -583,7 +614,6 @@ public class DoctorStationLabApplyServiceImpl implements IDoctorStationInspectio
|
||||
// 生成完整的申请单号
|
||||
String applyNo = prefix + sequenceStr;
|
||||
|
||||
log.debug("生成检验申请单号:{}", applyNo);
|
||||
return applyNo;
|
||||
}
|
||||
|
||||
|
||||
@@ -64,18 +64,4 @@ public class DoctorStationInspectionLabApplyController {
|
||||
log.debug("删除检验申请单:{}", applyNo);
|
||||
return R.ok(iDoctorStationInspectionLabApplyService.deleteInspectionLabApply(applyNo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成检验申请单号
|
||||
* 规则:LS + YYYYMMDD + 5位流水号(每日从1开始递增)
|
||||
* @return 申请单号
|
||||
*/
|
||||
@GetMapping(value = "/generate-apply-no")
|
||||
public R<?> generateApplyNo(){
|
||||
log.debug("生成检验申请单号");
|
||||
String applyNo = iDoctorStationInspectionLabApplyService.generateApplyNo();
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("applyNo", applyNo);
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user