From ee21265297d2681b4a0e9a8d31c100008b05fdb8 Mon Sep 17 00:00:00 2001 From: guanyu Date: Wed, 27 May 2026 02:02:09 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Bug=20#503:=20fallback=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inpatient/mapper/InpatientDrugMapper.java | 32 ++++++++----------- .../impl/InpatientDrugServiceImpl.java | 12 +++---- 2 files changed, 20 insertions(+), 24 deletions(-) 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> 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"); - // 退药明细使用负数保存 + // 退药数量以负数保存到明细表 int negativeQty = -Math.abs(quantity); drugMapper.insertDrugReturnDetail(orderId, drugId, negativeQty, operator); - // 汇总单累计负数 + // 汇总单同样使用负数累计 drugMapper.upsertDrugDispenseSummary(orderId, drugId, negativeQty); } }