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

import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoDto;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserRewardListVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import cn.com.ty.lift.ud.operation.mapper.entity.VipFeeSettingEntity;
import cn.com.ty.lift.ud.userInfo.controller.query.UserInfoQuery;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import org.apache.ibatis.annotations.Select;

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

/**
 * 用户表 -Repository
 *
 * @author Xson
 * @date 2020-05-09
 */
public interface UserInfoMapper extends BaseMapper<UserInfoEntity> {

    List<UserInfoEntity> selectByQuery(@Param("ew") Wrapper wrapper, @Param("page") UserInfoQuery page);

    int countByQuery(@Param("ew") Wrapper wrapper);

    @Select("SELECT * FROM user_info WHERE user_id=#{id}")
    UserInfoEntity getByUserId(@Param("id") Long id);

    /**
     * 获取用户详情
     * @param id
     * @return
     */
    @Select("SELECT rule.name as roleName ,info.* FROM user_info info left join user_level_rule rule " +
            " on rule.id = info.user_level WHERE user_id= #{id}")
    UserInfoEntity getUserInfo(@Param("id") Long id);

    @Select("SELECT *,IF(show_which_name=2,IFNULL(real_name,NAME),NAME)`name` FROM user_info LEFT JOIN user_account on user_info.user_id=user_account.user_id " +
            "WHERE user_account.expert_flag=1 and user_info.delete_flag=0 and user_info.statuz=1 " +
            " ORDER BY user_info.platform_invited_flag ASC ")
    List<UserInfoEntity> queryAllExpert();

    @Update("UPDATE user_info SET apply_flag=#{falg},user_level_name=#{name},service_fee=#{serviceFee}," +
            "qr_code_url=#{qrUrl},request_code=#{code},statuz=1,expert_level=1,platform_invited_flag=1 WHERE user_id=#{id} ")
    void examineById(@Param("id") Long id, @Param("falg") Integer falg,@Param("serviceFee")BigDecimal serviceFee,
                     @Param("code")Integer code,@Param("qrUrl") String qrUrl,@Param("name")String name);

    @Select("SELECT * FROM user_info WHERE mobile=#{mobile}")
    List<UserInfoEntity> getByMobile(@Param("mobile") String mobile);

    @Update("UPDATE user_info SET balance=balance-#{money} WHERE user_id=#{userId} ")
    int updateUserBalance(@Param("userId") Long userId, @Param("money") BigDecimal money);

    /**
     * 余额减少，冻结余额增加
     * @param userId
     * @param money
     * @return
     */
    @Update("UPDATE user_info SET balance=balance-#{money},freeze_balance=freeze_balance + #{money}  " +
            " WHERE user_id=#{userId} ")
    int updateExpertBalance(@Param("userId") Long userId, @Param("money") BigDecimal money);

    @Update("UPDATE user_info SET balance=balance + #{money} WHERE user_id=#{userId} ")
    int rechargePayBalance(@Param("userId") Long userId, @Param("money") BigDecimal money);

    /**
     * 附近的人
     *
     * @return
     */
    List<UserInfoVo> getNearUser(UserInfoDto param);

    /**
     * 删除用户信息表信息
     * 
     * @param id
     */
    @Update("update user_info set delete_flag = 1 where user_id = #{id}")
    void deleteUserInfo(Long id);

    /**
     * 用户列表
     * 
     * @param wrapper
     * @param page
     * @return
     */
    List<UserInfoEntity> selectUserList(@Param("ew") Wrapper wrapper, @Param("page") UserInfoQuery page);

    /**
     * 用户列表总数
     * 
     * @param wrapper
     * @return
     */
    int countUserList(@Param("ew") Wrapper wrapper);

    /**
     * 修改用户信息
     * 
     * @param userInfoEntity
     * @return
     */
    Integer updateUserInfo(UserInfoEntity userInfoEntity);

    /**
     * 修改用户信息
     * 
     * @param userInfoEntity
     * @return
     */
    Integer updateUserInfoByUserId(UserInfoEntity userInfoEntity);

    /**
     * 修改专家总积分
     * 
     * @param nowTotalScore
     * @param expertId
     * @param expertLevel
     */
    @Update("update user_info set level_total_score =  #{nowTotalScore}" +
            " ,expert_level = #{expertLevel} where user_id = #{expertId}")
    void addExpertScore(@Param("nowTotalScore") Integer nowTotalScore, @Param("expertId") Long expertId ,@Param("expertLevel") Integer expertLevel);

