<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.copy.trade.dao.StatisticsDataMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.copy.trade.entity.po.StatisticsData">
    <result column="id" property="id" />
    <result column="created_time" property="createdTime" />
    <result column="updated_time" property="updatedTime" />
        <result column="date_time" property="dateTime" />
        <result column="customer_id" property="customerId" />
        <result column="balance" property="balance" />
        <result column="earnings" property="earnings" />
        <result column="principal_amount" property="principalAmount" />
        <result column="open_position" property="openPosition" />
        <result column="profit_order_count" property="profitOrderCount" />
        <result column="day_yield_14" property="dayYield14" />
    </resultMap>

    <select id="dataCount" resultMap="BaseResultMap">
        SELECT IFNULL(SUM(earnings),0) AS earnings,IFNULL(SUM(principal_amount),0) AS principal_amount,IFNULL(SUM(open_position),0) AS open_position,IFNULL(SUM(profit_order_count),0) AS profit_order_count FROM statistics_data WHERE customer_id = #{traderId} AND <![CDATA[date_time >= #{startTime}]]>
    </select>
    
    <select id="selectStatisticsData" resultMap="BaseResultMap">
        SELECT date_time,balance,earnings,principal_amount  FROM statistics_data WHERE customer_id = #{traderId} AND <![CDATA[date_time > #{startTime}]]>  order by date_time
    </select>
    
    <select id="selectStatisticsDataNew" resultType="java.math.BigDecimal">
        SELECT day_yield_14  FROM statistics_data WHERE customer_id = #{traderId} AND <![CDATA[date_time >= #{startTime}]]>  order by date_time
    </select>
    
    <select id="selectStatisticsDataForRetractionRate14" resultMap="BaseResultMap">
        SELECT * FROM statistics_data WHERE customer_id = #{traderId} AND <![CDATA[date_time >= #{startTime}]]>  order by date_time
    </select>

    <select id="earningsCount" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(earnings),0)  FROM statistics_data WHERE customer_id = #{traderId}
    </select>
    <select id="balanceSort" resultType="java.math.BigDecimal">
        SELECT
        <if test="sort == 'min'">
            MIN(balance)
        </if>
        <if test="sort == 'max'">
            MAX(balance)
        </if>
        FROM statistics_data WHERE customer_id = #{traderId} and  <![CDATA[date_time >= #{startTime}]]>  
    </select>
    
    <select id="traderDataList" resultMap="BaseResultMap">
        	select t.* from statistics_data t 
			LEFT JOIN customer c on c.id = t.customer_id
        <where>
            1 = 1 and c.trader = 20 and c.edit_data_switch =1  AND t.customer_id = #{traderId}
            ORDER by t.date_time DESC limit 0,14
        </where>
    </select>
    
    <select id="recentStatisticsData" resultMap="BaseResultMap">
        	select * from statistics_data where id = (select max(id) from statistics_data  where customer_id = #{traderId})
    </select>
    
    <update id="updateMaxDateDate" >
	    update statistics_data  set day_yield_14 = #{yield14} 
			where id in(
			select a.id from (select max(id) as id from statistics_data where customer_id = #{traderId}) a)
    
    </update>
</mapper>
