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

    <resultMap type="cn.cecep.talroad.domain.analysis.SRaOnlineEmissionsGas" id="SRaOnlineEmissionsGasResult">
        <result property="id"    column="id"    />
        <result property="factoryId"    column="factory_id"    />
        <result property="pointName"    column="point_name"    />
        <result property="outletCode"    column="outlet_code"    />
        <result property="outletType"    column="outlet_type"    />
        <result property="pollutantName"    column="pollutant_name"    />
        <result property="monitorTime"    column="monitor_time"    />
        <result property="hourAvg"    column="hour_avg"    />
        <result property="unit"    column="unit"    />
        <result property="flueGasFlow"    column="flue_gas_flow"    />
        <result property="pollutantEmissions"    column="pollutant_emissions"    />
        <result property="isValid"    column="is_valid"    />
        <result property="xyEmissions"    column="xy_emissions"    />
        <result property="xyDesc"    column="xy_desc"    />
        <result property="bringIntoEmissions"    column="bring_into_emissions"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>


    <sql id="selectSRaOnlineEmissionsGasVo">
        select id, factory_id, point_name, outlet_code, outlet_type, pollutant_name, monitor_time, hour_avg, unit, flue_gas_flow, pollutant_emissions, is_valid, xy_emissions, xy_desc, bring_into_emissions, create_by, create_time, update_by, update_time from s_ra_online_emissions_gas
    </sql>

    <select id="selectSRaOnlineEmissionsGasList" parameterType="cn.cecep.talroad.domain.analysis.SRaOnlineEmissionsGas" resultMap="SRaOnlineEmissionsGasResult">
        <include refid="selectSRaOnlineEmissionsGasVo"/>
        <where>
            <if test="factoryId != null  and factoryId != ''"> and factory_id = #{factoryId}</if>
            <if test="pointName != null  and pointName != ''"> and point_name like concat('%', #{pointName}, '%')</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="pollutantName != null  and pollutantName != ''"> and pollutant_name like concat('%', #{pollutantName}, '%')</if>
            <if test="monitorTime != null  and monitorTime != ''"> and monitor_time = #{monitorTime}</if>
            <if test="hourAvg != null  and hourAvg != ''"> and hour_avg = #{hourAvg}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="flueGasFlow != null  and flueGasFlow != ''"> and flue_gas_flow = #{flueGasFlow}</if>
            <if test="pollutantEmissions != null  and pollutantEmissions != ''"> and pollutant_emissions = #{pollutantEmissions}</if>
            <if test="isValid != null  and isValid != ''"> and is_valid = #{isValid}</if>
            <if test="xyEmissions != null  and xyEmissions != ''"> and xy_emissions = #{xyEmissions}</if>
            <if test="xyDesc != null  and xyDesc != ''"> and xy_desc = #{xyDesc}</if>
            <if test="bringIntoEmissions != null  and bringIntoEmissions != ''"> and bring_into_emissions = #{bringIntoEmissions}</if>
        </where>
    </select>



    <select id="select720HourAgoMaxEmissions"
            resultType="cn.cecep.talroad.vo.analysis.MaxEmissionsVO">
        --查询720小时内最大排放量
        select
            pollutant_name,
            max(pollutant_emissions) as max_emissions from s_ra_online_emissions_gas
        where  factory_id = #{factoryId} and outlet_code = #{outletCode} and
            monitor_time >= date_trunc('hour', now() - interval '720 hour')
        group by pollutant_emissions,pollutant_name
    </select>


    <select id="selectMonthTotalData" resultType="cn.cecep.talroad.vo.analysis.MonthTotalEmissionsVO">
        SELECT SUM
                   ( cast(pollutant_emissions as decimal) ) AS total_emissions,
               pollutant_name,
               #{selectType} as selectType
        FROM
            s_ra_online_emissions_gas
        WHERE
            factory_id = #{factoryId}
          AND monitor_time >= to_timestamp( #{dateStr}, 'YYYY-MM-01 00:00:00' )
        GROUP BY
            pollutant_name
    </select>

    <select id="selectEmissionDataList" resultType="cn.cecep.talroad.vo.GasPollEmissionsVO">
        SELECT
            <if test="groupByField != null and groupByField != ''"> ${groupByField} AS groupByField, </if>
            SUM ( CASE pollutant_code WHEN 'A21026' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     so2Emissions,
            SUM ( CASE pollutant_code WHEN 'A21002' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     noxEmissions,
            SUM ( CASE pollutant_code WHEN 'A99911' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     pmEmissions,
            SUM ( CASE pollutant_code WHEN 'A99901' THEN CAST ( pollutant_emissions AS DECIMAL ) ELSE 0 END )     vocsEmissions

        FROM
            s_ra_online_emissions_gas
        WHERE pollutant_code in
            <foreach collection="pollNames" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
            <if test="factoryId != null and factoryId != ''">AND factory_id = #{factoryId}</if>
            <if test="startDateTime != null and startDateTime != '' and endDateTime != null and endDateTime != ''">
                AND monitor_time BETWEEN #{startDateTime}::TIMESTAMP AND #{endDateTime}::TIMESTAMP
            </if>

        <if test="groupByField != null and groupByField != ''"> GROUP BY groupByField </if>
        <if test="groupByField != null and groupByField != ''"> ORDER BY groupByField </if>

    </select>
    <select id="selectChartData" resultType="cn.cecep.talroad.vo.analysis.SRaOnlineEmissionsGasVo$ChartDataVo">
        SELECT
            to_char(monitor_time, 'yyyy-mm-dd hh24') monitor_time,
            round(sum(pollutant_emissions::numeric), 6) pollutant_emissions
        FROM s_ra_online_emissions_gas
        WHERE factory_id = #{searchVo.factoryId}
          AND monitor_time >= to_timestamp(#{searchVo.startTime}, 'yyyy-mm-dd hh24:MI:SS')
          AND monitor_time &lt;= to_timestamp(#{searchVo.endTime}, 'yyyy-mm-dd hh24:MI:SS')
          AND pollutant_code = #{searchVo.pollutantCode}
        GROUP BY to_char(monitor_time, 'yyyy-mm-dd hh24')
    </select>

</mapper>
