package com.fdkankan.jp.xspace.service.impl;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.util.Base64Converter;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.jp.xspace.common.ResultCode;
import com.fdkankan.jp.xspace.common.redis.RedisConstant;
import com.fdkankan.jp.xspace.constant.XspaceErrorCode;
import com.fdkankan.jp.xspace.dto.LoginDTO;
import com.fdkankan.jp.xspace.entity.*;
import com.fdkankan.jp.xspace.common.exception.BusinessException;
import com.fdkankan.jp.xspace.mapper.ISysUserMapper;
import com.fdkankan.jp.xspace.mapper.IUserMapper;
import com.fdkankan.jp.xspace.service.*;
import com.fdkankan.jp.xspace.vo.LoginVO;
import com.fdkankan.jp.xspace.vo.UserVO;
import com.fdkankan.redis.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
* 用户信息表 服务实现类
*
*
* @author
* @since 2022-12-23
*/
@Service
public class UserServiceImpl extends ServiceImpl implements IUserService {
@Resource
private RedisUtil redisUtil;
@Autowired
private IUserRoleService userRoleService;
@Autowired
private IXspaceUserService xspaceUserService;
@Autowired
private IUserPlatformService userPlatformService;
@Autowired
private ISysUserService sysUserService;
@Override
public User getByUserName(String managerPhone) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getUserName,managerPhone);
return this.getOne(wrapper);
}
@Override
public LoginVO login(LoginDTO param) {
if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
throw new BusinessException(ResultCode.PARAM_ERROR);
}
String passwordMd5 = SecurityUtil.MD52(Base64Converter.decode(Base64Converter.subText(param.getPassword())));
// User user = this.getByUserName(param.getPhoneNum());
SysUser user = sysUserService.getByUserName(param.getPhoneNum());
if(user == null){
throw new BusinessException(ResultCode.USER_NOT_EXIST);
}
if(!user.getPassword().equals(passwordMd5)){
throw new BusinessException(ResultCode.PASSWORD_ERROR);
}
// List xspases = userPlatformService.list(new LambdaQueryWrapper().eq(UserPlatform::getUserId, user.getId()).eq(UserPlatform::getPlatformKey, "xspase"));
// if(CollUtil.isEmpty(xspases)){
// throw new BusinessException(XspaceErrorCode.CODE_90001.code(), XspaceErrorCode.CODE_90001.message());
// }
String token = this.redisLogin(user.getUserName(), JSONObject.toJSONString(user),"user");
UserVO userVo = getUserVo(user);
LoginVO vo = new LoginVO();
vo.setToken(token);
vo.setUser(userVo);
XspaceUser xspaceUser = xspaceUserService.getByUserId(userVo.getId());
if(Objects.isNull(xspaceUser)){
xspaceUser = new XspaceUser();
xspaceUser.setUserId(userVo.getId());
xspaceUserService.save(xspaceUser);
}
return vo;
}
@Override
public void logout(String token) {
redisUtil.del(String.format(RedisConstant.KEY_XSPACE_TOKEN,token));
}
public String redisLogin(String userName,String value,String loginType){
String token = JwtUtil.createJWT(-1,userName,loginType);
String redisKey = String.format(RedisConstant.KEY_XSPACE_TOKEN,token);
redisUtil.set(redisKey, value,2 * 60 * 60);
return token;
}
private UserVO getUserVo(SysUser user){
UserVO userVo = new UserVO();
BeanUtils.copyProperties(user,userVo);
List userRoles = userRoleService.getByUserId(userVo.getId());
Set roleIds = userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
userVo.setRoleIds(roleIds);
return userVo;
}
@Override
public UserVO getUserInfo(String username) {
SysUser sysUser = sysUserService.getByUserName(username);
return getUserVo(sysUser);
}
}