<?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.hdkj.lt.si.dao.FhzgStateEstimationFeederMapper">

    <resultMap id="BaseResultMap" type="com.hdkj.lt.si.modle.FhzgStateEstimationFeeder">
            <id property="id" column="id" />
            <result property="feeder" column="feeder" />
            <result property="dataDate" column="data_date" />
            <result property="pointTime0000" column="point_time_0000" />
            <result property="pointTime0015" column="point_time_0015" />
            <result property="pointTime0030" column="point_time_0030" />
            <result property="pointTime0045" column="point_time_0045" />
            <result property="pointTime0100" column="point_time_0100" />
            <result property="pointTime0115" column="point_time_0115" />
            <result property="pointTime0130" column="point_time_0130" />
            <result property="pointTime0145" column="point_time_0145" />
            <result property="pointTime0200" column="point_time_0200" />
            <result property="pointTime0215" column="point_time_0215" />
            <result property="pointTime0230" column="point_time_0230" />
            <result property="pointTime0245" column="point_time_0245" />
            <result property="pointTime0300" column="point_time_0300" />
            <result property="pointTime0315" column="point_time_0315" />
            <result property="pointTime0330" column="point_time_0330" />
            <result property="pointTime0345" column="point_time_0345" />
            <result property="pointTime0400" column="point_time_0400" />
            <result property="pointTime0415" column="point_time_0415" />
            <result property="pointTime0430" column="point_time_0430" />
            <result property="pointTime0445" column="point_time_0445" />
            <result property="pointTime0500" column="point_time_0500" />
            <result property="pointTime0515" column="point_time_0515" />
            <result property="pointTime0530" column="point_time_0530" />
            <result property="pointTime0545" column="point_time_0545" />
            <result property="pointTime0600" column="point_time_0600" />
            <result property="pointTime0615" column="point_time_0615" />
            <result property="pointTime0630" column="point_time_0630" />
            <result property="pointTime0645" column="point_time_0645" />
            <result property="pointTime0700" column="point_time_0700" />
            <result property="pointTime0715" column="point_time_0715" />
            <result property="pointTime0730" column="point_time_0730" />
            <result property="pointTime0745" column="point_time_0745" />
            <result property="pointTime0800" column="point_time_0800" />
            <result property="pointTime0815" column="point_time_0815" />
            <result property="pointTime0830" column="point_time_0830" />
            <result property="pointTime0845" column="point_time_0845" />
            <result property="pointTime0900" column="point_time_0900" />
            <result property="pointTime0915" column="point_time_0915" />
            <result property="pointTime0930" column="point_time_0930" />
            <result property="pointTime0945" column="point_time_0945" />
            <result property="pointTime1000" column="point_time_1000" />
            <result property="pointTime1015" column="point_time_1015" />
            <result property="pointTime1030" column="point_time_1030" />
            <result property="pointTime1045" column="point_time_1045" />
            <result property="pointTime1100" column="point_time_1100" />
            <result property="pointTime1115" column="point_time_1115" />
            <result property="pointTime1130" column="point_time_1130" />
            <result property="pointTime1145" column="point_time_1145" />
            <result property="pointTime1200" column="point_time_1200" />
            <result property="pointTime1215" column="point_time_1215" />
            <result property="pointTime1230" column="point_time_1230" />
            <result property="pointTime1245" column="point_time_1245" />
            <result property="pointTime1300" column="point_time_1300" />
            <result property="pointTime1315" column="point_time_1315" />
            <result property="pointTime1330" column="point_time_1330" />
            <result property="pointTime1345" column="point_time_1345" />
            <result property="pointTime1400" column="point_time_1400" />
            <result property="pointTime1415" column="point_time_1415" />
            <result property="pointTime1430" column="point_time_1430" />
            <result property="pointTime1445" column="point_time_1445" />
            <result property="pointTime1500" column="point_time_1500" />
            <result property="pointTime1515" column="point_time_1515" />
            <result property="pointTime1530" column="point_time_1530" />
            <result property="pointTime1545" column="point_time_1545" />
            <result property="pointTime1600" column="point_time_1600" />
            <result property="pointTime1615" column="point_time_1615" />
            <result property="pointTime1630" column="point_time_1630" />
            <result property="pointTime1645" column="point_time_1645" />
            <result property="pointTime1700" column="point_time_1700" />
            <result property="pointTime1715" column="point_time_1715" />
            <result property="pointTime1730" column="point_time_1730" />
            <result property="pointTime1745" column="point_time_1745" />
            <result property="pointTime1800" column="point_time_1800" />
            <result property="pointTime1815" column="point_time_1815" />
            <result property="pointTime1830" column="point_time_1830" />
            <result property="pointTime1845" column="point_time_1845" />
            <result property="pointTime1900" column="point_time_1900" />
            <result property="pointTime1915" column="point_time_1915" />
            <result property="pointTime1930" column="point_time_1930" />
            <result property="pointTime1945" column="point_time_1945" />
            <result property="pointTime2000" column="point_time_2000" />
            <result property="pointTime2015" column="point_time_2015" />
            <result property="pointTime2030" column="point_time_2030" />
            <result property="pointTime2045" column="point_time_2045" />
            <result property="pointTime2100" column="point_time_2100" />
            <result property="pointTime2115" column="point_time_2115" />
            <result property="pointTime2130" column="point_time_2130" />
            <result property="pointTime2145" column="point_time_2145" />
            <result property="pointTime2200" column="point_time_2200" />
            <result property="pointTime2215" column="point_time_2215" />
            <result property="pointTime2230" column="point_time_2230" />
            <result property="pointTime2245" column="point_time_2245" />
            <result property="pointTime2300" column="point_time_2300" />
            <result property="pointTime2315" column="point_time_2315" />
            <result property="pointTime2330" column="point_time_2330" />
            <result property="pointTime2345" column="point_time_2345" />
    </resultMap>

    <sql id="Base_Column_List">
        id,feeder,data_date,point_time_0000,point_time_0015,point_time_0030,
        point_time_0045,point_time_0100,point_time_0115,point_time_0130,point_time_0145,
        point_time_0200,point_time_0215,point_time_0230,point_time_0245,point_time_0300,
        point_time_0315,point_time_0330,point_time_0345,point_time_0400,point_time_0415,
        point_time_0430,point_time_0445,point_time_0500,point_time_0515,point_time_0530,
        point_time_0545,point_time_0600,point_time_0615,point_time_0630,point_time_0645,
        point_time_0700,point_time_0715,point_time_0730,point_time_0745,point_time_0800,
        point_time_0815,point_time_0830,point_time_0845,point_time_0900,point_time_0915,
        point_time_0930,point_time_0945,point_time_1000,point_time_1015,point_time_1030,
        point_time_1045,point_time_1100,point_time_1115,point_time_1130,point_time_1145,
        point_time_1200,point_time_1215,point_time_1230,point_time_1245,point_time_1300,
        point_time_1315,point_time_1330,point_time_1345,point_time_1400,point_time_1415,
        point_time_1430,point_time_1445,point_time_1500,point_time_1515,point_time_1530,
        point_time_1545,point_time_1600,point_time_1615,point_time_1630,point_time_1645,
        point_time_1700,point_time_1715,point_time_1730,point_time_1745,point_time_1800,
        point_time_1815,point_time_1830,point_time_1845,point_time_1900,point_time_1915,
        point_time_1930,point_time_1945,point_time_2000,point_time_2015,point_time_2030,
        point_time_2045,point_time_2100,point_time_2115,point_time_2130,point_time_2145,
        point_time_2200,point_time_2215,point_time_2230,point_time_2245,point_time_2300,
        point_time_2315,point_time_2330,point_time_2345
    </sql>

    <!--
        单点 upsert 辅助查询: 仅取主键 id 用于存在性判断, 避免读取 96 个 JSON 列.
        (feeder, data_date) 无唯一键, 见 README 关于并发与补跑的说明.
    -->
    <select id="selectIdByFeederAndDate" resultType="java.lang.Long">
        SELECT id
        FROM fhzg_state_estimation_feeder
        WHERE feeder = #{feeder} AND data_date = #{dataDate}
        LIMIT 1
    </select>

    <!--
        更新单一时刻列. ${pointColumn} 为列名(动态), 调用方 SePeriodCollectServiceImpl 已做 96 个列名白名单校验,
        杜绝 SQL 注入. #{json} 为 JSON 字符串参数, MySQL JSON 列接受合法 JSON 字符串.
    -->
    <update id="updatePointTime">
        UPDATE fhzg_state_estimation_feeder
        SET ${pointColumn} = #{json}
        WHERE feeder = #{feeder} AND data_date = #{dataDate}
    </update>

    <!--
        新建行并写入单一时刻列, 其余时刻列留 null, id 自增.
    -->
    <insert id="insertPointTime">
        INSERT INTO fhzg_state_estimation_feeder (feeder, data_date, ${pointColumn})
        VALUES (#{feeder}, #{dataDate}, #{json})
    </insert>
</mapper>
