<?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.mq.mapper.base.FeederTopoMapper">

    <insert id="saveDistLineSwitchSection">
        INSERT INTO fhzg_dist_line_switch_section(line_id, load_switch_id, load_switch_name, start_switch_id,
        start_switch_name,
        start_switch_type, end_switch_id, end_switch_name, create_time, users_count)
        WITH res_tab AS (SELECT count(psr_id) zxkg_count FROM t_feeder_topo
        WHERE feeder = #{feeder} AND zxfz_type='主线' group by feeder)
        SELECT t.*, ifnull(v.user_count,0) user_count FROM (
        WITH RECURSIVE hierarchy AS (
        SELECT psr_id switch_id, name switch_name, zxfz_type, psr_id, name, start_kg_id, psr_type
        FROM t_feeder_topo WHERE is_start_kg = '1' and feeder = #{feeder}
        UNION ALL
        SELECT if(child.zxfz_type='主线' or (select * from res_tab)=0, child.psr_id, parent.switch_id) switch_id,
        if(child.zxfz_type='主线' or (select * from res_tab)=0, child.name, parent.switch_name) switch_name,
        child.zxfz_type, child.psr_id, child.name, child.start_kg_id, child.psr_type
        FROM t_feeder_topo child JOIN hierarchy parent ON child.start_kg_id = parent.psr_id
        WHERE child.feeder = #{feeder}
        AND child.psr_type IN
        <foreach collection='kgPsrType' item='item' open='(' separator=',' close=')'>
            #{item}
        </foreach>
        ) SELECT #{feeder} lineId, parent.switch_id AS loadSwitchId, parent.switch_name AS loadSwitchName,
        parent.psr_id AS startSwitchId, parent.name AS startSwitchName,parent.psr_type as startSwitchType,
        GROUP_CONCAT(child.psr_id SEPARATOR ',') AS endSwitchId,
        GROUP_CONCAT(child.name SEPARATOR ',') AS endSwitchName, now() nowTime
        FROM hierarchy child RIGHT JOIN hierarchy parent ON child.start_kg_id = parent.psr_id
        GROUP BY parent.psr_id, parent.name , parent.psr_type, parent.switch_id, parent.switch_name) t
        left join (select start_kg_id, count(distinct if(d.psr_id is null,t.psr_id,d.join_ec)) user_count
        from t_feeder_topo t left join dwd_shb_ds_joinec_transformer_base d on t.psr_id = d.psr_id
        where t.psr_type in
        <foreach collection='pbPsrType' item='item' open='(' separator=',' close=')'>
            #{item}
        </foreach>
        and t.feeder = #{feeder} group by t.start_kg_id) v on t.startSwitchId = v.start_kg_id
    </insert>

    <insert id="insertDisableLlkgConfig">
        insert into t_disable_llkg_config(LLSB_ID, CITY_CODE, CREATE_TIME, LAST_TIME, LLSB_TYPE)
        select distinct t.psr_id,
                        t.city_code,
                        now()                                                                          c_time,
                        now()                                                                          u_time,
                        if(t.sszf_type not in ('PD_30000004', 'PD_32400000', 'PD_20400000'), '0', '2') l_type
        from t_feeder_topo t
                 left join t_disable_llkg_config f on t.psr_id = f.llsb_id
        where t.normal_open = 'true'
          and f.id is null
    </insert>

    <update id="updateDisableLlkgConfig">
        update t_disable_llkg_config x, (select distinct t.psr_id,
                                                         case
                                                             when f.llsb_type in ('1', '3') then f.llsb_type
                                                             when sszf_type not in ('PD_30000004', 'PD_32400000', 'PD_20400000')
                                                                 then '0'
                                                             else '2' end llsb_type
                                         from t_feeder_topo t
                                                  join t_disable_llkg_config f on t.psr_id = f.llsb_id
                                         where t.normal_open = 'true') y
        set x.llsb_type = y.llsb_type,
            x.last_time = now()
        where x.llsb_id = y.psr_id
    </update>

    <delete id="deleteDistLineSwitchSectionByLineId">
        delete
        from fhzg_dist_line_switch_section
        where line_id = #{feeder}
    </delete>
    <delete id="deleteDisableLlkgConfig">
        delete f.*
        from t_disable_llkg_config f
                 join t_feeder_topo t on t.psr_id = f.llsb_id
        where t.normal_open = 'false'
    </delete>

    <select id="getDeviceType" resultType="com.hdkj.lt.mq.entity.dto.DeviceTypeDto">
        SELECT
        a.psr_id, b.dwd_psr_type, a.start_kg_id,a.is_start_kg
        FROM
        t_feeder_topo a
        LEFT JOIN fhzg_device_type_mapping b ON a.psr_type = b.topo_psr_type
        <where>
            <if test="psrIds != null and psrIds.size() > 0">
                and a.psr_id in
                <foreach item="item" collection="psrIds" open="(" separator="," close=")">
                    #{item, jdbcType=BIGINT}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getFeederCheckRule" resultType="com.hdkj.lt.modle.dto.simulation.FeederCheckRule">
        SELECT DISTINCT(a.feeder)
        FROM t_feeder_topo a
                 JOIN(SELECT *
                      from t_feeder_topo
                      WHERE feeder = #{lineId}
                        and normal_open = 'true') b
                     ON a.psr_id = b.psr_id
    </select>
    <select id="getFeederInfoById" resultType="com.hdkj.lt.core.bizms.modle.po.FeederTopo">
        SELECT *
        FROM t_feeder_topo
        where feeder = #{lineId}
          and is_start_kg = '1'
    </select>

    <select id="getCityShortListByIds" resultType="java.lang.String">
        select distinct a.tab_suffix
        from fhzg_dist_organ_table_suffix_config a
        join dwd_shb_ds_feeder_base b on a.org_id=b.city
        where b.psr_id in
        <foreach item="item" collection="psrIds" open="(" separator="," close=")">#{item}</foreach>
    </select>

    <select id="getSwitchInfoByPsrID" resultType="com.hdkj.lt.mq.entity.dto.BreakerInfo">
        select distinct name as breakerName, dtm.dwd_psr_type as breakerPsrType, psr_id as breakerPsrId
        from t_feeder_topo ft
                 left join fhzg_device_type_mapping dtm on ft.psr_type = dtm.topo_psr_type
        where psr_id = #{psrId}

    </select>


</mapper>
