<?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.effective.QEnvgasAreaTargetMapper">
    <delete id="deleteByRegionCode">
        DELETE FROM q_envgas_area_target WHERE region_code = #{regionCode}
    </delete>

    <delete id="deleteAutumnByRegionCode">
        DELETE FROM q_envgas_area_target WHERE region_code = #{regionCode} AND time_type IN (5,6) and fk_factor_id = #{field}
    </delete>

    <select id="queryQEnvgasAreaTargetList" resultType="cn.cecep.talroad.domain.effective.QEnvgasAreaTarget">
         select *  from q_envgas_area_target
    </select>
    <select id="findTargets" resultType="java.util.Map">
        SELECT
            a.target_value as target,
            a.actual_value as actual,
            b.field
        FROM
            q_envgas_area_target a
            LEFT JOIN p_code_index_pollutant b ON a.fk_factor_id = b.pk_id
        WHERE  a.region_code = #{regionCode}
            AND a.time_type = #{timeType}
            AND a.time = #{time}
    </select>

    <select id="findMonthTarget" resultType="java.util.Map">
        SELECT
            a.target_value as target,
            a.actual_value as actual,
            SUBSTR(a.time,6,2) as month,
            b.field
        FROM
            q_envgas_area_target a
            LEFT JOIN p_code_index_pollutant b ON a.fk_factor_id = b.pk_id
        WHERE a.region_code = #{regionCode}
            AND a.time_type= #{timeType}
            AND SUBSTR(a.time,1,4) = #{year}::varchar
    </select>
    <select id="findAutumnWinterTarget" resultType="java.util.Map">
        SELECT
        SUM(a.actual_value) as actual,
        SUM(a.target_value) as target
        FROM
        q_envgas_area_target a
        left JOIN p_code_index_pollutant b
        ON a.fk_factor_id = b.pk_id
        WHERE
        a.time IN (
        <foreach collection="month" item="item" separator=",">
          #{item}
        </foreach>
        )
        AND a.time_type = #{type}
        AND a.region_code = #{regionCode}
        AND b.field = #{factor}
    </select>
    <select id="findAutumTarget" resultType="java.util.Map">
        SELECT
            a.actual_value as actual,
            a.target_value as target,
            b.field field
        FROM
            q_envgas_area_target a
            left JOIN p_code_index_pollutant b
            ON a.fk_factor_id = b.pk_id
        WHERE
            a.time = #{year}::TEXT
            AND a.time_type = #{type}
            AND a.region_code = #{regionCode}
    </select>
</mapper>
