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

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaMultiSource" id="SRaMultiSourceResult">
        <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="pollutantsType"    column="pollutants_type"    />
        <result property="pollutantsName"    column="pollutants_name"    />
        <result property="pollutantsCode"    column="pollutants_code"    />
        <result property="monitorTime"    column="monitor_time"    />
        <result property="outletCode"    column="outlet_code"    />
        <result property="outletType"    column="outlet_type"    />
        <result property="moitorName"    column="moitor_name"    />
        <result property="monitorType"    column="monitor_type"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="permission"    column="permission"    />
        <result property="permissionAmendments"    column="permission_amendments"    />
        <result property="online"    column="online"    />
        <result property="onlineAmendments"    column="online_amendments"    />
        <result property="dataFlag"    column="data_flag"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>


    <sql id="selectSRaMultiSourceVo">
        select data_id, factory_id, factory_name, industry_category_name, industry_category_code, district_name, district_code, pollutants_type, pollutants_name, pollutants_code, monitor_time, outlet_code, outlet_type, moitor_name, monitor_type, monitor_id, permission, permission_amendments, online, online_amendments, data_flag, create_time from s_ra_multi_source
    </sql>
    <sql id="selectSRaMultiSourceVo2">
        select  factory_id, factory_name, industry_category_name, district_name, pollutants_name,pollutants_type, sum(permission_amendments) as permission, sum(online_amendments) as online from s_ra_multi_source
    </sql>
    <sql id="selectSRaMultiSourceVo1">
        select  factory_id, factory_name, industry_category_name, district_name, pollutants_name,pollutants_type, sum(permission) as permission, sum(online) as online from s_ra_multi_source
    </sql>
    <sql id="selectSRaMultiSourceDetailsVo2">
        select   factory_name, outlet_type,moitor_name,monitor_type,outlet_code, sum(permission_amendments) as permission, sum(online_amendments) as online from s_ra_multi_source
    </sql>
    <sql id="selectSRaMultiSourceDetailsVo1">
        select   factory_name, outlet_type,moitor_name,monitor_type,outlet_code, sum(permission) as permission, sum(online) as online from s_ra_multi_source
    </sql>

    <select id="selectSRaMultiSourceList" parameterType="cn.cecep.talroad.domain.analysis.SRaMultiSource" resultMap="SRaMultiSourceResult">
        <include refid="selectSRaMultiSourceVo"/>
        <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="pollutantsType != null  and pollutantsType != ''"> and pollutants_type = #{pollutantsType}</if>
            <if test="pollutantsName != null  and pollutantsName != ''"> and pollutants_name like concat('%', #{pollutantsName}, '%')</if>
            <if test="pollutantsCode != null  and pollutantsCode != ''"> and pollutants_code = #{pollutantsCode}</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="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="monitorId != null  and monitorId != ''"> and monitor_id = #{monitorId}</if>
            <if test="permission != null  and permission != ''"> and permission = #{permission}</if>
            <if test="permissionAmendments != null  and permissionAmendments != ''"> and permission_amendments = #{permissionAmendments}</if>
            <if test="online != null  and online != ''"> and online = #{online}</if>
            <if test="onlineAmendments != null  and onlineAmendments != ''"> and online_amendments = #{onlineAmendments}</if>
            <if test="dataFlag != null  and dataFlag != ''"> and data_flag = #{dataFlag}</if>
        </where>
    </select>

    <select id="selectSRaMultiSourceListBy" resultMap="SRaMultiSourceResult">
        <if test="lambdaQueryWrapper.invalidDataCalculated == 1">
            <include refid="selectSRaMultiSourceVo2"/>
        </if>
        <if test="lambdaQueryWrapper.invalidDataCalculated == 0">
            <include refid="selectSRaMultiSourceVo1"/>
        </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>
            <if test="lambdaQueryWrapper.regionCodes != null and lambdaQueryWrapper.regionCodes.size() != 0">
                and district_code IN
                <foreach collection="lambdaQueryWrapper.regionCodes" separator="," item="item" close=")" open="(">
                    #{item,jdbcType=VARCHAR}
                </foreach>
            </if>
        </where>
        GROUP BY factory_id,factory_name,industry_category_name,district_name,pollutants_name,pollutants_type
        order by factory_name,pollutants_name,pollutants_type
    </select>

    <select id="selectSRaMultiSourceListDetailsBy" resultMap="SRaMultiSourceResult">
        <if test="lambdaQueryWrapper.invalidDataCalculated == 1">
            <include refid="selectSRaMultiSourceDetailsVo2"/>
        </if>
        <if test="lambdaQueryWrapper.invalidDataCalculated == 0">
            <include refid="selectSRaMultiSourceDetailsVo1"/>
        </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.pollutantsName != null  and lambdaQueryWrapper.pollutantsName != ''"> and pollutants_name = #{lambdaQueryWrapper.pollutantsName}</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,outlet_type,moitor_name,monitor_type,outlet_code
        order by moitor_name
    </select>

    <select id="pullMonitorNameOptions" resultType="string">
        select
        distinct  pollutants_type
        from s_ra_multi_source
        <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.pollutantsName != null  and lambdaQueryWrapper.pollutantsName != ''"> and pollutants_name = #{lambdaQueryWrapper.pollutantsName}</if>
        </where>
    </select>
</mapper>
