<?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.core.sys.dao.SwitchProtectionSettingsMapper">

    <update id="updateParentLinePsrId">
        update fhzg_switch_protection_settings t join dwd_shb_ds_feeder_base b
        on b.name  like CONCAT('%',t.parent_line_name,'%')
        set t.parent_line_psr_id = b.psr_id
        where t.parent_line_psr_id is null  and t.parent_line_name in
        <foreach collection="nameList" open="(" item="name" separator="," close=")">
            #{name}
        </foreach>
    </update>

    <update id="updatedDevicePrsId">
        update fhzg_switch_protection_settings t join t_feeder_topo b
        on b.name like CONCAT('%',t.device_name,'%') and t.parent_line_psr_id = b.feeder
            set t.device_prs_id = b.psr_id
        where b.psr_type in
        <foreach collection="kgPsrTypes" open="(" item="psrType" separator="," close=")">
            #{psrType}
        </foreach>
          and t.device_prs_id is null and  t.device_name in
        <foreach collection="nameList" open="(" item="name" separator="," close=")">
            #{name}
        </foreach>
    </update>

    <update id="updateBatchByPrsId" parameterType="com.hdkj.lt.core.bizms.modle.po.FhzgSwitchProtectionSettings">
        update fhzg_switch_protection_settings
        set oc1_value = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc1Value}
        </foreach>
        end,
        oc1_delay = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc1Delay}
        </foreach>
        end,
        oc1_setting = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc1Setting}
        </foreach>
        end,
        oc2_value = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc2Value}
        </foreach>
        end,
        oc2_delay = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc2Delay}
        </foreach>
        end,
        oc2_setting = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc2Setting}
        </foreach>
        end,
        oc3_value = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc3Value}
        </foreach>
        end,
        oc3_delay = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc3Delay}
        </foreach>
        end,
        oc3_setting = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.oc3Setting}
        </foreach>
        end,
        prot_enable_status = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.protEnableStatus}
        </foreach>
        end,
        reclose_enable_status = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.recloseEnableStatus}
        </foreach>
        end,
        hazard_type = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.hazardType}
        </foreach>
        end,
        hazard_desc = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.hazardDesc}
        </foreach>
        end,
        last_time = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.lastTime}
        </foreach>
        end,
        last_editor = case
        <foreach collection="list" item="item" separator="">
            when device_prs_id = #{item.devicePrsId} then #{item.lastEditor}
        </foreach>
        end
        where device_prs_id in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item.devicePrsId}
        </foreach>
    </update>

    <delete id="deleteRepeated">
        WITH temp AS ( SELECT id, device_prs_id,ROW_NUMBER() over ( PARTITION BY device_prs_id ORDER BY id asc ) AS rn FROM fhzg_switch_protection_settings WHERE device_prs_id IS NOT NULL )
        DELETE
        FROM
            fhzg_switch_protection_settings
        WHERE
            id IN ( SELECT id FROM temp  WHERE rn > 1 )
    </delete>

</mapper>
