yxh
6 天以前 68ff33beff11310e830b0615b5a81f83fad0f683
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/QueueMapper.java
@@ -10,6 +10,7 @@
import cn.lihu.jh.module.ecg.controller.admin.queue.vo.*;
import java.util.List;
import java.util.Map;
/**
 * 排队 Mapper
@@ -36,21 +37,20 @@
                .orderByDesc(QueueDO::getId));
    }
    @Select("SELECT max(seq_num) FROM lihu.queue")
    Integer getMaxSeqNum();
    @Select("SELECT * FROM lihu.queue where seq_num = #{seqNum}")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    QueueDO getQueueItemBySeqNum(@Param("seqNum")Integer seqNum);
    @Select("SELECT * FROM lihu.queue where pat_id = #{patId} and book_check_type = #{bookCheckType}")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    QueueDO getQueueByPatId(@Param("patId")String patId, @Param("bookCheckType")Integer bookCheckType);
    /**
     * 诊间屏({@code ecg_call/small.html})取数:本诊室的检查队列。
     *
     * <p>排序规则(号统一后的最终规则):<b>就诊中(30) 置顶 → 加急(jump_flag=1) → 其余按排队号 seq_num 升序</b>;
     * 号为空(改造当天尚未迁移的存量行)的排在最后。
     * <p>过号(3/5) <b>不再单独提前一组</b>:它与「等候」混排、留在自己的号位上,
     * 由屏上用红色 +「(过)」小字区分(需求:诊间屏「等候」与「过号」合并为一个「等候」面板)。
     */
    @Select("""
            <script>
                SELECT * FROM lihu.queue
@@ -60,7 +60,8 @@
                 </foreach> )
                order by case
                           when status = 30 then 0
                           when status in (3, 5) then 1
                           when jump_flag = 1 then 1
                           when seq_num is null then 3
                           else 2
                         end,
                         seq_num, book_timeslot
@@ -73,13 +74,18 @@
    /**
     * 大屏取数。
     * <p>
     * 排序规则(需求8):**就诊中(30) 置顶 → 过号(3/5) 与等候(10/15/20/12) 按排队序号 seq_num 升序混排**。
     * 原先用 {@code order by status desc},会把过号(3/5)排到每一列最底部,
     * 在 {@code overflow-y:auto} 的列里滚不到,等同于"过号不显示"。
     * <p>
     * 同时补上 {@code deleted = 0}:本方法是自定义 {@code @Select},
     * 会绕过 MyBatis-Plus 的 {@code @TableLogic} 逻辑删除过滤。
     *
     * <p>排序规则(号统一后的最终规则):<b>就诊中(30) 置顶 → 加急(jump_flag=1) → 其余按排队号 seq_num 升序</b>;
     * 号为空(改造当天尚未迁移的存量行)的排在最后。
     * <ul>
     *   <li>过号(3/5) <b>不再单独提前一组</b>:它留在自己的号位上,屏上用红色 + 过号标记区分,
     *       与诊间屏({@code ecg_call/small.html})改造后的口径一致。
     *       (原实现是 {@code status desc},会把过号压到最底部、等同于"过号不显示"。)</li>
     *   <li>加急必须参与排序:排队号改为签到时一次性分配、之后不再改动,
     *       加急无法再靠"分流时改号/插队重编号"体现。</li>
     *   <li>沿用 {@code deleted = 0}:本方法是自定义 {@code @Select},
     *       会绕过 MyBatis-Plus 的 {@code @TableLogic} 逻辑删除过滤。</li>
     * </ul>
     */
    @Select("""
            <script>
@@ -90,7 +96,8 @@
                 </foreach> )
                order by case
                           when status = 30 then 0
                           when status in (3, 5) then 1
                           when jump_flag = 1 then 1
                           when seq_num is null then 3
                           else 2
                         end,
                         seq_num, book_timeslot
