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

    <insert id="insertOrUpdateList" parameterType="list">
        INSERT INTO stock_daily_basic_tushare
            (ts_code, trade_date, close, turnover_rate, turnover_rate_f, volume_ratio, pe, pe_ttm, pb, ps, ps_ttm, dv_ratio, dv_ttm, total_share, float_share, free_share, total_mv, circ_mv)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.ts_code}, 
            TO_DATE(#{item.trade_date}, 'YYYYMMDD'),
            #{item.close},
			#{item.turnover_rate},
			#{item.turnover_rate_f},
			#{item.volume_ratio},
			#{item.pe},
			#{item.pe_ttm},
			#{item.pb},
			#{item.ps},
			#{item.ps_ttm},
			#{item.dv_ratio},
			#{item.dv_ttm},
			#{item.total_share},
			#{item.float_share},
			#{item.free_share},
			#{item.total_mv},
			#{item.circ_mv}
             )
        </foreach>
        ON CONFLICT (ts_code, trade_date) DO UPDATE SET
			ts_code = EXCLUDED.ts_code,
			trade_date = EXCLUDED.trade_date,
			close = EXCLUDED.close,
			turnover_rate = EXCLUDED.turnover_rate,
			turnover_rate_f = EXCLUDED.turnover_rate_f,
			volume_ratio = EXCLUDED.volume_ratio,
			pe = EXCLUDED.pe,
			pe_ttm = EXCLUDED.pe_ttm,
			pb = EXCLUDED.pb,
			ps = EXCLUDED.ps,
			ps_ttm = EXCLUDED.ps_ttm,
			dv_ratio = EXCLUDED.dv_ratio,
			dv_ttm = EXCLUDED.dv_ttm,
			total_share = EXCLUDED.total_share,
			float_share = EXCLUDED.float_share,
			free_share = EXCLUDED.free_share,
			total_mv = EXCLUDED.total_mv,
			circ_mv = EXCLUDED.circ_mv
    </insert>
    
    
    <select id="getLatestTradeDate" resultType="java.time.LocalDateTime">
        SELECT
            MAX(trade_date)
        FROM
            stock_daily_basic_tushare
        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_daily_basic_tushare
        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_daily_basic_tushare
            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>
