<?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.analysis.SRaGasPermittedOnlineMapper">

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaGasPermittedOnline" id="SRaGasPermittedOnlineResult">
        <result property="dataId"    column="data_id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="factoryName"    column="factory_name"    />
        <result property="industryCategoryName"    column="industry_category_name"    />
        <result property="industryCategoryCode"    column="industry_category_code"    />
        <result property="districtName"    column="district_name"    />
        <result property="districtCode"    column="district_code"    />
        <result property="outletType"    column="outlet_type"    />
        <result property="moitorName"    column="moitor_name"    />
        <result property="monitorType"    column="monitor_type"    />
        <result property="monitorTime"    column="monitor_time"    />
        <result property="outletCode"    column="outlet_code"    />
        <result property="nox"    column="nox"    />
        <result property="noxAmendments"    column="nox_amendments"    />
        <result property="so2"    column="so2"    />
        <result property="so2Amendments"    column="so2_amendments"    />
        <result property="pm"    column="pm"    />
        <result property="pmAmendments"    column="pm_amendments"    />
        <result property="vocs"    column="vocs"    />
        <result property="vocsAmendments"    column="vocs_amendments"    />
        <result property="dataFlag"    column="data_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="includedEmissions"    column="included_emissions"    />
    </resultMap>


    <sql id="selectSRaGasPermittedOnlineVo">
        select data_id, factory_id, factory_name, industry_category_name, industry_category_code, district_name, district_code, outlet_type, moitor_name, monitor_type, monitor_time, outlet_code, nox, nox_amendments, so2, so2_amendments, pm, pm_amendments, vocs, vocs_amendments, data_flag, create_time, included_emissions from s_ra_gas_permitted_online
    </sql>
    <sql id="selectSRaGasPermittedOnlineVo2">
        select  factory_name, industry_category_name, district_name, outlet_type, moitor_name, monitor_type, outlet_code, sum(nox_amendments) as nox, sum(so2_amendments) as so2, sum(pm_amendments) as pm, sum(vocs_amendments) as vocs from s_ra_gas_permitted_online
    </sql>
    <sql id="selectSRaGasPermittedOnlineVo1">
        select  factory_name, industry_category_name, district_name, outlet_type, moitor_name, monitor_type, outlet_code, sum(nox) as nox, sum(so2) as so2, sum(pm) as pm, sum(vocs) as vocs from s_ra_gas_permitted_online
    </sql>

    <select id="selectSRaGasPermittedOnlineList" parameterType="cn.cecep.talroad.domain.analysis.SRaGasPermittedOnline" resultMap="SRaGasPermittedOnlineResult">
        <include refid="selectSRaGasPermittedOnlineVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="factoryName != null  and factoryName != ''"> and factory_name like concat('%', #{factoryName}, '%')</if>
            <if test="industryCategoryName != null  and industryCategoryName != ''"> and industry_category_name like concat('%', #{industryCategoryName}, '%')</if>
            <if test="industryCategoryCode != null  and industryCategoryCode != ''"> and industry_category_code = #{industryCategoryCode}</if>
            <if test="districtName != null  and districtName != ''"> and district_name like concat('%', #{districtName}, '%')</if>
            <if test="districtCode != null  and districtCode != ''"> and district_code = #{districtCode}</if>
            <if test="outletType != null  and outletType != ''"> and outlet_type = #{outletType}</if>
            <if test="moitorName != null  and moitorName != ''"> and moitor_name like concat('%', #{moitorName}, '%')</if>
            <if test="monitorType != null  and monitorType != ''"> and monitor_type = #{monitorType}</if>
            <if test="monitorTime != null  and monitorTime != ''"> and monitor_time = #{monitorTime}</if>
            <if test="outletCode != null  and outletCode != ''"> and outlet_code = #{outletCode}</if>
            <if test="nox != null  and nox != ''"> and nox = #{nox}</if>
            <if test="noxAmendments != null  and noxAmendments != ''"> and nox_amendments = #{noxAmendments}</if>
            <if test="so2 != null  and so2 != ''"> and so2 = #{so2}</if>
            <if test="so2Amendments != null  and so2Amendments != ''"> and so2_amendments = #{so2Amendments}</if>
            <if test="pm != null  and pm != ''"> and pm = #{pm}</if>
            <if test="pmAmendments != null  and pmAmendments != ''"> and pm_amendments = #{pmAmendments}</if>
            <if test="vocs != null  and vocs != ''"> and vocs = #{vocs}</if>
            <if test="vocsAmendments != null  and vocsAmendments != ''"> and vocs_amendments = #{vocsAmendments}</if>
            <if test="dataFlag != null  and dataFlag != ''"> and data_flag = #{dataFlag}</if>
            <if test="includedEmissions != null  and includedEmissions != ''"> and included_emissions = #{includedEmissions}</if>
        </where>
    </select>

    <select id="selectSRaGasPermittedOnlineListBy" resultMap="SRaGasPermittedOnlineResult">
        <if test="lambdaQueryWrapper.invalidDataCalculated == 1">
            <include refid="selectSRaGasPermittedOnlineVo2"/>
        </if>
        <if test="lambdaQueryWrapper.invalidDataCalculated == 0">
            <include refid="selectSRaGasPermittedOnlineVo1"/>
        </if>
        <where>
            <if test="lambdaQueryWrapper.factoryId != null  and lambdaQueryWrapper.factoryId != ''"> and factory_id = #{lambdaQueryWrapper.factoryId}</if>
            <if test="lambdaQueryWrapper.factoryName != null  and lambdaQueryWrapper.factoryName != ''"> and factory_name like concat('%', #{lambdaQueryWrapper.factoryName}, '%')</if>
            <if test="lambdaQueryWrapper.industryCategoryName != null  and lambdaQueryWrapper.industryCategoryName != ''"> and industry_category_name like concat('%', #{lambdaQueryWrapper.industryCategoryName}, '%')</if>
            <if test="lambdaQueryWrapper.industryCategoryCode != null  and lambdaQueryWrapper.industryCategoryCode != ''"> and industry_category_code = #{lambdaQueryWrapper.industryCategoryCode}</if>
            <if test="lambdaQueryWrapper.districtName != null  and lambdaQueryWrapper.districtName != ''"> and district_name like concat('%', #{lambdaQueryWrapper.districtName}, '%')</if>
            <if test="lambdaQueryWrapper.districtCode != null  and lambdaQueryWrapper.districtCode != ''"> and district_code = #{lambdaQueryWrapper.districtCode}</if>
            <if test="lambdaQueryWrapper.outletType != null  and lambdaQueryWrapper.outletType != ''"> and outlet_type = #{lambdaQueryWrapper.outletType}</if>
            <if test="lambdaQueryWrapper.moitorName != null  and lambdaQueryWrapper.moitorName != ''"> and moitor_name like concat('%', #{lambdaQueryWrapper.moitorName}, '%')</if>
            <if test="lambdaQueryWrapper.monitorType != null  and lambdaQueryWrapper.monitorType != ''"> and monitor_type = #{lambdaQueryWrapper.monitorType}</if>
            <if test="lambdaQueryWrapper.monitorStartTime != null  and lambdaQueryWrapper.monitorStartTime != ''"> and monitor_time >=CAST(#{lambdaQueryWrapper.monitorStartTime} AS timestamp) </if>
            <if test="lambdaQueryWrapper.monitorEndTime != null  and lambdaQueryWrapper.monitorEndTime != ''"> and monitor_time &lt;=CAST(#{lambdaQueryWrapper.monitorEndTime} AS timestamp) </if>
            <if test="lambdaQueryWrapper.dataFlagArr != null and lambdaQueryWrapper.dataFlagArr.size() != 0">
                <foreach collection="lambdaQueryWrapper.dataFlagArr" item="chile" close=" )" open="  and data_flag in (" separator=",">
                    #{chile}
                </foreach>
            </if>
        </where>
        GROUP BY factory_name,industry_category_name,district_name,outlet_type,moitor_name,monitor_type,outlet_code
        order by factory_name,moitor_name
    </select>

    <select id="pullMonitorNameOptions" resultType="java.util.Map">
        select
        moitor_name AS label,
        monitor_id AS value
        from
        s_ra_gas_permitted_online
        <where>
            <if test="factoryName != null  and factoryName != ''"> and factory_name like concat('%', #{factoryName}, '%')</if>
        </where>
        group by moitor_name, monitor_id
    </select>
</mapper>
