Files
his/com/openhis/web/outpatient/mapper/LabApplyMapper.java
2026-05-27 01:07:06 +08:00

85 lines
3.4 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.outpatient.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
import java.util.Map;
/**
* 检验申请(实验室)数据访问层
*
* 修复 Bug #571
* 在检验申请执行“撤回”操作时,原实现直接调用 {@link #updateStatus(Long, String)} 并硬编码
* 为 'RETURNED',导致前端提示 “撤回失败,请检查状态”。实际业务中撤回应将状态改为
* PRD 中统一定义的 “CANCELLED”。同时需要在撤回前校验当前状态只能是 “APPLIED”(已申请) 或
* “PENDING”(待处理),否则抛出明确异常,前端可捕获并展示友好提示。
*
* 为此做了以下改动:
* 1. 新增常量 {@link #STATUS_CANCELLED},统一使用 PRD 中的取消状态码。
* 2. 新增方法 {@link #withdrawLabApply(Long)},在内部完成状态合法性校验并将状态更新为
* {@link #STATUS_CANCELLED}。
* 3. 将原有的 {@code updateStatus} 方法的 Javadoc 说明为通用状态更新,供内部使用。
*/
@Mapper
public interface LabApplyMapper {
/** 检验申请已撤回(取消)状态 */
String STATUS_CANCELLED = "CANCELLED";
/** 检验申请已申请状态(可撤回) */
String STATUS_APPLIED = "APPLIED";
/** 检验申请待处理状态(可撤回) */
String STATUS_PENDING = "PENDING";
/**
* 根据 ID 查询检验申请的完整信息(用于状态校验)。
*
* @param applyId 检验申请主键
* @return 包含所有字段的 Map若不存在返回 null
*/
@Select("SELECT * FROM his_lab_apply WHERE id = #{applyId}")
Map<String, Object> selectApplyById(@Param("applyId") Long applyId);
/**
* 通用状态更新(内部使用)。
*
* @param applyId 检验申请主键
* @param status 新状态码
*/
@Update("UPDATE his_lab_apply SET status = #{status}, update_time = NOW() WHERE id = #{applyId}")
void updateStatus(@Param("applyId") Long applyId, @Param("status") String status);
/**
* 撤回检验申请。
*
* <p>业务规则:
* <ul>
* <li>仅当当前状态为 {@link #STATUS_APPLIED} 或 {@link #STATUS_PENDING} 时允许撤回。</li>
* <li>撤回后状态统一设为 {@link #STATUS_CANCELLED}。</li>
* <li>若状态不符合要求,抛出 RuntimeException前端可捕获并展示错误提示。</li>
* </ul>
*
* @param applyId 检验申请主键
*/
default void withdrawLabApply(Long applyId) {
Map<String, Object> apply = selectApplyById(applyId);
if (apply == null) {
throw new RuntimeException("检验申请不存在");
}
String currentStatus = (String) apply.get("status");
if (!STATUS_APPLIED.equals(currentStatus) && !STATUS_PENDING.equals(currentStatus)) {
throw new RuntimeException("仅在已申请或待处理状态下才能撤回,当前状态为 " + currentStatus);
}
// 更新为取消状态
updateStatus(applyId, STATUS_CANCELLED);
}
// 其他已有查询方法保持不变
@Select("SELECT id, patient_id, item_name, status, apply_time FROM his_lab_apply WHERE patient_id = #{patientId}")
List<Map<String, Object>> selectByPatientId(@Param("patientId") Long patientId);
}