package cn.pluss.platform.user.impl;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserLevel;
import cn.pluss.platform.enums.UserRoleEnum;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.UserLevelMapper;
import cn.pluss.platform.user.UserLevelService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.UserAppVO;
import cn.pluss.platform.vo.UserLevelVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 用户等级表 服务实现类
 * </p>
 *
 * @author bzg
 * @since 2022-01-13
 */
@Service
public class UserLevelServiceImpl extends ServiceImpl<UserLevelMapper, UserLevel> implements UserLevelService {

    @Resource
    private UserAppService userAppService;


    @Override
    public UserLevel getLevelInfoByRoleLevel(String roleCode, String levelCode) {
        QueryWrapper<UserLevel> query = new QueryWrapper<UserLevel>().eq("roleCode",roleCode)
                .eq("levelCode",levelCode);
        return this.baseMapper.selectOne(query);
    }

    /**
     * app 接口 获取用户等级配偶
     * @date: 2022/1/25 14:54
     * @return cn.pluss.platform.api.Result<java.lang.Object>
     */
    @Override
    public Result<Object> getAppLevelInfo() {
        UserApp userApp = userAppService.queryUserAppByToken();
        MsgException.checkNull(userApp,"未知请求！");
        MsgException.check(UserRoleEnum.ZY_BST.getCode().equals(userApp.getRoleCode()),"直营团队用户暂不支持当前请求！");
        UserAppVO vo = new UserAppVO();
        Converter.INSTANCE.cp(userApp,vo);
        if(StringUtil.isNotEmpty(userApp.getLevelCode())){
            if(!userApp.getLevelCode().equals(Constant.USER_MAX_LEVEL_CODE)){
                vo.setLevelName(userApp.getLevelCode().replace("LV","VIP"));
            }else{
                vo.setLevelName("SVIP");
            }
        }
        Integer status = 1;
        if(Constant.USER_MAX_LEVEL_CODE.equals(vo.getLevelCode())){
            status = 2;
        }
        List<UserLevelVO> list = getLevelInfoByRole(userApp.getRoleCode(),status);
        vo.setLevelVoList(list);
        return ResultGenerator.genSuccessResult("获取成功",vo);
    }

    @Override
    public List<UserLevelVO> getLevelInfoByRole(String roleCode,Integer type) {
        return this.baseMapper.getLevelInfoByRole(roleCode,type);
    }

    /**
     * 获取当前交易额度所在的等级
     * @date: 2022/1/25 23:42
     * @param teamTransFlow:
     * @return cn.pluss.platform.entity.UserLevel
     */
    @Override
    public UserLevel getNowQuotaLevel(BigDecimal teamTransFlow) {
        return baseMapper.getNowQuotaLevel(teamTransFlow);
    }
}
