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


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

import cn.com.ty.lift.ud.payment.controller.query.UserBillQuery;
import cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntity;
import cn.com.ty.lift.ud.payment.mapper.entity.UserBillEntityVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import  com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * 用户账单流水 -Repository
 * 
 * @author Xson
 * @date 2020-04-23
 */
public interface UserBillMapper extends BaseMapper<UserBillEntity> {

    /**
     * 查询最近一次的余额
     * @return
     * @param expert
     */
    @Select("select balance from user_bill where user_id = #{expert} ORDER BY create_time Desc")
    List<BigDecimal> getLatestBalance(Long expert);
    
    List<UserBillEntity> selectByQuery(@Param("ew") Wrapper wrapper, @Param("page") UserBillQuery query);

    int countByQuery(@Param("ew") Wrapper wrapper);
    
    Double sumByType(@Param("ew") Wrapper wrapper,@Param("type")Integer type);

    /**
     * 根据用户ID和业务类型查询流水
     * @param userId
     * @param businessType
     * @return
     */
    @Select("select * from user_bill WHERE user_id = #{userId}  and business_type  =  #{businessType} ")
    List<UserBillEntity> getByUserIdAndBusinessType(@Param("userId") Long userId, @Param("businessType") Integer businessType);

    /**
     * 根据订单号找到之前的 用户流水记录
     * @param orders
     * @return
     */
    @Select("select * from user_bill WHERE flow_num = #{orders} ")
    UserBillEntity getByFlowNum(@Param("orders") String orders);

    /**
     * 专家收入：打赏 2  出诊问诊收入 4  专家惩罚 9 （撤销惩罚）    之和
     * @param userId
     * @return
     */
    @Select("select sum(amount) as income from user_bill where user_id  = #{userId}  and type = 2 and business_type in (2,4,9)")
    BigDecimal selectIncome(@Param("userId")Long userId);

    /**
     * 专家支出：专家惩罚 9   申诉退款 10  之和
     * @param userId
     * @return
     */
    @Select("select sum(amount) as income from user_bill where user_id  = #{userId}  and type = 1 and business_type in (9,10);")
    BigDecimal selectOutcome(@Param("userId")Long userId);

    UserBillEntityVo getDetail(Long id);
}
