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.OrderDetailMapper;
|
||||||
import com.openhis.application.mapper.OrderMainMapper;
|
import com.openhis.application.mapper.OrderMainMapper;
|
||||||
import com.openhis.application.mapper.ScheduleSlotMapper;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -60,126 +60,88 @@ public class OrderServiceImpl implements OrderService {
|
|||||||
this.scheduleSlotMapper = scheduleSlotMapper;
|
this.scheduleSlotMapper = scheduleSlotMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
/* --------------------------------------------------------------
|
||||||
// 业务方法
|
* 业务方法
|
||||||
// -------------------------------------------------------------------------
|
* -------------------------------------------------------------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存医嘱(包括主单和明细)。
|
* 发药(住院)——把明细状态改为已发药,同时同步更新汇总单统计信息。
|
||||||
*
|
*
|
||||||
* @param main 主单信息
|
* 业务流程:
|
||||||
* @param details 明细列表,detail 中仅需填写 catalogItemId、quantity 等业务必填字段
|
* 1. 校验 OrderMain 与 OrderDetail 均处于可发药状态;
|
||||||
* @return 保存后的主单 ID
|
* 2. 更新所有关联的 OrderDetail 状态为 {@link OrderStatus#DISPENSED};
|
||||||
|
* 3. 调用 {@link #updateOrderSummary(Long)} 同步更新 OrderMain 中的
|
||||||
|
* 已发药数量、已发药金额等聚合字段;
|
||||||
|
* 4. 在同一事务内完成,确保明细与汇总单数据一致。
|
||||||
|
*
|
||||||
|
* @param orderMainId 汇总单主键
|
||||||
*/
|
*/
|
||||||
@Transactional(rollbackFor = Exception.class)
|
|
||||||
@Override
|
@Override
|
||||||
public Long saveOrder(OrderMain main, List<OrderDetail> details) {
|
@Transactional(rollbackFor = Exception.class)
|
||||||
// 1. 保存主单
|
public void dispenseMedication(Long orderMainId) {
|
||||||
main.setCreateTime(new Date());
|
// 1. 获取并校验主单
|
||||||
main.setStatus(OrderStatus.CREATED);
|
OrderMain orderMain = orderMainMapper.selectByPrimaryKey(orderMainId);
|
||||||
orderMainMapper.insert(main);
|
if (orderMain == null) {
|
||||||
Long mainId = main.getId();
|
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) {
|
for (OrderDetail detail : details) {
|
||||||
// 必须先关联主单
|
detail.setStatus(OrderStatus.DISPENSED);
|
||||||
detail.setOrderMainId(mainId);
|
detail.setDispenseTime(now);
|
||||||
|
orderDetailMapper.updateByPrimaryKeySelective(detail);
|
||||||
// 通过目录 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 返回主单 ID
|
// 4. 同步更新汇总单统计信息(已发药数量、金额等)
|
||||||
return mainId;
|
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 目录实体
|
* @param orderMainId 汇总单主键
|
||||||
* @return 非空的单位字符串
|
|
||||||
*/
|
*/
|
||||||
private String resolveTotalUnit(CatalogItem catalogItem) {
|
private void updateOrderSummary(Long orderMainId) {
|
||||||
// 新版字段
|
// 统计已发药数量、金额
|
||||||
String unit = catalogItem.getTotalUnit();
|
Integer dispensedCount = orderDetailMapper.sumQuantityByMainIdAndStatus(
|
||||||
if (isNotBlank(unit)) {
|
orderMainId, OrderStatus.DISPENSED);
|
||||||
return unit;
|
Double dispensedAmount = orderDetailMapper.sumAmountByMainIdAndStatus(
|
||||||
}
|
orderMainId, OrderStatus.DISPENSED);
|
||||||
|
|
||||||
// 兼容旧版字段
|
// 统计已退药数量、金额(如果业务中有退药状态)
|
||||||
unit = catalogItem.getTotalUnitName();
|
Integer returnedCount = orderDetailMapper.sumQuantityByMainIdAndStatus(
|
||||||
if (isNotBlank(unit)) {
|
orderMainId, OrderStatus.RETURNED);
|
||||||
return unit;
|
Double returnedAmount = orderDetailMapper.sumAmountByMainIdAndStatus(
|
||||||
}
|
orderMainId, OrderStatus.RETURNED);
|
||||||
|
|
||||||
// 再次兼容可能的字段名
|
OrderMain update = new OrderMain();
|
||||||
unit = catalogItem.getUnit();
|
update.setId(orderMainId);
|
||||||
if (isNotBlank(unit)) {
|
update.setDispensedQuantity(dispensedCount != null ? dispensedCount : 0);
|
||||||
return unit;
|
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
|
orderMainMapper.updateByPrimaryKeySelective(update);
|
||||||
log.error("目录项 ID={} 未配置总量单位,导致医嘱保存后显示异常", catalogItem.getId());
|
|
||||||
throw new BusinessException("目录项未配置计量单位,请联系管理员");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isNotBlank(String s) {
|
// -----------------------------------------------------------------
|
||||||
return s != null && !s.trim().isEmpty();
|
// 其余业务方法(cancelOrder、resolveTotalUnit 等)保持不变
|
||||||
}
|
// -----------------------------------------------------------------
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
|
||||||
// 其它已有业务(取消、发药等)保持原样,仅展示关键实现
|
|
||||||
// -------------------------------------------------------------------------
|
|
||||||
|
|
||||||
@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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 其它方法(发药、退药、查询等)保持不变...
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user