<?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.job.mapper.DealPsrDeviceMapper">

    <insert id="saveDistJoinEcTransformer">
        replace into dwd_shb_ds_joinec_transformer_base(psr_id, ast_Id, cons_no, `name`, psr_Type, join_ec, feeder,
                city, maint_org, maint_group, psr_state, pub_priv_flag, start_time, use_nature, voltage_level)
        values
        <foreach collection="saveList" item="item" index="index" separator=",">
            (#{item.psrId}, #{item.astId}, #{item.consNo}, #{item.name}, #{item.psrType}, #{item.joinEc}, #{item.feeder}, #{item.city},
            #{item.maintOrg}, #{item.maintGroup}, #{item.psrState}, #{item.pubPrivFlag}, #{item.startTime}, #{item.useNature}, #{item.voltageLevel})
        </foreach>
    </insert>
    <insert id="saveDistDeviceJoinEc">
        replace into dwd_shb_ds_joinec_base(psr_id, `name`, branch_feeder, branch_feeder_name, feeder, feeder_name, feeder_segment, voltage_level, voltage_level_name, line,
                line_name, installed_capacity, is_has_dg, join_dev_id, join_dev_type, psr_state, psr_state_name, pub_priv_flag, pub_priv_flag_name, reliable_segment, start_time,
                stop_time, user_id, city, city_name, maint_org, maint_org_name, maint_group, maint_group_name, equipment_owner, equipment_owner_name, ctime, last_update_time, del_flag)
        values
        <foreach collection="joinEcList" item="item" index="index" separator=",">
            (#{item.psrId}, #{item.name}, #{item.branchFeeder}, #{item.branchFeederName}, #{item.feeder}, #{item.feederName}, #{item.feederSegment}, #{item.voltageLevel},
            #{item.voltageLevelName}, #{item.line}, #{item.lineName}, #{item.installedCapacity}, #{item.isHasDg}, #{item.joinDevId}, #{item.joinDevType}, #{item.psrState},
            #{item.psrStateName}, #{item.pubPrivFlag}, #{item.pubPrivFlagName}, #{item.reliableSegment}, #{item.startTime}, #{item.stopTime}, #{item.userId}, #{item.city}, #{item.cityName},
            #{item.maintOrg}, #{item.maintOrgName}, #{item.maintGroup}, #{item.maintGroupName}, #{item.equipmentOwner}, #{item.equipmentOwnerName}, #{item.ctime}, #{item.lastUpdateTime}, '0')
        </foreach>
    </insert>

    <insert id="savePsrDeviceDIBeforeBak">
        <!-- 先删后增DI同步之前的线路数据 (馈线、档距段设备)-->
        truncate table dwd_shb_ds_feeder_base_di_before;
        insert into dwd_shb_ds_feeder_base_di_before
        select t.* from dwd_shb_ds_feeder_base t;

        truncate table dwd_shb_ds_span_segment_base_di_before;
        insert into dwd_shb_ds_span_segment_base_di_before
        select t.* from dwd_shb_ds_span_segment_base t;
    </insert>
    <insert id="updateFeederPsrDevice">
        truncate table T_PSR_DS_FEEDER;
        insert into T_PSR_DS_FEEDER (AST_ID,CABLE_LENGTH,CABLE_METHOD,CABLE_METHOD_NAME,CITY,CITY_NAME,
            CONS_NO,CTIME,DISPATCH_AREA,DISPATCH_ORG,EQUIPMENT_OWNER,EQUIPMENT_OWNER_NAME,ERECTION_METHOD,
            ERECTION_METHOD_NAME,FEEDER_RATE_CAPACITY,GRID_CODE,IMPORTANCE,IMPORTANCE_NAME,IS_RURAL,
            IS_RURAL_NAME,LAST_UPDATE_TIME,`LENGTH`,MAINT_GROUP,MAINT_GROUP_NAME,MAINT_ORG,MAINT_ORG_NAME,
            `NAME`,OVERHEAD_LENGTH,OVERHEAD_METHOD,OVERHEAD_METHOD_NAME,PSR_ID,PSR_STATE,PSR_STATE_NAME,
            PUB_PRIV_FLAG,PUB_PRIV_FLAG_NAME,REGIONALISM,REGIONALISM_NAME,RELIABLE_DEV_NAME,RELIABLE_DEV_NAME_NAME,
            RUN_DEV_NAME,START_STATION,START_STATION_NAME,START_SWITCH,START_SWITCH_TYPE,START_TIME,STOP_TIME,
            SUPPLY_AREA,SUPPLY_AREA_NAME,SUPPLY_RADIUS,VOLTAGE_LEVEL,VOLTAGE_LEVEL_NAME,CREATE_TIME,UPDATE_TIME)
        select AST_ID,CABLE_LENGTH,CABLE_METHOD,CABLE_METHOD_NAME,CITY,CITY_NAME,CONS_NO,CTIME,
               DISPATCH_LEVEL_NAME DISPATCH_AREA,DISPATCH_ORG,EQUIPMENT_OWNER,EQUIPMENT_OWNER_NAME,
               ERECTION_METHOD,ERECTION_METHOD_NAME,FEEDER_RATE_CAPACITY,null GRID_CODE,IMPORTANCE,
               IMPORTANCE_NAME,IS_RURAL,IS_RURAL_NAME,LAST_UPDATE_TIME,`LENGTH`,MAINT_GROUP,MAINT_GROUP_NAME,
               MAINT_ORG,MAINT_ORG_NAME,`NAME`,OVERHEAD_LENGTH,OVERHEAD_METHOD,OVERHEAD_METHOD_NAME,PSR_ID,
               PSR_STATE,PSR_STATE_NAME,PUB_PRIV_FLAG,PUB_PRIV_FLAG_NAME,REGIONALISM,REGIONALISM_NAME,
               null RELIABLE_DEV_NAME,null RELIABLE_DEV_NAME_NAME,RUN_DEV_NAME,START_STATION,START_STATION_NAME,
               START_SWITCH,START_SWITCH_TYPE,START_TIME,null STOP_TIME,SUPPLY_AREA,SUPPLY_AREA_NAME,
               SUPPLY_RADIUS,VOLTAGE_LEVEL,VOLTAGE_LEVEL_NAME,null CREATE_TIME,UPDATE_TIME
        from dwd_shb_ds_feeder_base t;
    </insert>
    <update id="updateFeederDIAfter">
        update dwd_shb_ds_feeder_base x, dwd_shb_ds_feeder_base_di_before y
        set x.voltage_level_name = y.voltage_level_name,x.cable_method_name = y.cable_method_name,
            x.psr_state_name = y.psr_state_name,x.pub_priv_flag_name = y.pub_priv_flag_name,
            x.start_station_name = y.start_station_name,x.start_switch_name = y.start_switch_name,
            x.start_time = y.start_time,x.supply_area_name = y.supply_area_name,
            x.regionalism_name = y.regionalism_name,x.dispatch_org_name = y.dispatch_org_name,
            x.dispatch_level_name = y.dispatch_level_name,x.equipment_owner_name = y.equipment_owner_name,
            x.erection_method_name = y.erection_method_name,x.feeder_rate_capacity = y.feeder_rate_capacity,
            x.importance_name = y.importance_name,x.is_da_feeder = y.is_da_feeder,
            x.is_rural_name = y.is_rural_name,x.city_name = y.city_name,x.maint_org_name = y.maint_org_name,
            x.maint_group_name = y.maint_group_name,x.normal_energizing_busbar = y.normal_energizing_busbar,
            x.overhead_method_name = y.overhead_method_name,x.ast_nature_name = y.ast_nature_name,
            x.deploy_state_name = y.deploy_state_name,x.is_commission_name = y.is_commission_name,
            x.source_name = y.source_name,x.del_flag=y.del_flag where x.psr_id=y.psr_id and x.ast_id=y.ast_id
    </update>
    <update id="updateSpanSegmentDIAfter">
        update dwd_shb_ds_span_segment_base x, dwd_shb_ds_span_segment_base_di_before y
        set x.feeder_name = y.feeder_name,x.branch_feeder_name = y.branch_feeder_name,x.line_name = y.line_name,
            x.voltage_level_name = y.voltage_level_name,x.ast_nature_name = y.ast_nature_name,
            x.manufacturer_name = y.manufacturer_name,x.start_pole_name = y.start_pole_name,
            x.stop_pole_name = y.stop_pole_name,x.supply_area_name = y.supply_area_name,
            x.wire_arrangement_name = y.wire_arrangement_name,x.wire_type_name = y.wire_type_name,
            x.city_name = y.city_name,x.maint_org_name = y.maint_org_name,x.maint_group_name = y.maint_group_name,
            x.equipment_owner_name = y.equipment_owner_name, x.del_flag = y.del_flag
        where x.psr_id = y.psr_id and x.ast_id = y.ast_id
    </update>

    <insert id="saveDistConductorSegment">
        replace into dwd_shb_ds_conductor_segment_base (psr_id, ast_id, `name`, feeder, feeder_name, branch_feeder, branch_feeder_name,
            line, line_name, voltage_level, voltage_level_name, ast_nature, ast_nature_name, equip_code, is_commission, is_rural, `length`,
            manufacturer, manufacturer_name, model, project_name, project_num, rated_ampacity, reference_length, start_pole, start_pole_name,
            stop_pole, stop_pole_name, supply_area, supply_area_name, wire_arrangement, wire_arrangement_name, wire_cross_section,
            wire_type, wire_type_name, city, city_name, maint_org, maint_org_name, maint_group, maint_group_name, equipment_owner,
            equipment_owner_name, psr_state, deploy_state, start_time, stop_time, pub_priv_flag, ctime, last_update_time, del_flag)
        values
        <foreach collection="deviceList" item="item" index="index" separator=",">
            (#{item.psrId}, #{item.astId}, #{item.name}, #{item.feeder}, #{item.feederName}, #{item.branchFeeder},
            #{item.branchFeederName}, #{item.line}, #{item.lineName}, #{item.voltageLevel}, #{item.voltageLevelName},
            #{item.astNature}, #{item.astNatureName}, #{item.equipCode}, #{item.isCommission}, #{item.isRural}, #{item.length},
            #{item.manufacturer}, #{item.manufacturerName}, #{item.model}, #{item.projectName}, #{item.projectNum}, #{item.ratedAmpacity},
            #{item.referenceLength}, #{item.startPole}, #{item.startPoleName}, #{item.stopPole}, #{item.stopPoleName}, #{item.supplyArea},
            #{item.supplyAreaName}, #{item.wireArrangement}, #{item.wireArrangementName}, #{item.wireCrossSection}, #{item.wireType},
            #{item.wireTypeName}, #{item.city}, #{item.cityName}, #{item.maintOrg}, #{item.maintOrgName}, #{item.maintGroup},
            #{item.maintGroupName}, #{item.equipmentOwner}, #{item.equipmentOwnerName}, #{item.psrState}, #{item.deployState},
            #{item.startTime}, #{item.stopTime}, #{item.pubPrivFlag}, #{item.ctime}, #{item.lastUpdateTime}, '0')
        </foreach>
    </insert>
    <insert id="saveDistCableSegment">
        replace into dwd_shb_ds_cable_segment_base (psr_id, ast_id, `name`, feeder, feeder_name, branch_feeder, branch_feeder_name,
            feeder_segment, line, line_name, voltage_level, voltage_level_name, ast_nature, ast_nature_name, ast_org, ast_org_name,
            cable, cable_name, core_material, core_material_name, core_quantity, core_quantity_name, cross_sectional_area, end_position,
            end_position_name, end_type, equip_code, importance, importance_name,insulation_type,insulation_type_name,is_commission,is_rural,
            lay_method, lay_method_name, `length`, manufacture_date, manufacturer, manufacturer_name, model, project_name, project_num,
            rated_ampacity, rated_voltage, reference_length, regionalism, regionalism_name, start_position,start_position_name,start_type,
            supply_area, supply_area_name, city, city_name, maint_org, maint_org_name, maint_group, maint_group_name, equipment_owner,
            equipment_owner_name, psr_state, deploy_state, start_time, stop_time, pub_priv_flag, ctime, last_update_time, del_flag)
        values
        <foreach collection="deviceList" item="item" index="index" separator=",">
            (#{item.psrId}, #{item.astId}, #{item.name}, #{item.feeder}, #{item.feederName}, #{item.branchFeeder}, #{item.branchFeederName},
            #{item.feederSegment}, #{item.line}, #{item.lineName}, #{item.voltageLevel}, #{item.voltageLevelName}, #{item.astNature},
            #{item.astNatureName}, #{item.astOrg}, #{item.astOrgName}, #{item.cable}, #{item.cableName}, #{item.coreMaterial},
            #{item.coreMaterialName}, #{item.coreQuantity}, #{item.coreQuantityName}, #{item.crossSectionalArea}, #{item.endPosition},
            #{item.endPositionName}, #{item.endType}, #{item.equipCode}, #{item.importance}, #{item.importanceName}, #{item.insulationType},
            #{item.insulationTypeName}, #{item.isCommission}, #{item.isRural}, #{item.layMethod}, #{item.layMethodName}, #{item.length},
            #{item.manufactureDate}, #{item.manufacturer}, #{item.manufacturerName}, #{item.model}, #{item.projectName}, #{item.projectNum},
            #{item.ratedAmpacity}, #{item.ratedVoltage}, #{item.referenceLength}, #{item.regionalism}, #{item.regionalismName}, #{item.startPosition},
            #{item.startPositionName}, #{item.startType}, #{item.supplyArea}, #{item.supplyAreaName}, #{item.city}, #{item.cityName}, #{item.maintOrg},
            #{item.maintOrgName}, #{item.maintGroup}, #{item.maintGroupName}, #{item.equipmentOwner}, #{item.equipmentOwnerName}, #{item.psrState},
            #{item.deployState}, #{item.startTime}, #{item.stopTime}, #{item.pubPrivFlag}, #{item.ctime}, #{item.lastUpdateTime}, '0')
        </foreach>
    </insert>
    <insert id="updateSegmentPsrDevice">
        truncate table T_PSR_DS_DX;
        insert into T_PSR_DS_DX
        select t.* from (
            select ast_id, branch_feeder, branch_feeder_name, city, city_name,
                   ctime, equipment_owner, equipment_owner_name, feeder, feeder_name,
                   is_rural, null, last_update_time, length, line, line_name,
                   maint_group, maint_group_name, maint_org, maint_org_name, name,
                   psr_id, psr_state, null, pub_priv_flag, reference_length,
                   start_pole, start_time, stop_pole, stop_time, supply_area, supply_area_name,
                   voltage_level, voltage_level_name, wire , wire_arrangement, wire_arrangement_name
            from dwd_shb_ds_span_segment_base
            union all
            select ast_id, branch_feeder, branch_feeder_name, city, city_name,
                   ctime, equipment_owner, equipment_owner_name, feeder, feeder_name,
                   is_rural, null, last_update_time, length, line, line_name,
                   maint_group, maint_group_name, maint_org, maint_org_name, name,
                   psr_id, psr_state, null, pub_priv_flag, reference_length,
                   start_pole, start_time, stop_pole, stop_time, supply_area, supply_area_name,
                   voltage_level, voltage_level_name, null , wire_arrangement, wire_arrangement_name
            from dwd_shb_ds_conductor_segment_base
            union all
            select ast_id, branch_feeder, branch_feeder_name, city, city_name,
                   ctime, equipment_owner, equipment_owner_name, feeder, feeder_name,
                   is_rural, null, last_update_time, length, line, line_name,
                   maint_group, maint_group_name, maint_org, maint_org_name, name,
                   psr_id, psr_state, null, pub_priv_flag, reference_length,
                   null, start_time, null, stop_time, supply_area, supply_area_name,
                   voltage_level, voltage_level_name, null , null, null
            from dwd_shb_ds_cable_segment_base) t;
    </insert>

    <select id="getDistJoinEcBaseInfo" resultType="java.lang.String">
        select t.psr_id from dwd_shb_ds_joinec_base t where t.psr_state = '20'
    </select>

    <select id="getConfigCityInfo" resultType="java.lang.String">
        select org_id from fhzg_dist_organ_table_suffix_config
        where id &lt; 29 and tab_suffix in
        <foreach collection="cityShorts" open="(" item="tabSuffix" separator="," close=")">
            #{tabSuffix}
        </foreach>
    </select>

</mapper>
