<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.his.api.db.dao.FlowRegulationDao">

    <resultMap id="BaseResultMap" type="com.example.his.api.db.pojo.FlowRegulationEntity">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="place" column="place" jdbcType="VARCHAR"/>
        <result property="realNum" column="real_num" jdbcType="INTEGER"/>
        <result property="maxNum" column="max_num" jdbcType="INTEGER"/>
        <result property="weight" column="weight" jdbcType="TINYINT"/>
        <result property="priority" column="priority" jdbcType="TINYINT"/>
        <result property="blueUuid" column="blue_uuid" jdbcType="VARCHAR"/>
    </resultMap>


    <!-- 人员调流-查询分页 -->
    <select id="searchFlowRegulationByPage" parameterType="Map" resultType="hashmap">
        SELECT
        id,
        place,
        real_num AS realNum,
        max_num AS maxNum,
        weight,
        priority,
        blue_uuid AS blueUuid
        FROM
        tb_flow_regulation
        <where>
            <if test="place!=null">
                place = #{place}
            </if>
            <if test="blueUuid!=null">
                AND blue_uuid = #{blueUuid}
            </if>
        </where>
    </select>

    <!-- 人员调流-查询调流信息 -->
    <select id="searchFlowRegulationById" parameterType="int" resultType="hashmap">
        SELECT id,
               place,
               real_num  AS realNum,
               max_num   AS maxNum,
               weight,
               priority,
               blue_uuid AS blueUuid
        FROM tb_flow_regulation
        WHERE id = #{id}
    </select>


    <!-- 人员调流-新增 -->
    <insert id="insertFlowRegulation" parameterType="Map">
        INSERT INTO tb_flow_regulation
        SET place     = #{place},
            max_num   = #{maxNum},
            weight    = #{weight},
            priority  = #{priority},
            blue_uuid = #{blueUuid}
    </insert>

    <!-- 人员调流-更新 -->
    <update id="updateFlowRegulation" parameterType="Map">
        UPDATE tb_flow_regulation
        SET place     = #{place},
            max_num   = #{maxNum},
            weight    = #{weight},
            priority  = #{priority},
            blue_uuid = #{blueUuid}
        WHERE id = #{id}
    </update>


    <!-- 人员调流-更新科室真实排队人数 -->
    <update id="updateRealNum" parameterType="Map">
        UPDATE tb_flow_regulation
        SET real_num = #{realNum}
        <if test="id!=null">
            WHERE id = #{id}
        </if>
    </update>

    <!-- 人员调流-权重排序 -->
    <select id="searchRecommendedWithWeight" resultType="HashMap">
        SELECT r.id,
               r.place,
               r.real_num AS realNum,
               r.weight,
               ( r.weight / t.sum ) * (r.max_num - r.real_num ) AS score
        FROM tb_flow_regulation r
            JOIN ( SELECT SUM( weight ) AS sum FROM tb_flow_regulation ) t
        ORDER BY score DESC
    </select>

    <!-- 人员调流-优先级排序 -->
    <select id="searchRecommendedWithPriority" resultType="HashMap">
        SELECT id,
               place,
               real_num AS realNum
        FROM tb_flow_regulation
        ORDER BY priority DESC, weight ASC
    </select>

    <!-- 人员限流-删除 -->
    <delete id="deleteFlowRegulationById">
        delete from tb_flow_regulation where id = #{id}
    </delete>



</mapper>
