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

    <resultMap type="cn.cecep.talroad.domain.PcLabelFactory" id="PcLabelFactoryResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="labelId"    column="label_id"    />
        <result property="lableUseing"    column="lable_useing"    />
        <result property="labelValue"    column="label_value"    />
    </resultMap>


    <sql id="selectPcLabelFactoryVo">
        select id, factory_id, label_id,lable_useing from pc_label_factory
    </sql>
    <insert id="batchSave">
        INSERT INTO public.pc_label_factory (id,factory_id,label_id,lable_useing,label_value) VALUES
        <foreach collection="addList" separator="," item="item">
            (#{item.id}, #{item.factoryId}, #{item.labelId}, #{item.lableUseing}, #{item.labelValue})
        </foreach>
    </insert>
    <update id="batchUpdateValue">
        <foreach collection="updateList" separator=";" item="item">
            UPDATE pc_label_factory SET label_value = #{item.labelValue} WHERE id = #{item.id}
        </foreach>
    </update>

    <select id="selectPcLabelFactoryList" parameterType="cn.cecep.talroad.domain.PcLabelFactory" resultMap="PcLabelFactoryResult">
        <include refid="selectPcLabelFactoryVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="labelId != null  and labelId != ''"> and label_id = #{labelId}</if>
            <if test="lableUseing != null  and lableUseing != ''"> and lable_useing = #{lableUseing}</if>
        </where>
    </select>

    <select id="selectLabelFactory" resultMap="PcLabelFactoryResult">
        select
            t1.id,
            t1.factory_id,
            t1.label_id,
            t1.lable_useing,
            t1.label_value
        from pc_label_factory t1
        INNER JOIN pc_factory t2 ON t1.factory_id = t2.id
        <where>
            <if test="labelIds != null and labelIds.size() > 0">
                AND t1.label_id IN
                <foreach collection="labelIds" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="factoryName != null and factoryName != ''">
                AND t2.factory_name LIKE concat('%', #{factoryName}, '%')
            </if>
        </where>
        ORDER BY t2.order_num, t2.id
    </select>

    <select id="selectWarningReal" resultType="cn.cecep.talroad.vo.WarningEnvGasMonRealVo">
        SELECT
        round( AVG ( CAST ( so2_convert AS DECIMAL ) ), 6 ) AS so2_convert,
        round( AVG ( CAST ( nox_convert AS DECIMAL ) ), 6 ) AS nox_convert,
        round( AVG ( CAST ( smoke_dust_convert AS DECIMAL ) ), 6 ) AS smoke_dust_convert,
        senv.outlet_id
        FROM
        s_env_gas_mon_real AS senv
        INNER JOIN ( SELECT DISTINCT outlet_id FROM base_control_concent_standards WHERE include_calculations = '1' ) AS outmp ON senv.outlet_id = outmp.outlet_id
        WHERE
        senv.data_type='M'
        and senv.monitoring_time >= #{startTime}
        AND senv.monitoring_time &lt; #{endTime}
        GROUP BY
        senv.outlet_id
    </select>
    <select id="selectPollutantLicenseLabel"
        resultType="cn.cecep.talroad.vo.FactoryLicenseVo">
        select
            pf.id as factoryId ,
            case t.business_type when 'TYPEA' then 'ALLOW_DIRTY'
                                 when 'TYPEB'  then 'ALLOW_DIRTY'
                                 when 'TYPEC'  then 'ALLOW_DIRTY'
                                 when 'TYPED'  then 'ALLOW_DIRTY'
                                 when 'TYPEH'  then 'ALLOW_DIRTY'
                                 else 'NON_ALLOW_DIRTY' end as labelValue
        from pc_factory pf
                 left join
             (
                 select
                     bm.factory_id ,
                     bm.business_type
                 from
                     (
                         select
                             factory_id,
                             business_type,
                             max(deadline_time) as deadline_time
                         from b_main_poll_dis
                         group by
                             factory_id,
                             business_type
                     )bd
                         left join b_main_poll_dis bm on bd.factory_id = bm.factory_id and bd.business_type=bm.business_type and bd.deadline_time = bm.deadline_time
             ) t on pf.id = t.factory_id
    </select>
    <select id="selectLicenseTypeLabel" resultType="cn.cecep.talroad.vo.FactoryLicenseVo">
        select
            id as factoryId,
            case license_man_type
                when 'FOCUS' then '1014'
                when 'SIMPLIFY' then '1015'
                when 'REGISTER' then '1016' end as labelValue
        from pc_factory pf
    </select>
    <select id="selectPollutantTypeLabel" resultType="cn.cecep.talroad.vo.FactoryLicenseVo">
        select
            id as factoryId,
            case wipollunit
                when '0' then '1018'
                when '1' then '1017' end as labelValue
        from pc_factory pf
    </select>
    <select id="selectCountByLableValue" resultType="java.lang.Long">
        select count(*)
        from pc_label_factory plf
        where plf.label_id = #{labelId}
          and #{argumentsCode} = any ( string_to_array(plf.label_value, ',') )
    </select>
    <select id="selectFactoryLabelList" resultType="cn.cecep.talroad.vo.PcLabelFactoryVo">
        select
            t1.id,
            t1.factory_id,
            t1.label_id,
            t2.factory_name,
            t3.label_val AS label_name,
            t1.label_value,
            t1.lable_useing
        from pc_label_factory t1
        INNER JOIN pc_factory t2 ON t1.factory_id = t2."id"
        INNER JOIN pc_label t3 ON t1.label_id = t3."id"
        <if test="query.factoryId != null and query.factoryId != ''">
            AND t1.factory_id = #{query.factoryId}
        </if>
        <if test="query.labelId != null and query.labelId != ''">
            AND t1.label_id = #{query.labelId}
        </if>
        ORDER BY t2.order_num, t3.order_num, t1.id
    </select>

</mapper>
