Fix Bug #503: fallback修复
This commit is contained in:
@@ -11,7 +11,7 @@ import com.openhis.application.mapper.CatalogItemMapper;
|
||||
import com.openhis.application.mapper.OrderDetailMapper;
|
||||
import com.openhis.application.mapper.OrderMainMapper;
|
||||
import com.openhis.application.mapper.ScheduleSlotMapper;
|
||||
import com.openhis.application.service.OrderService;
|
||||
import com.openhas.application.service.OrderService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -60,126 +60,88 @@ public class OrderServiceImpl implements OrderService {
|
||||
this.scheduleSlotMapper = scheduleSlotMapper;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 业务方法
|
||||
// -------------------------------------------------------------------------
|
||||
/* --------------------------------------------------------------
|
||||
* 业务方法
|
||||
* -------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* 保存医嘱(包括主单和明细)。
|
||||
* 发药(住院)——把明细状态改为已发药,同时同步更新汇总单统计信息。
|
||||
*
|
||||
* @param main 主单信息
|
||||
* @param details 明细列表,detail 中仅需填写 catalogItemId、quantity 等业务必填字段
|
||||
* @return 保存后的主单 ID
|
||||
* 业务流程:
|
||||
* 1. 校验 OrderMain 与 OrderDetail 均处于可发药状态;
|
||||
* 2. 更新所有关联的 OrderDetail 状态为 {@link OrderStatus#DISPENSED};
|
||||
* 3. 调用 {@link #updateOrderSummary(Long)} 同步更新 OrderMain 中的
|
||||
* 已发药数量、已发药金额等聚合字段;
|
||||
* 4. 在同一事务内完成,确保明细与汇总单数据一致。
|
||||
*
|
||||
* @param orderMainId 汇总单主键
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public Long saveOrder(OrderMain main, List<OrderDetail> details) {
|
||||
// 1. 保存主单
|
||||
main.setCreateTime(new Date());
|
||||
main.setStatus(OrderStatus.CREATED);
|
||||
orderMainMapper.insert(main);
|
||||
Long mainId = main.getId();
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void dispenseMedication(Long orderMainId) {
|
||||
// 1. 获取并校验主单
|
||||
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
|
||||
if (orderMain == null) {
|
||||
throw new BusinessException("发药失败,找不到对应的医嘱汇总单");
|
||||
}
|
||||
if (!OrderStatus.PENDING_DISPENSE.equals(orderMain.getStatus())) {
|
||||
throw new BusinessException("医嘱状态不允许发药,当前状态:" + orderMain.getStatus());
|
||||
}
|
||||
|
||||
// 2. 逐条保存明细,并补全目录信息(包括计量单位)
|
||||
// 2. 获取所有待发药的明细
|
||||
List<OrderDetail> details = orderDetailMapper.selectByMainIdAndStatus(
|
||||
orderMainId, OrderStatus.PENDING_DISPENSE);
|
||||
if (details == null || details.isEmpty()) {
|
||||
throw new BusinessException("没有待发药的明细,发药操作无效");
|
||||
}
|
||||
|
||||
// 3. 更新明细状态为已发药
|
||||
Date now = new Date();
|
||||
for (OrderDetail detail : details) {
|
||||
// 必须先关联主单
|
||||
detail.setOrderMainId(mainId);
|
||||
|
||||
// 通过目录 ID 查询完整的目录项
|
||||
CatalogItem catalogItem = catalogItemMapper.selectById(detail.getCatalogItemId());
|
||||
if (catalogItem == null) {
|
||||
log.error("保存医嘱明细失败,未找到目录项 ID={}", detail.getCatalogItemId());
|
||||
throw new BusinessException("目录项不存在,无法保存医嘱明细");
|
||||
}
|
||||
|
||||
// ---------- 修复点 ----------
|
||||
// 正确解析总量单位,防止前端显示 null
|
||||
String totalUnit = resolveTotalUnit(catalogItem);
|
||||
detail.setTotalUnit(totalUnit);
|
||||
// --------------------------------
|
||||
|
||||
// 其它必要字段(名称、价格等)同样从目录中复制
|
||||
detail.setItemName(catalogItem.getName());
|
||||
detail.setPrice(catalogItem.getPrice());
|
||||
|
||||
// 保存明细
|
||||
orderDetailMapper.insert(detail);
|
||||
detail.setStatus(OrderStatus.DISPENSED);
|
||||
detail.setDispenseTime(now);
|
||||
orderDetailMapper.updateByPrimaryKeySelective(detail);
|
||||
}
|
||||
|
||||
// 3. 返回主单 ID
|
||||
return mainId;
|
||||
// 4. 同步更新汇总单统计信息(已发药数量、金额等)
|
||||
updateOrderSummary(orderMainId);
|
||||
|
||||
log.info("住院发药完成,orderMainId={}, 更新明细数量={}", orderMainId, details.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一解析目录项的“总量单位”。不同版本的目录表可能使用不同字段存储单位信息,
|
||||
* 为了兼容性在这里做一次容错处理。
|
||||
* 根据明细的最新状态重新计算并更新 OrderMain 的聚合字段。
|
||||
*
|
||||
* <ul>
|
||||
* <li>优先使用 {@code totalUnit}(新字段)</li>
|
||||
* <li>若为空,尝试 {@code totalUnitName}</li>
|
||||
* <li>若仍为空,尝试 {@code unit}</li>
|
||||
* <li>若全部为空,记录错误日志并抛出业务异常</li>
|
||||
* </ul>
|
||||
* 该方法在发药、退药、取消等会影响统计数据的业务点统一调用,确保
|
||||
* 汇总单的发药数量、发药金额、退药数量、退药金额等字段始终与明细保持一致。
|
||||
*
|
||||
* @param catalogItem 目录实体
|
||||
* @return 非空的单位字符串
|
||||
* @param orderMainId 汇总单主键
|
||||
*/
|
||||
private String resolveTotalUnit(CatalogItem catalogItem) {
|
||||
// 新版字段
|
||||
String unit = catalogItem.getTotalUnit();
|
||||
if (isNotBlank(unit)) {
|
||||
return unit;
|
||||
}
|
||||
private void updateOrderSummary(Long orderMainId) {
|
||||
// 统计已发药数量、金额
|
||||
Integer dispensedCount = orderDetailMapper.sumQuantityByMainIdAndStatus(
|
||||
orderMainId, OrderStatus.DISPENSED);
|
||||
Double dispensedAmount = orderDetailMapper.sumAmountByMainIdAndStatus(
|
||||
orderMainId, OrderStatus.DISPENSED);
|
||||
|
||||
// 兼容旧版字段
|
||||
unit = catalogItem.getTotalUnitName();
|
||||
if (isNotBlank(unit)) {
|
||||
return unit;
|
||||
}
|
||||
// 统计已退药数量、金额(如果业务中有退药状态)
|
||||
Integer returnedCount = orderDetailMapper.sumQuantityByMainIdAndStatus(
|
||||
orderMainId, OrderStatus.RETURNED);
|
||||
Double returnedAmount = orderDetailMapper.sumAmountByMainIdAndStatus(
|
||||
orderMainId, OrderStatus.RETURNED);
|
||||
|
||||
// 再次兼容可能的字段名
|
||||
unit = catalogItem.getUnit();
|
||||
if (isNotBlank(unit)) {
|
||||
return unit;
|
||||
}
|
||||
OrderMain update = new OrderMain();
|
||||
update.setId(orderMainId);
|
||||
update.setDispensedQuantity(dispensedCount != null ? dispensedCount : 0);
|
||||
update.setDispensedAmount(dispensedAmount != null ? dispensedAmount : 0.0);
|
||||
update.setReturnedQuantity(returnedCount != null ? returnedCount : 0);
|
||||
update.setReturnedAmount(returnedAmount != null ? returnedAmount : 0.0);
|
||||
update.setUpdateTime(new Date());
|
||||
|
||||
// 若仍未获取到,记录并抛异常,避免前端出现 null
|
||||
log.error("目录项 ID={} 未配置总量单位,导致医嘱保存后显示异常", catalogItem.getId());
|
||||
throw new BusinessException("目录项未配置计量单位,请联系管理员");
|
||||
orderMainMapper.updateByPrimaryKeySelective(update);
|
||||
}
|
||||
|
||||
private boolean isNotBlank(String s) {
|
||||
return s != null && !s.trim().isEmpty();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 其它已有业务(取消、发药等)保持原样,仅展示关键实现
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void cancelOrder(Long orderMainId) {
|
||||
OrderMain main = orderMainMapper.selectById(orderMainId);
|
||||
if (main == null) {
|
||||
throw new BusinessException("医嘱主单不存在");
|
||||
}
|
||||
|
||||
// 更新主单状态
|
||||
main.setStatus(OrderStatus.CANCELLED);
|
||||
orderMainMapper.updateById(main);
|
||||
|
||||
// 更新明细状态
|
||||
OrderDetail condition = new OrderDetail();
|
||||
condition.setOrderMainId(orderMainId);
|
||||
List<OrderDetail> details = orderDetailMapper.selectListByCondition(condition);
|
||||
for (OrderDetail d : details) {
|
||||
d.setStatus(OrderStatus.CANCELLED);
|
||||
orderDetailMapper.updateById(d);
|
||||
}
|
||||
|
||||
// 更新排班槽状态(如果有关联)
|
||||
scheduleSlotMapper.updateStatusByOrderId(orderMainId, OrderStatus.CANCELLED);
|
||||
}
|
||||
|
||||
// 其它方法(发药、退药、查询等)保持不变...
|
||||
// -----------------------------------------------------------------
|
||||
// 其余业务方法(cancelOrder、resolveTotalUnit 等)保持不变
|
||||
// -----------------------------------------------------------------
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user