<?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="com.weather.modules.weather.dailydata.dao.WeatherDailyDataDao">

    <resultMap type="com.weather.modules.weather.dailydata.entity.WeatherDailyDataEntity" id="weatherDailyDataMap">
        <result property="id" column="id"/>
        <result property="stationId" column="station_id"/>
        <result property="observeDate" column="observe_date"/>
        <result property="avgTemp" column="avg_temp"/>
        <result property="maxTemp" column="max_temp"/>
        <result property="maxTempTime" column="max_temp_time"/>
        <result property="minTemp" column="min_temp"/>
        <result property="minTempTime" column="min_temp_time"/>
        <result property="rain2020" column="rain_20_20"/>
        <result property="rain0808" column="rain_08_08"/>
        <result property="maxWindSpeed" column="max_wind_speed"/>
        <result property="maxWindDirection" column="max_wind_direction"/>
        <result property="maxWindTime" column="max_wind_time"/>
        <result property="extremeWindSpeed" column="extreme_wind_speed"/>
        <result property="extremeWindDirection" column="extreme_wind_direction"/>
        <result property="extremeWindTime" column="extreme_wind_time"/>
        <result property="createDate" column="create_date"/>
    </resultMap>


    <sql id="summarizeColumns">
        id, station_id, observe_date,
        avg_temp, max_temp, max_temp_time, min_temp, min_temp_time,
        rain_20_20, rain_08_08,
        relative_humidity, atmospheres,
        day_avg_wind_speed, day_avg_wind_direction,
        max_wind_speed, max_wind_direction, max_wind_time,
        extreme_wind_speed, extreme_wind_direction, extreme_wind_time,
        dept_id
    </sql>

    <select id="selectSummarizeList" resultMap="weatherDailyDataMap">
        SELECT <include refid="summarizeColumns" />
        FROM weather_daily_data
        ${ew.customSqlSegment}
        ORDER BY station_id, observe_date
    </select>

    <select id="selectSummarizeByMonthDay" resultMap="weatherDailyDataMap">
        SELECT <include refid="summarizeColumns" />
        FROM weather_daily_data
        WHERE station_id IN
        <foreach collection="stationIds" item="sid" open="(" separator="," close=")">
            #{sid}
        </foreach>
        AND MONTH(observe_date) = #{month}
        AND DAY(observe_date)   = #{day}
        AND YEAR(observe_date)  BETWEEN #{startYear} AND #{endYear}
        ORDER BY station_id, observe_date
    </select>

    <insert id="insertBatchMultiRow" parameterType="list" useGeneratedKeys="false">
        INSERT INTO weather_daily_data (
            id, station_id, observe_date,
            avg_temp, max_temp, max_temp_time, min_temp, min_temp_time,
            rain_20_20, rain_08_08,
            relative_humidity, atmospheres,
            day_avg_wind_speed, day_avg_wind_direction,
            max_wind_speed, max_wind_direction, max_wind_time,
            extreme_wind_speed, extreme_wind_direction, extreme_wind_time,
            dept_id, creator, create_date, updater, update_date
        ) VALUES
        <foreach item="item" collection="list" separator=",">
            (#{item.id}, #{item.stationId}, #{item.observeDate},
             #{item.avgTemp}, #{item.maxTemp}, #{item.maxTempTime}, #{item.minTemp}, #{item.minTempTime},
             #{item.rain2020}, #{item.rain0808},
             #{item.relativeHumidity}, #{item.atmospheres},
             #{item.dayAvgWindSpeed}, #{item.dayAvgWindDirection},
             #{item.maxWindSpeed}, #{item.maxWindDirection}, #{item.maxWindTime},
             #{item.extremeWindSpeed}, #{item.extremeWindDirection}, #{item.extremeWindTime},
             #{item.deptId}, #{item.creator}, #{item.createDate}, #{item.updater}, #{item.updateDate})
        </foreach>
    </insert>

</mapper>