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


    <select id="getOrganSuffixByFaultIdOrLineId" resultType="java.lang.String">
        select tab_suffix
        from(
            select c.*
            from fhzg_fault_power_cut a
            left join dwd_shb_ds_feeder_base b on a.line_id = b.psr_id
            left join fhzg_dist_organ_table_suffix_config c on b.city = c.org_id
            where 1 = 1
            <if test="id != null and id != ''">
                AND a.id = #{id}
            </if>
            <if test="lineId != null and lineId != ''">
                AND a.line_id = #{lineId}
            </if>
        ) t limit 1
    </select>

    <select id="getOrganSuffixByPlanIdOrLineId" resultType="java.lang.String">
        select tab_suffix
        from(
            select c.*
            from fhzg_plan_power_cut a
            left join dwd_shb_ds_feeder_base b on a.line_id = b.psr_id
            left join fhzg_dist_organ_table_suffix_config c on b.city = c.org_id
            where 1 = 1
            <if test="id != null and id != ''">
                AND a.id = #{id}
            </if>
            <if test="lineId != null and lineId != ''">
                AND a.line_id = #{lineId}
            </if>
        ) t limit 1
    </select>

    <select id="getOrganSuffixPlanByCustomerId" resultType="java.lang.String">
        select tab_suffix
        from(
            select d.*
            from fhzg_plan_power_cut a
            left join fhzg_plan_power_cut_pv_users b on b.plan_id = a.id
            left join fhzg_dist_pv_customer c on b.cust_id = c.cust_id
            left join fhzg_dist_organ_table_suffix_config d on d.org_id = a.city_code
            where  c.cust_id = #{customerId}
        ) t limit 1
    </select>

    <select id="getOrganSuffixFaultByCustomerId" resultType="java.lang.String">
        select tab_suffix
        from(
            select d.*
            from fhzg_fault_power_cut a
            left join fhzg_fault_power_cut_pv_users b on b.fault_id = a.id
            left join fhzg_dist_pv_customer c on b.cust_id = c.cust_id
            left join fhzg_dist_organ_table_suffix_config d on d.org_id = a.city_code
            where  c.cust_id = #{customerId}
        ) t limit 1
    </select>

    <select id="getOrganSuffixFaultBySchemeId" resultType="java.lang.String">
        select tab_suffix
        from(
            select c.*
            from fhzg_fault_power_cut a
            left join fhzg_fault_transfer_scheme b on b.fault_id = a.id
            left join fhzg_dist_organ_table_suffix_config c on c.org_id = a.city_code
            where b.id = #{schemeId}
        ) t limit 1
    </select>

    <select id="getOrganSuffixByPlanSchemeId" resultType="java.lang.String">
        select tab_suffix
        from(
             select c.*
             from fhzg_plan_power_cut a
             left join fhzg_plan_transfer_scheme b on b.plan_id = a.id
             left join fhzg_dist_organ_table_suffix_config c on c.org_id = a.city_code
             where b.id = #{schemeId}
            ) t limit 1
    </select>

    <select id="getOrganSuffixByPsrIdOrFeeder" resultType="java.lang.String">
        select tab_suffix
        from(
             select b.*
             from dwd_shb_ds_switch_base a
             inner join fhzg_dist_organ_table_suffix_config b on a.city = b.org_id
             where 1 = 1
        <if test="psrId != null and psrId != ''">
            AND psr_id = #{psrId}
        </if>
        <if test="feeder != null and feeder != ''">
            AND feeder = #{feeder}
        </if>
        ) t limit 1
    </select>

    <select id="getOrganSuffixList" resultType="java.util.HashMap">
        select b.tab_suffix, a.name
        from dwd_shb_ds_switch_base a
        inner join fhzg_dist_organ_table_suffix_config b on a.city = b.org_id
        where a.name in
        <foreach collection="nameList" open="(" item="name" separator="," close=")">
            #{name}
        </foreach>

    </select>
    <select id="getOrganSuffixByMaintOrg" resultType="java.lang.String">
        select tab_suffix
        from(
                select  a.* from fhzg_dist_organ_table_suffix_config a left join
                                 SYS_STRU b on a.org_id = b.PARENT_ID
                where b.ISC_ORG_ID = #{maintOrg}
            ) t limit 1
    </select>

    <select id="getOrganSuffixByStationId" resultType="java.lang.String">
        select tab_suffix
        from(
                select  a.* from fhzg_dist_organ_table_suffix_config a left join
                                 dwd_shb_ds_feeder_base b on a.org_id = b.city
                where b.start_station = #{stationId}
            ) t limit 1
    </select>

</mapper>