@@ -226,7 +233,10 @@
                  <foreach collection='statusList' separator=',' item='status'>
                    #{status} 
                  </foreach> ) 
                order by status desc, seq_num, book_timeslot
                order by case when status = 30 then 0 else 1 end,
                         jump_flag desc,
                         case when seq_num is null then 1 else 0 end,
                         seq_num, book_timeslot
            </script>
            """)
    @Results({
@@ -234,9 +244,20 @@
    })
    List<QueueDO> getBedQueueByStatus(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("statusList")List<Integer> statusList);
    /**
     * 叫号取人:本工位「候诊中(20)」里**应优先叫**的那一位。
     *
     * <p>排序(号统一后的最终规则):<b>加急(jump_flag=1) → 排队号 seq_num 升序</b>,号为空(存量行)排最后。
     * <p>排队号在签到时一次性分配、之后不再改动,所以"下一位叫谁"完全由本方法决定;
     * 加急必须在这里参与排序,否则加急患者永远不会被优先叫到。
     */
    @Select("""
            SELECT * from lihu.queue where room_id = #{roomId} and bed_no = #{bedNo}
               and status = #{curStatus} order by seq_num, book_timeslot limit 1
               and status = #{curStatus} and deleted = 0
             order by jump_flag desc,
                      case when seq_num is null then 1 else 0 end,
                      seq_num, book_timeslot
             limit 1
            """)
    QueueDO getBedFirstQueueItemByStatus(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("curStatus")Integer curStatus);
@@ -289,22 +310,30 @@
    // 某个诊室范围内 [已领用] [已召回-安装] 人员 抢占, 会更新 [工位号]
    // 没有限定检查类型,不要使用时间槽
    // 排序:加急优先 → 排队号升序(号为空排最后)。装机叫号取人也走这里。
    @Select("<script> " +
            "select * from lihu.queue where room_id = #{roomId} and status in ( " +
            "select * from lihu.queue where deleted = 0 and room_id = #{roomId} and status in ( " +
            "       <foreach collection='curStatusList' separator=',' item='status'>" +
            "           #{status} " +
            "       </foreach> )" +
            "       order by seq_num, book_timeslot limit 1 " +
            "       order by jump_flag desc, case when seq_num is null then 1 else 0 end, seq_num, book_timeslot limit 1 " +
            "</script>" )
    QueueDO getRoomFirstQueueItemByStatus(@Param("roomId")Long roomId, @Param("curStatusList")List<Integer> curStatusList);
    /**
     * 按预约单 id 批量取 queue 行。
     *
     * <p>用途:预约签到前,把「该预约是否已排队、拿到的排队号是多少」回填给前端。
     * 号统一后取的是 {@code seq_num}(见 AppointmentServiceImpl#queryAndCreateAppointmentByPatId)。
     * <p>原方法名为 {@code selectBookSeqNumByAppointIdList},因号已统一为 seq_num 而改名。
     */
    @Select("<script> " +
            "select * from lihu.queue where appoint_id in ( " +
            "select * from lihu.queue where deleted = 0 and appoint_id in ( " +
            "       <foreach collection='appointIdList' separator=',' item='appointId'>" +
            "           #{appointId} " +
            "       </foreach> )" +
            "</script>" )
    List<QueueDO> selectBookSeqNumByAppointIdList(@Param("appointIdList") List<Long> appointIdList);
    List<QueueDO> selectQueueByAppointIdList(@Param("appointIdList") List<Long> appointIdList);
/*
    @Select("select * from lihu.queue where room_id = #{roomId} and bed_no = #{bedNo} " +
@@ -317,9 +346,21 @@
    @Update("update lihu.queue set status = #{newStatus} where pat_id = #{patId} and book_check_type = #{checkType} and status = #{curStatus}; " )
    Integer updatePatientQueueStatus(@Param("patId")String patId, @Param("checkType")Integer checkType, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus);
    @Update("update lihu.queue set status = #{newStatus} where status = #{curStatus} and seq_num = \n" +
            "(select a.min_seq_num from \n" +
            " (select min(seq_num) as min_seq_num from lihu.queue where room_id = #{roomId} and bed_no = #{bedNo} and status = #{curStatus}) a )")
    /**
     * 把本工位、指定状态中「排队号最小」的一位改成新状态(过号用)。
     *
     * <p><b>【号统一改造必修】必须按行 id 更新。</b>
     * 旧实现外层条件是 {@code where status = ? and seq_num = (select min(seq_num) ...)}——
     * 外层<b>没有 room_id / bed_no 条件</b>,完全依赖"seq_num 全表唯一"才不会误伤。
     * 本需求把排队号改为「按检查类型各自编号」,号会跨检查类型重复,
     * 旧写法会一次性把<b>全院所有同号同状态的患者</b>一起改掉
     * (例:常规心电 5 号过号,动态心电的 5 号也被过号)。故改为先取 id、再按 id 更新。
     */
    @Update("update lihu.queue set status = #{newStatus} where id = " +
            "  (select a.id from " +
            "    (select id from lihu.queue where room_id = #{roomId} and bed_no = #{bedNo} " +
            "       and status = #{curStatus} and deleted = 0 " +
            "       order by case when seq_num is null then 1 else 0 end, seq_num, book_timeslot limit 1) a)")
    Integer updateBedQueueStatus(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus);
    // 弃用原因:需要先获取,判断逻辑处理后,再更新
@@ -340,40 +381,84 @@
    )
    Integer preemptToBeInstalledPatient(@Param("roomId")Long roomId, @Param("bedNo")String bedNo, @Param("curStatusList")List<Integer> curStatusList, @Param("newStatus")Integer newStatus);
    // 整个系统范围内 [排队中]人员 [被工位医生]抢占
    @Update("update lihu.queue set status = #{newStatus}, room_id = #{roomId}, room_name = #{roomName}, \n" +
            " bed_no = #{bedNo}, seq_num = #{seqNum} where id = \n" +
            "  (select a.id from \n" +
            "    (select id from lihu.queue where status = #{curStatus} and book_check_type= #{checkType} order by jump_flag desc, book_timeslot, create_time limit 1) a)")
    Integer preemptWaitingPatient(@Param("roomId")Long roomId, @Param("roomName")String roomName, @Param("bedNo")String bedNo,
                                  @Param("seqNum")Integer seqNum, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus,
                                  @Param("checkType")Integer checkType);
    // 整个系统范围内 [排队中]人员 [被工位医生]抢占 根据 多个检查项目
    @Update("<script>" +
            "update lihu.queue set status = #{newStatus}, room_id = #{roomId}, room_name = #{roomName}, \n" +
            " bed_no = #{bedNo}, seq_num = #{seqNum} where id = \n" +
            "  (select a.id from \n" +
            "    (select id from lihu.queue where status = #{curStatus} " +
            "       and book_check_type in ( " +
            "           <foreach collection='checkTypeArray' separator=',' item='_checkType'>" +
            "             #{_checkType} " +
            "           </foreach> )" +
            "       order by jump_flag desc, book_timeslot, create_time limit 1) a) " +
    /**
     * 取「本工位开展的检查项目」中下一位应分流的「排队中(10)」患者。
     *
     * <p>排序:<b>加急(jump_flag=1) → 预约时段 → 建单时间</b>;号为空(存量行)排最后。
     * <p><b>这里不再分配排队号</b>:号已在签到时一次性写入 {@code seq_num},
     * 分流只负责把患者从「排队中」搬到某个工位的「候诊中」。
     * <p>取出后再用 {@link #preemptWaitingPatientById} 做条件更新,
     * 靠 {@code and status = 10} 保证并发下只有一方能抢到该行。
     */
    @Select("<script>" +
            "select * from lihu.queue where deleted = 0 and status = #{curStatus} " +
            "   and book_check_type in ( " +
            "     <foreach collection='checkTypeArray' separator=',' item='_checkType'>" +
            "       #{_checkType} " +
            "     </foreach> )" +
            "   order by jump_flag desc, case when seq_num is null then 1 else 0 end, book_timeslot, create_time limit 1" +
            "</script>")
    Integer preemptWaitingPatientWithBedCheckTypes(@Param("roomId")Long roomId, @Param("roomName")String roomName, @Param("bedNo")String bedNo,
                                  @Param("seqNum")Integer seqNum, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus,
                                  @Param("checkTypeArray")Integer[] checkTypeArray);
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    QueueDO getFirstWaitingByCheckTypes(@Param("curStatus")Integer curStatus, @Param("checkTypeArray")Integer[] checkTypeArray);
    @Update("update lihu.queue set status = #{newStatus}, seq_num = #{seqNum} " +
            " where id = " +
            "  (select a.id from \n" +
            "    (select id from lihu.queue where room_id = #{roomId} and bed_no = #{bedNo} and status = #{curStatus} " +
            "      order by seq_num, book_timeslot limit 1" +
            "    ) a" +
            "  )" )
    Integer queueRecalledPatient(@Param("roomId")Long roomId, @Param("roomName")String roomName, @Param("bedNo")String bedNo,
                                 @Param("seqNum")Integer seqNum, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus);
    /**
     * 「本诊室/本工位**排队中、还没分配工位**」的患者(工作台"本诊室排队中(未分配)"分区用)。
     *
     * <p>背景:工作台的候诊列表按 {@code room_id + bed_no} 查,只包含**已经分流到该工位**的患者;
     * 而"排队中(10)"的患者 {@code bed_no} 是空的(见 QueueServiceImpl 重置逻辑:room_id=0 / bed_no=' '),
     * 大屏会显示他们(大屏不按诊室/工位过滤),于是出现"大屏有、工作台列表没有"的困惑。
     * 这里把"本工位检查类型、尚未分配工位"的排队患者单独查出来,由医生**手工拉入**。
     *
     * <p>判定"未分配"用 {@code bed_no} 为空(兼容 null / 空串 / 单个空格),
     * 不依赖 {@code room_id = 0} 这一约定,容错更好。
     */
    @Select("""
            <script>
                SELECT * FROM lihu.queue
                where deleted = 0 and status = #{curStatus}
                  and (bed_no is null or trim(bed_no) = '')
                  and book_check_type in (
                    <foreach collection='checkTypeArray' separator=',' item='_checkType'>
                      #{_checkType}
                    </foreach> )
                order by jump_flag desc,
                         case when seq_num is null then 1 else 0 end,
                         book_timeslot, create_time
            </script>
            """)
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    List<QueueDO> selectUnassignedWaitingByCheckTypes(@Param("curStatus")Integer curStatus,
                                                      @Param("checkTypeArray")Integer[] checkTypeArray);
    /**
     * 把「排队中」的某一行抢占到指定工位。
     *
     * <p><b>不改排队号</b>(号是签到时定的):只改状态、诊室、工位。
     * <p>{@code and status = #{curStatus}} 是并发保护 —— 两个工位同时抢同一位患者时,
     * 只有第一个 UPDATE 会命中(返回 1),另一方返回 0,需重新取人。
     */
    @Update("update lihu.queue set status = #{newStatus}, room_id = #{roomId}, room_name = #{roomName}, bed_no = #{bedNo} " +
            " where id = #{id} and status = #{curStatus} and deleted = 0")
    Integer preemptWaitingPatientById(@Param("id")Integer id, @Param("roomId")Long roomId, @Param("roomName")String roomName,
                                      @Param("bedNo")String bedNo, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus);
    /**
     * 【本需求·并发加固】按主键 + **当前状态**条件推进状态(叫号:候诊中(20) → 就诊中(30))。
     *
     * <p>为什么需要它:{@code nextPatient} 原来是"先查最小号的候诊行 → updateById",
     * 两个并发请求(双击 / 多端)可能各推进一条 READY,导致同一工位出现 **2 个「就诊中」**。
     * 有了 {@code and status = #{curStatus}}:只有第一个 UPDATE 命中(返回 1),
     * 另一方返回 0,由调用方放弃本次叫号,等下一次再取。
     */
    @Update("update lihu.queue set status = #{newStatus}, update_time = NOW() "
            + " where id = #{id} and status = #{curStatus} and deleted = 0")
    Integer updateStatusByIdIfCurrent(@Param("id") Integer id,
                                      @Param("curStatus") Integer curStatus,
                                      @Param("newStatus") Integer newStatus);
    // 常规检查 或 领用 的患者召回, 原工位处理
    @Update("update lihu.queue set status = #{newStatus}, passed = 1 " +
@@ -397,6 +482,24 @@
    // ==================== 需求2:误点「叫号 / 检查完成」的撤销 ====================
    /**
     * 批量取「这些患者今日的队列状态」(用于判断"已完成记录"是否被撤销过)。
     *
     * <p>【需求:撤销标识】撤销「检查完成」只是把队列行 40(已就诊) 改回 20(准备中),
     * 不碰 job_record。因此"这条完成记录是否已撤销"用**当日队列行的当前状态**判断即可:
     * 状态 ≠ 40 → 已被撤销;再次完成时会变回 40 → 自动恢复成"已完成"(无需额外清标记)。
     *
     * <p>只取当日(撤销本身就只支持当日),一次查询解决整页记录。
     */
    @Select("<script>" +
            "select pat_id as patId, book_check_type as checkType, status " +
            "  from lihu.queue " +
            " where deleted = 0 and TO_DAYS(book_date) = TO_DAYS(NOW()) " +
            "   and pat_id in ( " +
            "     <foreach collection='patIdList' separator=',' item='_patId'>#{_patId}</foreach> ) " +
            "</script>")
    List<Map<String, Object>> selectTodayStatusByPatIds(@Param("patIdList")List<String> patIdList);
    /**
     * 撤销「检查完成」:把本工位今日的**指定患者**从 已就诊(40) 退回 候诊中(20)。
     * <p>
     * 必须按 {@code pat_id + book_check_type + 今日} 精确定位,**不能**只按