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

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline" id="SRaWaterPermittedOnlineResult">
        <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="cod"    column="cod"    />
        <result property="codAmendments"    column="cod_amendments"    />
        <result property="nitrogen"    column="nitrogen"    />
        <result property="nitrogenAmendments"    column="nitrogen_amendments"    />
        <result property="ammonia"    column="ammonia"    />
        <result property="ammoniaAmendments"    column="ammonia_amendments"    />
        <result property="phosphorus"    column="phosphorus"    />
        <result property="phosphorusAmendments"    column="phosphorus_amendments"    />
        <result property="dataFlag"    column="data_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="includedEmissions"    column="included_emissions"    />
    </resultMap>


    <sql id="selectSRaWaterPermittedOnlineVo">
        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, cod, cod_amendments, nitrogen, nitrogen_amendments, ammonia, ammonia_amendments, phosphorus, phosphorus_amendments, data_flag, create_time, included_emissions from s_ra_water_permitted_online
    </sql>
    <sql id="selectSRaWaterPermittedOnlineVo2">
        select  factory_name, industry_category_name, district_name, outlet_type, moitor_name, monitor_type, outlet_code, sum(cod_amendments) as cod, sum(nitrogen_amendments) as nitrogen, sum(ammonia_amendments) as ammonia, sum(phosphorus_amendments) as phosphorus from s_ra_water_permitted_online
    </sql>
    <sql id="selectSRaWaterPermittedOnlineVo1">
        select  factory_name, industry_category_name, district_name, outlet_type, moitor_name, monitor_type, outlet_code, sum(cod) as cod, sum(nitrogen) as nitrogen, sum(ammonia) as ammonia, sum(phosphorus) as phosphorus from s_ra_water_permitted_online
    </sql>
    <select id="selectSRaWaterPermittedOnlineList" parameterType="cn.cecep.talroad.domain.analysis.SRaWaterPermittedOnline" resultMap="SRaWaterPermittedOnlineResult">
        <include refid="selectSRaWaterPermittedOnlineVo"/>
        <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="cod != null  and cod != ''"> and cod = #{cod}</if>
            <if test="codAmendments != null  and codAmendments != ''"> and cod_amendments = #{codAmendments}</if>
            <if test="nitrogen != null  and nitrogen != ''"> and nitrogen = #{nitrogen}</if>
            <if test="nitrogenAmendments != null  and nitrogenAmendments != ''"> and nitrogen_amendments = #{nitrogenAmendments}</if>
            <if test="ammonia != null  and ammonia != ''"> and ammonia = #{ammonia}</if>
            <if test="ammoniaAmendments != null  and ammoniaAmendments != ''"> and ammonia_amendments = #{ammoniaAmendments}</if>
            <if test="phosphorus != null  and phosphorus != ''"> and phosphorus = #{phosphorus}</if>
            <if test="phosphorusAmendments != null  and phosphorusAmendments != ''"> and phosphorus_amendments = #{phosphorusAmendments}</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="SRaWaterPermittedOnlineResult">
        <if test="lambdaQueryWrapper.invalidDataCalculated == 1">
            <include refid="selectSRaWaterPermittedOnlineVo2"/>
        </if>
        <if test="lambdaQueryWrapper.invalidDataCalculated == 0">
            <include refid="selectSRaWaterPermittedOnlineVo1"/>
        </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_water_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>
