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


    <select id="selectSchemeList" resultType="com.hdkj.lt.bf.entity.vo.FhzgFaultPowerCutVO">
        <include refid="schemeListSql"/>
    </select>

    <sql id="schemeListSql">
        select t.id,
        t.fault_no fault_id,
        t.fault_start_time,
        t.fault_end_time,
        t.fault_type,
        t.fault_source,
        t.line_id,
        t.line_name,
        t.fault_description,
        t.strategy_id,
        t.feeder_name,
        t.city_code,
        t.city_name,
        t.scheme_create_time,
        t.scheme_create_duration,
        t.scheme_state,
        t.power_on_success,
        t.creator,
        t.create_time,
        t.last_editor,
        t.last_time,
        t.county_code,
        t.county_name,
        b.outage_number faultNo,
        ROW_NUMBER() over(partition by t.fault_no ORDER BY t.scheme_create_time asc) as rn
        from fhzg_fault_power_cut t
        left join grid_pd_outage_event_base b on t.fault_no = b.id
        <where>
            <if test="params.schemeState != null  and params.schemeState != ''">
                AND t.scheme_state = #{params.schemeState}
            </if>
            <if test="params.faultType != null ">
                AND t.fault_type = #{params.faultType}
            </if>
            <if test="params.countyCode != null and params.countyCode != ''">
                AND t.county_code = #{params.countyCode}
            </if>
            <if test="params.countyCodeList != null and params.countyCodeList.size > 0">
                AND t.county_code IN
                <foreach collection="params.countyCodeList" open="(" item="item" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="params.lineName != null and params.lineName != '' ">
                AND t.line_name like concat('%' ,#{params.lineName}, '%')
            </if>
            <if test="params.faultStartTime != null ">
                AND t.fault_start_time &gt; #{params.faultStartTime}
            </if>
            <if test="params.faultEndTime != null">
                AND t.fault_start_time &lt; #{params.faultEndTime}
            </if>
            <if test="params.faultNo != null and params.faultNo != ''">
                AND b.outage_number like concat('%' ,#{params.faultNo}, '%')
            </if>
        </where>
        ORDER BY t.fault_start_time DESC ,t.id desc
    </sql>
    <select id="selectSchemeList2" resultType="com.hdkj.lt.bf.entity.vo.FhzgFaultPowerCutVO">
        select t.id,
        t.fault_id,
        t.fault_start_time,
        t.fault_end_time,
        t.fault_type,
        t.fault_source,
        t.line_id,
        t.line_name,
        t.fault_description,
        t.strategy_id,
        t.feeder_name,
        t.city_code,
        t.city_name,
        t.scheme_create_time,
        t.scheme_create_duration,
        t.scheme_state,
        t.power_on_success,
        t.creator,
        t.create_time,
        t.last_editor,
        t.last_time,
        t.county_code,
        t.county_name,
        t.faultNo
        from (<include refid="schemeListSql"/>) t
        <where>
            t.rn =1
        </where>

    </select>
    <select id="getPowerCutByFaultNo" resultType="com.hdkj.lt.core.bizms.modle.po.fault.FaultPowerCut">
        select *
        from fhzg_fault_power_cut pc
                 left join grid_pd_outage_event_base oe on pc.fault_no = oe.id
        where pc.fault_source = '供指' and oe.outage_number = #{faultNo}
    </select>
</mapper>