    /**
     * 升级为vip用户
     * @param userId
     * @param userLevel
     * @param requestCode   邀请码
     * @param vipStartTime
     * @param vipEndTime
     * @param period 有效期，单位月
     * @param vipLevel
     *  @return
     */
    @Update("update user_info set vip_flag =2 , user_level=#{userLevel}, " +
            " user_level_name=#{userLevelName},request_code = #{requestCode} ,vip_start_time = #{vipStartTime} " +
            " ,vip_end_time = #{vipEndTime},vip_validity = #{period}" +
            "  ,vip_level = #{vipLevel} where user_id = #{userId}")
    Integer toVip(@Param("userLevel") Long userLevel,
                  @Param("userLevelName")String userLevelName, @Param("requestCode")Integer requestCode,
                  @Param("vipStartTime")LocalDateTime vipStartTime, @Param("vipEndTime")LocalDateTime vipEndTime,
                  @Param("period")Integer period, @Param("vipLevel")Long vipLevel,@Param("userId") Long userId);



    /**
     * 取消会员
     *
     * @param id
     * @param userLevel
     * @param vipEndTime
     * @param userLevelName
     * @return
     */
    @Update("update user_info set vip_flag =1 , user_level = #{userLevel},request_code = null," +
            " vip_end_time  = #{vipEndTime} , vip_level = null ,user_level_name = #{userLevelName} where user_id = #{id}")
    Integer cancelVip(@Param("id") Long id,@Param("userLevel") Long userLevel,
                      @Param("vipEndTime") LocalDateTime vipEndTime,@Param("userLevelName") String userLevelName);



    /**
     *
     * @param mobile
     * @return
     */
    @Select("select * from user_info  where mobile = #{mobile}")
    UserInfoEntity getUserByMobile(String mobile);

    /**
     * 解冻用户信息表信息
     * 
     * @param id
     */
    @Update("update user_info set delete_flag = 0 where user_id = #{id}")
    void noDeleteUserInfo(Long id);

    /**
     * 查询所有过期的vip
     * @return
     */
    @Select("select * from user_info where vip_flag = 2 and vip_end_time < NOW()")
    List<UserInfoEntity> getOverdueVip();

    /**
     * 定时任务取消会员
     * @param userLevel
     * @param userId
     * @param userLevelName
     * @return
     */
    @Update("update user_info SET request_code = null ,vip_flag = 1 , user_level = #{userLevel} " +
            "   ,vip_level = null ,user_level_name = #{userLevelName}  where user_id = #{userId}")
    void cancelOverTimeVip(@Param("userId") Long userId,@Param("userLevel") Long  userLevel,
                           @Param("userLevelName") String userLevelName);

    /**
     * 根据userId 查询会费信息
     * @param userId
     * @return
     */
    @Select("select vf.* from user_info ui  left join vip_fee_setting vf on vf.id = ui.vip_level" +
            " where ui.user_id = #{userId} ")
    VipFeeSettingEntity getVipByUserId(Long userId);

    /**
     * 根据userLevel 查询用户信息
     * @param userLevel
     * @return
     */
    @Select("select * from user_info where user_level = #{userLevel}")
    List<UserInfoEntity> getByUserLevel(Long userLevel);

    /**
     * 修改所有的 UserLevelName
     * @param userLevelName
     * @param OldUserLevelName
     */
    @Update("update user_info set user_level_name = #{userLevelName} where user_level_name = #{OldUserLevelName}")
    void updateUserLevelName(@Param("userLevelName") String userLevelName,@Param("OldUserLevelName") String OldUserLevelName);

    /**
     * 撤销专家处罚
     * @param userId
     * @param money
     * @return
     */
    @Update("UPDATE user_info SET balance=balance + #{money},freeze_balance = freeze_balance - #{money} " +
            "  WHERE user_id=#{userId} ")
    int addExpertBalance(@Param("userId")Long userId, @Param("money")BigDecimal money);
    
    @Update("UPDATE user_info SET user_level_name=#{name},request_code=NULL where user_id=#{userId}")
    void updateByLevelName(@Param("name")String name,@Param("userId")Long userId);
    
    @Update("UPDATE user_info SET average_score=#{avsore} where user_id=#{userId}")
    void updateAverageScore(@Param("userId")Long id,@Param("avsore")Integer avsore);
    
    @Select("select * from user_info where maintenance_company_id = #{companyId}")
    List<UserInfoEntity>getUserByCompanyId(@Param("companyId")Long companyId);


    /**
     * 案例中心-专家列表
     * @return
     */
    List<UserInfoEntity> listGroupExpert();
    /**
     * 案例中心-热门专家
     * @return
     */
    List<UserInfoEntity> listGroupHotExpert();

    /**
     * 获取专家被打赏次数
     * @return
     */
    int getExpertRewardedCount(Long expert);

    /**
     * 获取用户打赏清单
     * @param userId
     * @return
     */
    List<UserRewardListVo> getUserRewardList(Long userId);
}
