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

    <select id="realTimeMonitoring" resultType="cn.cecep.talroad.vo.effective.PcNationalCityRanking">
        SELECT id, pcode , pname ,"cityName","cityId",aqi,"maxPollution",pm25,
               pm10,o3,so2 ,no2,co,date_time,index_337,is_337,
               is_168,index_168,index_province_city,index_province_county,sea.aqi_type
        FROM
            pc_national_city_ranking_hour ph
        LEFT JOIN s_envgas_aqilevel sea ON ph.aqi::numeric BETWEEN sea.lower_value AND sea.upper_value
        WHERE
            ph."cityId"=#{regionCode}::int
        ORDER BY date_time DESC limit 1
    </select>

    <select id="cityRanking" resultType="cn.cecep.talroad.vo.effective.PcNationalCityRanking">
        SELECT DISTINCT date_type,
                        FIRST_VALUE ( date_time ) OVER partition_win AS latest_time,
                FIRST_VALUE ( index_337 ) OVER partition_win AS  index_337,
                FIRST_VALUE ( index_168 ) OVER partition_win AS  index_168,
                FIRST_VALUE ( index_province_city ) OVER partition_win AS  index_province_city
        FROM pc_national_city_ranking_other WHERE  "cityId" = #{regionCode}::int
    WINDOW partition_win AS (
            PARTITION BY date_type ORDER BY date_time DESC
            )
    </select>

    <select id="countyRanking" resultType="cn.cecep.talroad.domain.effective.PcNationalCountyRankingOther">
        SELECT DISTINCT date_type,  FIRST_VALUE ( date_time ) OVER partition_win AS latest_time,
                FIRST_VALUE ( index ) OVER partition_win AS  index
        FROM pc_national_county_ranking_other WHERE
          all_name  like concat('%',#{regionCode})
            WINDOW partition_win AS (
                PARTITION BY date_type ORDER BY date_time DESC
                )
    </select>


<select id="selectByNames" resultType="cn.cecep.talroad.vo.effective.PcNationalCityRanking">
    SELECT ID ,  city_code,
    country_code,
    SUBSTRING ( all_name FROM '唐山市(.*)' ) city_name,
    all_name,
    aqi,
    "maxPollution",
    pm25,
    pm10,
    o3,
    so2,
    no2,
    co,
    date_time,
    INDEX,
    date_type
    FROM
    "pc_national_county_ranking_other"
    WHERE
    EXTRACT ( YEAR FROM date_time ) = EXTRACT ( YEAR FROM NOW( ) )
    AND all_name LIKE'唐山市%'
    AND SUBSTRING ( all_name FROM '唐山市(.*)' ) IN
    <foreach collection="collection" separator="," item="item" close=")" open="(">
        #{item,jdbcType=VARCHAR}
    </foreach>
</select>

    <select id="selecthourByNames" resultType="cn.cecep.talroad.vo.effective.PcNationalCityRanking">
        SELECT ID
                ,
               city_code,
               country_code,
               SUBSTRING ( all_name FROM '唐山市(.*)' ) city_name,
               all_name,
               aqi,
               "maxPollution",
               pm25,
               pm10,
               o3,
               so2,
               no2,
               co,
               date_time,
               INDEX
        FROM
            "pc_national_county_ranking_hour"
        WHERE
            date_time BETWEEN #{param2,jdbcType=DATE} AND #{param3,jdbcType=DATE}
          AND all_name LIKE'唐山市%'
        AND SUBSTRING ( all_name FROM '唐山市(.*)' ) IN
        <foreach collection="param1" separator="," item="item" close=")" open="(">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </select>
</mapper>

