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

    <select id="getOperationAbnormalityList" resultType="com.hdkj.lt.bf.entity.vo.FhzgGridOperationAbnormalityVO">
        select t1.*, t3.short_name countyName, t4.id schemeId, t4.file_id schemeFileId
        from fhzg_grid_operation_abnormality t1
        left join dwd_shb_ds_feeder_base t2 on t1.line_id = t2.psr_id
        left join sys_dept t3 on t3.isc_org_id = t2.maint_org
        left join fhzg_operation_abnormality_transfer_scheme t4 on t1.id =t4.oa_id
        <where>
            <if test="queryDTO.countyCode != null and queryDTO.countyCode != ''">
                and t3.dept_id = #{queryDTO.countyCode}
            </if>
            <if test="queryDTO.countyCodeList != null and queryDTO.countyCodeList.size > 0">
                AND t3.dept_id IN
                <foreach collection="queryDTO.countyCodeList" open="(" item="item" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="queryDTO.feederName != null and queryDTO.feederName != ''">
                and t1.line_name like concat('%',#{queryDTO.feederName},'%')
            </if>
            <if test="queryDTO.volStartTime != null ">
                and t1.alarm_start_time > #{queryDTO.volStartTime}
            </if>
            <if test="queryDTO.volEndTime != null ">
                and t1.alarm_end_time &lt; #{queryDTO.volEndTime}
            </if>
        </where>
        order by t1.alarm_start_time desc
    </select>

    <select id="getBreakerAreaInfo" resultType="com.hdkj.lt.bf.entity.vo.FeederDeviceVO">
        <!-- 首先根据转供路径开关分合动作，确定开关被带供、带供线路、开关开合信息 -->
        with line_switch_tab as (
          select f.line_id bdg_line_id,e.switch_id dk_kg_id,d.line_id dg_line_id,s.switch_id bh_kg_id,e.switching_seq
          from fhzg_operation_abnormality_switching_sequence s
          join fhzg_operation_abnormality_await_transfer_path_detail d on d.oa_id=s.oa_id and d.close_switch_id=s.switch_id
          join fhzg_operation_abnormality_await_transfer_path_detail f on d.group_line_id=f.group_line_id and f.close_switch_id is null
          join (select oa_id, (switching_seq - 1) switch_seq, switch_id, switching_seq
            from fhzg_operation_abnormality_switching_sequence where operation_type = 'open'
          ) e on s.switching_seq= e.switch_seq and s.oa_id= e.oa_id and s.operation_type='close'where s.oa_id=#{oaId} )
        <!-- 第一部分查被带供线路的开关，通过被带供线路的分开开关，查级联开关母线设备并归属到带供线路 -->
        select after_feeder, psr_id, psr_name, psr_type, feeder, switch_id, end_switch_id, switching_seq
        from (with recursive res_tab as (
            select t.psr_id, t.name, t.psr_type, feeder, st.dk_kg_id switch_id, st.switching_seq, st.dg_line_id
            from t_feeder_topo t join line_switch_tab st on t.feeder = st.bdg_line_id and t.psr_id = st.dk_kg_id
            union all
            select t.psr_id, t.name, t.psr_type, t.feeder, p.switch_id, p.switching_seq, p.dg_line_id
            from t_feeder_topo t join res_tab p on t.start_kg_id = p.psr_id and t.feeder = p.feeder
            where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="('PD_31100000'," separator=',' close=')'>#{item}</foreach>
          ) select feeder after_feeder,psr_id,name psr_name,psr_type,dg_line_id feeder,switch_id,null end_switch_id,switching_seq
          from res_tab where psr_id != switch_id) t
        union all
        <!-- 第一部分查带供线路的开关，通过带供线路的出线开关查到闭合开关截止(闭合开一部分已经查过所以屏蔽) -->
        select after_feeder, psr_id, psr_name, psr_type, feeder, switch_id, end_switch_id, switching_seq
        from (with recursive res_tab as (
            select st.bdg_line_id, t.psr_id, t.name, t.psr_type, t.feeder, st.dk_kg_id switch_id, st.switching_seq, st.bh_kg_id, t.psr_id kg_id
            from t_feeder_topo t join line_switch_tab st on t.feeder = st.dg_line_id
            and (case when st.dg_line_id = st.bdg_line_id then t.psr_id = st.dk_kg_id else t.is_start_kg = '1' end)
            union all
            select p.bdg_line_id,if(t.psr_id=p.bh_kg_id,'-',t.psr_id) psr_id,t.name,t.psr_type,t.feeder,p.switch_id,p.switching_seq,p.bh_kg_id,p.kg_id
            from t_feeder_topo t join res_tab p on t.start_kg_id = p.psr_id and t.feeder = p.feeder
            where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="('PD_31100000'," separator=',' close=')'>#{item}</foreach>
          ) select bdg_line_id after_feeder, psr_id, name psr_name, psr_type, feeder, switch_id, null end_switch_id, switching_seq
          from res_tab where psr_id not in ('-', kg_id)) t
    </select>

    <select id="getDefaultBreakerAreaInfo" resultType="com.hdkj.lt.bf.entity.vo.FeederDeviceVO">
        with recursive res_tab as (
            select t.feeder, t.feeder_name, t.psr_id, t.name, t.psr_type, t.normal_open, t.start_kg_id
            from t_feeder_topo t join t_feeder_topo f on t.psr_id = f.psr_id and t.feeder != f.feeder
            where f.normal_open = 'true' and f.feeder = #{feederId}
            union all
            select t.feeder, t.feeder_name, t.psr_id, t.name, t.psr_type, t.normal_open, t.start_kg_id
            from t_feeder_topo t join res_tab p on t.psr_id = p.start_kg_id and t.feeder = p.feeder
            where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="('PD_31100000'," separator=',' close=')'>#{item}</foreach>
        ) select if(t.normal_open = 'true', '-', t.psr_id) psr_id, t.name, t.normal_open,
            t.psr_type, t.feeder, if(t.normal_open = 'true', t.psr_id, '-') end_switch_id from res_tab t union
        select if(t.normal_open = 'true', '-', t.psr_id) psr_id, t.name, t.normal_open,
            t.psr_type, t.feeder, if(t.normal_open = 'true', t.psr_id, '-') end_switch_id
        from t_feeder_topo t join res_tab rt on t.start_kg_id = rt.psr_id and t.feeder = rt.feeder
        where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="('PD_31100000'," separator=',' close=')'>#{item}</foreach>
    </select>

    <select id="getDistLineDynamicSvgDirectCurrent" resultType="com.hdkj.lt.bf.entity.vo.DynamicSvgDirectVO">
        select obj_id, cl_list_json, kg_list_json from fhzg_dist_line_dynamic_svg_direct_current where obj_id = #{objId} limit 1
    </select>

    <select id="getFeederSwitchBusbar" resultType="com.hdkj.lt.bf.entity.vo.FeederDeviceVO">
        select psr_id, `name` psr_name, psr_type, feeder, start_kg_id switch_id
        from t_feeder_topo t where psr_type = 'PD_31100000'
        and t.feeder in <foreach collection='feederList' item='item' open="(" separator=',' close=')'>#{item}</foreach>
    </select>

    <select id="getFeederSwitchByPsrId" resultType="com.hdkj.lt.bf.entity.vo.FeederSwitchVO">
        select psr_id, `name` psr_name, feeder, feeder_name, start_kg_id, is_start_kg
        from t_feeder_topo t where t.psr_id in
        <foreach collection='psrIdList' item='item' open="(" separator=',' close=')'>#{item}</foreach>
    </select>

    <select id="getDistDynamicSvgDirectCurrentInfo" resultType="com.hdkj.lt.bf.entity.vo.DirectCurrentSwitchVO">
        with switch_res_tab as (
        <choose>
            <!-- objType：1-计划场景，2-计划方案场景，3-故障场景，4-故障方案场景，5-运行调优场景，6-运行调优方案场景 -->
            <when test='queryDTO.objType == "1" or queryDTO.objType == "2"'>
                select distinct b.feeder line_id,ifnull(start_switch,kg_id) kg_id,ifnull(closed_state,'-') closed_state
                from (select a.psr_id kg_id, a.normal_open
                    from t_feeder_topo a join fhzg_plan_power_cut c on a.feeder=c.line_id and a.normal_open='true'
                    <choose>
                        <when test='queryDTO.objType == "1"'>
                            where c.id = #{queryDTO.objId}
                        </when>
                        <otherwise>
                            join fhzg_plan_transfer_scheme ts on c.id = ts.plan_id where ts.id = #{queryDTO.objId}
                        </otherwise>
                    </choose>
                ) a join t_feeder_topo b on a.kg_id=b.psr_id and a.normal_open=b.normal_open
                left join (
                    select c.line_id, s.start_switch, '0' closed_state from fhzg_plan_power_cut c
                    <choose>
                        <when test='queryDTO.objType == "1"'>
                            join fhzg_plan_power_cut_switch s on s.plan_id = c.id where s.plan_id = #{queryDTO.objId}
                        </when>
                        <otherwise>
                            join (
                                select a.plan_id, b.switch_id start_switch, b.operation_content from fhzg_plan_transfer_scheme a
                                join fhzg_plan_transfer_scheme_switching_sequence b on b.scheme_id=a.id where a.id=#{queryDTO.objId}
                                union
                                select a.plan_id, b.start_switch, '0' closed_state from fhzg_plan_transfer_scheme a
                                join fhzg_plan_power_cut_switch b on b.plan_id = a.plan_id where a.id = #{queryDTO.objId}
                            ) s on s.plan_id = c.id
                        </otherwise>
                    </choose>
                ) kg on b.feeder = kg.line_id
            </when>
            <when test='queryDTO.objType == "3" or queryDTO.objType == "4"'>
                select distinct b.feeder line_id, ifnull(start_switch,kg_id) kg_id, ifnull(closed_state,'-') closed_state
                from (select a.psr_id kg_id, a.normal_open
                    from t_feeder_topo a join fhzg_fault_power_cut c on a.feeder=c.line_id and a.normal_open='true'
                    <choose>
                        <when test='queryDTO.objType == "3"'>
                            where c.id = #{queryDTO.objId}
                        </when>
                        <otherwise>
                            join fhzg_fault_transfer_scheme ts on c.id = ts.fault_id where ts.id = #{queryDTO.objId}
                        </otherwise>
                    </choose>
                ) a join t_feeder_topo b on a.kg_id=b.psr_id and a.normal_open=b.normal_open
                left join (
                    select c.line_id, s.start_switch, '0' closed_state from fhzg_fault_power_cut c
                    <choose>
                        <when test='queryDTO.objType == "3"'>
                            join fhzg_fault_power_cut_switch s on s.fault_id = c.id where s.fault_id = #{queryDTO.objId}
                        </when>
                        <otherwise>
                            join (
                                select a.fault_id, b.switch_id start_switch, b.operation_content from fhzg_fault_transfer_scheme a
                                join fhzg_fault_transfer_scheme_switching_sequence b on b.scheme_id=a.id where a.id=#{queryDTO.objId}
                                union
                                select a.fault_id, b.start_switch, '0' closed_state from fhzg_fault_transfer_scheme a
                                join fhzg_fault_power_cut_switch b on b.fault_id = a.fault_id where a.id = #{queryDTO.objId}
                            ) s on s.fault_id = c.id
                        </otherwise>
                    </choose>
                ) kg on b.feeder = kg.line_id
            </when>
            <when test='queryDTO.objType == "5" or queryDTO.objType == "6"'>
                select s.line_id, s.switch_id kg_id, s.closed_state
                from fhzg_dist_line_switch_status s
                <choose>
                    <when test='queryDTO.objType == "5"'>
                        where s.obj_id = #{queryDTO.objId}
                    </when>
                    <otherwise>
                        left join (
                          select ts.id, s.switch_id from fhzg_operation_abnormality_switching_sequence s
                          join fhzg_operation_abnormality_transfer_scheme ts on s.oa_id = ts.oa_id
                        ) t on s.obj_id = t.id and s.switch_id = t.switch_id
                        where s.obj_id = #{queryDTO.objId} and t.id is null
                        union all
                        select c.line_id, s.switch_id kg_id, if(operation_type='open','0','1') closed_state
                        from fhzg_grid_operation_abnormality c
                        join fhzg_operation_abnormality_switching_sequence s on s.oa_id = c.id
                        join fhzg_operation_abnormality_transfer_scheme ts on s.oa_id=ts.oa_id where ts.id=#{queryDTO.objId}
                    </otherwise>
                </choose>
            </when>
            <otherwise>
                <!-- 线路场景时，开关状态设置成非开合状态，保证不影响开关朝流判断，仅需要对侧线路信息 -->
                select distinct b.feeder line_id, b.psr_id kg_id, '-' closed_state
                from t_feeder_topo a join t_feeder_topo b on a.psr_id=b.psr_id and a.normal_open=b.normal_open
                where a.feeder = #{queryDTO.objId} and a.normal_open = 'true'
            </otherwise>
        </choose>)
        <include refid="forwardCurrentSql"/>
        union
        <include refid="reverseCurrentSql"/>
    </select>

    <select id="getFilterDirectCurrentSwitchInfo" resultType="java.lang.String">
        with feeder_switch_tab as (
        <foreach collection='filterLineSwitch' item='item' separator=' union all '>
            select #{item.feeder} feeder, #{item.switchIds} switch_id from dual
        </foreach>)
        select t.psr_id from (with recursive res_tab as (
            select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id
            from t_feeder_topo t join feeder_switch_tab f on t.feeder = f.feeder and FIND_IN_SET(t.psr_id,switch_id) > 0
            union all
            select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id
            from t_feeder_topo t join res_tab p on t.start_kg_id = p.psr_id and t.feeder = p.feeder
            where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="(" separator=',' close=')'>#{item}</foreach>)
          select psr_id, name, psr_type, feeder, feeder_name, normal_open, start_kg_id from res_tab) t
    </select>
    <select id="getPlanCreateTimeById" resultType="java.time.LocalDateTime">
        select create_time from fhzg_operation_abnormality_transfer_area_result where oa_id = #{id}
    </select>

    <sql id="forwardCurrentSql">
        <!-- 正向朝流经过的开关信息 -->
        select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id, t.link_id, '0' direct_current
        from (with recursive res_tab as (
            select t.psr_id link_id, t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id
            from t_feeder_topo t join (select distinct line_id
                from switch_res_tab) f on t.feeder = f.line_id where t.is_start_kg = '1'
            union all
            select if(t.psr_id in (select kg_id from switch_res_tab where closed_state='0'),'-',t.psr_id) link_id,
                   t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id
            from t_feeder_topo t join res_tab p on t.start_kg_id = p.link_id and t.feeder = p.feeder
            where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="(" separator=',' close=')'>#{item}</foreach>)
          select psr_id, name, psr_type, feeder, feeder_name, normal_open, start_kg_id, link_id from res_tab
        ) t
    </sql>
    <sql id="reverseCurrentSql">
        <!-- 反向朝流经过的开关信息 -->
        select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id, t.link_id, '1' direct_current
        from (with recursive f_res_tab as (
            select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id, t.start_kg_id link_id
            from t_feeder_topo t join (select distinct line_id, kg_id from switch_res_tab
              where closed_state = '1') f on t.feeder = f.line_id and t.psr_id = f.kg_id and t.normal_open = 'true'
            union all
            select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id,
                if(t.psr_id in (select kg_id from switch_res_tab where closed_state='0'), '-', t.start_kg_id) link_id
            from t_feeder_topo t join f_res_tab p on t.psr_id = p.link_id and t.feeder = p.feeder
            where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="(" separator=',' close=')'>#{item}</foreach>)
          select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id, t.link_id
          from (with recursive z_res_tab as (
              select t.psr_id,t.name,t.psr_type,t.feeder,t.feeder_name,t.normal_open,t.start_kg_id,ifnull(f.link_id,t.psr_id) link_id
              from t_feeder_topo t join (select distinct psr_id, feeder, link_id
                from f_res_tab r) f on t.feeder = f.feeder and t.psr_id = f.psr_id
              union all
              select t.psr_id, t.name, t.psr_type, t.feeder, t.feeder_name, t.normal_open, t.start_kg_id, t.psr_id link_id
              from t_feeder_topo t join z_res_tab p on t.start_kg_id = p.link_id and t.feeder = p.feeder
              where t.psr_type in <foreach collection='kgPsrTypes' item='item' open="(" separator=',' close=')'>#{item}</foreach>)
            select psr_id, name, psr_type, feeder, feeder_name, normal_open, start_kg_id, link_id from z_res_tab) t
        ) t
    </sql>



</mapper>
