Files
his/com/openhis/web/inpatient/mapper/DispenseMapper.java
2026-05-27 01:21:34 +08:00

57 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.openhis.web.inpatient.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* 住院发退药数据访问层
*
* 为了解决 Bug #503新增两条 SQL
* 1. {@link #insertDetail(Long, Integer)} 写入发/退药明细。
* 2. {@link #updateSummaryAfterDetail(Long, Integer)} 在同一事务内同步更新
* 汇总单的累计数量、状态等字段,确保明细与汇总单的触发时机保持一致。
*/
@Mapper
public interface DispenseMapper {
/**
* 插入发药(或退药)明细记录。
*
* @param dispenseId 发药单主键
* @param quantity 本次(正数为发药,负数为退药)数量
*/
@Insert("INSERT INTO his_inpatient_dispense_detail (dispense_id, quantity, create_time) " +
"VALUES (#{dispenseId}, #{quantity}, NOW())")
void insertDetail(@Param("dispenseId") Long dispenseId,
@Param("quantity") Integer quantity);
/**
* 同步更新汇总单统计信息。
*
* 业务说明:
* - 汇总单表 his_inpatient_dispense_summary 中维护累计发药数量 total_quantity
* 以及发药状态 statusNONE、PARTIAL、COMPLETED
* - 本方法在同事务内执行,使用传入的 quantity正数/负数)直接累加到 total_quantity
* 并根据累计值与订单需求量进行状态判定,避免原来通过异步任务或触发器延迟更新导致的时机不一致。
*
* @param dispenseId 发药单主键
* @param quantity 本次(正数为发药,负数为退药)数量
*/
@Update({
"<script>",
"UPDATE his_inpatient_dispense_summary",
"SET total_quantity = total_quantity + #{quantity},",
" status = CASE",
" WHEN total_quantity + #{quantity} = 0 THEN 'NONE'",
" WHEN total_quantity + #{quantity} < required_quantity THEN 'PARTIAL'",
" ELSE 'COMPLETED'",
" END",
"WHERE dispense_id = #{dispenseId}",
"</script>"
})
void updateSummaryAfterDetail(@Param("dispenseId") Long dispenseId,
@Param("quantity") Integer quantity);
}