<?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.DwdShbDsSwitchBaseMapper">

    <select id="getSwitchInfoPageByCondition" resultType="com.hdkj.lt.bf.entity.vo.SwitchInfoVO">

        select DISTINCT
        a.psr_Id switchId,
        a.name switchName,
        a.switch_role_name switchRoleName,
        a.feeder_name feederName,
        s.short_name maintOrgName,
        a.station_name stationName,
        c.oc1_value oc1Value,
        c.oc1_delay oc1Delay,
        c.oc1_setting oc1Setting,
        c.oc2_value oc2Value,
        c.oc2_delay oc2Delay,
        c.oc2_setting oc2Setting,
        c.oc3_value oc3Value,
        c.oc3_delay oc3Delay,
        c.oc3_setting oc3Setting,
        c.prot_enable_status protEnableStatus,
        c.reclose_enable_status recloseEnableStatus,
        c.hazard_type hazardType,
        c.hazard_desc hazardDesc,
        if(d.id is null ,'0','1') isContactSwitch,
        case
        when d.id is null then null
        when d.llsb_type = '0' then '0'
        when d.llsb_type = '1' then '0'
        when d.llsb_type = '2' then '1'
        when d.llsb_type = '3' then '1'
        else null
        end contactEffective
        from dwd_shb_ds_switch_base a
        left join fhzg_switch_protection_settings c on a.psr_id = c.device_prs_id
        left join t_disable_llkg_config d on a.psr_id= d.llsb_id
        left join sys_dept s on a.maint_org = s.isc_org_id
        <where>
            a.pub_priv_flag = '0' and a.psr_state = '20'
            <if test="params.feederName != null and params.feederName != ''">
                AND a.feeder_name LIKE CONCAT('%',#{params.feederName},'%')
            </if>
            <if test="params.feederId != null and params.feederId != ''">
                AND a.feeder = #{params.feederId}
            </if>
            <if test="params.switchName != null and params.switchName != ''">
                AND a.name LIKE CONCAT('%',#{params.switchName},'%')
            </if>
            <if test="params.switchId != null and params.switchId != ''">
                AND a.psr_id LIKE CONCAT('%',#{params.switchId},'%')
            </if>
            <if test="params.maintOrg != null and params.maintOrg != ''">
                AND a.maint_org = #{params.maintOrg}
            </if>
            <if test='params.isContactSwitch == "0" '>
                AND d.id is null
            </if>
            <if test='params.isContactSwitch == "1" '>
                AND d.id is not null
            </if>
            <if test='params.contactEffective == "0" '>
                AND d.llsb_type in ('0','1')
            </if>
            <if test='params.contactEffective == "1" '>
                AND d.llsb_type in ('2','3')
            </if>
            <if test="params.hazardType != null and params.hazardType != ''">
                AND c.hazard_type = #{params.hazardType}
            </if>

        </where>
        order by a.psr_id desc
    </select>

    <select id="getSwitchInfoByCondition" resultType="com.hdkj.lt.bf.entity.vo.SwitchInfoVO">

        select DISTINCT
        a.psr_Id switchId,
        a.name switchName,
        a.switch_role_name switchRoleName,
        a.feeder_name feederName,
        s.short_name maintOrgName,
        a.station_name stationName,
        c.oc1_value oc1Value,
        c.oc1_delay oc1Delay,
        c.oc1_setting oc1Setting,
        c.oc2_value oc2Value,
        c.oc2_delay oc2Delay,
        c.oc2_setting oc2Setting,
        c.oc3_value oc3Value,
        c.oc3_delay oc3Delay,
        c.oc3_setting oc3Setting,
        c.prot_enable_status protEnableStatus,
        c.reclose_enable_status recloseEnableStatus,
        c.hazard_type hazardType,
        c.hazard_desc hazardDesc,
        if(d.id is null ,'0','1') isContactSwitch,
        case
        when d.id is null then null
        when d.llsb_type = '0' then '0'
        when d.llsb_type = '1' then '0'
        when d.llsb_type = '2' then '1'
        when d.llsb_type = '3' then '1'
        else null
        end contactEffective
        from dwd_shb_ds_switch_base a
        left join fhzg_switch_protection_settings c on a.psr_id = c.device_prs_id
        left join t_disable_llkg_config d on a.psr_id= d.llsb_id
        left join sys_dept s on a.maint_org = s.isc_org_id
        <where>
            a.pub_priv_flag = '0' and a.psr_state = '20'
            <if test="params.feederName != null and params.feederName != ''">
                AND a.feeder_name LIKE CONCAT('%',#{params.feederName},'%')
            </if>
            <if test="params.feederId != null and params.feederId != ''">
                AND a.feeder = #{params.feederId}
            </if>
            <if test="params.switchName != null and params.switchName != ''">
                AND a.name LIKE CONCAT('%',#{params.switchName},'%')
            </if>
            <if test="params.switchId != null and params.switchId != ''">
                AND a.psr_id LIKE CONCAT('%',#{params.switchId},'%')
            </if>
            <if test="params.maintOrg != null and params.maintOrg != ''">
                AND a.maint_org = #{params.maintOrg}
            </if>
            <if test='params.isContactSwitch == "0" '>
                AND d.id is null
            </if>
            <if test='params.isContactSwitch == "1" '>
                AND d.id is not null
            </if>
            <if test='params.contactEffective == "0" '>
                AND d.llsb_type in ('0','1')
            </if>
            <if test='params.contactEffective == "1" '>
                AND d.llsb_type in ('2','3')
            </if>
            <if test="params.hazardType != null and params.hazardType != ''">
                AND c.hazard_type = #{params.hazardType}
            </if>

        </where>
        order by a.psr_id desc
    </select>


    <select id="getSwitchListByFeederId" resultType="com.hdkj.lt.bf.entity.vo.FeederSwitchInfoVO">
        select psr_id,
               pid,
               psr_type,
               name
        from t_feeder_topo
        where feeder = #{feederId}
    </select>

    <select id="getStartSwitchInfoByFeederId" resultType="com.hdkj.lt.bf.entity.vo.FeederSwitchInfoVO">
        select psr_id,
               pid,
               psr_type,
               name
        from t_feeder_topo
        where feeder = #{feederId}
          and is_start_kg = 1
    </select>
    <select id="getSwitchAndTransformerList" resultType="com.hdkj.lt.bf.entity.vo.SwitchOrTransformerVO">
        select distinct(a.psr_id),a.name as switchOrTransformerName,a.psr_type
        from t_feeder_topo a
        left join fhzg_dist_line_switch_section_show b ON a.psr_id=b.psr_id
        where a.feeder = #{feeder}
        and name LIKE CONCAT('%', #{name}, '%')
        and a.psr_type in
        <foreach collection='psrType' item='item' open='(' separator=',' close=')'>
            #{item}
        </foreach>
    </select>
    <select id="getShowSwitch" resultType="com.hdkj.lt.bf.entity.vo.SwitchOrTransformerVO">
        SELECT show_switch_id AS startSwitchId,
               psr_id
        FROM fhzg_dist_line_switch_section_show
        WHERE obj_id = #{feederId}
          and psr_Id = #{psrId}
    </select>
    <select id="getTree" resultType="com.hdkj.lt.bf.entity.dto.TopoDTO">
        SELECT psr_id, pid, feeder, sf_fz_sdkg, normal_open, psr_type
        FROM t_feeder_topo
        where feeder = #{feederId};
    </select>

</mapper>
