<?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="max_principal_amount" property="maxPrincipalAmount" />
    </resultMap>

    <resultMap id="traderMap" type="com.copy.trade.entity.vo.TraderVo">
        <result column="id" property="id" />
        <result column="member_id" property="memberId" />
        <result column="nickname" property="nickname" />
        <result column="avatar" property="avatar" />
        <result column="description" property="description" />
        <result column="min_follow_amount" property="minFollowAmount" />
        <result column="win_rate_14" property="winRate14" />
        <result column="day_yield_14" property="dayYield14" />
        <result column="order_deal_14" property="orderDeal14" />
        <result column="retraction_rate_14" property="retractionRate14"/>
        <result column="follow_customer" property="followCustomer" />
        <result column="profit_amount" property="profitAmount" />
        <result column="trading_days" property="tradingDays" />
        <result column="profit_days" property="profitDays" />
        <result column="day_yield_30" property="dayYield30" />
        <result column="current_month_follow" property="currentMonthFollow" />
        <result column="maxFollow" property="maxFollow" />
        <result column="balance" property="balance" />
        <result column="registerDays" property="registerDays" />
        <result column="trader_level_id" property="traderLevelId" />
        <result column="levelName" property="levelName" />
        <result column="isFollow" property="isFollow" />
        <result column="team_profit14" property="teamProfit14" />
        <result column="default_follow_customer" property="defaultFollowCustomer" />
        <result column="default_register_days" property="defaultRegisterDays" />
        <result column="is_top" property="isTop" />
        <result column="dividend_percent" property="dividendPercent" />
    </resultMap>

    <select id="traderList" resultMap="traderMap">
        SELECT c.id,c.member_id,c.nickname,c.trader_level_id,c.avatar,c.balance,DATEDIFF(NOW(),c.trader_time)+1 as registerDays,c.description,c.min_follow_amount,c.default_follow_customer,c.default_register_days,tl.name as levelName,tl.max_follow as maxFollow,tl.dividend_percent,td.win_rate_14,td.day_yield_14,td.day_yield_30,
        td.profit_amount, td.trading_days,
        td.order_deal_14,td.retraction_rate_14,td.follow_customer,td.team_profit14,(td.win_rate_14+td.day_yield_14) AS zonghe
        <!-- ,(CASE WHEN f.status=10 then 1 else 0 end) as isFollow -->
         FROM customer c LEFT JOIN trader_data td ON c.id = td.trader_id
        LEFT JOIN trader_level tl ON c.trader_level_id = tl.id
        <!-- LEFT JOIN follow f ON c.id = f.trader_id -->
        <where>
            1 = 1 and c.trader = 20
            AND (select count(*) from trader_symbol where trader_id=c.id) > 0
            <if test="traderQueryForm.nickName != null and traderQueryForm.nickName !=''">
               AND c.nickname LIKE concat('%',#{traderQueryForm.nickName},'%')
            </if>
           <!-- <if test="traderQueryForm.customerId != null">
                AND f.customer_id = #{traderQueryForm.customerId}
            </if>-->
            <choose>
		        <when test="traderQueryForm.traderType == 0">
		            AND c.trader_level_id &lt; 4
		        </when>
		        <when test="traderQueryForm.traderType == 1">
		            AND c.trader_level_id &gt;= 4
		        </when>
		        <otherwise>
		            <!-- No condition applied, returns all users -->
		        </otherwise>
		    </choose>
        </where>
        <choose>
            <when test="traderQueryForm.orderBy == 20">
                ORDER BY td.day_yield_14 DESC
            </when>
            <when test="traderQueryForm.orderBy == 30">
                ORDER BY td.win_rate_14 DESC
            </when>
            <otherwise>
                ORDER BY c.trader_level_id>3 desc, zonghe DESC
            </otherwise>
        </choose>
    </select>

    <select id="topTraderList" resultMap="traderMap">
        SELECT c.id,c.member_id,c.nickname,c.trader_level_id,c.avatar,c.balance,DATEDIFF(NOW(),c.trader_time)+1 as registerDays,c.description,c.min_follow_amount,c.default_follow_customer,c.default_register_days,c.is_top,tl.name as levelName,tl.max_follow as maxFollow,tl.dividend_percent,td.win_rate_14,td.day_yield_14,td.day_yield_30,
        td.profit_amount, td.trading_days,
        td.order_deal_14,td.retraction_rate_14,td.follow_customer,td.team_profit14,(td.win_rate_14+td.day_yield_14) AS zonghe
        FROM customer c LEFT JOIN trader_data td ON c.id = td.trader_id
        LEFT JOIN trader_level tl ON c.trader_level_id = tl.id
        <where>
            1 = 1 AND c.trader = 20
            AND (select count(*) from trader_symbol where trader_id=c.id) > 0
            <if test="traderQueryForm.isTop != null">
                AND c.is_top = #{traderQueryForm.isTop}
            </if>
            <if test="traderQueryForm.nickName != null and traderQueryForm.nickName !=''">
               AND c.nickname LIKE concat('%',#{traderQueryForm.nickName},'%')
            </if>
            <choose>
                <when test="traderQueryForm.traderType == 0">
                    AND c.trader_level_id &lt; 4
                </when>
                <when test="traderQueryForm.traderType == 1">
                    AND c.trader_level_id &gt;= 4
                </when>
                <otherwise>
                    <!-- No condition applied, returns all traders -->
                </otherwise>
            </choose>
        </where>
        <choose>
            <when test="traderQueryForm.orderBy == 10">
                ORDER BY c.trader_level_id>3 desc, zonghe DESC
            </when>
            <when test="traderQueryForm.orderBy == 20">
                ORDER BY td.day_yield_14 DESC
            </when>
            <when test="traderQueryForm.orderBy == 30">
                ORDER BY td.win_rate_14 DESC
            </when>
            <otherwise>
                ORDER BY td.day_yield_14 DESC
            </otherwise>
        </choose>
    </select>

     <select id="traderInfo" resultMap="traderMap">
        SELECT c.id,c.member_id,c.nickname,c.avatar,c.balance,DATEDIFF(NOW(),c.trader_time)+1 as registerDays,c.description,c.min_follow_amount,c.trader_level_id,c.default_follow_customer,c.default_register_days,tl.name AS levelName,tl.max_follow AS maxFollow,tl.dividend_percent,
        td.win_rate_14,td.day_yield_14,td.order_deal_14,td.retraction_rate_14,td.follow_customer,
        td.profit_amount,td.trading_days,td.profit_days,td.day_yield_30,td.current_month_follow,td.team_profit14
         FROM customer c
        LEFT JOIN trader_level tl ON c.trader_level_id = tl.id
        LEFT JOIN trader_data td ON  c.id = td.trader_id
        WHERE c.id = #{traderId} AND trader=20
    </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>
</mapper>
