<?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="quant.rich.emoney.mapper.postgre.EmoneyIndexMapper">

    <insert id="insertOrUpdate" parameterType="list">
        INSERT INTO emoney_index
            (ts_code, trade_date, "value", index_param, index_name, line_name, line_shape, data_period)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.tsCode}, 
             #{item.date},
             #{item.value},
             #{item.indexParam, typeHandler=quant.rich.data.typehandler.PostgreSQLJsonbTypeHandler},
             #{item.indexName},
             #{item.lineName},
             #{item.lineShape},
             #{item.dataPeriod}
             )
        </foreach>
        ON CONFLICT (ts_code, trade_date, data_period, index_name, index_param, line_name ) DO UPDATE SET
            ts_code = EXCLUDED.ts_code,
            trade_date = EXCLUDED.trade_date,
            "value" = EXCLUDED."value",
            index_param = EXCLUDED.index_param,
            index_name = EXCLUDED.index_name,
            line_shape = EXCLUDED.line_shape,
            data_period = EXCLUDED.data_period
    </insert>
    
    <select id="getLatestTradeDate" resultType="java.util.Date">
        SELECT MAX(trade_date) FROM emoney_index
        WHERE 1 = 1
        AND index_name = #{indexName}
        <if test="tsCode != null and tsCode != ''">
            AND ts_code = #{tsCode}
        </if>
        <if test="stockSpan != null">
            AND data_period = #{stockSpan.emoneyCode}
        </if>
        LIMIT 1
    </select>
    
    <select id="getEmoneyIndex" resultType="quant.rich.emoney.entity.postgre.EmoneyIndex">
        SELECT *, trade_date AS date FROM emoney_index
        WHERE 
        ts_code = #{tsCode}
        AND index_name = #{indexName}
        AND line_name = #{lineName}
        AND data_period = #{stockSpan.emoneyCode}
        <if test="startDate != null">
            AND trade_date &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND trade_date &lt;= #{endDate}
        </if>
        ORDER BY trade_date ASC
    </select>
    
    <select id="getAllMissingDates" resultType="java.util.Date">
        SELECT DISTINCT stock_daily.trade_date
        FROM   stock_daily
        WHERE  1 = 1
        <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
            AND stock_daily.ts_code = #{stockInfo.tsCode}
        </if>
        AND stock_daily.trade_date NOT IN (
            SELECT DISTINCT emoney_index.trade_date
            FROM   emoney_index
            WHERE  1 = 1
            <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
                AND emoney_index.ts_code = #{stockInfo.tsCode}
            </if>
            <if test="indexName != null and indexName != ''">
                AND emoney_index.index_name = #{indexName}
            </if>
            <if test="lineName != null and lineName != ''">
                AND emoney_index.line_name = #{lineName}
            </if>
        )
        <if test="stockInfo != null and stockInfo.getListDate() != null">
            AND stock_daily.trade_date >= #{stockInfo.listDate}
        </if>
        <choose>
            <when test="stockInfo.getDelistDate() != null">
                AND stock_daily.trade_date &lt; #{stockInfo.delistDate}
            </when>
            <otherwise>
                AND stock_daily.trade_date &lt; CURRENT_DATE + INTERVAL '1 DAY'
            </otherwise>
        </choose>
        <if test="startDate != null">
            AND stock_daily.trade_date >= #{startDate}
        </if>
        <if test="endDate != null">
            AND stock_daily.trade_date &lt;= #{endDate}
        </if>
        ORDER BY stock_daily.trade_date ASC
    </select>
    
</mapper>
