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 75a15aa35..997939a79 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,31 +52,27 @@ public interface InpatientDrugMapper {
@Param("operator") String operator);
/**
- * 汇总发药/退药数量(UPSERT)。
+ * 汇总单 UPSERT:累计药品发放/退药数量
*
- * 表结构示例(仅供参考):
+ * 表结构(示例):
* inpatient_drug_dispense_summary
- * - id (PK, 自增)
- * - order_id
- * - drug_id
- * - total_quantity (累计正负数量)
- * - last_update_time
+ * - order_id BIGINT
+ * - drug_id BIGINT
+ * - total_quantity INT // 正负累计
+ * - update_time DATETIME
*
- * 该 SQL 在不存在对应汇总记录时 INSERT,存在时 UPDATE total_quantity。
+ * 主键 (order_id, drug_id) 用于 ON DUPLICATE KEY UPDATE。
*
* @param orderId 医嘱ID
* @param drugId 药品ID
- * @param quantity 本次操作的数量(正数为发药,负数为退药)
+ * @param quantity 本次发药或退药数量(正负均可)
*/
- @Insert({
- ""
- })
+ @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()")
int upsertDrugDispenseSummary(@Param("orderId") Long orderId,
@Param("drugId") Long drugId,
@Param("quantity") Integer quantity);
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 040bcaa05..78f0d9fac 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
@@ -50,16 +50,16 @@ public class InpatientDrugServiceImpl implements InpatientDrugService {
Integer quantity = (Integer) drug.get("quantity");
String operator = (String) drug.get("operator");
- // 明细记录(正向数量)
+ // 插入明细(正向数量)
drugMapper.insertDrugDispenseDetail(orderId, drugId, quantity, operator);
- // 汇总单累计(正向数量)
+ // 更新/插入汇总单(正向累计)
drugMapper.upsertDrugDispenseSummary(orderId, drugId, quantity);
}
}
/**
- * 退药(数量为负数,业务同样先写明细后更新汇总)
+ * 退药(数量为负数)
*
* @param orderId 医嘱主键
* @param drugList 每种药品的退药信息,键包括 drugId、quantity(正数表示退药量)、operator 等
@@ -69,14 +69,14 @@ public class InpatientDrugServiceImpl implements InpatientDrugService {
public void returnDrugs(Long orderId, List