package com.fdkankan.manage_jp.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.util.Base64Converter;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.common.util.PasswordUtils;
import com.fdkankan.common.util.SecurityUtil;
import com.fdkankan.manage_jp.common.PageInfo;
import com.fdkankan.manage_jp.common.ResultCode;
import com.fdkankan.manage_jp.entity.Company;
import com.fdkankan.manage_jp.entity.Role;
import com.fdkankan.manage_jp.entity.User;
import com.fdkankan.manage_jp.entity.UserRole;
import com.fdkankan.manage_jp.exception.BusinessException;
import com.fdkankan.manage_jp.mapper.IUserMapper;
import com.fdkankan.manage_jp.service.ICompanyService;
import com.fdkankan.manage_jp.service.IRoleService;
import com.fdkankan.manage_jp.service.IUserRoleService;
import com.fdkankan.manage_jp.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.manage_jp.util.PasswordUtil;
import com.fdkankan.manage_jp.vo.request.LoginParam;
import com.fdkankan.manage_jp.vo.request.RequestCompany;
import com.fdkankan.manage_jp.vo.request.UserListParam;
import com.fdkankan.manage_jp.vo.request.UserParam;
import com.fdkankan.manage_jp.vo.response.LoginVo;
import com.fdkankan.manage_jp.vo.response.UserVo;
import com.fdkankan.redis.constant.RedisKey;
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 java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
*
* 用户信息表 服务实现类
*
*
* @author
* @since 2022-12-23
*/
@Service
public class UserServiceImpl extends ServiceImpl implements IUserService {
@Autowired
ICompanyService companyService;
@Autowired
private RedisUtil redisUtil;
@Autowired
IUserRoleService userRoleService;
@Autowired
IRoleService roleService;
@Override
public User getByUserName(String managerPhone) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getUserName,managerPhone);
return this.getOne(wrapper);
}
@Override
public PageInfo getPageByCompany(RequestCompany bo) {
Company company = companyService.getById(bo.getId());
if(company == null){
throw new BusinessException(ResultCode.NOT_RECORD);
}
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getCompanyId,bo.getId());
wrapper.ne(User::getId,company.getManagerId());
if(StringUtils.isNotBlank(bo.getUserName())){
wrapper.like(User::getUserName,bo.getUserName());
}
Page page = this.page(new Page<>(bo.getPageNum(), bo.getPageSize()), wrapper);
for (User record : page.getRecords()) {
record.setPassword(null);
}
return PageInfo.PageInfo(page);
}
@Override
public User findByUserName(String phoneNum) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getUserName,phoneNum);
return this.getOne(wrapper);
}
@Override
public List findByCompanyId(Long id) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(User::getCompanyId,id);
return this.list(wrapper);
}
@Override
public List findByUserNameList(String userName) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.like(User::getUserName,userName);
return this.list(wrapper);
}
public LoginVo login(LoginParam param) {
if (StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getPhoneNum())){
throw new BusinessException(ResultCode.PARAM_ERROR);
}
String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
String passwordCode = SecurityUtil.MD5(password);
User user = this.getByUserName(param.getPhoneNum());
if(user == null){
throw new BusinessException(ResultCode.USER_NOT_EXIST);
}
if(!user.getPassword().equals(passwordCode)){
throw new BusinessException(ResultCode.PASSWORD_ERROR);
}
String token = this.redisLogin(user.getUserName(), JSONObject.toJSONString(user),"user");
UserVo userVo = getUserVo(user);
LoginVo vo = new LoginVo();
vo.setToken(token);
vo.setUser(userVo);
return vo;
}
public String redisLogin(String userName,String value,String loginType){
String token = JwtUtil.createJWT(-1,userName,loginType);
String redisKey = String.format(RedisKey.TOKEN_V3,token);
redisUtil.set(redisKey, value,2 * 60 * 60);
return token;
}
@Override
public Object getUserInfo(String username) {
User user = this.getByUserName(username);
return getUserVo(user);
}
private UserVo getUserVo(User 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 Object pageList(UserListParam param,User user) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(param.getPhoneNum())){
wrapper.like(User::getUserName,param.getPhoneNum());
}
Set roleIds = userRoleService.getByUser(user);
if(roleIds.contains(6L)){
wrapper.eq(User::getCompanyId,user.getCompanyId());
}
wrapper.orderByDesc(User::getCreateTime);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
List userVos = new ArrayList<>();
for (User record : page.getRecords()) {
UserVo vo = new UserVo();
BeanUtils.copyProperties(record,vo);
List userRoleList = userRoleService.getByUserId(record.getId());
if(userRoleList.size() >0){
Role role = roleService.getById(userRoleList.get(0).getRoleId());
vo.setRoleName(role.getRoleName());
}
if(record.getCompanyId() != null){
Company company = companyService.getById(record.getCompanyId());
if(company != null){
vo.setCompanyName(company.getCompanyName());
}
}
userVos.add(vo);
}
Page pageVo = new Page<>(param.getPageNum(),param.getPageSize());
pageVo.setTotal(page.getTotal());
pageVo.setRecords(userVos);
return PageInfo.PageInfo(pageVo);
}
@Override
public void updatePassword(UserParam param) {
if(param.getId() == null || StringUtils.isBlank(param.getNewPassword())){
throw new BusinessException(ResultCode.PARAM_ERROR);
}
if(StringUtils.isNotBlank(param.getPassword())){
String oldPassword = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
String passwordOldCode = SecurityUtil.MD5(oldPassword);
User user = this.getById(param.getId());
if(!user.getPassword().equals(passwordOldCode)){
throw new BusinessException(ResultCode.OLD_PASSWORD_ERROR);
}
}
String password = Base64Converter.decode(Base64Converter.subText(param.getNewPassword()));
if(!PasswordUtil.checkPasswordFormal(password)){
throw new BusinessException(ResultCode.PASSWORD_TYPE_ERROR);
}
String passwordCode = SecurityUtil.MD5(password);
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.eq(User::getId,param.getId());
wrapper.set(User::getPassword,passwordCode);
this.update(wrapper);
}
@Override
public void logout(String token) {
redisUtil.del(String.format(RedisKey.TOKEN_V3,token));
}
@Override
public List allList(User param) {
if(StringUtils.isNotBlank(param.getUserName())){
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.like(User::getUserName,param.getUserName());
return this.list(wrapper);
}
return this.list();
}
}