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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.copy.trade.entity.po.Follow">
    <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="status" property="status" />
    </resultMap>
    
    <resultMap id="follower" type="com.copy.trade.entity.vo.FollowerVo">
    	<result column="id" property="id" />
    	<result column="member_id" property="memberId" />
        <result column="nickname" property="nickname" />
        <result column="updated_time" property="followTime" />
    </resultMap>
    
    <resultMap id="followerAgentVo" type="com.copy.trade.entity.vo.FollowerAgentVo">
    	<result column="id" property="id" />
    	<result column="member_id" property="memberId" />
        <result column="nickname" property="nickname" />
    	<result column="teacherId" property="teacherId" />
    	<result column="teacherMemberId" property="teacherMemberId" />
        <result column="teacherNickname" property="teacherNickname" />
        <result column="updated_time" property="followTime" />
    </resultMap>
	
	<select id="followering" resultType="java.lang.Long">
       select c.member_id from follow f LEFT JOIN customer c on f.customer_id = c.id   
        <where>
            1 = 1 and f.trader_id =#{traderId}  and status = 10  
        </where>
    </select>
    
<!--     <select id="followers" resultMap="follower">
        SELECT c.id,c.nickname,c.member_id,f.updated_time  FROM follow f LEFT JOIN customer c ON f.customer_id = c.id
        <where>
            1 = 1 
            <if test="studentPositionForm.traderId != null">
               and f.trader_id = #{studentPositionForm.traderId} AND f.status = 10
            </if>
            ORDER BY f.updated_time DESC
        </where>
    </select> -->
    <select id="followers" resultMap="follower">
        SELECT c.id,c.nickname,c.member_id,f.updated_time  FROM follow f LEFT JOIN customer c ON f.customer_id = c.id
        <where>
            1 = 1 
            <if test="studentPositionForm.traderId != null">
               and f.trader_id = #{studentPositionForm.traderId} 
            </if>
            <if test="studentPositionForm.memberId != null">
               and c.member_id = #{studentPositionForm.memberId} 
            </if>
            <if test="studentPositionForm.nickname != null and studentPositionForm.nickname !=''">
               and c.nickname = #{studentPositionForm.nickname} 
            </if>
            <if test="studentPositionForm.status != null">
               AND f.status = #{studentPositionForm.status}
            </if>
        	<if test="studentPositionForm.hasPosition == true">
        		and f.customer_id in(select DISTINCT(customer_id) from position  p where p.trader_id = #{studentPositionForm.traderId} 
        			and p.customer_id != p.trader_id and  (p.status =20 or p.status = 10)) 
        	</if>            
        	ORDER BY f.updated_time DESC
        </where>
    </select>
    
    <select id="followersAgent" resultMap="followerAgentVo">
        SELECT c1.id,c1.nickname,c1.member_id,c2.id as teacherId,c2.nickname as teacherNickname,c2.member_id as teacherMemberId,
	        f.updated_time  
	        FROM follow f 
	        LEFT JOIN customer c1 ON f.customer_id = c1.id
	        LEFT JOIN customer c2 ON f.trader_id = c2.id
        <where>
            1 = 1 
            <if test="agentStudentPositionForm.teacherIds != null">
              and f.trader_id in 
	          <foreach item="item" collection="agentStudentPositionForm.teacherIds" index="idx" open="(" separator="," close=")">
	                #{item}
	          </foreach>
            </if>
            <if test="agentStudentPositionForm.status != null">
               AND f.status = #{agentStudentPositionForm.status}
            </if>
        	ORDER BY f.updated_time DESC
        </where>
    </select>


</mapper>
