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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.copy.trade.entity.po.Customer">
    <result column="id" property="id" />
    <result column="created_time" property="createdTime" />
    <result column="updated_time" property="updatedTime" />
        <result column="nickname" property="nickname" />
        <result column="avatar" property="avatar" />
        <result column="description" property="description" />
        <result column="trader" property="trader" />
        <result column="max_loss" property="maxLoss" />
        <result column="max_profit" property="maxProfit" />
        <result column="member_id" property="memberId" />
        <result column="automatic_unfollow" property="automaticUnfollow" />
        <result column="trader_level_id" property="traderLevelId" />
        <result column="balance" property="balance" />
        <result column="trader_time" property="traderTime" />
        <result column="min_follow_amount" property="minFollowAmount" />
        <result column="edit_data_switch" property="editDataSwitch" />
        <result column="default_follow_customer" property="defaultFollowCustomer" />
        <result column="default_register_days" property="defaultRegisterDays" />
        <result column="max_principal_amount" property="maxPrincipalAmount" />
    </resultMap>
    
     <resultMap id="traderMap" type="com.copy.trade.entity.vo.TraderTotalVo">
        <result column="id" property="id" />
        <result column="member_id" property="memberId" />
        <result column="nickname" property="nickname" />
        <result column="username" property="username" />
        <result column="levelName" property="levelName" />
        <result column="followTeamBalance" property="followTeamBalance" />
        <result column="following" property="following" />
        <result column="balance" property="balance" />
        <result column="inAmount" property="inAmount" />
        <result column="outAmount" property="outAmount" />
        <result column="commission" property="commission" />
        <result column="totalFollowProfit" property="totalFollowProfit" />
        
        
    </resultMap>
    <resultMap id="customerMap" type="com.copy.trade.entity.vo.CustomerVo">
		<result column="id" property="id" />
    	<result column="created_time" property="createdTime" />
    	<result column="updated_time" property="updatedTime" />
        <result column="nickname" property="nickname" />
        <result column="avatar" property="avatar" />
        <result column="description" property="description" />
        <result column="trader" property="trader" />
        <result column="max_loss" property="maxLoss" />
        <result column="max_profit" property="maxProfit" />
        <result column="member_id" property="memberId" />
        <result column="automatic_unfollow" property="automaticUnfollow" />
        <result column="trader_level_id" property="traderLevelId" />
        <result column="balance" property="balance" />
        <result column="trader_time" property="traderTime" />
        
        <result column="min_follow_amount" property="minFollowAmount" />
        <result column="edit_data_switch" property="editDataSwitch" />
        <result column="default_follow_customer" property="defaultFollowCustomer" />
        <result column="default_register_days" property="defaultRegisterDays" />
        <result column="is_top" property="isTop" />
        <result column="max_principal_amount" property="maxPrincipalAmount" />

    </resultMap>
    
	<select id="listByTraderStatus" resultMap="BaseResultMap">
        SELECT * from customer
        <where>
            trader = #{status}
            ORDER by id DESC
        </where>
    </select>
    
	<select id="traderList" resultMap="customerMap">
        SELECT c.*
        FROM customer c
        <where>
            1 = 1 AND c.trader = 20
            <if test="traderQueryForm.nickname != null and traderQueryForm.nickname !=''">
               AND c.nickname LIKE concat('%',#{traderQueryForm.nickname},'%')
            </if>
            <if test="traderQueryForm.memberId != null and traderQueryForm.memberId !=''">
               AND c.member_id = #{traderQueryForm.memberId}
            </if>
            <if test="traderQueryForm.traderLevelId != null and traderQueryForm.traderLevelId !=''">
               AND c.trader_level_id = #{traderQueryForm.traderLevelId}
            </if>
            <if test="traderQueryForm.isTop != null and traderQueryForm.isTop !=''">
               AND c.is_top = #{traderQueryForm.isTop}
            </if>
        </where>
        ORDER BY c.id DESC
    </select>
    
    <select id="traderTotalList" resultMap="traderMap">
        select c.id,c.nickname,c.member_id,t1.name as levelName from customer c LEFT JOIN trader_level t1 ON c.trader_level_id = t1.id
        <where>
            1 = 1 and c.trader = 20
            <if test="traderQueryForm.nickname != null and traderQueryForm.nickname !=''">
               AND c.nickname LIKE concat('%',#{traderQueryForm.nickname},'%')
            </if>
            <if test="traderQueryForm.memberId != null">
                AND c.member_id = #{traderQueryForm.memberId}
            </if>
            <if test="traderQueryForm.traderLevelId != null and traderQueryForm.traderLevelId !=''">
               AND c.trader_level_id = #{traderQueryForm.traderLevelId}
            </if>
            ORDER BY c.trader_level_id=3 desc, c.trader_time DESC
        </where>
    </select>
    
    <update id="cancelTrader">
        update customer c set c.trader=10, c.trader_level_id =null, c.trader_time = null where c.id = #{traderId}
    </update>
    <update id="updateTraderLevelId">
        update customer c set c.trader_level_id=#{traderLevelId} where c.id = #{traderId}
    </update>
    <update id="updateTraderTop">
        update customer c set c.is_top=#{isTop} where c.id = #{traderId}
    </update>
    <update id="clearMaxPrincipalAmount">
        update customer c set c.max_principal_amount = null where c.id = #{traderId}
    </update>
</mapper>
