feat: 添加按部门ID查询用户列表接口
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package com.huertian.jinduguanli.controller;
|
||||
|
||||
import com.huertian.jinduguanli.common.ApiResponse;
|
||||
import com.huertian.jinduguanli.common.ErrorCode;
|
||||
import com.huertian.jinduguanli.dto.CreateUserRequest;
|
||||
import com.huertian.jinduguanli.dto.LoginResponse;
|
||||
import com.huertian.jinduguanli.dto.UserLoginRequest;
|
||||
@ -14,6 +15,8 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
@ -85,4 +88,18 @@ public class UserController {
|
||||
logger.info("收到禁用用户请求,用户ID: {}", userId);
|
||||
return userService.disableUser(userId);
|
||||
}
|
||||
|
||||
@GetMapping("/department/{departmentId}")
|
||||
public ApiResponse<List<User>> getUsersByDepartment(@PathVariable Long departmentId) {
|
||||
logger.info("收到查询部门用户请求,部门ID: {}", departmentId);
|
||||
try {
|
||||
List<User> users = userService.findByDepartmentId(departmentId);
|
||||
// 清除密码信息
|
||||
users.forEach(user -> user.setPassword(null));
|
||||
return ApiResponse.success(users);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询部门用户失败", e);
|
||||
return new ApiResponse<>(ErrorCode.SYSTEM_ERROR, "获取部门用户列表失败", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,4 +17,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
@Query(value = "SELECT * FROM users LIMIT :limit OFFSET :offset", nativeQuery = true)
|
||||
List<User> findAllWithPagination(@Param("offset") int offset, @Param("limit") int limit);
|
||||
|
||||
@Query("SELECT u FROM User u WHERE u.departmentId = :departmentId AND u.status = 1")
|
||||
List<User> findByDepartmentIdAndNormalStatus(@Param("departmentId") Long departmentId);
|
||||
}
|
||||
|
||||
@ -168,4 +168,9 @@ public class UserService implements UserDetailsService {
|
||||
savedUser.setPassword(null);
|
||||
return savedUser;
|
||||
}
|
||||
|
||||
public List<User> findByDepartmentId(Long departmentId) {
|
||||
logger.info("查询部门用户列表,部门ID: {}", departmentId);
|
||||
return userRepository.findByDepartmentIdAndNormalStatus(departmentId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user