/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.system.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictCache;
import org.springblade.common.cache.ParamCache;
import org.springblade.common.cache.SysCache;
import org.springblade.common.cache.UserCache;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.constant.TenantConstant;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.BladeTenantProperties;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.*;
import org.springblade.modules.api.service.IApiService;
import org.springblade.modules.auth.enums.UserEnum;
import org.springblade.modules.platform.service.UserAppService;
import org.springblade.modules.system.entity.*;
import org.springblade.modules.system.excel.UserExcel;
import org.springblade.modules.system.mapper.UserMapper;
import org.springblade.modules.system.service.*;
import org.springblade.modules.system.vo.UserAppVO;
import org.springblade.modules.system.vo.UserLvVo;
import org.springblade.modules.system.vo.UserVO;
import org.springblade.modules.system.wrapper.UserWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static org.springblade.common.constant.CommonConstant.DEFAULT_PARAM_PASSWORD;

/**
 * 服务实现类
 *
 * @author Chill
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {
	private static final String GUEST_NAME = "guest";

	@Resource
	private IUserDeptService userDeptService;
	@Resource
	private IUserOauthService userOauthService;
	@Resource
	private IRoleService roleService;
	@Resource
	private IApiService apiService;
	@Resource
	private BladeTenantProperties tenantProperties;

	@Resource
	private UserAppService userAppService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean submit(User user) {
		if (StringUtil.isBlank(user.getTenantId())) {
			user.setTenantId(BladeConstant.ADMIN_TENANT_ID);
		}
		String tenantId = user.getTenantId();
		Tenant tenant = SysCache.getTenant(tenantId);
		if (Func.isNotEmpty(tenant)) {
			Integer accountNumber = tenant.getAccountNumber();
			if (tenantProperties.getLicense()) {
				String licenseKey = tenant.getLicenseKey();
				String decrypt = DesUtil.decryptFormHex(licenseKey, TenantConstant.DES_KEY);
				accountNumber = JsonUtil.parse(decrypt, Tenant.class).getAccountNumber();
			}
			Integer tenantCount = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getTenantId, tenantId));
			if (accountNumber != null && accountNumber > 0 && accountNumber <= tenantCount) {
				throw new ServiceException("当前租户已到最大账号额度!");
			}
		}
		if (Func.isNotEmpty(user.getPassword())) {
			user.setPassword(DigestUtil.encrypt(user.getPassword()));
		}
		Integer userCount = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getTenantId, tenantId).eq(User::getAccount, user.getAccount()));
		if (userCount > 0 && Func.isEmpty(user.getId())) {
			throw new ServiceException(StringUtil.format("当前用户 [{}] 已存在!", user.getAccount()));
		}
		return save(user) && submitUserDept(user);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateUser(User user) {
		String tenantId = user.getTenantId();
		Integer userCount = baseMapper.selectCount(
			Wrappers.<User>query().lambda()
				.eq(User::getTenantId, tenantId)
				.eq(User::getAccount, user.getAccount())
				.notIn(User::getId, user.getId())
		);
		if (userCount > 0) {
			throw new ServiceException(StringUtil.format("当前用户 [{}] 已存在!", user.getAccount()));
		}
		return updateUserInfo(user) && submitUserDept(user);
	}

	@Override
	public boolean updateUserInfo(User user) {
		user.setPassword(null);
		return updateById(user);
	}

	private boolean submitUserDept(User user) {
		List<Long> deptIdList = Func.toLongList(user.getDeptId());
		List<UserDept> userDeptList = new ArrayList<>();
		deptIdList.forEach(deptId -> {
			UserDept userDept = new UserDept();
			userDept.setUserId(user.getId());
			userDept.setDeptId(deptId);
			userDeptList.add(userDept);
		});
		userDeptService.remove(Wrappers.<UserDept>update().lambda().eq(UserDept::getUserId, user.getId()));
		return userDeptService.saveBatch(userDeptList);
	}

	@Override
	public IPage<User> selectUserPage(IPage<User> page, User user, Long deptId, String tenantId) {
		List<Long> deptIdList = SysCache.getDeptChildIds(deptId);
		return page.setRecords(baseMapper.selectUserPage(page, user, deptIdList, tenantId));
	}

	@Override
	public IPage<UserVO> selectUserSearch(UserVO user, Query query) {
		LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>query().lambda();
		if (StringUtil.isNotBlank(user.getName())) {
			queryWrapper.like(User::getName, user.getName());
		}
		if (StringUtil.isNotBlank(user.getDeptName())) {
			String deptIds = SysCache.getDeptIdsByFuzzy(AuthUtil.getTenantId(), user.getDeptName());
			if (StringUtil.isNotBlank(deptIds)) {
				queryWrapper.and(wrapper -> {
					List<String> ids = Func.toStrList(deptIds);
					ids.forEach(id -> wrapper.like(User::getDeptId, id).or());
				});
			}
		}
		if (StringUtil.isNotBlank(user.getPostName())) {
			String postIds = SysCache.getPostIdsByFuzzy(AuthUtil.getTenantId(), user.getPostName());
			if (StringUtil.isNotBlank(postIds)) {
				queryWrapper.and(wrapper -> {
					List<String> ids = Func.toStrList(postIds);
					ids.forEach(id -> wrapper.like(User::getPostId, id).or());
				});
			}
		}
		IPage<User> pages = this.page(Condition.getPage(query), queryWrapper);
		return UserWrapper.build().pageVO(pages);
	}

	@Override
	public User userByPhone(String tenantId, String phone) {
		return baseMapper.selectOne(Wrappers.<User>query().lambda().eq(User::getTenantId, tenantId).eq(User::getPhone, phone).eq(User::getIsDeleted, BladeConstant.DB_NOT_DELETED));
	}

	@Override
	public User userByAccount(String tenantId, String account) {
		return baseMapper.selectOne(Wrappers.<User>query().lambda().eq(User::getTenantId, tenantId).eq(User::getAccount, account).eq(User::getIsDeleted, BladeConstant.DB_NOT_DELETED));
	}

	@Override
	public UserInfo userInfo(Long userId, UserEnum userEnum) {
		User user = baseMapper.selectById(userId);
		return buildUserInfo(user, userEnum);
	}

	@Override
	public UserInfo userInfo(Long userId) {
		User user = baseMapper.selectById(userId);
		return buildUserInfo(user);
	}

	@Override
	public UserInfo userInfo(String tenantId, String account, String password) {
		User user = baseMapper.getUser(tenantId, account, password);
		return buildUserInfo(user);
	}

	@Override
	public UserInfo userInfo(String tenantId, String account, String password, UserEnum userEnum) {
		User user = baseMapper.getUser(tenantId, account, password);
		return buildUserInfo(user, userEnum);
	}

	private UserInfo buildUserInfo(User user) {
		return buildUserInfo(user, UserEnum.WEB);
	}

	private UserInfo buildUserInfo(User user, UserEnum userEnum) {
		if (ObjectUtil.isEmpty(user)) {
			return null;
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUser(user);
		if (Func.isNotEmpty(user)) {
			if (Objects.nonNull(user.getRoleId())){
				List<String> roleAlias = roleService.getRoleAliases(user.getRoleId());
				userInfo.setRoles(roleAlias);
			}
		}
		// 根据每个用户平台，建立对应的detail表，通过查询将结果集写入到detail字段
		Kv detail = Kv.create().set("type", userEnum.getName());
		if (userEnum == UserEnum.WEB) {
			UserWeb userWeb = new UserWeb();
			UserWeb query = userWeb.selectOne(Wrappers.<UserWeb>lambdaQuery().eq(UserWeb::getUserId, user.getId()));
			if (ObjectUtil.isNotEmpty(query)) {
				detail.set("ext", query.getUserExt());
			}
		} else if (userEnum == UserEnum.APP) {
			UserAppVO query = apiService.getUserAppVo(user.getId());
			if (ObjectUtil.isNotEmpty(query)) {
				detail.set("avatar", query.getAvatar());
				detail.set("code", query.getCode());
				detail.set("codeUrl", query.getCodeUrl());
				detail.set("creditScore", query.getCreditScore());
				detail.set("intergral", query.getIntegral());
				detail.set("name", query.getName());
				detail.set("username", query.getUsername());
				detail.set("nowMoney", query.getNowMoney());
				detail.set("parentId", query.getParentId());
				detail.set("realName", query.getRealName());
				detail.set("sexDisplayName", query.getSexDisplayName());
				detail.set("state", query.getState());
				detail.set("stateDisplayName", query.getStateDisplayName());
				detail.set("teamNum", query.getTeamNum());
				detail.set("ticket", query.getTicket());
				detail.set("userExplain", query.getUserExplain());
				detail.set("userExt", query.getUserExt());
				detail.set("userId", query.getUserId());
				detail.set("birthday", query.getBirthday());
				detail.set("alipayUserId", query.getAlipayUserId());
				detail.set("isOauth", query.getIsOauth());
				detail.set("isAddress", query.getIsAddress());
				detail.set("isCard", query.getIdCard());
				detail.set("negativeUrl", query.getNegativeUrl());
				detail.set("positiveUrl", query.getPositiveUrl());
				//是否用于预收款付款、特殊账号
				detail.set("enableAuth", query.getEnableAuth());
			}
		} else {
			UserOther userOther = new UserOther();
			UserOther query = userOther.selectOne(Wrappers.<UserOther>lambdaQuery().eq(UserOther::getUserId, user.getId()));
			if (ObjectUtil.isNotEmpty(query)) {
				detail.set("ext", query.getUserExt());
			}
		}
		userInfo.setDetail(detail);
		return userInfo;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public UserInfo userInfo(UserOauth userOauth) {
		UserOauth uo = userOauthService.getOne(Wrappers.<UserOauth>query().lambda().eq(UserOauth::getUuid, userOauth.getUuid()).eq(UserOauth::getSource, userOauth.getSource()));
		UserInfo userInfo;
		if (Func.isNotEmpty(uo) && Func.isNotEmpty(uo.getUserId())) {
			userInfo = this.userInfo(uo.getUserId());
			userInfo.setOauthId(Func.toStr(uo.getId()));
		} else {
			userInfo = new UserInfo();
			if (Func.isEmpty(uo)) {
				userOauthService.save(userOauth);
				userInfo.setOauthId(Func.toStr(userOauth.getId()));
			} else {
				userInfo.setOauthId(Func.toStr(uo.getId()));
			}
			User user = new User();
			user.setAccount(userOauth.getUsername());
			user.setTenantId(userOauth.getTenantId());
			userInfo.setUser(user);
			userInfo.setRoles(Collections.singletonList(GUEST_NAME));
		}
		return userInfo;
	}

	@Override
	public boolean grant(String userIds, String roleIds) {
		User user = new User();
		user.setRoleId(roleIds);
		return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds)));
	}

	@Override
	public boolean resetPassword(String userIds) {
		User user = new User();
		user.setPassword(DigestUtil.encrypt(CommonConstant.DEFAULT_PASSWORD));
		user.setUpdateTime(DateUtil.now());
		return this.update(user, Wrappers.<User>update().lambda().in(User::getId, Func.toLongList(userIds)));
	}

	@Override
	public boolean resetPasswordById(Long userId) {
		User user = new User();
		user.setPassword(DigestUtil.encrypt(CommonConstant.DEFAULT_PASSWORD));
		user.setUpdateTime(DateUtil.now());
		return this.update(user, Wrappers.<User>update().lambda().eq(User::getId, userId));
	}

	@Override
	public boolean updatePassword(Long userId, String oldPassword, String newPassword, String newPassword1) {
		User user = getById(userId);
		if (!newPassword.equals(newPassword1)) {
			throw new ServiceException("请输入正确的确认密码!");
		}
		if (!user.getPassword().equals(DigestUtil.hex(oldPassword))) {
			throw new ServiceException("原密码不正确!");
		}
		return this.update(Wrappers.<User>update().lambda().set(User::getPassword, DigestUtil.hex(newPassword)).eq(User::getId, userId));
	}

	@Override
	public boolean removeUser(String userIds) {
		if (Func.contains(Func.toLongArray(userIds), AuthUtil.getUserId())) {
			throw new ServiceException("不能删除本账号!");
		}
		return deleteLogic(Func.toLongList(userIds));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importUser(List<UserExcel> data, Boolean isCovered) {
		data.forEach(userExcel -> {
			User user = Objects.requireNonNull(BeanUtil.copy(userExcel, User.class));
			// 设置用户平台
			user.setUserType(Func.toInt(DictCache.getKey(DictEnum.USER_TYPE, userExcel.getUserTypeName()), 1));
			// 设置部门ID
			user.setDeptId(Func.toStrWithEmpty(SysCache.getDeptIds(userExcel.getTenantId(), userExcel.getDeptName()), StringPool.EMPTY));
			// 设置岗位ID
			user.setPostId(Func.toStrWithEmpty(SysCache.getPostIds(userExcel.getTenantId(), userExcel.getPostName()), StringPool.EMPTY));
			// 设置角色ID
			user.setRoleId(Func.toStrWithEmpty(SysCache.getRoleIds(userExcel.getTenantId(), userExcel.getRoleName()), StringPool.EMPTY));
			// 设置租户ID
			if (!AuthUtil.isAdministrator() || StringUtil.isBlank(user.getTenantId())) {
				user.setTenantId(AuthUtil.getTenantId());
			}
			// 覆盖数据
			if (isCovered) {
				// 查询用户是否存在
				User oldUser = UserCache.getUser(userExcel.getTenantId(), userExcel.getAccount());
				if (oldUser != null && oldUser.getId() != null) {
					user.setId(oldUser.getId());
					this.updateUser(user);
					return;
				}
			}
			// 获取默认密码配置
			String initPassword = ParamCache.getValue(DEFAULT_PARAM_PASSWORD);
			user.setPassword(initPassword);
			this.submit(user);
		});
	}

	@Override
	public List<UserExcel> exportUser(Wrapper<User> queryWrapper) {
		List<UserExcel> userList = baseMapper.exportUser(queryWrapper);
		userList.forEach(user -> {
			user.setUserTypeName(DictCache.getValue(DictEnum.USER_TYPE, user.getUserType()));
			user.setRoleName(StringUtil.join(SysCache.getRoleNames(user.getRoleId())));
			user.setDeptName(StringUtil.join(SysCache.getDeptNames(user.getDeptId())));
			user.setPostName(StringUtil.join(SysCache.getPostNames(user.getPostId())));
		});
		return userList;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean registerGuest(User user, Long oauthId) {
		Tenant tenant = SysCache.getTenant(user.getTenantId());
		if (tenant == null || tenant.getId() == null) {
			throw new ServiceException("租户信息错误!");
		}
		UserOauth userOauth = userOauthService.getById(oauthId);
		if (userOauth == null || userOauth.getId() == null) {
			throw new ServiceException("第三方登陆信息错误!");
		}
		user.setRealName(user.getName());
		user.setAvatar(userOauth.getAvatar());
		user.setRoleId(StringPool.MINUS_ONE);
		user.setDeptId(StringPool.MINUS_ONE);
		user.setPostId(StringPool.MINUS_ONE);
		boolean userTemp = this.submit(user);
		userOauth.setUserId(user.getId());
		userOauth.setTenantId(user.getTenantId());
		boolean oauthTemp = userOauthService.updateById(userOauth);
		return (userTemp && oauthTemp);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean registerApp(User user, Long parentId) {
		user.setUserType(2);//APP
		user.setPassword(DigestUtil.encrypt(user.getPassword()));
		user.setAvatar(ParamCache.getValue(CommonConstant.DEFAULT_AVATAR));
		user.setCode(getCode());
		user.setRoleId(ParamCache.getValue(CommonConstant.DEFAULT_ROLD));
		user.setDeptId(ParamCache.getValue(CommonConstant.DEFAULT_DEPT));
		user.setPostId(ParamCache.getValue(CommonConstant.DEFAULT_POST));
		Integer userCount = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getTenantId, user.getTenantId()).eq(User::getAccount, user.getAccount()));
		if (userCount > 0 && Func.isEmpty(user.getId())) {
			throw new ServiceException("该账号已注册");
		}
		boolean flg = save(user);
		if (!flg) {
			return false;
		}
		UserApp userApp = new UserApp();
		userApp.setUserId(parentId);
		UserApp parentUserApp = userApp.selectOne(Condition.getQueryWrapper(userApp));
		User query = baseMapper.selectOne(Wrappers.<User>query().lambda().eq(User::getAccount, user.getAccount()));
		userApp.setUserId(query.getId());
		userApp.setParentId(parentId);
		if (parentUserApp != null) {
//			//取出当前用户父节点
//			String parentPath = parentUserApp.getParentPath();
//			String[] split = parentPath.split(",");
//			//判断是否超过5层,超过5层则选取后4层加上当前用户用来作为加当前用户的父节点
//			if(split.length >= 5){
//				int length = split.length;
//				StringBuffer sb = new StringBuffer();
//				for (int i = 1; i < 5; i++){
//					sb.append(split[length - i]);
//					sb.append(",");
//				}
//				userApp.setParentPath(sb.toString() + parentId);
//			}else{
			userApp.setParentPath(parentUserApp.getParentPath() + "," + parentId);
//			}
		} else {
			userApp.setParentPath(Long.toString(parentId));
		}
//		userApp.setIntegral(Double.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_REGISTER_INTEGRAL)));
		Integer creditScore = Integer.valueOf(ParamCache.getValue(CommonConstant.DEFAULT_CREDIT_SCORE));
		userApp.setCreditScore(creditScore);
		flg = userApp.insert();
		if (!flg) {
			throw new ServiceException("注册失败");
		}
		return true;
	}

	@Override
	public boolean resetPassword(Long userId, String password) {
		User user = new User();
		user.setId(userId);
		user.setPassword(DigestUtil.encrypt(password));
		int i = baseMapper.updateById(user);
		if (i == 1) {
			return true;
		}
		return false;
	}

	/**
	 * 生成不重复的Code
	 */
	private String getCode() {
		String code = StringUtil.random(6, RandomType.ALL).toUpperCase();
		Integer codeCount = baseMapper.selectCount(Wrappers.<User>query().lambda().eq(User::getCode, code));
		if (codeCount > 0) {
			return getCode();
		}
		return code;
	}

	@Override
	public boolean updatePlatform(Long userId, Integer userType, String userExt) {
		if (userType.equals(UserEnum.WEB.getCategory())) {
			UserWeb userWeb = new UserWeb();
			UserWeb query = userWeb.selectOne(Wrappers.<UserWeb>lambdaQuery().eq(UserWeb::getUserId, userId));
			if (ObjectUtil.isNotEmpty(query)) {
				userWeb.setId(query.getId());
			}
			userWeb.setUserId(userId);
			userWeb.setUserExt(userExt);
			return userWeb.insertOrUpdate();
		} else if (userType.equals(UserEnum.APP.getCategory())) {
			UserApp userApp = new UserApp();
			UserApp query = userApp.selectOne(Wrappers.<UserApp>lambdaQuery().eq(UserApp::getUserId, userId));
			if (ObjectUtil.isNotEmpty(query)) {
				userApp.setId(query.getId());
			}
			userApp.setUserId(userId);
			userApp.setUserExt(userExt);
			return userApp.insertOrUpdate();
		} else {
			UserOther userOther = new UserOther();
			UserOther query = userOther.selectOne(Wrappers.<UserOther>lambdaQuery().eq(UserOther::getUserId, userId));
			if (ObjectUtil.isNotEmpty(query)) {
				userOther.setId(query.getId());
			}
			userOther.setUserId(userId);
			userOther.setUserExt(userExt);
			return userOther.insertOrUpdate();
		}
	}

	@Override
	public UserVO platformDetail(User user) {
		User detail = baseMapper.selectOne(Condition.getQueryWrapper(user));
		UserVO userVO = UserWrapper.build().entityVO(detail);
		if (userVO.getUserType().equals(UserEnum.WEB.getCategory())) {
			UserWeb userWeb = new UserWeb();
			UserWeb query = userWeb.selectOne(Wrappers.<UserWeb>lambdaQuery().eq(UserWeb::getUserId, user.getId()));
			if (ObjectUtil.isNotEmpty(query)) {
				userVO.setUserExt(query.getUserExt());
			}
		} else if (userVO.getUserType().equals(UserEnum.APP.getCategory())) {
			UserApp userApp = new UserApp();
			UserApp query = userApp.selectOne(Wrappers.<UserApp>lambdaQuery().eq(UserApp::getUserId, user.getId()));
			if (ObjectUtil.isNotEmpty(query)) {
				userVO.setUserExt(query.getUserExt());
			}
		} else {
			UserOther userOther = new UserOther();
			UserOther query = userOther.selectOne(Wrappers.<UserOther>lambdaQuery().eq(UserOther::getUserId, user.getId()));
			if (ObjectUtil.isNotEmpty(query)) {
				userVO.setUserExt(query.getUserExt());
			}
		}
		return userVO;
	}


	@Override
	public Map<Integer, List<UserLvVo>> getUserTree(Long userId) {
		Map<Integer, List<UserLvVo>> map = new HashMap<>();
		List<UserLvVo> list = this.baseMapper.getUserTree(userId);
		List<UserLvVo> oneList = list.stream().filter(vo -> vo.getLevel() == 1).collect(Collectors.toList());
		List<UserLvVo> twoList = list.stream().filter(vo -> vo.getLevel() == 2).collect(Collectors.toList());
		List<UserLvVo> threeList = list.stream().filter(vo -> vo.getLevel() == 3).collect(Collectors.toList());
		List<UserLvVo> fourList = list.stream().filter(vo -> vo.getLevel() == 4).collect(Collectors.toList());
		List<UserLvVo> fiveList = list.stream().filter(vo -> vo.getLevel() == 5).collect(Collectors.toList());
		map.put(1, oneList);
		map.put(2, twoList);
		map.put(3, threeList);
		map.put(4, fourList);
		map.put(5, fiveList);
		return map;
	}


	/**
	 * 找出符合直推创客人数条件的用户
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @return
	 */
	@Override
	public List<Long> getMakerList(List<Long> userIds, Integer num){
		return  baseMapper.getMakerList(userIds, num);
	}

	/**
	 * 找出符合团队人数条件的用户
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @return
	 */
	@Override
	public List<Long> getTeamList(List<Long> userIds, Integer num){
		return baseMapper.getTeamList(userIds, num);
	}

	/**
	 * 找出符合团队购买数量达标的客户id
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @param consignDate 购买时间
	 * @return
	 */
	@Override
	public List<Long> getTeamConsignList(List<Long> userIds, Integer num, Date consignDate){
		return baseMapper.getTeamConsignList(userIds, num, consignDate);
	}

	/**
	 * 找出符合直推购买数量达标的客户id
	 * @param userIds 用户查询用户id列表
	 * @param num 创客数量
	 * @param consignDate 购买时间
	 * @return
	 */
	@Override
	public List<Long> getRecomConsignList(List<Long> userIds, Integer num, Date consignDate){
		return baseMapper.getRecomConsignList(userIds, num, consignDate);
	}

	/**
	 * 找出符合轰动条件条件的用户
	 * @param userIds 用户查询用户id列表
	 * @return
	 */
	@Override
	public List<Long> getNewList(List<Long> userIds){
		return baseMapper.getNewList(userIds);
	}

	/**
	 * 查询当天用户的团队和直推人员购买数量
	 * @param userId 用户查询用户账号
	 * @param consignDate 购买时间
	 * @param consignConfigId 场次id
	 * @return
	 */
	@Override
	public List<Map<String,String>> countRecomAndTeamConsign(Long userId, Date consignDate, Long consignConfigId){
		return baseMapper.countRecomAndTeamConsign(userId, consignDate, consignConfigId);
	}

	@Override
	public User getByAccount(String account) {
		return getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccount, account));
	}

	@Override
	public void setRoleByRoleName(Long userId, String roleName) {
		Long roleId = roleService.getRoleIdByRoleName(roleName);
		Assert.notNull(roleId, "请先到权限管理添加该角色信息");
		User user = getById(userId);
		if (StrUtil.isNotBlank(user.getRoleId())) {
			String[] split = user.getRoleId().split(",");
			HashSet<String> roles = CollUtil.newHashSet(split);
			roles.add(roleId.toString());
			user.setRoleId(CollUtil.join(roles, ","));
		} else {
			user.setRoleId(roleId.toString());
		}
		updateById(user);
	}

	@Override
	public void removeRoleByRoleName(Long userId, String roleName) {
		Long roleId = roleService.getRoleIdByRoleName(roleName);
		Assert.notNull(roleId, "请先到权限管理添加该角色信息");
		User user = getById(userId);
		String userRoleId = user.getRoleId();
		if (StrUtil.isNotBlank(userRoleId)) {
			String[] split = userRoleId.split(",");
			List<String> roles = new ArrayList<>();
			for (String s : split) {
				if (!s.equals(roleId.toString())) {
					roles.add(s);
				}
			}
			user.setRoleId(CollUtil.join(roles, ","));
		}
		updateById(user);
	}
}
