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

    <sql id="selectSRaGasFlagVo">
        select id, outlet_id, data_flag, start_time, end_time, duration, duration_avg, oxygen_emission, smoke_dust_convert, smoke_dust_emission, nox_convert, nox_emission, so2_convert, so2_emission, o3, smoke_dust_flow_velocity, smoke_dust_tem, smoke_dust_pressure from s_ra_gas_flag
    </sql>
    <select id="selectGasFlagList" resultType="cn.cecep.talroad.vo.analysis.SRaGasFlagVo$Row">
        select
            t4.factory_name,
            t5.arguments_name as "industryName",
            t4.country_name as "regionName",
            t1.outlet_id as "pointId",
            t2.monitoring_point_name as "pointName",
            t3.outlet_code,
            t3.outlet_types,
            t1.data_flag,
            to_char(t1.start_time, 'yyyy-mm-dd hh24:MI:SS') start_time,
            to_char(t1.end_time, 'yyyy-mm-dd hh24:MI:SS') end_time,
            t1.duration,
            t1.duration_avg,
            t1.oxygen_emission,
            t1.smoke_dust_convert,
            t1.smoke_dust_emission,
            t1.nox_convert,
            t1.nox_emission,
            t1.so2_convert,
            t1.so2_emission,
            t1.o3,
            t1.smoke_dust_flow_velocity,
            t1.smoke_dust_tem,
            t1.smoke_dust_pressure
        from s_ra_gas_flag t1
        LEFT JOIN pc_outlet t2 ON t1.outlet_id = t2.id
        LEFT JOIN pc_outlet t3 ON t2.parent_outlet_id = t3.id
        LEFT JOIN pc_factory t4 ON t2.factory_id = t4.id
        <!-- 关联查询企业的行业标签 -->
        LEFT JOIN (
            select
                plf.factory_id, plv.arguments_code, plv.arguments_name
            from pc_label_factory plf
            left join pc_label_value plv on plf.label_id = plv.label_id and plf.label_value = plv.arguments_code
            where plf.label_id = '4'
        ) t5 on t2.factory_id = t5.factory_id
        <where>
            <if test="query.factoryName != null and query.factoryName != ''">
                AND t4.factory_name LIKE CONCAT('%', #{query.factoryName}, '%')
            </if>
            <if test="query.industryCode != null and query.industryCode != ''">
                AND t5.arguments_code = #{query.industryCode}
            </if>
            <if test="query.regionCode != null and query.regionCode != ''">
                AND t4.country_code = #{query.regionCode}
            </if>
            <if test="query.regionCodeList != null and query.regionCodeList.size() > 0">
                AND t4.country_code IN
                <foreach collection="query.regionCodeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.pointName != null and query.pointName != ''">
                AND t2.monitoring_point_name LIKE CONCAT('%', #{query.pointName}, '%')
            </if>
            <if test="query.pointId != null and query.pointId != ''">
                AND t1.outlet_id = #{query.pointId}
            </if>
            <if test="query.outletTypes != null and query.outletTypes != ''">
                AND t3.outlet_types = #{query.outletTypes}
            </if>
            <if test="query.outletCode != null and query.outletCode != ''">
                AND t3.outlet_code LIKE CONCAT('%', #{query.outletCode}, '%')
            </if>
            <if test="query.dataFlag != null and query.dataFlag != ''">
                AND t1.data_flag = #{query.dataFlag}
            </if>
            <if test="query.startTime != null and query.startTime != ''">
                AND t1.start_time >= to_timestamp(#{query.startTime}, 'yyyy-mm-dd')
            </if>
            <if test="query.endTime != null and query.endTime != ''">
                AND t1.start_time &lt;= to_timestamp(CONCAT(#{query.endTime}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS')
            </if>
        </where>
        ORDER BY t1.start_time desc, t1.id
    </select>

    <select id="selectGasFlagDetailList" resultType="cn.cecep.talroad.vo.analysis.SRaGasFlagVo$DetailRow">
        select
            t4.factory_name,
            t5.arguments_name as "industryName",
            t4.country_name as "regionName",
            t2.monitoring_point_name as "pointName",
            t3.outlet_code,
            t3.outlet_types,
            t1.data_flag,
            case t1.data_type
                when 'D' then to_char(t1.date_time, 'yyyy-mm-dd')
                when 'H' then to_char(t1.date_time, 'yyyy-mm-dd hh24')
                when 'M' then to_char(t1.date_time, 'yyyy-mm-dd hh24:MI')
            end monitor_time,
            t1.oxygen_emission,
            t1.smoke_dust_convert,
            t1.smoke_dust_emission,
            t1.nox_convert,
            t1.nox_emission,
            t1.so2_convert,
            t1.so2_emission,
            t1.o3,
            t1.smoke_dust_flow_velocity,
            t1.smoke_dust_tem,
            t1.smoke_dust_pressure
        from s_env_gas_mon_real t1
        LEFT JOIN pc_outlet t2 ON t1.outlet_id = t2.id
        LEFT JOIN pc_outlet t3 ON t2.parent_outlet_id = t3.id
        LEFT JOIN pc_factory t4 ON t2.factory_id = t4.id
        <!-- 关联查询企业的行业标签 -->
        LEFT JOIN (
            select
                plf.factory_id, plv.arguments_code, plv.arguments_name
            from pc_label_factory plf
            left join pc_label_value plv on plf.label_id = plv.label_id and plf.label_value = plv.arguments_code
            where plf.label_id = '4'
        ) t5 on t2.factory_id = t5.factory_id
        <where>
            <if test="query.factoryName != null and query.factoryName != ''">
                AND t4.factory_name LIKE CONCAT('%', #{query.factoryName}, '%')
            </if>
            <if test="query.industryCode != null and query.industryCode != ''">
                AND t5.arguments_code = #{query.industryCode}
            </if>
            <if test="query.regionCode != null and query.regionCode != ''">
                AND t4.country_code = #{query.regionCode}
            </if>
            <if test="query.regionCodeList != null and query.regionCodeList.size() > 0">
                AND t4.country_code IN
                <foreach collection="query.regionCodeList" open="(" separator="," close=")" item="item">
                    #{item}
                </foreach>
            </if>
            <if test="query.pointName != null and query.pointName != ''">
                AND t2.monitoring_point_name LIKE CONCAT('%', #{query.pointName}, '%')
            </if>
            <if test="query.pointId != null and query.pointId != ''">
                AND t1.outlet_id = #{query.pointId}
            </if>
            <if test="query.outletTypes != null and query.outletTypes != ''">
                AND t3.outlet_types = #{query.outletTypes}
            </if>
            <if test="query.outletCode != null and query.outletCode != ''">
                AND t3.outlet_code LIKE CONCAT('%', #{query.outletCode}, '%')
            </if>
            <if test="query.dataFlag != null and query.dataFlag != ''">
                AND t1.data_flag = #{query.dataFlag}
            </if>
            <if test="query.dataType != null and query.dataType != ''">
                AND t1.data_type = #{query.dataType}
            </if>
            <if test="query.startTime != null and query.startTime != ''">
                AND t1.date_time >= to_timestamp(#{query.startTime}, 'yyyy-mm-dd hh24:MI:SS')
            </if>
            <if test="query.endTime != null and query.endTime != ''">
                AND t1.date_time &lt;= to_timestamp(CONCAT(#{query.endTime}, ' 23:59:59'), 'yyyy-mm-dd hh24:MI:SS')
            </if>
        </where>
        ORDER BY t1.date_time desc, t1.id
    </select>

</mapper>
