<?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.market.mapper.FuturesKlineMapper">

    <resultMap id="klineResultMap" type="com.market.entity.Kline">
        <id     column="id"         property="id"/>
        <result column="period"     property="period"/>
        <result column="open"       property="open"/>
        <result column="close"      property="close"/>
        <result column="high"       property="high"/>
        <result column="low"        property="low"/>
        <result column="volume"     property="volume"/>
        <result column="turnover"   property="turnover"/>
        <result column="begin_time" property="beginTime"/>
        <result column="end_time"   property="endTime"/>
    </resultMap>

    <!-- 按开始时间 + 周期查询，判断新增还是修改 -->
    <select id="selectByBeginTime" resultMap="klineResultMap">
        SELECT id, period, open, close, high, low, volume, turnover, begin_time, end_time
        FROM `${tableName}`
        WHERE begin_time = #{beginTime}
          AND period = #{period}
        LIMIT 1
    </select>

    <!-- 新增一条 K 线 -->
    <insert id="insert">
        INSERT INTO `${tableName}`
            (period, open, close, high, low, volume, turnover, begin_time, end_time)
        VALUES
            (#{k.period}, #{k.open}, #{k.close}, #{k.high}, #{k.low},
             #{k.volume}, #{k.turnover}, #{k.beginTime}, #{k.endTime})
    </insert>

    <!-- 更新 K 线（按 begin_time + period 定位） -->
    <update id="update">
        UPDATE `${tableName}`
        SET open     = #{k.open},
            close    = #{k.close},
            high     = #{k.high},
            low      = #{k.low},
            volume   = #{k.volume},
            turnover = #{k.turnover},
            end_time = #{k.endTime}
        WHERE begin_time = #{k.beginTime}
          AND period = #{k.period}
    </update>

    <!-- 查询时间范围内的 K 线历史，按 begin_time 升序 -->
    <select id="selectByTimeRange" resultMap="klineResultMap">
        SELECT id, period, open, close, high, low, volume, turnover, begin_time, end_time
        FROM `${tableName}`
        WHERE period = #{period}
          AND begin_time &gt;= #{from}
          AND begin_time &lt;= #{to}
        ORDER BY begin_time ASC
    </select>

    <!-- 批量查询已存在的 begin_time -->
    <select id="selectExistingBeginTimes" resultType="java.lang.Long">
        SELECT begin_time
        FROM `${tableName}`
        WHERE period = #{period}
          AND begin_time IN
        <foreach collection="beginTimes" item="t" open="(" separator="," close=")">
            #{t}
        </foreach>
    </select>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        INSERT INTO `${tableName}`
            (period, open, close, high, low, volume, turnover, begin_time, end_time)
        VALUES
        <foreach collection="list" item="k" separator=",">
            (#{k.period}, #{k.open}, #{k.close}, #{k.high}, #{k.low},
             #{k.volume}, #{k.turnover}, #{k.beginTime}, #{k.endTime})
        </foreach>
    </insert>

    <!-- 批量更新 -->
    <update id="batchUpdate">
        <foreach collection="list" item="k" separator=";">
            UPDATE `${tableName}`
            SET open     = #{k.open},
                close    = #{k.close},
                high     = #{k.high},
                low      = #{k.low},
                volume   = #{k.volume},
                turnover = #{k.turnover},
                end_time = #{k.endTime}
            WHERE begin_time = #{k.beginTime}
              AND period = #{k.period}
        </foreach>
    </update>

</mapper>
