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

    <resultMap type="cn.cecep.talroad.domain.SEnvGasReal" id="SEnvGasRealResult">
        <result property="id"    column="id"    />
        <result property="outletId"    column="outlet_id"    />
        <result property="stationName"    column="station_name"    />
        <result property="licenceStationName"    column="licence_station_name"    />
        <result property="monitoringTime"    column="monitoring_time"    />
        <result property="pm10"    column="pm10"    />
        <result property="pm25"    column="pm25"    />
        <result property="tsp"    column="tsp"    />
        <result property="nmhc"    column="nmhc"    />
        <result property="ch4"    column="ch4"    />
        <result property="hydrocarbon"    column="hydrocarbon"    />
        <result property="benzene"    column="benzene"    />
        <result property="methylbenzene"    column="methylbenzene"    />
        <result property="temperature"    column="temperature"    />
        <result property="humidity"    column="humidity"    />
        <result property="windDirection"    column="wind_direction"    />
        <result property="windSpeed"    column="wind_speed"    />
        <result property="barometricPressure"    column="barometric_pressure"    />
        <result property="dataType"    column="data_type"    />
        <result property="factoryId"    column="factory_id"    />
    </resultMap>


    <sql id="selectSEnvGasRealVo">
        select id, outlet_id, station_name, licence_station_name, monitoring_time, pm10, pm25, tsp, nmhc, ch4, hydrocarbon, benzene, methylbenzene, temperature, humidity, wind_direction, wind_speed, barometric_pressure, data_type, factory_id from s_env_gas_real
    </sql>

    <select id="selectSEnvGasRealList" parameterType="cn.cecep.talroad.domain.SEnvGasReal" resultMap="SEnvGasRealResult">
        <include refid="selectSEnvGasRealVo"/>
        <where>
            <if test="outletId != null  and outletId != ''"> and outlet_id = #{outletId}</if>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="licenceStationName != null  and licenceStationName != ''"> and licence_station_name like concat('%', #{licenceStationName}, '%')</if>
            <if test="monitoringTime != null  and monitoringTime != ''"> and monitoring_time = #{monitoringTime}</if>
            <if test="pm10 != null  and pm10 != ''"> and pm10 = #{pm10}</if>
            <if test="pm25 != null  and pm25 != ''"> and pm25 = #{pm25}</if>
            <if test="tsp != null  and tsp != ''"> and tsp = #{tsp}</if>
            <if test="nmhc != null  and nmhc != ''"> and nmhc = #{nmhc}</if>
            <if test="ch4 != null  and ch4 != ''"> and ch4 = #{ch4}</if>
            <if test="hydrocarbon != null  and hydrocarbon != ''"> and hydrocarbon = #{hydrocarbon}</if>
            <if test="benzene != null  and benzene != ''"> and benzene = #{benzene}</if>
            <if test="methylbenzene != null  and methylbenzene != ''"> and methylbenzene = #{methylbenzene}</if>
            <if test="temperature != null  and temperature != ''"> and temperature = #{temperature}</if>
            <if test="humidity != null  and humidity != ''"> and humidity = #{humidity}</if>
            <if test="windDirection != null  and windDirection != ''"> and wind_direction = #{windDirection}</if>
            <if test="windSpeed != null  and windSpeed != ''"> and wind_speed = #{windSpeed}</if>
            <if test="barometricPressure != null  and barometricPressure != ''"> and barometric_pressure = #{barometricPressure}</if>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
        </where>
    </select>

    <select id="overviewUnorganizedMonitors" resultType="cn.cecep.talroad.vo.homepage.OverviewUnorganizedMonitorsVo">
        SELECT COUNT
                   ( DISTINCT CASE pollutant_type WHEN '3' THEN po.factory_id END ) unorganizedEnterprise,
               COALESCE ( SUM ( CASE WHEN pollutant_type = '3' THEN 1 ELSE 0 END ), 0 ) monitorNumber
        FROM
            pc_factory pf
                INNER JOIN pc_outlet po ON pf."id" = po.factory_id
        <if test="regionCodes != null and regionCodes.size() != 0">
            and pf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="monitoringPointWarning" resultType="cn.cecep.talroad.vo.homepage.MonitoringPointWarningVo">
        SELECT COALESCE ( SUM ( CASE WHEN t2.TYPE = '0' THEN 1 ELSE 0 END ), 0 ) gasMonitoringWarning,
               COALESCE ( SUM ( CASE WHEN t2.problem_type = '005001' THEN 1 ELSE 0 END ), 0 ) gasMonitoringExceeding,
               COALESCE (
                       SUM (
                               CASE

                                   WHEN t2.problem_type = '001011'
                                       OR t2.problem_type = '001006'
                                       OR t2.problem_type = '001007'
                                       OR t2.problem_type = '001008'
                                       OR t2.problem_type = '001009'
                                       OR t2.problem_type = '001010'
                                       OR t2.problem_type = '003001' THEN
                                       1 ELSE 0
                                   END
                           ),
                       0
                   ) gasMonitoringAbnormal
        FROM
                ( SELECT ID,factory_id FROM pc_outlet WHERE pollutant_type = '3' ) AS t1
                    INNER JOIN ( SELECT * FROM m_am_problem_fi WHERE feedback_date IS NULL ) AS t2 ON t1.ID = t2.station_id
                INNER JOIN pc_factory pf on pf.id=t1.factory_id
            <where>
                <if test="monitoringStartTime != null and monitoringStartTime!=''">
                    and t2.analyse_time  >=CAST(#{monitoringStartTime} AS timestamp)
                </if>
                <if test="monitoringEndTime != null and monitoringEndTime!=''">
                    and t2.analyse_time  &lt;=CAST(#{monitoringEndTime}AS timestamp)
                </if>
                <if test="regionCodes != null and regionCodes.size() != 0">
                    and pf.country_code IN
                    <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                        #{item,jdbcType=VARCHAR}
                    </foreach>
                </if>
            </where>
    </select>

    <select id="numberMonitoringPoints" resultType="cn.cecep.talroad.vo.homepage.RegionalOnlineMonitoringVo">
        SELECT
            pf.country_code AS districtCode,
            COUNT ( outlet.factory_id ) AS numberMonitoringPoints
        FROM
            pc_factory AS pf
                INNER JOIN ( SELECT factory_id FROM pc_outlet WHERE pollutant_type = '3' ) AS outlet ON pf.ID = outlet.factory_id
        <if test="regionCodes != null and regionCodes.size() != 0">
            and pf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        GROUP BY
            pf.country_code,
            country_name
    </select>

    <select id="monitoringRate" resultType="cn.cecep.talroad.vo.homepage.RegionalOnlineMonitoringVo">
        SELECT
            district_code,
            COALESCE ( SUM ( unorganized_transmitt_hour_num ), 0 ) AS total,
            COALESCE ( SUM ( unorganized_actual_transmitt_hour_num ), 0 ) AS divisor
        FROM
            s_ra_factory_transfer_rate_analysis AS ra
                INNER JOIN ( SELECT DISTINCT factory_id FROM pc_outlet WHERE pollutant_type = '3'
            ) AS pf ON pf.factory_id = ra.factory_id
            inner join pc_factory as ppf on pf.factory_id=ppf.id
        WHERE
            count_type = 'M'
          AND statistics_time = REPLACE ( #{month}, '-', '' )
        <if test="regionCodes != null and regionCodes.size() != 0">
            and ppf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        GROUP BY
            district_code
    </select>

    <select id="excessRate" resultType="cn.cecep.talroad.vo.homepage.RegionalOnlineMonitoringVo">
        SELECT COALESCE
        ( count (t2.problem_type), 0 ) divisor,
        t1.country_code as districtCode
        FROM
        (
        SELECT
        outlet.ID,
        pf.country_code
        FROM
        pc_outlet AS outlet
        INNER JOIN pc_factory AS pf ON pf.ID = outlet.factory_id
        WHERE
        outlet.pollutant_type = '3'
        <if test="regionCodes != null and regionCodes.size() != 0">
            and pf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
        ) AS t1
        INNER JOIN ( SELECT * FROM m_am_problem_fi WHERE problem_type = '005001' and feedback_date IS NULL ) AS t2 ON t1.ID = t2.station_id
        <where>
            <if test="month != null and month!=''">
                and TO_CHAR(t2.analyse_time, 'YYYY-MM') =#{month}
            </if>
        </where>
        GROUP BY
        t1.country_code
    </select>

    <select id="exceedingStandardPoints" resultType="cn.cecep.talroad.vo.homepage.RegionalOnlineMonitoringVo">
        SELECT COALESCE
                   ( count (  DISTINCT t2.station_id ), 0 ) total,
               t1.country_code as districtCode
        FROM
            (
                SELECT
                    outlet.ID,
                    pf.country_code
                FROM
                    pc_outlet AS outlet
                        INNER JOIN pc_factory AS pf ON pf.ID = outlet.factory_id
                WHERE
                    outlet.pollutant_type = '3'
        <if test="regionCodes != null and regionCodes.size() != 0">
            and pf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
            ) AS t1
                INNER JOIN ( SELECT * FROM m_am_problem_fi WHERE problem_type = '005001' and feedback_date IS NULL ) AS t2 ON t1.ID = t2.station_id
        <where>
            <if test="monitoringStartTime != null and monitoringStartTime!=''">
                and t2.analyse_time  >=CAST(#{monitoringStartTime} AS timestamp)
            </if>
            <if test="monitoringEndTime != null and monitoringEndTime!=''">
                and t2.analyse_time  &lt;=CAST(#{monitoringEndTime}AS timestamp)
            </if>
        </where>
        GROUP BY
            t1.country_code
        order by total desc
    </select>

    <select id="exceedingIndustryPoints" resultType="cn.cecep.talroad.vo.homepage.RegionalOnlineMonitoringVo">
        SELECT COALESCE
                   ( count (  DISTINCT t2.station_id ), 0 ) total,
               t1.industry_permission_name
        FROM
            (
                SELECT
                    outlet.ID,
                    pf.industry_permission_name
                FROM
                    pc_outlet AS outlet
                        INNER JOIN pc_factory AS pf ON pf.ID = outlet.factory_id
                WHERE
                    outlet.pollutant_type = '3'
        <if test="regionCodes != null and regionCodes.size() != 0">
            and pf.country_code IN
            <foreach collection="regionCodes" separator="," item="item" close=")" open="(">
                #{item,jdbcType=VARCHAR}
            </foreach>
        </if>
            ) AS t1
                INNER JOIN ( SELECT * FROM m_am_problem_fi WHERE problem_type = '005001' and feedback_date IS NULL ) AS t2 ON t1.ID = t2.station_id
        <where>
            <if test="monitoringStartTime != null and monitoringStartTime!=''">
                and t2.analyse_time  >=CAST(#{monitoringStartTime} AS timestamp)
            </if>
            <if test="monitoringEndTime != null and monitoringEndTime!=''">
                and t2.analyse_time  &lt;=CAST(#{monitoringEndTime}AS timestamp)
            </if>
        </where>
        GROUP BY t1.industry_permission_name
        order BY t1.industry_permission_name desc
    </select>
    <select id="selectLastDayData"
        resultType="cn.cecep.talroad.vo.analysis.FactoryOutletStaticsVo">
        select a.* from
            (select
                 factory_id as factoryId,
                 outlet_id as outletId ,
                 count(1) as actualCount
             from
                 s_env_gas_real segmh
             WHERE
                 data_type = 'H'
               AND monitoring_time between #{lastDayBegin} and #{lastDayEnd}
             group by factory_id , outlet_id
            ) a
                inner join pc_outlet po on a.factoryId = po.factory_id and a.outletId = po.id
        where po.pollutant_type ='3'

    </select>
</mapper>
