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

    <insert id="insertOrUpdateList" parameterType="list">
        INSERT INTO stock_ths_daily
            (ts_code, trade_date, close, open, high, low, pre_close, avg_price, "change", pct_change, vol, turnover_rate, total_mv, float_mv)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.ts_code}, 
            TO_DATE(#{item.trade_date}, 'YYYYMMDD'),
            #{item.close},
            #{item.open},
            #{item.high},
            #{item.low},
			#{item.pre_close},
			#{item.avg_price},
			#{item.change},
			#{item.pct_change},
			#{item.vol},
			#{item.turnover_rate},
			#{item.total_mv},
			#{item.float_mv}
             )
        </foreach>
        ON CONFLICT (ts_code, trade_date) DO UPDATE SET
			ts_code = EXCLUDED.ts_code,
			trade_date = EXCLUDED.trade_date,
			close = EXCLUDED.close,
			open = EXCLUDED.open,
			high = EXCLUDED.high,
			low = EXCLUDED.low,
			pre_close = EXCLUDED.pre_close,
			avg_price = EXCLUDED.avg_price,
			"change" = EXCLUDED."change",
			pct_change = EXCLUDED.pct_change,
			vol = EXCLUDED.vol,
			turnover_rate = EXCLUDED.turnover_rate,
			total_mv = EXCLUDED.total_mv,
			float_mv = EXCLUDED.float_mv
    </insert>
    
    
    <select id="getLatestTradeDate" resultType="java.time.LocalDateTime">
        SELECT
            MAX(trade_date)
        FROM
            stock_ths_daily
        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_ths_daily
        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_ths_daily
            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>
    
    <select id="getDailyBeforeTushare" resultType="link.at17.mid.tushare.data.models.StockValue">
	    SELECT * FROM (
	        SELECT *, s.trade_date AS date FROM
		        stock_ths_daily s
	        WHERE
	            s.ts_code = #{stockCode}
	            <if test="endDate != null">
	               AND s.trade_date &lt;= #{endDate}
	            </if>
	            AND s.open IS NOT NULL
	        ORDER BY 
	            s.trade_date DESC
            <if test="before != null">
	        LIMIT #{before}
	        </if>
        ) f ORDER BY f.trade_date ASC
    </select>
    
</mapper>
