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

    <insert id="insertOrUpdateList" parameterType="list">
        INSERT INTO stock_holder
            (ts_code, ann_date, end_date, holder_name, hold_amount, hold_ratio, holder_type)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.ts_code}, 
             TO_DATE(#{item.ann_date}, 'YYYYMMDD'),
             TO_DATE(#{item.end_date}, 'YYYYMMDD'),
             #{item.holder_name},
             #{item.hold_amount},
             <choose>
	             <when test="item.containsKey('hold_ratio')">
	                #{item.hold_ratio}
	             </when>
	             <otherwise>
	               NULL
	             </otherwise>
             </choose>
             ,#{holderType.isFloat}
             )
        </foreach>
        ON CONFLICT (ts_code, holder_name, end_date, holder_type) DO UPDATE SET
            ts_code = EXCLUDED.ts_code,
            ann_date = EXCLUDED.ann_date,
            end_date = EXCLUDED.end_date,
            holder_name = EXCLUDED.holder_name,
            hold_amount = EXCLUDED.hold_amount,
            hold_ratio = EXCLUDED.hold_ratio,
            holder_type = EXCLUDED.holder_type
    </insert>
    
    <select id="getLatestTradeDate" resultType="java.time.LocalDateTime">
        SELECT
            MAX(end_date)
        FROM
            stock_holder
        WHERE
            1 = 1
            <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
                AND ts_code = #{stockInfo.tsCode}
            </if>
        LIMIT 1
    </select>
    
    <select id="getAllTradeDates" resultType="java.time.LocalDateTime">
        SELECT
            DISTINCT end_date
        FROM
            stock_holder
        WHERE
            1 = 1
            <if test="stockInfo != null and stockInfo.getTsCode() != null and stockInfo.getTsCode() != ''">
                AND ts_code = #{stockInfo.tsCode}
            </if>
        ORDER BY end_date ASC
    </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_holder
            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="getAllByStockCode" resultType="link.at17.mid.tushare.data.models.StockHolder">
        SELECT * FROM stock_holder WHERE
            ts_code = #{stockCode}
            AND holder_type = #{holderType.isFloat}
        ORDER BY end_date ASC
    </select>
</mapper>
