simulation-backend/src/main/java/com/hivekion/system/controller/SysUserController.java

196 lines
7.9 KiB
Java
Raw Normal View History

2025-08-07 18:04:07 +08:00
package com.hivekion.system.controller;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.common.entity.TreeNode;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
2025-09-11 14:29:58 +08:00
import com.hivekion.system.domain.SysRole;
2025-08-07 18:04:07 +08:00
import com.hivekion.system.domain.SysUser;
2025-09-11 14:29:58 +08:00
import com.hivekion.system.domain.SysUserRoles;
import com.hivekion.system.domain.vo.sysuser.SysUserCreateInputVo;
import com.hivekion.system.domain.vo.sysuser.SysUserModelVo;
import com.hivekion.system.domain.vo.sysuser.SysUserResetPwdVo;
import com.hivekion.system.domain.vo.sysuser.SysUserSearchPageInputVo;
import com.hivekion.system.domain.vo.sysuser.SysUserUpdateInputVo;
import com.hivekion.system.domain.vo.sysuser.SysUserViewVo;
import com.hivekion.system.service.ISysRoleService;
2025-08-07 18:04:07 +08:00
import com.hivekion.system.service.ISysUserRolesService;
import com.hivekion.system.service.ISysUserService;
import io.swagger.annotations.Api;
2025-09-11 14:29:58 +08:00
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
2025-08-07 18:04:07 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
2025-09-11 14:29:58 +08:00
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
2025-08-07 18:04:07 +08:00
/**
* 用户信息
*
* @author sutao
*/
@RestController
@RequestMapping("/system/user")
@Api(value = "用户管理", tags = {"系统管理-用户管理"})
public class SysUserController {
2025-09-11 14:29:58 +08:00
@Autowired
private ISysUserService userService;
@Autowired
private ISysUserRolesService userRolesService;
@Resource
private ISysRoleService roleService;
@GetMapping("/list")
public PagedResultVo<SysUserViewVo> list(SysUserSearchPageInputVo inputVo) {
Map<String, List<SysUserRoles>> userRoleMap = userRolesService.list().stream()
.collect(Collectors.groupingBy(SysUserRoles::getUserId));
List<SysRole> roleList = roleService.list();
Map<String, SysRole> roleMap = roleList.stream()
.collect(Collectors.toMap(SysRole::getId, x -> x));
PagedResultVo<SysUserViewVo> list = userService.selectUserList(inputVo);
list.getData().forEach(a -> {
if (userRoleMap.get(a.getId()) != null) {
StringBuffer sb = new StringBuffer();
userRoleMap.get(a.getId()).forEach(b -> {
sb.append(roleMap.get(b.getRoleId()).getRoleName()).append(",");
});
a.setRoles(userRoleMap.get(a.getId()).stream().map(SysUserRoles::getRoleId)
.collect(Collectors.toList()));
a.setRoleName(
!sb.toString().isEmpty() ? sb.substring(0, sb.toString().length()-1):"");
}
});
return list;
}
@GetMapping("/getUserName")
public String getUserName(String userIds) {
return userService.getUserName(userIds);
}
@PostMapping("/create")
@PreAuthorize("@Permission.hasPermi('pro:sys:user:add')")
@AutoLog(value = "新增用户", operationType = OperationTypeEnum.INSERT, module = "系统管理/用户管理")
public boolean create(@RequestBody SysUserCreateInputVo inputDto) {
if (inputDto.getUserName() == null && inputDto.getPhoneNumber() == null) {
throw new BusinessException(500, "新增用户的用户名和密码都为空!");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (inputDto.getUserName() == null && inputDto.getPhoneNumber() != null) {
inputDto.setUserName(inputDto.getPhoneNumber());
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (StringUtils.isBlank(inputDto.getUserName())) {
throw new BusinessException(500,
"新增用户'" + inputDto.getUserName() + "'失败,登录账号不能为空");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (userService.checkUserNameUnique(inputDto.getUserName(), "")) {
throw new BusinessException(500,
"新增用户'" + inputDto.getUserName() + "'失败,登录账号已存在");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (inputDto.getPhoneNumber().equals("")
&& !userService.checkPhoneUnique(inputDto.getPhoneNumber(), "")) {
throw new BusinessException(500,
"新增用户'" + inputDto.getUserName() + "'失败,手机号码已存在");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (!inputDto.getEmail().equals("")
&& userService.checkEmailUnique(inputDto.getEmail(), "")) {
throw new BusinessException(500,
"新增用户'" + inputDto.getUserName() + "'失败,邮箱账号已存在");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
SysUser sysUser = userService.create(inputDto);
userRolesService.updateUserRoles(sysUser.getId(), inputDto.getRoles());
return true;
}
@PutMapping("/update")
@AutoLog(value = "变更用户信息", operationType = OperationTypeEnum.UPDATE, module = "系统管理/用户管理")
@PreAuthorize("@Permission.hasPermi('pro:sys:user:edit')")
public boolean update(@RequestBody SysUserUpdateInputVo inputVo) {
SysUserModelVo info = userService.getInfo(inputVo.getId());
if (info != null
&& !info.getUserName().equals(inputVo.getUserName())
&& userService.checkUserNameUnique(inputVo.getUserName(), inputVo.getId())) {
throw new BusinessException(500,
"修改用户'" + inputVo.getUserName() + "'失败,登录账号已存在");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (info != null
&& info.getPhoneNumber() != null
&& !info.getPhoneNumber().equals(inputVo.getPhoneNumber())
&& StringUtils.isNotBlank(inputVo.getPhoneNumber())
&& userService.checkPhoneUnique(inputVo.getPhoneNumber(), inputVo.getId())) {
throw new BusinessException(500,
"修改用户'" + inputVo.getUserName() + "'失败,手机号码已存在");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
if (info != null
&& info.getEmail() != null
&& !info.getEmail().equals(inputVo.getEmail())
&& !inputVo.getEmail().equals("")
&& userService.checkEmailUnique(inputVo.getEmail(), inputVo.getId())) {
throw new BusinessException(500,
"修改用户'" + inputVo.getUserName() + "'失败,邮箱账号已存在");
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
userRolesService.updateUserRoles(inputVo.getId(), inputVo.getRoles());
return userService.update(inputVo);
}
@PostMapping("/checkUserNameUnique")
public boolean checkUserNameUnique(String userName, String userId) {
if (!userService.checkUserNameUnique(userName, userId)) {
return true;
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
return false;
}
2025-08-07 18:04:07 +08:00
2025-09-11 14:29:58 +08:00
@PostMapping("/checkPhoneUnique")
public boolean checkPhoneUnique(String phone, String userId) {
if (!userService.checkPhoneUnique(phone, userId)) {
return true;
2025-08-07 18:04:07 +08:00
}
2025-09-11 14:29:58 +08:00
return false;
}
@DeleteMapping("/delete")
@PreAuthorize("@Permission.hasPermi('pro:sys:user:remove')")
@AutoLog(value = "删除用户信息", operationType = OperationTypeEnum.DELETE, module = "系统管理/用户管理")
public boolean delete(String userId) {
return userService.delete(userId);
}
@PostMapping("/resetPwd")
@PreAuthorize("@Permission.hasPermi('pro:sys:user:resetPwd')")
@AutoLog(value = "重置用户密码", operationType = OperationTypeEnum.UPDATE, module = "系统管理/用户管理")
public boolean resetPwd(SysUserResetPwdVo pwdVo) {
return userService.resetPwd(pwdVo);
}
@PostMapping("/changeStatus")
@PreAuthorize("@Permission.hasPermi('pro:sys:user:edit')")
@AutoLog(value = "修改用户状态", operationType = OperationTypeEnum.UPDATE, module = "系统管理/用户管理")
public boolean changeStatus(String userId, Integer status) {
return userService.changeStatus(userId, status);
}
@GetMapping("/getInfo")
@AutoLog(value = "用户管理", operationType = OperationTypeEnum.SELECT, module = "系统管理/用户管理")
public SysUserModelVo getInfo(String userId) {
return userService.getInfo(userId);
}
@GetMapping("/getUserTree")
public List<TreeNode> getUserTree() {
return userService.getUserTree();
}
2025-08-07 18:04:07 +08:00
}