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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.copy.trade.entity.po.ClosePosition">
    <result column="id" property="id" />
    <result column="created_time" property="createdTime" />
    <result column="updated_time" property="updatedTime" />
        <result column="position_id" property="positionId" />
        <result column="entrust_price" property="entrustPrice" />
        <result column="quantity" property="quantity" />
        <result column="entrust_time" property="entrustTime" />
        <result column="status" property="status" />
        <result column="close_type" property="closeType" />
        <result column="type" property="type" />
        <result column="close_position_no" property="closePositionNo" />
        <result column="contract_order_entrust_id" property="contractOrderEntrustId" />
        <result column="copy_contract_order_entrust_id" property="copyContractOrderEntrustId" />
        <result column="transaction_fee" property="transactionFee" />
        <result column="transaction_price" property="transactionPrice" />
        <result column="transaction_amount" property="transactionAmount" />
        <result column="transaction_time" property="transactionTime" />
        <result column="error" property="error" />
    </resultMap>

    <resultMap id="closePositionVo" type="com.copy.trade.entity.vo.ClosePositionVo">

        <result column="customerId" property="customerId" />
        <result column="customerMemberId" property="customerMemberId" />
        <result column="traderMemberId" property="traderMemberId" />
        <result column="traderName" property="traderName" />
        <result column="customerName" property="customerName" />

		<result column="id" property="id" />
    	<result column="created_time" property="createdTime" />
    	<result column="updated_time" property="updatedTime" />
        <result column="position_id" property="positionId" />
        <result column="entrust_price" property="entrustPrice" />
        <result column="quantity" property="quantity" />
        <result column="entrust_time" property="entrustTime" />
        <result column="status" property="status" />
        <result column="close_type" property="closeType" />
        <result column="type" property="type" />
        <result column="close_position_no" property="closePositionNo" />
        <result column="contract_order_entrust_id" property="contractOrderEntrustId" />
        <result column="copy_contract_order_entrust_id" property="copyContractOrderEntrustId" />
        <result column="transaction_fee" property="transactionFee" />
        <result column="transaction_price" property="transactionPrice" />
        <result column="transaction_amount" property="transactionAmount" />
        <result column="transaction_time" property="transactionTime" />
        <result column="error" property="error" />
    </resultMap>


    <select id="page" resultMap="closePositionVo">
		select c1.id as customerId,c1.nickname as customerName, c1.member_id as customerMemberId,c2.nickname as traderName, c2.member_id as traderMemberId,
			cp.id,cp.created_time,cp.updated_time,cp.position_id,cp.entrust_price,cp.quantity,cp.entrust_time,cp.status,cp.close_type,cp.type,cp.close_position_no,
			cp.contract_order_entrust_id,cp.copy_contract_order_entrust_id,cp.transaction_fee,cp.transaction_price,cp.transaction_amount,cp.transaction_time,cp.error
		from close_position cp force index(PRI)
		LEFT JOIN position p on p.position_id = cp.position_id
		LEFT JOIN customer c1 on p.customer_id = c1.id
		LEFT JOIN customer c2 on p.trader_id = c2.id
        <where>
            p.customer_id &lt;&gt; p.trader_id 
            <if test="closePositionForm.customerMemberId != null">
               AND c1.member_id = #{closePositionForm.customerMemberId}
            </if>
            <if test="closePositionForm.traderMemberId != null">
               AND c2.member_id = #{closePositionForm.traderMemberId}
            </if>
            <if test="closePositionForm.positionId != null">
               AND cp.position_id = #{closePositionForm.positionId}
            </if>
            <if test="closePositionForm.closeType != null">
               AND cp.close_type = #{closePositionForm.closeType}
            </if>
            ORDER by cp.id DESC
        </where>
    </select>
</mapper>
