package cn.com.ty.lift.ud.userRebate.mapper;


import cn.com.ty.lift.ud.userRebate.mapper.entity.UserRebateEntity;
import cn.com.ty.lift.ud.userRebate.mapper.entity.UserRebateVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 个人返利记录 -Repository
 * 
 * @author Xson
 * @date 2020-05-19
 */
public interface UserRebateMapper extends BaseMapper<UserRebateEntity> {

    /**
     * 查询上一级邀请人
     * @param lowerUserId
     * @return
     */
    @Select("select user_id  from user_rebate where lower_user_id = #{lowerUserId}")
    UserRebateEntity getUpUser(Long lowerUserId);

    /**
     * 查询当前用户的一级下线
     * @param id
     * @return
     */
    @Select("select ur.lower_user_id as userId, " +
                "ui.mobile as mobile, " +
                "ur.vip_fee as vipFee, " +
                "ur.vip_fee_up as vipFeeUp " +
            "from user_rebate ur " +
            "left join user_info ui  on ui.user_id = ur.lower_user_id " +
            "where  ur.user_id = #{id} ")
    List<UserRebateVo> getOneLowerUser(Long id);

    /**
     * 查询当前用户的二级下线
     * @param id
     * @return
     */
    @Select( "select ur.lower_user_id as userId, " +
                "ui.mobile as mobile, " +
                "ur.vip_fee as vipFee, " +
                "ur.vip_fee_up as vipFeeUp " +
            "from user_rebate ur " +
            "left join user_info ui  on ui.user_id = ur.lower_user_id " +
            "where  ur.user_id in (select lower_user_id   from user_rebate ur where  ur.user_id = #{id}) ")
    List<UserRebateVo> getTwoLowerUser(Long id);

    /**
     * 判断当前用户是否是vip或者专家
     * @param userId
     * @return
     */
    @Select("select ui.user_id as userId," +
                "ui.mobile as mobile, " +
                "ulr.id as userLevel " +
            "from user_info ui " +
            "left join user_level_rule ulr on ui.user_level = ulr.id " +
            "where ui.user_id = #{userId} and ui.vip_flag <> 1 " +
            "UNION " +
            "select ui.user_id as userId, " +
                "ui.mobile as mobile, " +
                "ulr.id as userLevel " +
            "from user_info ui " +
            "left join user_account ua on ui.user_id = ua.user_id " +
            "left join user_level_rule ulr on ui.user_level = ulr.id " +
            "where ui.user_id = #{userId} and ua.expert_flag = 1 ")
    UserRebateVo isVipOrExpert(Long userId);

    /**
     * 判断当前用户是否是vip
     * @param userId
     * @return
     */
    @Select("select ui.user_id as userId," +
                "ui.mobile as mobile, " +
                "ulr.id as userLevel " +
            "from user_info ui " +
            "left join user_level_rule ulr on ui.user_level = ulr.id " +
            "where ui.user_id = #{userId} and ui.vip_flag <> 1 and ui.delete_flag  = 0")
    UserRebateVo isVip(Long userId);

    /**
     * 查询当前用户的一级会员下线
     * @param id
     * @return
     */
    @Select("select ur.lower_user_id as userId, " +
                "ui.mobile as mobile " +
            "from user_rebate ur " +
            "left join user_info ui  on ui.user_id = ur.lower_user_id " +
            "where  ur.user_id = #{id} ")
    List<UserRebateVo> getOneLowerVipUser(Long id);

    /**
     * 根据用户ID和下级用户ID修改
     * @param userId
     * @param lowerUserId
     */
    @Update("update user_rebate set vip_fee = #{vipFee} ,vip_fee_up = #{vipFeeUp} " +
            "where user_id = #{userId}  and  lower_user_id = #{lowerUserId}")
    void updateByTwoId(@Param("userId") Long userId,@Param("lowerUserId") Long lowerUserId,
                       @Param("vipFee") BigDecimal vipFee,@Param("vipFeeUp") BigDecimal vipFeeUp);
}
