<?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.hdkj.lt.bf.mapper.FaultAwaitTransferUsersMapper">

    <select id="selectAwaitUserCountByFaultId"
            resultType="com.hdkj.lt.core.bizms.modle.dto.FhzgFaultAwaitTransferUsersCountDTO">
        SELECT
        atu.fault_id faultId,
        count( id ) userCount,
        count( CASE WHEN atu.is_critical_user = 1 THEN 1 END ) transferCriticalUsers,
        count( CASE WHEN atu.is_sensitive_user = 1 THEN 1 END ) transferSensitiveUsers,
        count( CASE WHEN atu.is_protect_power = 1 THEN 1 END ) protectUsers,
        sum( atu.user_loads ) userLoads
        FROM
        fhzg_fault_await_transfer_users atu
        where atu.fault_id in
        <if test="faultIdList != null and faultIdList.size > 0">
            <foreach collection="faultIdList" open="(" item="faultId" separator="," close=")">
                #{faultId}
            </foreach>
        </if>
        GROUP BY
        atu.fault_id
    </select>

    <select id="selectUserList" resultType="com.hdkj.lt.core.bizms.modle.vo.FhzgFaultAwaitTransferUsersVO">
        SELECT
        t.id id,
        t.fault_id faultId,
        t.transformer_no transformerNo,
        t.transformer_name transformerName,
        ss.show_switch_id loadSwitchId,
        ss.show_switch_name loadSwitchName,
        t.user_loads userLoads,
        t.user_current userCurrent,
        t.user_voltage userVoltage,
        t.user_reactive_power userReactivePower,
        t.is_critical_user isCriticalUser,
        t.is_sensitive_user isSensitiveUser,
        t.is_dual_power isDualPower
        FROM fhzg_fault_await_transfer_users t
        LEFT JOIN fhzg_fault_await_transfer_switch_section s ON t.switch_section_id = s.id and t.fault_id=s.fault_id
        LEFT JOIN fhzg_dist_line_switch_section_show ss ON ss.psr_id = s.start_switch_id and ss.obj_id = CONCAT(#{params.faultId})
        where t.fault_id = #{params.faultId}
        <if test="params.transformerName != null and params.transformerName != '' ">
            and t.transformer_name  like concat('%' ,#{params.transformerName}, '%')
        </if>
        ORDER BY
        t.user_reactive_power DESC,t.id desc
    </select>
</mapper>
