<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="link.at17.mid.tushare.dao.StockLimitDao">

    <insert id="insertOrUpdateList" parameterType="list">
        INSERT INTO stock_limit
            (trade_date, ts_code, industry, name, close, pct_chg, amount, limit_amount, float_mv, total_mv, turnover_ratio, fd_amount, first_time, last_time, open_times, up_stat, limit_times, "limit")
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (
            TO_DATE(#{item.trade_date}, 'YYYYMMDD'),
            #{item.ts_code},
			#{item.industry},
			#{item.name},
			#{item.close},
			#{item.pct_chg},
			#{item.amount},
			#{item.limit_amount},
			#{item.float_mv},
			#{item.total_mv},
			#{item.turnover_ratio},
			#{item.fd_amount},
            TO_TIMESTAMP(CONCAT(#{item.trade_date}, LPAD(#{item.first_time}, 6, '0')), 'YYYYMMDDHH24MISS'),
            TO_TIMESTAMP(CONCAT(#{item.trade_date}, LPAD(#{item.last_time},  6, '0')), 'YYYYMMDDHH24MISS'),
			#{item.open_times},
			#{item.up_stat},
			#{item.limit_times},
			#{item.limit}
             )
        </foreach>
        ON CONFLICT (ts_code, trade_date) DO UPDATE SET
            trade_date = EXCLUDED.trade_date,
			ts_code = EXCLUDED.ts_code,
			industry = EXCLUDED.industry,
			name = EXCLUDED.name,
			"close" = EXCLUDED."close",
			pct_chg = EXCLUDED.pct_chg,
			amount = EXCLUDED.amount,
			limit_amount = EXCLUDED.limit_amount,
			float_mv = EXCLUDED.float_mv,
			total_mv = EXCLUDED.total_mv,
			turnover_ratio = EXCLUDED.turnover_ratio,
			fd_amount = EXCLUDED.fd_amount,
			first_time = EXCLUDED.first_time,
			last_time = EXCLUDED.last_time,
			open_times = EXCLUDED.open_times,
			up_stat = EXCLUDED.up_stat,
			limit_times = EXCLUDED.limit_times,
			"limit" = EXCLUDED."limit"
    </insert>
    
    <select id="getLatestTradeDate" resultType="java.time.LocalDateTime">
        SELECT
            MAX(trade_date)
        FROM
            stock_limit
        WHERE
            1 = 1
            <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
                AND ts_code = #{stockInfo.tsCode}
            </if>
    </select>
    
    <select id="getAllTradeDates" resultType="java.time.LocalDateTime">
        SELECT
            DISTINCT trade_date
        FROM
            stock_limit
        WHERE
            1 = 1
            <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
                AND ts_code = #{stockInfo.tsCode}
            </if>
    </select>
    
    <select id="getAllMissingDates" resultType="java.time.LocalDateTime">
        SELECT DISTINCT date
        FROM   stock_calendar
        WHERE  stock_calendar.is_open = TRUE
        <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
            <choose>
                <when test="stockInfo.getTsCode().endsWith('SZ')">
                    AND stock_calendar.exchange = 'SZSE'
                </when>
                <when test="stockInfo.getTsCode().endsWith('SH')">
                    AND stock_calendar.exchange = 'SSE'
                </when>
                <otherwise></otherwise>
            </choose>
        </if>
        AND date NOT IN (
            SELECT trade_date
            FROM   stock_limit
            WHERE  1 = 1
            <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
                AND ts_code = #{stockInfo.tsCode}
            </if>
        )
        <if test="stockInfo != null and stockInfo.getListDate() != null">
            AND date >= #{stockInfo.listDate}
        </if>
        AND date &lt; CURRENT_DATE + INTERVAL '1 DAY'
        ORDER BY date ASC
    </select>

</mapper>
