200 lines
7.5 KiB
Java
200 lines
7.5 KiB
Java
package com.designhub.controller;
|
||
|
||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
import com.designhub.common.Result;
|
||
import com.designhub.dto.UserOffboardingRequest;
|
||
import com.designhub.entity.User;
|
||
import com.designhub.service.UserOffboardingService;
|
||
import com.designhub.service.UserService;
|
||
import com.designhub.util.JwtUtil;
|
||
import com.designhub.vo.UserOffboardingExecuteVO;
|
||
import com.designhub.vo.UserOffboardingPreviewVO;
|
||
import io.swagger.v3.oas.annotations.Operation;
|
||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
import lombok.RequiredArgsConstructor;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 用户管理Controller
|
||
*/
|
||
@Tag(name = "用户管理", description = "用户管理相关API")
|
||
@RestController
|
||
@RequestMapping("/api/users")
|
||
@RequiredArgsConstructor
|
||
@Slf4j
|
||
public class UserController {
|
||
|
||
private final UserService userService;
|
||
private final UserOffboardingService userOffboardingService;
|
||
private final JwtUtil jwtUtil;
|
||
|
||
@Operation(summary = "分页查询用户列表")
|
||
@GetMapping
|
||
public Result<Page<User>> getUserList(
|
||
@RequestParam(defaultValue = "1") int pageNum,
|
||
@RequestParam(defaultValue = "10") int pageSize,
|
||
@RequestParam(required = false) String keyword,
|
||
@RequestParam(required = false) Long deptId,
|
||
@RequestParam(required = false) Integer status,
|
||
@RequestParam(required = false, defaultValue = "false") Boolean includeChildren
|
||
) {
|
||
return Result.success(userService.getUserList(pageNum, pageSize, keyword, deptId, status, includeChildren));
|
||
}
|
||
|
||
@Operation(summary = "获取全部用户列表")
|
||
@GetMapping("/all")
|
||
public Result<List<User>> getAllUsers() {
|
||
return Result.success(userService.getAllUsers());
|
||
}
|
||
|
||
@Operation(summary = "获取所有需求部门的成员列表")
|
||
@GetMapping("/demand-dept-members")
|
||
public Result<List<User>> getAllDemandDeptMembers() {
|
||
return Result.success(userService.getAllDemandDeptMembers());
|
||
}
|
||
|
||
@Operation(summary = "根据ID获取用户")
|
||
@GetMapping("/{id}")
|
||
public Result<User> getUserById(@PathVariable Long id) {
|
||
return Result.success(userService.getUserById(id));
|
||
}
|
||
|
||
@Operation(summary = "创建用户")
|
||
@PostMapping
|
||
public Result<User> createUser(@RequestBody User user) {
|
||
return Result.success(userService.createUser(user));
|
||
}
|
||
|
||
@Operation(summary = "更新用户")
|
||
@PutMapping("/{id}")
|
||
public Result<Boolean> updateUser(@PathVariable Long id, @RequestBody User user) {
|
||
user.setId(id);
|
||
return Result.success(userService.updateUser(user));
|
||
}
|
||
|
||
@Operation(summary = "删除用户")
|
||
@DeleteMapping("/{id}")
|
||
public Result<Boolean> deleteUser(@PathVariable Long id) {
|
||
return Result.success(userService.deleteUser(id));
|
||
}
|
||
|
||
@Operation(summary = "重置密码")
|
||
@PostMapping("/{id}/reset-password")
|
||
public Result<Boolean> resetPassword(
|
||
@PathVariable Long id,
|
||
@RequestBody Map<String, String> params
|
||
) {
|
||
String newPassword = params.getOrDefault("newPassword", "123456");
|
||
return Result.success(userService.resetPassword(id, newPassword));
|
||
}
|
||
|
||
@Operation(summary = "修改密码")
|
||
@PostMapping("/{id}/change-password")
|
||
public Result<Boolean> changePassword(
|
||
@PathVariable Long id,
|
||
@RequestBody Map<String, String> params
|
||
) {
|
||
String oldPassword = params.get("oldPassword");
|
||
String newPassword = params.get("newPassword");
|
||
return Result.success(userService.changePassword(id, oldPassword, newPassword));
|
||
}
|
||
|
||
@Operation(summary = "为用户分配角色")
|
||
@PostMapping("/{id}/roles")
|
||
public Result<Boolean> assignRoles(
|
||
@PathVariable Long id,
|
||
@RequestBody List<Long> roleIds
|
||
) {
|
||
return Result.success(userService.assignRoles(id, roleIds));
|
||
}
|
||
|
||
@Operation(summary = "获取用户的角色ID列表")
|
||
@GetMapping("/{id}/roles")
|
||
public Result<List<Long>> getUserRoleIds(@PathVariable Long id) {
|
||
return Result.success(userService.getUserRoleIds(id));
|
||
}
|
||
|
||
@Operation(summary = "启用/禁用用户")
|
||
@PutMapping("/{id}/status")
|
||
public Result<Boolean> toggleUserStatus(
|
||
@PathVariable Long id,
|
||
@RequestParam Integer status
|
||
) {
|
||
return Result.success(userService.toggleUserStatus(id, status));
|
||
}
|
||
|
||
@Operation(summary = "调整用户所属部门")
|
||
@PutMapping("/{id}/department")
|
||
public Result<Boolean> updateUserDepartment(
|
||
@PathVariable Long id,
|
||
@RequestBody Map<String, Long> params
|
||
) {
|
||
Long deptId = params.get("deptId");
|
||
log.info("调整用户部门请求: userId={}, deptId={}, params={}", id, deptId, params);
|
||
boolean result = userService.updateUserDepartment(id, deptId);
|
||
log.info("调整用户部门结果: userId={}, deptId={}, result={}", id, deptId, result);
|
||
return Result.success(result);
|
||
}
|
||
|
||
@Operation(summary = "获取设计师列表及工作量", description = "获取部门内的设计师列表,包含每个设计师当前进行中的任务数量")
|
||
@GetMapping("/designers-with-workload")
|
||
public Result<List<Map<String, Object>>> getDesignersWithWorkload(
|
||
@RequestParam(required = false) Long deptId,
|
||
@RequestParam(required = false) Long taskTypeId,
|
||
@RequestHeader("Authorization") String authHeader
|
||
) {
|
||
// 如果传入了 taskTypeId,则根据任务类型配置的接单部门来获取设计师
|
||
if (taskTypeId != null) {
|
||
log.info("根据任务类型获取设计师列表: taskTypeId={}", taskTypeId);
|
||
return Result.success(userService.getDesignersByTaskType(taskTypeId));
|
||
}
|
||
|
||
// 如果没有传入deptId,则默认为当前用户的部门
|
||
if (deptId == null) {
|
||
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
||
Long userId = jwtUtil.getUserIdFromToken(token);
|
||
User currentUser = userService.getUserById(userId);
|
||
if (currentUser != null) {
|
||
deptId = currentUser.getDeptId();
|
||
}
|
||
}
|
||
|
||
log.info("获取设计师列表及工作量: deptId={}", deptId);
|
||
return Result.success(userService.getDesignersWithWorkload(deptId));
|
||
}
|
||
|
||
@Operation(summary = "设置/取消管理员", description = "设置或取消用户的管理员身份")
|
||
@PutMapping("/{id}/admin")
|
||
public Result<Boolean> setAdmin(
|
||
@PathVariable Long id,
|
||
@RequestBody Map<String, Integer> params
|
||
) {
|
||
Integer isAdmin = params.get("isAdmin");
|
||
log.info("设置用户管理员身份: userId={}, isAdmin={}", id, isAdmin);
|
||
return Result.success(userService.setAdmin(id, isAdmin));
|
||
}
|
||
|
||
@Operation(summary = "离职交接预览")
|
||
@PostMapping("/offboarding/preview")
|
||
public Result<UserOffboardingPreviewVO> previewOffboarding(
|
||
@RequestBody UserOffboardingRequest request
|
||
) {
|
||
return Result.success(userOffboardingService.preview(request));
|
||
}
|
||
|
||
@Operation(summary = "执行离职交接")
|
||
@PostMapping("/offboarding/execute")
|
||
public Result<UserOffboardingExecuteVO> executeOffboarding(
|
||
@RequestHeader("Authorization") String authHeader,
|
||
@RequestBody UserOffboardingRequest request
|
||
) {
|
||
String token = jwtUtil.extractTokenFromHeader(authHeader);
|
||
Long operatorId = jwtUtil.getUserIdFromToken(token);
|
||
return Result.success(userOffboardingService.execute(request, operatorId));
|
||
}
|
||
}
|