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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.copy.trade.entity.po.Position">
    <result column="id" property="id" />
    <result column="created_time" property="createdTime" />
    <result column="updated_time" property="updatedTime" />
        <result column="customer_id" property="customerId" />
        <result column="position_id" property="positionId" />
        <result column="trader_id" property="traderId" />
        <result column="symbol" property="symbol" />
        <result column="direction" property="direction" />
        <result column="leverage" property="leverage" />
        <result column="open_price" property="openPrice" />
        <result column="total_position" property="totalPosition" />
        <result column="total_principal_amount" property="totalPrincipalAmount" />
        <result column="available_position" property="availablePosition" />
        <result column="current_position" property="currentPosition" />
        <result column="principal_amount" property="principalAmount" />
        <result column="contract_order_entrust_id" property="contractOrderEntrustId" />
        <result column="status" property="status" />
        <result column="transaction_fee" property="transactionFee" />
        <result column="open_time" property="openTime" />
        <result column="close_time" property="closeTime" />
        <result column="close_price" property="closePrice" />
        <result column="profit_and_loss" property="profitAndLoss" />
        <result column="is_send_close_message" property="isSendCloseMessage" />
        
    </resultMap>
    <resultMap id="followOrderMap" type="com.copy.trade.entity.vo.FollowOrderVo">
        <result column="symbol" property="symbol" />
        <result column="direction" property="direction" />
        <result column="leverage" property="leverage" />
        <result column="total_position" property="totalPosition" />
        <result column="position_id" property="positionId" />
        <result column="trader_id" property="traderId" />
        <result column="open_price" property="openPrice" />
        <result column="open_time" property="openTime" />
        <result column="profit_and_loss" property="profit" />
        <result column="close_time" property="closeTime" />
        <result column="close_price" property="closePrice" />
        <result column="principal_amount" property="principalAmount" />
        <result column="transaction_fee" property="transactionFee" />
        <result column="nickname" property="traderNickname" />
        <result column="avatar" property="traderAvatar" />
        <result column="trader_position_id" property="traderPositionId" />
        <result column="specialTrader" property="specialTrader" />
    </resultMap>

    <sql id="baseFollow">
		SELECT p.position_id,p.symbol,p.direction,p.leverage,p.total_position,p.open_price,p.open_time,p.trader_id,
		       p.profit_and_loss,p.close_time,p.close_price,p.total_principal_amount as principal_amount,p.transaction_fee,p.trader_position_id,
		       c.nickname,c.avatar
		FROM position p 
		LEFT JOIN customer c ON p.trader_id = c.id
    </sql>
    <sql id="baseFollowCurrent">
		SELECT p.position_id,p.symbol,p.direction,p.leverage,p.current_position as total_position,
		       p.open_price,p.open_time,p.profit_and_loss,p.close_time,p.close_price,p.principal_amount,p.transaction_fee,p.trader_position_id,
		       c.nickname,c.avatar,IF(c.trader_level_id = 5,true,false) as specialTrader
		FROM position p 
		LEFT JOIN customer c ON p.trader_id = c.id
    </sql>

    <sql id="basePosition">
        select p.position_id,p.symbol,p.direction,p.leverage,p.total_position,p.open_price,p.open_time,p.profit_and_loss,p.close_time,p.close_price,p.total_principal_amount as principal_amount,p.transaction_fee from position p
    </sql>
    
    <sql id="basePositionCurrent">
        select p.position_id,p.symbol,p.direction,p.leverage,p.current_position as total_position,p.open_price,p.open_time,p.profit_and_loss,p.close_time,p.close_price,p.principal_amount,p.transaction_fee from position p
    </sql>

    <select id="currentFollow" resultMap="followOrderMap">
       <include refid="baseFollowCurrent" />
        <where>
            p.customer_id = #{customerId} AND (p.status=10 OR p.status=20) and p.customer_id != p.trader_id
        </where>
    </select>

    <select id="historyFollow" resultMap="followOrderMap">
        <include refid="baseFollow" />
        <where>
            p.customer_id = #{customerId} AND p.status=30 and p.customer_id != p.trader_id
        </where>
        order by p.close_time desc
    </select>

    <select id="leadCurrent" resultMap="followOrderMap">
        <include refid="basePositionCurrent" />
        <where>
            p.customer_id = #{traderId} AND (p.status=10 OR p.status=20) and p.customer_id = p.trader_id
        </where>
    </select>

    <select id="leadHistory" resultMap="followOrderMap">
        <include refid="baseFollow" />
        <where>
            p.customer_id = #{traderId} AND p.status=30 and p.customer_id = p.trader_id
        </where>
        order by p.close_time desc
    </select>
    
     <select id="selectDividendFee" resultType="java.math.BigDecimal">
     	select sum(dividend_fee) from position 
			<where>
                 trader_id != customer_id and status = 30 and dividend_fee is not null
            <if test="position.traderId != null">
               and trader_id = #{position.traderId}
            </if>
            <if test="position.createdTime != null">
                <![CDATA[  AND updated_time >= #{position.createdTime} ]]>
            </if>
            <if test="position.updatedTime != null">
                <![CDATA[  AND updated_time <= #{position.updatedTime} ]]>
            </if>
             <if test="position.customerId != null">
               and customer_id = #{position.customerId}
            </if>
        </where>
     </select>
     <select id="selectStudentProfit" resultType="java.math.BigDecimal">
     	select sum(profit_and_loss) from position 
			<where>
                 trader_id != customer_id and status = 30 and profit_and_loss is not null
            <if test="position.traderId != null">
               and trader_id = #{position.traderId}
            </if>
            <if test="position.createdTime != null">
                <![CDATA[  AND updated_time >= #{position.createdTime} ]]>
            </if>
            <if test="position.updatedTime != null">
                <![CDATA[  AND updated_time <= #{position.updatedTime} ]]>
            </if>
        </where>
     </select>
</mapper>
