yxh
14 小时以前 db6ffd6a99b46bd27173503c10aa4cd1afc9c775
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/dal/mysql/queue/QueueMapper.java
@@ -285,6 +285,55 @@
                                          @Param("curStatus")Integer curStatus);
    /**
     * 按「工位 + 患者 + 检查类型 + 状态**集合**」取一行(今日,号最小)。
     *
     * <p>【本需求·不叫号直接检查】用于「检查完成」时收尾**他自己的**排队行:
     * 从工作台右侧列表点「检查」加载的患者,全程不会被置为「就诊中(30)」
     * (因为要求不上大屏、不叫号),完成检查时就没有 30 的行可以改 40,
     * 若不处理他会一直留在候诊列表和大屏上。
     *
     * <p>允许的状态:排队中(10)/已召回(15)/准备中(20)——都是"还没做完"的状态。
     */
    @Select("<script>" +
            "SELECT * from lihu.queue " +
            "where room_id = #{roomId} and bed_no = #{bedNo} " +
            "  and pat_id = #{patId} and book_check_type = #{checkType} " +
            "  and status in ( " +
            "    <foreach collection='statusList' separator=',' item='_status'>#{_status}</foreach> " +
            "  ) " +
            "  and TO_DAYS(book_date) = TO_DAYS(NOW()) " +
            "order by seq_num limit 1" +
            "</script>")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    QueueDO getBedQueueItemByPatAndStatuses(@Param("roomId")Long roomId, @Param("bedNo")String bedNo,
                                            @Param("patId")String patId, @Param("checkType")Integer checkType,
                                            @Param("statusList")List<Integer> statusList);
    /**
     * 取「还没分配工位」的排队行({@code bed_no} 为空,今日,号最小)。
     *
     * <p>场景同上:医生在「本诊室排队中(未分配工位)」里直接点「检查」做掉某位患者,
     * 该行没有 room/bed,完成检查时要按患者 + 检查项目把它收尾。
     */
    @Select("<script>" +
            "SELECT * from lihu.queue " +
            "where pat_id = #{patId} and book_check_type = #{checkType} " +
            "  and (bed_no is null or trim(bed_no) = '') " +
            "  and status in ( " +
            "    <foreach collection='statusList' separator=',' item='_status'>#{_status}</foreach> " +
            "  ) " +
            "  and TO_DAYS(book_date) = TO_DAYS(NOW()) " +
            "order by seq_num limit 1" +
            "</script>")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    QueueDO getUnassignedQueueItemByPatAndStatuses(@Param("patId")String patId, @Param("checkType")Integer checkType,
                                                   @Param("statusList")List<Integer> statusList);
    /**
     * 按工位 + 状态 + 今日取**最近更新**的一条。
     * <p>
     * 比「seq_num 最小」更贴合"当前正在就诊/装机的那位患者":seq_num 最小是本时段最早排队的,
@@ -447,6 +496,27 @@
                                      @Param("bedNo")String bedNo, @Param("curStatus")Integer curStatus, @Param("newStatus")Integer newStatus);
    /**
     * 【修复·现场问题】把某工位上的「准备中/已召回/亲和」患者**退回「排队中」**,并清空诊室/工位。
     *
     * <p>用于「管理员清空工位」与「闭诊」。原实现只改 `clinic_room`,患者仍挂在这个工位上,
     * 而工位已经没人了 —— 他们既不在任何医生的候诊列表里,也不在
     * 「本诊室排队中(未分配)」里(那个列表只查 {@code status=10 且 bed_no 为空}),
     * 等于"消失",只能等每日重置。退回后即可被其它开诊工位重新分流 / 手工「拉入」。
     *
     * <p>同时把 {@code affinity_item} 复位为 0:亲和项重新变成独立的排队患者,
     * 下次被分流时会由亲和逻辑重新挂到主项上({@code procAffinityWhenPreempt})。
     */
    @Update("<script>" +
            "update lihu.queue " +
            "set status = #{newStatus}, room_id = 0, room_name = '', bed_no = null, affinity_item = 0 " +
            "where room_id = #{roomId} and bed_no = #{bedNo} and deleted = 0 " +
            "  and status in ( <foreach collection='statusList' separator=',' item='_status'>#{_status}</foreach> )" +
            "</script>")
    Integer releaseBedPatientsToWaiting(@Param("roomId") Long roomId, @Param("bedNo") String bedNo,
                                        @Param("newStatus") Integer newStatus,
                                        @Param("statusList") List<Integer> statusList);
    /**
     * 【本需求·并发加固】按主键 + **当前状态**条件推进状态(叫号:候诊中(20) → 就诊中(30))。
     *
     * <p>为什么需要它:{@code nextPatient} 原来是"先查最小号的候诊行 → updateById",
@@ -546,5 +616,127 @@
    //@Update("TRUNCATE TABLE lihu.queue")
    @Delete("delete from lihu.queue where TO_DAYS(book_date) != TO_DAYS(NOW())")
    void clearQueue();
    // ========================================================================
    // 共享候诊池(check_type.share_pool = 1,详见 _prep/方案-1-2诊室共享候诊池.md)
    //
    // 池中患者的判定:status = 20(准备中) 且 **未绑定诊室**(bed_no 为空)。
    // 与"已绑定某工位的 20"区分:后者 room_id/bed_no 有值,仍按工位查。
    // ========================================================================
    /**
     * 取共享池/共享排队中「号最小」的一位(今日,未绑定诊室)。
     *
     * <p>{@code curStatus}=20 取"准备中",=10 取"排队中"(池空时叫号的兜底)。
     * 排序与现有叫号一致:加急优先 → 排队号 → 时段 → 创建时间。
     */
    @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='_t'>#{_t}</foreach> " +
            "  ) " +
            "  AND TO_DAYS(book_date) = TO_DAYS(NOW()) " +
            "ORDER BY jump_flag DESC, CASE WHEN seq_num IS NULL THEN 1 ELSE 0 END, seq_num, book_timeslot, create_time " +
            "LIMIT 1" +
            "</script>")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    QueueDO selectSharedFirst(@Param("curStatus") Integer curStatus,
                              @Param("checkTypeArray") Integer[] checkTypeArray);
    /**
     * 取共享池/共享排队中的患者列表(工作台「共享候诊区」用)。
     */
    @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='_t'>#{_t}</foreach> " +
            "  ) " +
            "  AND TO_DAYS(book_date) = TO_DAYS(NOW()) " +
            "ORDER BY jump_flag DESC, CASE WHEN seq_num IS NULL THEN 1 ELSE 0 END, seq_num, book_timeslot, create_time" +
            "</script>")
    @Results({
            @Result(property = "patDetails", column = "pat_details", typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler.class)
    })
    List<QueueDO> selectSharedList(@Param("curStatus") Integer curStatus,
                                   @Param("checkTypeArray") Integer[] checkTypeArray);
    /**
     * 统计共享池当前「准备中且未绑定」的人数(用于池容量控制)。
     */
    @Select("<script>" +
            "SELECT COUNT(1) FROM lihu.queue WHERE deleted = 0 AND status = 20 " +
            "  AND (bed_no IS NULL OR TRIM(bed_no) = '') " +
            "  AND book_check_type IN ( " +
            "    <foreach collection='checkTypeArray' separator=',' item='_t'>#{_t}</foreach> " +
            "  ) " +
            "  AND TO_DAYS(book_date) = TO_DAYS(NOW())" +
            "</script>")
    Integer countSharedPool(@Param("checkTypeArray") Integer[] checkTypeArray);
    /**
     * 把「排队中(10)」提升为共享池「准备中(20)」,保持不绑定诊室。
     * <p>{@code and status = 10} 保证并发下只有一方抢到该行。
     */
    @Update("update lihu.queue set status = 20, room_id = 0, room_name = '', bed_no = null " +
            " where id = #{id} and status = 10 and deleted = 0")
    Integer promoteToSharedPool(@Param("id") Integer id);
    /**
     * 「拉入」共享池患者:把池中「准备中(20) 未绑定」的行绑定到本工位(状态仍为 20)。
     * <p>{@code bed_no 为空} 是并发保护:池中同一行只会被一个诊室拉走。
     */
    @Update("update lihu.queue set room_id = #{roomId}, room_name = #{roomName}, bed_no = #{bedNo} " +
            " where id = #{id} and status = 20 and (bed_no is null or trim(bed_no) = '') and deleted = 0")
    Integer bindSharedPoolItem(@Param("id") Integer id, @Param("roomId") Long roomId,
                               @Param("roomName") String roomName, @Param("bedNo") String bedNo);
    /**
     * 叫号:把共享池(20)/共享排队中(10)的患者改为「就诊中(30)」并绑定到叫号诊室。
     * <p>{@code and status = #{curStatus}} 保证两个诊室同时叫同一人时只有一个成功。
     */
    @Update("update lihu.queue set status = 30, room_id = #{roomId}, room_name = #{roomName}, bed_no = #{bedNo} " +
            " where id = #{id} and status = #{curStatus} and deleted = 0")
    Integer callSharedItem(@Param("id") Integer id, @Param("roomId") Long roomId,
                           @Param("roomName") String roomName, @Param("bedNo") String bedNo,
                           @Param("curStatus") Integer curStatus);
    /**
     * 闭诊/跨天收尾:把共享池里未绑定的「准备中(20)」退回「排队中(10)」。
     */
    @Update("update lihu.queue set status = 10, room_id = 0, room_name = '', bed_no = null " +
            " where status = 20 and (bed_no is null or trim(bed_no) = '') and deleted = 0")
    Integer releaseSharedPoolToWaiting();
    /**
     * 共享项目「过号(5)」召回:**回共享池**(清空诊室/工位,置为排队中(10)),
     * 随后由池填充器按容量提升为准备中,任一诊室都可叫。
     */
    @Update("update lihu.queue set status = #{newStatus}, passed = 1, room_id = 0, room_name = '', bed_no = null " +
            " where room_id = #{roomId} and bed_no = #{bedNo} and status = #{curStatus} " +
            "   and pat_id = #{patId} and book_check_type = #{checkType} and deleted = 0")
    Integer recallPassedPatientToPool(@Param("roomId") Long roomId, @Param("bedNo") String bedNo,
                                      @Param("patId") String patId, @Param("checkType") Integer checkType,
                                      @Param("curStatus") Integer curStatus, @Param("newStatus") Integer newStatus);
    /**
     * 共享项目「撤销叫号」:就诊中(30) → 排队中(10),并清空诊室/工位(回共享池)。
     */
    @Update("update lihu.queue set status = 10, room_id = 0, room_name = '', bed_no = null " +
            " where id = #{id} and status = 30 and deleted = 0")
    Integer undoCallToSharedPool(@Param("id") Integer id);
    /**
     * 共享项目「撤销完成」:已就诊(40) → 排队中(10),并清空诊室/工位(回共享池)。
     */
    @Update("update lihu.queue set status = 10, passed = 0, room_id = 0, room_name = '', bed_no = null " +
            " where room_id = #{roomId} and bed_no = #{bedNo} and status = 40 " +
            "   and pat_id = #{patId} and book_check_type = #{checkType} " +
            "   and TO_DAYS(book_date) = TO_DAYS(NOW())")
    Integer undoFinishToSharedPool(@Param("roomId") Long roomId, @Param("bedNo") String bedNo,
                                   @Param("patId") String patId, @Param("checkType") Integer checkType);
}