289 lines
12 KiB
Java
289 lines
12 KiB
Java
package com.isu.gaswellwatch.service.impl;
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import com.alibaba.excel.EasyExcel;
|
|
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.isu.gaswellwatch.config.SnowflakeConfig;
|
|
import com.isu.gaswellwatch.constants.UserConstant;
|
|
import com.isu.gaswellwatch.dao.MenuDao;
|
|
import com.isu.gaswellwatch.dao.UserDao;
|
|
import com.isu.gaswellwatch.dto.LoginDTO;
|
|
import com.isu.gaswellwatch.dto.ModifyPasswordDTO;
|
|
import com.isu.gaswellwatch.dto.UserDTO;
|
|
import com.isu.gaswellwatch.dto.UserEditDTO;
|
|
import com.isu.gaswellwatch.entity.User;
|
|
import com.isu.gaswellwatch.entity.UserDepartment;
|
|
import com.isu.gaswellwatch.entity.UserExport;
|
|
import com.isu.gaswellwatch.entity.UserRole;
|
|
import com.isu.gaswellwatch.exception.BusinessException;
|
|
import com.isu.gaswellwatch.service.UserDepartmentService;
|
|
import com.isu.gaswellwatch.service.UserRoleService;
|
|
import com.isu.gaswellwatch.service.UserService;
|
|
import com.isu.gaswellwatch.utils.ConverterUtil;
|
|
import com.isu.gaswellwatch.vo.*;
|
|
import jakarta.annotation.Resource;
|
|
import jakarta.servlet.ServletOutputStream;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
import java.net.URLEncoder;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.regex.Pattern;
|
|
|
|
@Service("userService")
|
|
@Transactional
|
|
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
|
|
|
|
@Resource
|
|
private UserDao userDao;
|
|
@Resource
|
|
private UserRoleService userRoleService;
|
|
@Resource
|
|
private UserDepartmentService userDepartmentService;
|
|
@Resource
|
|
private SnowflakeConfig snowflakeConfig;
|
|
@Resource
|
|
private MenuServiceImpl menuService;
|
|
@Resource
|
|
private MenuDao menuDao;
|
|
|
|
@Override
|
|
public UserLoginInfoVO doLogin(LoginDTO loginDTO){
|
|
User user = getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, loginDTO.getUsername()));
|
|
if(user == null) throw new BusinessException("账号不存在");
|
|
if(UserConstant.FORBIDDEN.equals(user.getIsEnable())) throw new BusinessException("账号已被禁用,请联系管理员");
|
|
if(!loginDTO.getPassword().equals(user.getPassword())) throw new BusinessException("账号密码错误");
|
|
StpUtil.login(loginDTO.getUsername());
|
|
UserLoginInfoVO userLoginInfoVO = buildTokenSession(loginDTO.getUsername());
|
|
StpUtil.getTokenSession().set(UserConstant.TOKEN_SESSION, userLoginInfoVO);
|
|
String tokenValue = StpUtil.getTokenValue();
|
|
userLoginInfoVO.setToken(tokenValue);
|
|
return userLoginInfoVO;
|
|
}
|
|
|
|
@Override
|
|
public List<MenuTreeVO> getMenuTreeByToken(){
|
|
UserLoginInfoVO userLoginInfoVO = (UserLoginInfoVO) StpUtil.getTokenSession().get(UserConstant.TOKEN_SESSION);
|
|
UserVO userVO = userDao.selectUserInfo(userLoginInfoVO.getUserVO().getUsername());
|
|
if(userVO != null){
|
|
if(!CollectionUtil.isEmpty(userVO.getRoles())){
|
|
List<Long> list = userVO.getRoles().stream().map(RoleVO::getId).toList();
|
|
return menuService.getMenuTreeByRoleIds(list);
|
|
}
|
|
}
|
|
return new ArrayList<>();
|
|
}
|
|
|
|
@Override
|
|
public void logout(){
|
|
StpUtil.logout();
|
|
}
|
|
|
|
@Override
|
|
public void modifyPassword(ModifyPasswordDTO modifyPasswordDTO){
|
|
UserLoginInfoVO userLoginInfoVO = (UserLoginInfoVO) StpUtil.getTokenSession().get(UserConstant.TOKEN_SESSION);
|
|
User user = getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, userLoginInfoVO.getUserVO().getUsername()));
|
|
if(!modifyPasswordDTO.getOldPassword().equals(user.getPassword())) throw new BusinessException("密码错误");
|
|
if(!modifyPasswordDTO.getNewPassword().equals(modifyPasswordDTO.getNewPasswordConfirm())) throw new BusinessException("确认新密码跟新密码不一致");
|
|
user.setPassword(modifyPasswordDTO.getNewPassword());
|
|
updateById(user);
|
|
StpUtil.logout();
|
|
}
|
|
|
|
@Override
|
|
public Page<UserVO> page(Integer currentPage, Integer pageSize, String username, String name, Long roleId, String isEnable){
|
|
List<Long> userIds = null;
|
|
if(roleId != null){
|
|
List<UserRole> list = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, roleId));
|
|
if(!CollectionUtils.isEmpty(list)){
|
|
userIds = list.stream().map(UserRole::getUserId).toList();
|
|
}else{
|
|
return new Page<>();
|
|
}
|
|
}
|
|
return userDao.page(new Page<>(currentPage, pageSize),username,name,userIds,isEnable);
|
|
}
|
|
|
|
@Override
|
|
public void add(UserDTO userDTO){
|
|
if(StringUtils.isNotBlank(userDTO.getPhone()) && !Pattern.matches("^\\d{11}$", userDTO.getPhone())){
|
|
throw new BusinessException("手机号必须是11位纯数字");
|
|
}
|
|
//查重
|
|
List<User> users = list(new LambdaQueryWrapper<User>().eq(User::getUsername, userDTO.getUsername()));
|
|
if(!CollectionUtils.isEmpty(users)){
|
|
throw new BusinessException("账号已存在,请重新输入");
|
|
}
|
|
//用户新增
|
|
User user = ConverterUtil.convert(userDTO, User.class);
|
|
user.setId(snowflakeConfig.snowflakeId());
|
|
user.setPassword(DigestUtil.sha1Hex(UserConstant.DEFAULT_PASSWORD));
|
|
save(user);
|
|
//关联新增
|
|
addRelation(userDTO.getRoles(), user);
|
|
|
|
if(userDTO.getDepartmentId()!=null){
|
|
userDepartmentService.remove(new LambdaQueryWrapper<UserDepartment>().eq(UserDepartment::getUserId, user.getId()));
|
|
addDepartment(userDTO.getDepartmentId(), user);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void edit(UserEditDTO userEditDTO){
|
|
if(StringUtils.isNotBlank(userEditDTO.getPhone()) && !Pattern.matches("^\\d{11}$", userEditDTO.getPhone())){
|
|
throw new BusinessException("手机号必须是11位纯数字");
|
|
}
|
|
//用户更新
|
|
List<User> list = list(new LambdaQueryWrapper<User>().eq(User::getUsername, userEditDTO.getUsername()).ne(User::getId, userEditDTO.getId()));
|
|
if(CollectionUtil.isNotEmpty(list)) throw new BusinessException("账号已存在,请重新输入");
|
|
User user = ConverterUtil.convert(userEditDTO, User.class);
|
|
updateById(user);
|
|
//关联更新
|
|
userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()));
|
|
addRelation(userEditDTO.getRoles(), user);
|
|
|
|
if(userEditDTO.getDepartmentId()!=null){
|
|
userDepartmentService.remove(new LambdaQueryWrapper<UserDepartment>().eq(UserDepartment::getUserId, user.getId()));
|
|
addDepartment(userEditDTO.getDepartmentId(), user);
|
|
}
|
|
|
|
}
|
|
|
|
private void addDepartment(Long departmentId, User user) {
|
|
UserDepartment.builder().id(snowflakeConfig.snowflakeId()).userId(user.getId()).departmentId(departmentId).build().insert();
|
|
}
|
|
|
|
@Override
|
|
public UserVO getUser(Long id) {
|
|
User user = getById(id);
|
|
if(user == null) return null;
|
|
return userDao.selectUserInfo(user.getUsername());
|
|
}
|
|
|
|
@Override
|
|
public void delete(Long id){
|
|
//如果是超管 不能删除
|
|
if(Objects.equals(id, UserConstant.SUPER_ADMIN_ID)) throw new BusinessException("该用户为内置超管用户,不能删除");
|
|
User byId = getById(id);
|
|
if(byId != null){
|
|
removeById(id);
|
|
userRoleService.remove(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, id));
|
|
StpUtil.kickout(byId.getUsername());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
public void reset(Long id){
|
|
User byId = getById(id);
|
|
if(byId != null){
|
|
update(new LambdaUpdateWrapper<User>().eq(User::getId, id).set(User::getPassword, DigestUtil.sha1Hex("123456")));
|
|
StpUtil.kickout(byId.getUsername());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setEnable(Long userId, String isEnable){
|
|
User byId = getById(userId);
|
|
if(byId != null){
|
|
update(new LambdaUpdateWrapper<User>().eq(User::getId, userId).set(User::getIsEnable, isEnable));
|
|
if(UserConstant.FORBIDDEN.equals(isEnable)) StpUtil.kickout(byId.getUsername());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void export(HttpServletResponse response, String username, String name, Long roleId, String isEnable){
|
|
List<UserVO> list = userDao.list(username, name, roleId, isEnable);
|
|
List<UserExport> exportList = new ArrayList<>();
|
|
if(!CollectionUtils.isEmpty(list)){
|
|
int i= 1;
|
|
for(UserVO userVO : list){
|
|
//角色
|
|
StringBuilder roles = new StringBuilder();
|
|
if(!CollectionUtils.isEmpty(userVO.getRoles())){
|
|
for(int k = 0;k < userVO.getRoles().size();k++){
|
|
if( k == 0){
|
|
roles = new StringBuilder(userVO.getRoles().getFirst().getName());
|
|
}else {
|
|
roles.append(",").append(userVO.getRoles().get(k).getName());
|
|
}
|
|
}
|
|
}
|
|
//状态
|
|
String status;
|
|
if(UserConstant.NORMAL.equals(userVO.getIsEnable())){
|
|
status = "正常";
|
|
}else{
|
|
status = "冻结";
|
|
}
|
|
exportList.add(UserExport.builder().number(i).username(userVO.getUsername()).nickname(userVO.getNickname())
|
|
.roles(roles.toString()).phone(userVO.getPhone()).createTime(userVO.getCreateTime()).status(status).build());
|
|
i++;
|
|
}
|
|
}
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
response.setCharacterEncoding("utf-8");
|
|
String fileName = null;
|
|
try {
|
|
ServletOutputStream outputStream = response.getOutputStream();
|
|
fileName = URLEncoder.encode("用户", "UTF-8").replaceAll("\\+", "%20");
|
|
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
|
|
if (!CollectionUtils.isEmpty(exportList)) {
|
|
EasyExcel.write(outputStream)
|
|
// 这里放入动态头
|
|
.head(UserExport.class).sheet("data")
|
|
// 当然这里数据也可以用 List<List<String>> 去传入
|
|
.doWrite(exportList);
|
|
}
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
public List<MenuVO> getMenuList(String username){
|
|
UserVO userVO = userDao.selectUserInfo(username);
|
|
if(userVO != null) {
|
|
if (!CollectionUtil.isEmpty(userVO.getRoles())) {
|
|
List<Long> list = userVO.getRoles().stream().map(RoleVO::getId).toList();
|
|
return menuDao.selectRoleMenu(list);
|
|
}
|
|
}
|
|
return new ArrayList<>();
|
|
}
|
|
|
|
private UserLoginInfoVO buildTokenSession(String username){
|
|
UserLoginInfoVO userInfoVO = new UserLoginInfoVO();
|
|
UserVO userVO = userDao.selectUserInfo(username);
|
|
if(userVO != null){
|
|
userInfoVO.setUserVO(userVO);
|
|
}
|
|
return userInfoVO;
|
|
}
|
|
|
|
|
|
private void addRelation(List<RoleVO> roles, User user) {
|
|
if (roles != null && !roles.isEmpty()) {
|
|
List<UserRole> userRoles = new ArrayList<>();
|
|
roles.stream().map(RoleVO::getId).toList().forEach(roleId -> {
|
|
userRoles.add(UserRole.builder().id(snowflakeConfig.snowflakeId()).userId(user.getId()).roleId(roleId).build());
|
|
});
|
|
userRoleService.saveBatch(userRoles);
|
|
}
|
|
}
|
|
|
|
|
|
} |