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

    <insert id="insertOrUpdateList" parameterType="list">
    	<if test="list != null and list.size() > 0">
	        INSERT INTO stock_info
	            (ts_code, name, list_status, list_date, delist_date)
	        VALUES
	        <foreach collection="list" item="item" index="index" separator=",">
	            (#{item.ts_code}, #{item.name}, #{item.list_status},
	            	<choose>
	            		<when test="item.list_date == null">NULL</when>
	            		<otherwise>TO_DATE(#{item.list_date}, 'YYYYMMDD')</otherwise>
	            	</choose>,
	            	<choose>
	            		<when test="item.delist_date == null">NULL</when>
	            		<otherwise>TO_DATE(#{item.delist_date}, 'YYYYMMDD')</otherwise>
	            	</choose>)
	        </foreach>
	        ON CONFLICT (ts_code) DO UPDATE SET
	            ts_code = EXCLUDED.ts_code,
	            name = EXCLUDED.name,
	            list_status = EXCLUDED.list_status,
	            list_date = EXCLUDED.list_date,
	            delist_date = EXCLUDED.delist_date
        </if>
    </insert>
    
    <select id="getStockInfoByStockCode" resultType="link.at17.mid.tushare.data.models.StockInfo">
        SELECT
            i.*,
            STRING_AGG(l."name", ', ' ORDER BY l.list_date DESC) AS ths_belongings
        FROM
            stock_info i
            
        
	    LEFT JOIN stock_ths_member m ON (m.con_code = i.ts_code
        <if test="stockCode != null">
            AND m.con_code = #{stockCode}
        </if>
        )
	    LEFT JOIN stock_ths_list l ON l.ts_code = m.ts_code
        WHERE
            1 = 1
        <if test="stockCode != null">
            AND i.ts_code = #{stockCode}
        </if>
        GROUP BY i.ts_code 
        LIMIT 1
    </select>

    <select id="getStockListByListStatus" resultType="link.at17.mid.tushare.data.models.StockInfo">
        SELECT
            i.*,
            STRING_AGG(l."name", ', ' ORDER BY l.list_date DESC) AS ths_belongings
        FROM
        stock_info i
        LEFT JOIN stock_ths_member m ON m.con_code = i.ts_code
        LEFT JOIN stock_ths_list l ON l.ts_code = m.ts_code
        WHERE
            1 = 1
        <if test="listStatus != null">
            AND i.list_status = #{listStatus.listStatus}
        </if>
        GROUP BY i.ts_code 
        ORDER BY i.ts_code ASC
    </select>
    
    <select id="list" resultType="link.at17.mid.tushare.data.models.StockInfo">
        SELECT
            i.*,
            STRING_AGG(l."name", ', ' ORDER BY l.list_date DESC) AS ths_belongings
        FROM
        stock_info i
        LEFT JOIN stock_ths_member m ON m.con_code = i.ts_code
        LEFT JOIN stock_ths_list l ON l.ts_code = m.ts_code
        <if test="ew != null">
          ${ew.customSqlSegment}
        </if>
        GROUP BY i.ts_code 
        ORDER BY i.ts_code ASC
    </select>
    
    <select id="selectList" resultType="link.at17.mid.tushare.data.models.StockInfo">
        SELECT
            i.*,
            STRING_AGG(l."name", ', ' ORDER BY l.list_date DESC) AS ths_belongings
        FROM
        stock_info i
        LEFT JOIN stock_ths_member m ON m.con_code = i.ts_code
        LEFT JOIN stock_ths_list l ON l.ts_code = m.ts_code
        <if test="ew != null">
          ${ew.customSqlSegment}
        </if>
        GROUP BY i.ts_code 
        ORDER BY i.ts_code ASC
    </select>
</mapper>
