<?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.ruoyi.powerdistribution.mapper.PdmMaintenanceCompletionStatisticsMapper">
    
    <resultMap type="PdmMaintenanceCompletionStatistics" id="PdmMaintenanceCompletionStatisticsResult">
        <result property="eventNo"    column="event_no"    />
        <result property="electLineName"    column="elect_line_name"    />
        <result property="branchName"    column="branch_name"    />
        <result property="dataStatus"    column="data_status"    />
        <result property="pushStatus"    column="push_status"    />
        <result property="isMaintainable"    column="is_maintainable"    />
        <result property="isPureSpecia"    column="is_pure_specia"    />
        <result property="eventType"    column="event_type"    />
        <result property="powerOutageStart"    column="power_outage_start"    />
        <result property="powerOutageEnd"    column="power_outage_end"    />
        <result property="powerOutageDuringHouseholds"    column="power_outage_during_households"    />
        <result property="powerOutageNature"    column="power_outage_nature"    />
        <result property="city"    column="city"    />
        <result property="county"    column="county"    />
        <result property="station"    column="station"    />
        <result property="oranNo"    column="oran_no"    />
        <result property="substationName"    column="substation_name"    />
        <result property="powerOutageHouseholds"    column="power_outage_households"    />
        <result property="powerOutageLine"    column="power_outage_line"    />
        <result property="faultFreeDuration"    column="fault_free_duration"    />
        <result property="rewardAmount"    column="reward_amount"    />
    </resultMap>

    <sql id="selectPdmMaintenanceCompletionStatisticsVo">
        select event_no, elect_line_name, branch_name, data_status, push_status, is_maintainable, is_pure_specia, event_type, power_outage_start, power_outage_end, power_outage_during_households, power_outage_nature, city, county, station, oran_no, substation_name, power_outage_households, power_outage_line, fault_free_duration, reward_amount from pdm_maintenance_completion_statistics a
    </sql>

    <select id="selectPdmMaintenanceCompletionStatisticsList" parameterType="PdmMaintenanceCompletionStatistics" resultMap="PdmMaintenanceCompletionStatisticsResult">
        <include refid="selectPdmMaintenanceCompletionStatisticsVo"/>
        <where>  
            <if test="eventNo != null  and eventNo != ''"> and event_no = #{eventNo}</if>
            <if test="electLineName != null  and electLineName != ''"> and elect_line_name like concat('%', #{electLineName}, '%')</if>
            <if test="branchName != null  and branchName != ''"> and branch_name like concat('%', #{branchName}, '%')</if>
            <if test="dataStatus != null  and dataStatus != ''"> and data_status = #{dataStatus}</if>
            <if test="pushStatus != null  and pushStatus != ''"> and push_status = #{pushStatus}</if>
            <if test="isMaintainable != null  and isMaintainable != ''"> and is_maintainable = #{isMaintainable}</if>
            <if test="isPureSpecia != null  and isPureSpecia != ''"> and is_pure_specia = #{isPureSpecia}</if>
            <if test="eventType != null  and eventType != ''"> and event_type = #{eventType}</if>
            <if test="powerOutageStart != null "> and power_outage_start = #{powerOutageStart}</if>
            <if test="powerOutageEnd != null "> and power_outage_end = #{powerOutageEnd}</if>
            <if test="powerOutageDuringHouseholds != null "> and power_outage_during_households = #{powerOutageDuringHouseholds}</if>
            <if test="powerOutageNature != null  and powerOutageNature != ''"> and power_outage_nature = #{powerOutageNature}</if>
            <if test="city != null  and city != ''"> and city = #{city}</if>
            <if test="county != null  and county != ''"> and county = #{county}</if>
            <if test="station != null  and station != ''"> and station = #{station}</if>
            <if test="oranNo != null  and oranNo != ''"> and oran_no = #{oranNo}</if>
            <if test="substationName != null  and substationName != ''"> and substation_name like concat('%', #{substationName}, '%')</if>
            <if test="powerOutageHouseholds != null "> and power_outage_households = #{powerOutageHouseholds}</if>
            <if test="powerOutageLine != null "> and power_outage_line = #{powerOutageLine}</if>
            <if test="faultFreeDuration != null  and faultFreeDuration != ''"> and fault_free_duration = #{faultFreeDuration}</if>
            <if test="rewardAmount != null "> and reward_amount = #{rewardAmount}</if>
            <if test="maintenanceOverdue != null and maintenanceOverdue!=''">
                <if test="maintenanceOverdueUnit == 'month'">
                  and  TIMESTAMPDIFF(MONTH,create_time, SYSDATE()) &gt; ${maintenanceOverdueVal}
                  and  not exists(select 1 from pdm_maintenance_completion_statistics c where TIMESTAMPDIFF(MONTH,c.create_time, SYSDATE()) &gt; ${maintenanceOverdueVal} and c.elect_line_name=a.elect_line_name
                  and  c.branch_name=a.branch_name)
                </if>
                <if test="maintenanceOverdueUnit == 'year'">
                    and  TIMESTAMPDIFF(YEAR,create_time, SYSDATE()) &gt; ${maintenanceOverdueVal}
                    and  not exists(select 1 from pdm_maintenance_completion_statistics c where TIMESTAMPDIFF(YEAR,c.create_time, SYSDATE()) &gt; ${maintenanceOverdueVal} and c.elect_line_name=a.elect_line_name
                    and  c.branch_name=a.branch_name)
                </if>
             </if>
            <if test="repeatPowerCut != null and repeatPowerCut!=''">
                <if test="repeatPowerCutUnit == '次'">
                    and  (select count(1) from pdm_maintenance_completion_statistics c where  c.elect_line_name=a.elect_line_name
                    and  c.branch_name=a.branch_name) &gt; ${repeatPowerCutVal}
                </if>
            </if>
        </where>
    </select>
    
    <select id="selectPdmMaintenanceCompletionStatisticsByEventNo" parameterType="String" resultMap="PdmMaintenanceCompletionStatisticsResult">
        <include refid="selectPdmMaintenanceCompletionStatisticsVo"/>
        where event_no = #{eventNo}
    </select>
        
    <insert id="insertPdmMaintenanceCompletionStatistics" parameterType="PdmMaintenanceCompletionStatistics">
        insert into pdm_maintenance_completion_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="eventNo != null and eventNo != ''">event_no,</if>
            <if test="electLineName != null">elect_line_name,</if>
            <if test="branchName != null">branch_name,</if>
            <if test="dataStatus != null">data_status,</if>
            <if test="pushStatus != null">push_status,</if>
            <if test="isMaintainable != null">is_maintainable,</if>
            <if test="isPureSpecia != null">is_pure_specia,</if>
            <if test="eventType != null">event_type,</if>
            <if test="powerOutageStart != null">power_outage_start,</if>
            <if test="powerOutageEnd != null">power_outage_end,</if>
            <if test="powerOutageDuringHouseholds != null">power_outage_during_households,</if>
            <if test="powerOutageNature != null">power_outage_nature,</if>
            <if test="city != null">city,</if>
            <if test="county != null">county,</if>
            <if test="station != null">station,</if>
            <if test="oranNo != null">oran_no,</if>
            <if test="substationName != null">substation_name,</if>
            <if test="powerOutageHouseholds != null">power_outage_households,</if>
            <if test="powerOutageLine != null">power_outage_line,</if>
            <if test="faultFreeDuration != null">fault_free_duration,</if>
            <if test="rewardAmount != null">reward_amount,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="eventNo != null and eventNo != ''">#{eventNo},</if>
            <if test="electLineName != null">#{electLineName},</if>
            <if test="branchName != null">#{branchName},</if>
            <if test="dataStatus != null">#{dataStatus},</if>
            <if test="pushStatus != null">#{pushStatus},</if>
            <if test="isMaintainable != null">#{isMaintainable},</if>
            <if test="isPureSpecia != null">#{isPureSpecia},</if>
            <if test="eventType != null">#{eventType},</if>
            <if test="powerOutageStart != null">#{powerOutageStart},</if>
            <if test="powerOutageEnd != null">#{powerOutageEnd},</if>
            <if test="powerOutageDuringHouseholds != null">#{powerOutageDuringHouseholds},</if>
            <if test="powerOutageNature != null">#{powerOutageNature},</if>
            <if test="city != null">#{city},</if>
            <if test="county != null">#{county},</if>
            <if test="station != null">#{station},</if>
            <if test="oranNo != null">#{oranNo},</if>
            <if test="substationName != null">#{substationName},</if>
            <if test="powerOutageHouseholds != null">#{powerOutageHouseholds},</if>
            <if test="powerOutageLine != null">#{powerOutageLine},</if>
            <if test="faultFreeDuration != null">#{faultFreeDuration},</if>
            <if test="rewardAmount != null">#{rewardAmount},</if>
         </trim>
    </insert>

    <update id="updatePdmMaintenanceCompletionStatistics" parameterType="PdmMaintenanceCompletionStatistics">
        update pdm_maintenance_completion_statistics
        <trim prefix="SET" suffixOverrides=",">
            <if test="electLineName != null">elect_line_name = #{electLineName},</if>
            <if test="branchName != null">branch_name = #{branchName},</if>
            <if test="dataStatus != null">data_status = #{dataStatus},</if>
            <if test="pushStatus != null">push_status = #{pushStatus},</if>
            <if test="isMaintainable != null">is_maintainable = #{isMaintainable},</if>
            <if test="isPureSpecia != null">is_pure_specia = #{isPureSpecia},</if>
            <if test="eventType != null">event_type = #{eventType},</if>
            <if test="powerOutageStart != null">power_outage_start = #{powerOutageStart},</if>
            <if test="powerOutageEnd != null">power_outage_end = #{powerOutageEnd},</if>
            <if test="powerOutageDuringHouseholds != null">power_outage_during_households = #{powerOutageDuringHouseholds},</if>
            <if test="powerOutageNature != null">power_outage_nature = #{powerOutageNature},</if>
            <if test="city != null">city = #{city},</if>
            <if test="county != null">county = #{county},</if>
            <if test="station != null">station = #{station},</if>
            <if test="oranNo != null">oran_no = #{oranNo},</if>
            <if test="substationName != null">substation_name = #{substationName},</if>
            <if test="powerOutageHouseholds != null">power_outage_households = #{powerOutageHouseholds},</if>
            <if test="powerOutageLine != null">power_outage_line = #{powerOutageLine},</if>
            <if test="faultFreeDuration != null">fault_free_duration = #{faultFreeDuration},</if>
            <if test="rewardAmount != null">reward_amount = #{rewardAmount},</if>
        </trim>
        where event_no = #{eventNo}
    </update>

    <delete id="deletePdmMaintenanceCompletionStatisticsByEventNo" parameterType="String">
        delete from pdm_maintenance_completion_statistics where event_no = #{eventNo}
    </delete>

    <delete id="deletePdmMaintenanceCompletionStatisticsByEventNos" parameterType="String">
        delete from pdm_maintenance_completion_statistics where event_no in 
        <foreach item="eventNo" collection="array" open="(" separator="," close=")">
            #{eventNo}
        </foreach>
    </delete>
</mapper>