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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.copy.trade.entity.po.CopiedTrade">
    <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="trader_id" property="traderId" />
        <result column="entrust_no" property="entrustNo" />
        <result column="contract_order_entrust_id" property="contractOrderEntrustId" />
        <result column="symbol" property="symbol" />
        <result column="contract_symbol_id" property="contractSymbolId" />
        <result column="order_type" property="orderType" />
        <result column="direction" property="direction" />
        <result column="entrust_price" property="entrustPrice" />
        <result column="trigger_price" property="triggerPrice" />
        <result column="leverage" property="leverage" />
        <result column="type" property="type" />
        <result column="volume" property="volume" />
        <result column="stop_loss_price" property="stopLossPrice" />
        <result column="stop_profit_price" property="stopProfitPrice" />
        <result column="copy_contract_order_entrust_id" property="copyContractOrderEntrustId" />
        <result column="principal_amount" property="principalAmount" />
        <result column="status" property="status" />
        <result column="entrust_time" property="entrustTime" />
        <result column="error" property="error" />
        <result column="open_price" property="openPrice" />
        <result column="position_id" property="positionId" />
    </resultMap>
    <resultMap id="CopiedTradeVo" type="com.copy.trade.entity.vo.CopiedTradeVo">
    	<result column="id" property="id" />
    	<result column="created_time" property="createdTime" />
        <result column="customer_id" property="customerId" />
        <result column="trader_id" property="traderId" />
        <result column="symbol" property="symbol" />
        <result column="direction" property="direction" />
        <result column="leverage" property="leverage" />
        <result column="volume" property="volume" />
        <result column="principal_amount" property="principalAmount" />
        <result column="entrust_time" property="entrustTime" />
        <result column="trigger_price" property="openPrice" />
        <result column="customerName" property="customerName" />
        <result column="customerMemberId" property="customerMemberId" />
        <result column="traderName" property="traderName" />
        <result column="traderMemberId" property="traderMemberId" />
    </resultMap>
    <select id="sumPrincipalAmount" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(principal_amount),0) FROM copied_trade WHERE customer_id=#{customerId} AND trader_id=#{traderId} AND status=0;
    </select>
    <select id="page" resultMap="CopiedTradeVo">
        SELECT t.id,t.customer_id,t.trader_id,t.created_time,t.symbol,t.leverage,t.direction,t.volume,t.principal_amount,t.entrust_time
        ,t.trigger_price, 
        c1.nickname as customerName, c1.member_id as customerMemberId, c2.nickname as traderName, c2.member_id as traderMemberId 
        
        from copied_trade t left join customer c1 on t.customer_id=c1.id
        left join customer c2 on t.trader_id=c2.id
        <where>
            t.customer_id &lt;&gt; t.trader_id
            <if test="copiedTradeForm.customerMemberId != null">
               AND c1.member_id = #{copiedTradeForm.customerMemberId}
            </if>
            <if test="copiedTradeForm.traderMemberId != null">
               AND c2.member_id = #{copiedTradeForm.traderMemberId}
            </if>
            <if test="copiedTradeForm.customerName != null and copiedTradeForm.customerName !=''">
               AND c1.nickname LIKE concat('%',#{copiedTradeForm.customerName},'%')
            </if>
            <if test="copiedTradeForm.traderName != null and copiedTradeForm.traderName !=''">
               AND c2.nickname LIKE concat('%',#{copiedTradeForm.traderName},'%')
            </if>
            ORDER by t.id DESC
        </where>
    </select>

</mapper>
