feat: 添加按部门ID查询用户列表接口

This commit is contained in:
Hvemi_han 2024-12-20 16:48:32 +08:00
parent 2ac66ed9eb
commit 58c41317ce
4 changed files with 73 additions and 0 deletions
API文档.md
src/main/java/com/huertian/jinduguanli

View File

@ -221,6 +221,54 @@
}
```
### 7. 查询部门用户列表
- **接口**`GET /api/users/department/{departmentId}`
- **描述**:获取指定部门下的所有正常状态用户列表
- **认证**:需要
- **路径参数**
- `departmentId`: 部门 ID
- **成功响应**
```json
{
"code": 10000,
"message": "成功",
"data": [
{
"id": 2,
"username": "普通管理员账号2",
"email": "user2@qq.com",
"password": null,
"departmentId": 1,
"roles": 2,
"jobs": 2,
"avatar": null,
"creatorId": 1,
"status": 1,
"createdAt": 1734504506,
"updatedAt": 1734504506,
"enabled": true,
"authorities": [
{
"authority": "ROLE_USER"
}
],
"accountNonExpired": true,
"accountNonLocked": true,
"credentialsNonExpired": true
}
]
}
```
- **错误响应**
```json
{
"code": 50000,
"message": "获取部门用户列表失败",
"data": null
}
```
## 课程任务接口
### 1. 获取课程任务列表

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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);
}
}