<?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="cn.cecep.talroad.mapper.PcAdslFactoryMapper">

    <resultMap type="cn.cecep.talroad.domain.PcAdslFactory" id="PcAdslFactoryResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="adslCode"    column="adsl_code"    />
    </resultMap>


    <sql id="selectPcAdslFactoryVo">
        select id, factory_id, adsl_code from pc_adsl_factory
    </sql>

    <select id="selectPcAdslFactoryList" parameterType="cn.cecep.talroad.domain.PcAdslFactory" resultMap="PcAdslFactoryResult">
        <include refid="selectPcAdslFactoryVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="adslCode != null  and adslCode != ''"> and adsl_code = #{adslCode}</if>
        </where>
    </select>

    <select id="selectEntInDatas" resultType="java.util.Map">
        SELECT
            distinct on (pa.adsl_code)
            pa.adsl_name,
        <!--highlight 不为空则点亮-->
            id highlight
        FROM
            pc_adsl AS pa
                left join pc_adsl_factory AS paf on pa.adsl_code = paf.adsl_code AND paf.factory_id = #{factoryId}
        order by pa.adsl_code, pa.sort asc
    </select>

    <delete id="truncateAll">
        TRUNCATE pc_adsl_factory;
    </delete>

    <insert id="onlineAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A001' adslCode,
            factory_id factoryId
        FROM
            pc_outlet t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            pollutant_type = '4'
            AND t1.is_status = '1'
            AND t1.id IS NOT NULL;
    </insert>

    <insert id="elecAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A002' adsl_code,
            factory_id factoryId
        FROM
            bas_device_site t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            t2."id" IS NOT NULL
            AND t1.is_status = '1';
    </insert>

    <insert id="dcsAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A003' adsl_code,
            t1.factory_id factoryId
        FROM
            bas_device t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            t1.industry IN ( 'JH', 'SN', 'BL', 'FS', 'DL', 'GT' )
            AND t2."id" IS NOT NULL
            AND t1.is_status = '1';
    </insert>

    <insert id="inorgAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A004' adsl_code,
            factory_id factoryId
        FROM
            pc_outlet t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            pollutant_type = '3'
            AND t2."id" IS NOT NULL;
    </insert>

    <insert id="videoAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A005' adsl_code,
            factory_id factoryId
        FROM
            m_am_video_dev_fi t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            t2."id" IS NOT NULL;
    </insert>

    <insert id="dynamicAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A006' adsl_code,
            factory_id factoryId
        FROM
            s_main_dynamic_control t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            t2."id" IS NOT NULL;
    </insert>

    <insert id="doorAdsl">
        INSERT INTO "public"."pc_adsl_factory"("adsl_code", "factory_id")
        SELECT DISTINCT ON
            ( factoryId ) 'A007' adsl_code,
            factory_id factoryId
        FROM
            pc_factory_access_control t1
            LEFT JOIN pc_factory t2 ON t1.factory_id = t2."id"
        WHERE
            t2."id" IS NOT NULL;
    </insert>

</mapper>
