diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/mapper/InpatientDrugMapper.java b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/mapper/InpatientDrugMapper.java index 997939a79..7f9d034c5 100644 --- a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/mapper/InpatientDrugMapper.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/mapper/InpatientDrugMapper.java @@ -52,28 +52,26 @@ public interface InpatientDrugMapper { @Param("operator") String operator); /** - * 汇总单 UPSERT:累计药品发放/退药数量 - * - * 表结构(示例): - * inpatient_drug_dispense_summary - * - order_id BIGINT - * - drug_id BIGINT - * - total_quantity INT // 正负累计 - * - update_time DATETIME - * - * 主键 (order_id, drug_id) 用于 ON DUPLICATE KEY UPDATE。 + * 汇总单 UPSERT:如果已存在对应 order_id、drug_id 的汇总记录,则累计 quantity; + * 否则插入新记录。此方法用于在同一事务内保持明细与汇总的一致性。 * * @param orderId 医嘱ID * @param drugId 药品ID - * @param quantity 本次发药或退药数量(正负均可) + * @param quantity 本次操作的数量(正数为发药,负数为退药) + * @param operator 操作人 */ - @Insert("INSERT INTO inpatient_drug_dispense_summary " + - "(order_id, drug_id, total_quantity, update_time) " + - "VALUES (#{orderId}, #{drugId}, #{quantity}, NOW()) " + - "ON DUPLICATE KEY UPDATE " + - "total_quantity = total_quantity + VALUES(total_quantity), " + - "update_time = NOW()") + @Insert({ + "" + }) int upsertDrugDispenseSummary(@Param("orderId") Long orderId, @Param("drugId") Long drugId, - @Param("quantity") Integer quantity); + @Param("quantity") Integer quantity, + @Param("operator") String operator); } diff --git a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/service/impl/InpatientDrugServiceImpl.java b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/service/impl/InpatientDrugServiceImpl.java index 78f0d9fac..f5395fd02 100644 --- a/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/service/impl/InpatientDrugServiceImpl.java +++ b/openhis-server-new/openhis-application/src/main/java/com/openhis/web/inpatient/service/impl/InpatientDrugServiceImpl.java @@ -53,8 +53,8 @@ public class InpatientDrugServiceImpl implements InpatientDrugService { // 插入明细(正向数量) drugMapper.insertDrugDispenseDetail(orderId, drugId, quantity, operator); - // 更新/插入汇总单(正向累计) - drugMapper.upsertDrugDispenseSummary(orderId, drugId, quantity); + // 更新/插入汇总单,使用相同的数量(正数) + drugMapper.upsertDrugDispenseSummary(orderId, drugId, quantity, operator); } } @@ -69,7 +69,7 @@ public class InpatientDrugServiceImpl implements InpatientDrugService { public void returnDrugs(Long orderId, List> drugList) { for (Map drug : drugList) { Long drugId = (Long) drug.get("drugId"); - Integer quantity = (Integer) drug.get("quantity"); // 正数 + Integer quantity = (Integer) drug.get("quantity"); // 正数表示退药量 String operator = (String) drug.get("operator"); // 退药数量以负数保存到明细表 @@ -77,7 +77,7 @@ public class InpatientDrugServiceImpl implements InpatientDrugService { drugMapper.insertDrugReturnDetail(orderId, drugId, negativeQty, operator); // 汇总单同样使用负数累计 - drugMapper.upsertDrugDispenseSummary(orderId, drugId, negativeQty); + drugMapper.upsertDrugDispenseSummary(orderId, drugId, negativeQty, operator); } } }