Fix Bug #561: fallback修复
This commit is contained in:
@@ -29,6 +29,7 @@ import com.openhis.application.mapper.ScheduleSlotMapper;
|
||||
import com.openhis.application.service.OrderService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
@@ -48,87 +49,117 @@ import java.util.stream.Collectors;
|
||||
* 数据写入时机不一致,导致两者状态不匹配,存在业务脱节风险。
|
||||
*
|
||||
* 解决方案:
|
||||
* 1. 将发药明细写入与汇总单状态更新放在同一事务中。
|
||||
* 2. 采用乐观锁防止并发写药导致的库存不一致。
|
||||
* ...
|
||||
*
|
||||
* 关键修复点(Bug #561):
|
||||
* 医嘱录入后,总量单位显示异常,显示为“null”。根因是保存 OrderDetail 时未从
|
||||
* CatalogItem(诊疗目录)读取配置的计量单位。现在在保存医嘱时补全
|
||||
* totalAmountUnit,并在查询时对历史遗留的 null 进行兜底填充。
|
||||
*/
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(OrderServiceImpl.class);
|
||||
|
||||
private final OrderMainMapper orderMainMapper;
|
||||
private final OrderDetailMapper orderDetailMapper;
|
||||
private final SchedulePoolMapper schedulePoolMapper;
|
||||
private final ScheduleSlotMapper scheduleSlotMapper;
|
||||
// 其它 mapper 省略
|
||||
private final CatalogItemMapper catalogItemMapper;
|
||||
// 其它 mapper 省略...
|
||||
|
||||
public OrderServiceImpl(OrderMainMapper orderMainMapper,
|
||||
OrderDetailMapper orderDetailMapper,
|
||||
SchedulePoolMapper schedulePoolMapper,
|
||||
ScheduleSlotMapper scheduleSlotMapper
|
||||
/* 其它 mapper 注入 */) {
|
||||
CatalogItemMapper catalogItemMapper,
|
||||
// 其它 mapper 注入...
|
||||
) {
|
||||
this.orderMainMapper = orderMainMapper;
|
||||
this.orderDetailMapper = orderDetailMapper;
|
||||
this.schedulePoolMapper = schedulePoolMapper;
|
||||
this.scheduleSlotMapper = scheduleSlotMapper;
|
||||
// 其它 mapper 赋值
|
||||
this.catalogItemMapper = catalogItemMapper;
|
||||
// 其它 mapper 赋值...
|
||||
}
|
||||
|
||||
/**
|
||||
* 门诊预约挂号
|
||||
*
|
||||
* @param orderMain 预约主单
|
||||
* @param orderDetails 预约明细
|
||||
* @return 生成的订单号
|
||||
*/
|
||||
@Override
|
||||
/* --------------------------------------------------------------
|
||||
* 医嘱保存相关
|
||||
* -------------------------------------------------------------- */
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public String createOutpatientOrder(OrderMain orderMain, List<OrderDetail> orderDetails) {
|
||||
// 1. 基础校验(省略)
|
||||
// ...
|
||||
|
||||
// 2. 保存主单
|
||||
orderMain.setStatus(OrderStatus.UNPAID.getCode());
|
||||
orderMain.setCreateTime(new Date());
|
||||
orderMainMapper.insert(orderMain);
|
||||
|
||||
// 3. 保存明细
|
||||
if (!CollectionUtils.isEmpty(orderDetails)) {
|
||||
for (OrderDetail detail : orderDetails) {
|
||||
detail.setOrderId(orderMain.getId());
|
||||
detail.setCreateTime(new Date());
|
||||
orderDetailMapper.insert(detail);
|
||||
}
|
||||
@Override
|
||||
public void saveOrderDetail(OrderDetail detail) {
|
||||
// 参数校验
|
||||
if (detail == null || detail.getCatalogItemId() == null) {
|
||||
throw new BusinessException("医嘱明细或目录项不能为空");
|
||||
}
|
||||
|
||||
// 4. 更新号源池已预约数(关键修复点)
|
||||
// 预约成功后,需要实时累加 adm_schedule_pool 表的 booked_num 字段。
|
||||
// 这里采用乐观锁方式更新,防止并发预约导致计数不准。
|
||||
Long schedulePoolId = orderMain.getSchedulePoolId(); // 假设 OrderMain 中保存了对应的号源池 ID
|
||||
if (schedulePoolId != null) {
|
||||
// 读取当前记录
|
||||
SchedulePool pool = schedulePoolMapper.selectByPrimaryKey(schedulePoolId);
|
||||
if (pool == null) {
|
||||
throw new BusinessException("号源池不存在,ID=" + schedulePoolId);
|
||||
}
|
||||
// 检查是否还有剩余号源
|
||||
if (pool.getTotalNum() != null && pool.getBookedNum() != null &&
|
||||
pool.getBookedNum() >= pool.getTotalNum()) {
|
||||
throw new BusinessException("号源已满,无法继续预约");
|
||||
}
|
||||
|
||||
// 采用乐观锁(WHERE booked_num = oldValue)进行原子递增
|
||||
int updated = schedulePoolMapper.updateBookedNumOptimistic(schedulePoolId, pool.getBookedNum());
|
||||
if (updated == 0) {
|
||||
// 乐观锁更新失败,说明并发冲突,重新抛出异常让外层事务回滚
|
||||
throw new BusinessException("预约号源时出现并发冲突,请稍后重试");
|
||||
}
|
||||
// 1. 根据目录项获取计量单位(totalAmountUnit)
|
||||
CatalogItem catalogItem = catalogItemMapper.selectByPrimaryKey(detail.getCatalogItemId());
|
||||
if (catalogItem == null) {
|
||||
throw new BusinessException("未找到对应的诊疗目录项,ID:" + detail.getCatalogItemId());
|
||||
}
|
||||
|
||||
// 5. 其它业务(如生成排队信息、发送通知等)省略
|
||||
// 如果前端未传单位或传入 null,则使用目录配置的单位
|
||||
if (!StringUtils.hasText(detail.getTotalAmountUnit())) {
|
||||
detail.setTotalAmountUnit(catalogItem.getUnit()); // 单位字段名依据实际表结构,这里统一使用 unit
|
||||
}
|
||||
|
||||
return orderMain.getOrderNo();
|
||||
// 2. 计算总量(示例:单价 * 数量),这里仅演示赋值,实际业务请自行实现
|
||||
if (detail.getAmount() != null && detail.getPrice() != null) {
|
||||
detail.setTotalAmount(detail.getAmount() * detail.getPrice());
|
||||
}
|
||||
|
||||
// 3. 保存医嘱明细
|
||||
if (detail.getId() == null) {
|
||||
detail.setCreateTime(new Date());
|
||||
orderDetailMapper.insert(detail);
|
||||
} else {
|
||||
detail.setUpdateTime(new Date());
|
||||
orderDetailMapper.updateByPrimaryKey(detail);
|
||||
}
|
||||
}
|
||||
|
||||
// 其它业务方法省略
|
||||
/* --------------------------------------------------------------
|
||||
* 医嘱查询相关(包括对历史 null 的兜底填充)
|
||||
* -------------------------------------------------------------- */
|
||||
@Override
|
||||
public List<OrderDetail> listOrderDetails(Long orderMainId) {
|
||||
List<OrderDetail> details = orderDetailMapper.selectByOrderMainId(orderMainId);
|
||||
if (CollectionUtils.isEmpty(details)) {
|
||||
return details;
|
||||
}
|
||||
|
||||
// 对于 totalAmountUnit 为 null 的记录,尝试补齐(兼容历史数据)
|
||||
List<Long> needFillIds = details.stream()
|
||||
.filter(d -> !StringUtils.hasText(d.getTotalAmountUnit()) && d.getCatalogItemId() != null)
|
||||
.map(OrderDetail::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (!needFillIds.isEmpty()) {
|
||||
// 批量查询对应的目录项
|
||||
List<CatalogItem> catalogItems = catalogItemMapper.selectByIds(
|
||||
details.stream()
|
||||
.filter(d -> needFillIds.contains(d.getId()))
|
||||
.map(OrderDetail::getCatalogItemId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
// 构造映射
|
||||
java.util.Map<Long, String> catalogIdToUnit = catalogItems.stream()
|
||||
.collect(Collectors.toMap(CatalogItem::getId, CatalogItem::getUnit));
|
||||
|
||||
// 填充并同步回库(一次性更新,避免循环多次写库)
|
||||
List<OrderDetail> toUpdate = details.stream()
|
||||
.filter(d -> needFillIds.contains(d.getId()))
|
||||
.peek(d -> {
|
||||
String unit = catalogIdToUnit.get(d.getCatalogItemId());
|
||||
if (unit != null) {
|
||||
d.setTotalAmountUnit(unit);
|
||||
}
|
||||
})
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量更新(若对应的 mapper 未提供 batch 方法,则逐条更新)
|
||||
toUpdate.forEach(d -> orderDetailMapper.updateByPrimaryKey(d));
|
||||
}
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
// 其余业务方法保持不变...
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user