From ca2d95008fc48b965f9842988a3307fc18c02c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E7=AB=9E=E7=94=B1?= <1193626695@qq.com> Date: Sun, 14 Dec 2025 14:12:20 +0800 Subject: [PATCH] =?UTF-8?q?repair=E4=BA=BA=E5=91=98=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E5=92=8CUser=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3=E7=9A=84=E5=A2=9E?= =?UTF-8?q?=E5=88=A0=E6=94=B9=E6=9F=A5=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/web/RepairmanController.java | 56 +++++++++- .../water/controller/web/UserController.java | 103 ++++++++++++++---- .../water/service/RepairmanService.java | 36 +++++- .../com/campus/water/service/UserService.java | 40 ++++++- 4 files changed, 203 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/campus/water/controller/web/RepairmanController.java b/src/main/java/com/campus/water/controller/web/RepairmanController.java index 49dc261..6e47322 100644 --- a/src/main/java/com/campus/water/controller/web/RepairmanController.java +++ b/src/main/java/com/campus/water/controller/web/RepairmanController.java @@ -5,20 +5,19 @@ import com.campus.water.service.RepairmanService; import com.campus.water.util.ResultVO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Optional; @RestController @RequestMapping("/api/web/repairman") @RequiredArgsConstructor -@Tag(name = "维修人员管理接口", description = "Web管理端维修人员列表查询接口") +@Tag(name = "维修人员管理接口", description = "Web管理端维修人员列表查询、新增、修改、删除接口") public class RepairmanController { private final RepairmanService repairmanService; @@ -69,4 +68,51 @@ public class RepairmanController { return ResponseEntity.ok(ResultVO.error(500, "获取状态列表失败:" + e.getMessage())); } } + + /** + * 新增/编辑维修人员 + */ + @PostMapping("/save") + @PreAuthorize("hasRole('SUPER_ADMIN')") // 仅超级管理员可操作 + @Operation(summary = "保存维修人员信息", description = "新增或编辑维修人员信息,ID存在则更新,不存在则新增") + public ResponseEntity> saveRepairman(@Valid @RequestBody Repairman repairman) { + try { + Repairman savedRepairman = repairmanService.saveRepairman(repairman); + return ResponseEntity.ok(ResultVO.success(savedRepairman, + repairman.getRepairmanId() == null ? "维修人员新增成功" : "维修人员更新成功")); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "保存维修人员失败: " + e.getMessage())); + } + } + + /** + * 删除维修人员 + */ + @DeleteMapping("/{repairmanId}") + @PreAuthorize("hasRole('SUPER_ADMIN')") // 仅超级管理员可操作 + @Operation(summary = "删除维修人员", description = "根据维修人员ID删除指定维修人员") + public ResponseEntity> deleteRepairman(@PathVariable String repairmanId) { + try { + repairmanService.deleteRepairman(repairmanId); + return ResponseEntity.ok(ResultVO.success(null, "维修人员删除成功")); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "删除维修人员失败: " + e.getMessage())); + } + } + + /** + * 根据ID查询维修人员详情 + */ + @GetMapping("/{repairmanId}") + @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')") + @Operation(summary = "获取维修人员详情", description = "根据ID查询维修人员详细信息") + public ResponseEntity> getRepairmanById(@PathVariable String repairmanId) { + try { + Optional repairman = repairmanService.getRepairmanById(repairmanId); + return repairman.map(value -> ResponseEntity.ok(ResultVO.success(value))) + .orElseGet(() -> ResponseEntity.ok(ResultVO.error(404, "维修人员不存在"))); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "查询维修人员详情失败: " + e.getMessage())); + } + } } \ No newline at end of file diff --git a/src/main/java/com/campus/water/controller/web/UserController.java b/src/main/java/com/campus/water/controller/web/UserController.java index 28df30d..fdaa784 100644 --- a/src/main/java/com/campus/water/controller/web/UserController.java +++ b/src/main/java/com/campus/water/controller/web/UserController.java @@ -8,46 +8,105 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; // 补充Web注解的统一导入 import java.util.List; +import java.util.Optional; // 补充Optional的导入 + @RestController @RequestMapping("/api/web/user") @RequiredArgsConstructor -@Tag(name = "学生管理接口", description = "Web管理端学生列表查询接口") +@Tag(name = "学生管理接口", description = "Web管理端学生操作接口") public class UserController { - private final UserService userService; + private final UserService userService; // 只依赖Service,不直接依赖Repository + /** - * 加载学生用户列表(支持筛选) - * @param studentName 学生姓名模糊查询(可选) - * @param status 状态筛选(可选,值:active/inactive) + * 获取学生列表(支持姓名/状态筛选) */ @GetMapping("/list") - @PreAuthorize("hasAnyRole('STUDENT', 'SUPER_ADMIN', 'AREA_ADMIN', 'VIEWER')")// 仅管理员可访问 - @Operation(summary = "获取学生用户列表", description = "支持按姓名和状态筛选学生") + @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')") + @Operation(summary = "获取学生列表", description = "支持按姓名模糊搜索、按状态筛选") public ResponseEntity>> getUserList( @RequestParam(required = false) String studentName, - @RequestParam(required = false) String status + @RequestParam(required = false) User.UserStatus status ) { try { - // 转换状态参数为枚举类型 - User.UserStatus userStatus = status != null ? User.UserStatus.valueOf(status) : null; - - // 调用服务层查询 - List userList = userService.getUserList(studentName, userStatus); + List userList = userService.getUserList(studentName, status); return ResponseEntity.ok(ResultVO.success(userList)); - } catch (IllegalArgumentException e) { - // 处理枚举参数错误 - return ResponseEntity.ok(ResultVO.error(400, "无效的状态参数: " + e.getMessage())); } catch (Exception e) { - // 处理其他异常 - return ResponseEntity.ok(ResultVO.error(500, "查询学生列表失败: " + e.getMessage())); + return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage())); + } + } + + + /** + * 获取所有学生状态枚举 + */ + @GetMapping("/status") + @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')") + @Operation(summary = "获取学生状态列表", description = "返回所有可选状态(active/inactive)") + public ResponseEntity> getAllStatus() { + try { + User.UserStatus[] statuses = User.UserStatus.values(); + return ResponseEntity.ok(ResultVO.success(statuses)); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "获取状态列表失败:" + e.getMessage())); + } + } + + + /** + * 新增/编辑学生(移除直接依赖Repository的逻辑,交给Service处理) + */ + @PostMapping("/save") // 已补充@PostMapping的导入 + @PreAuthorize("hasRole('SUPER_ADMIN')") + @Operation(summary = "保存学生信息", description = "新增/编辑学生,支持指定状态") + public ResponseEntity> saveUser(@RequestBody User user) { // 已补充@RequestBody的导入 + try { + // 学号唯一性校验移到Service层处理,Controller只调用Service + User savedUser = userService.saveUser(user); + return ResponseEntity.ok(ResultVO.success( + savedUser, + user.getStudentId() == null ? "学生新增成功" : "学生信息更新成功" + )); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "保存失败:" + e.getMessage())); + } + } + + + /** + * 删除学生 + */ + @DeleteMapping("/{studentId}") // 已补充@DeleteMapping的导入 + @PreAuthorize("hasRole('SUPER_ADMIN')") + @Operation(summary = "删除学生", description = "按学号删除学生") + public ResponseEntity> deleteUser(@PathVariable String studentId) { // 已补充@PathVariable的导入 + try { + userService.deleteUser(studentId); + return ResponseEntity.ok(ResultVO.success(null, "删除成功")); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "删除失败:" + e.getMessage())); + } + } + + + /** + * 根据学号查询学生详情 + */ + @GetMapping("/{studentId}") + @PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')") + @Operation(summary = "获取学生详情", description = "按学号查询学生详细信息") + public ResponseEntity> getUserById(@PathVariable String studentId) { + try { + Optional user = userService.getUserById(studentId); + return user.map(value -> ResponseEntity.ok(ResultVO.success(value))) + .orElseGet(() -> ResponseEntity.ok(ResultVO.error(404, "学生不存在"))); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "查询失败:" + e.getMessage())); } } } \ No newline at end of file diff --git a/src/main/java/com/campus/water/service/RepairmanService.java b/src/main/java/com/campus/water/service/RepairmanService.java index a8163d2..bb0a02f 100644 --- a/src/main/java/com/campus/water/service/RepairmanService.java +++ b/src/main/java/com/campus/water/service/RepairmanService.java @@ -4,8 +4,9 @@ import com.campus.water.entity.Repairman; import com.campus.water.mapper.RepairmanRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; - +import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; /** * 维修人员服务类(合并接口+实现) @@ -24,7 +25,7 @@ public class RepairmanService { * @return 符合条件的维修人员列表 */ public List getRepairmanList(String name, String areaId, Repairman.RepairmanStatus status) { - // 组合查询条件(与原实现逻辑完全一致) + // 组合查询条件 if (name != null && !name.isEmpty() && areaId != null && !areaId.isEmpty() && status != null) { return repairmanRepository.findByRepairmanNameContainingAndAreaIdAndStatus(name, areaId, status); } else if (name != null && !name.isEmpty() && areaId != null && !areaId.isEmpty()) { @@ -52,4 +53,35 @@ public class RepairmanService { public Repairman.RepairmanStatus[] getAllStatus() { return Repairman.RepairmanStatus.values(); } + + /** + * 新增/编辑维修人员 + */ + public Repairman saveRepairman(Repairman repairman) { + // 设置时间戳 + if (repairman.getCreatedTime() == null) { + repairman.setCreatedTime(LocalDateTime.now()); + } + + // 新增时默认状态为空闲 + if (repairman.getRepairmanId() == null && repairman.getStatus() == null) { + repairman.setStatus(Repairman.RepairmanStatus.idle); + } + + return repairmanRepository.save(repairman); + } + + /** + * 删除维修人员 + */ + public void deleteRepairman(String repairmanId) { + repairmanRepository.deleteById(repairmanId); + } + + /** + * 根据ID查询维修人员 + */ + public Optional getRepairmanById(String repairmanId) { + return repairmanRepository.findById(repairmanId); + } } \ No newline at end of file diff --git a/src/main/java/com/campus/water/service/UserService.java b/src/main/java/com/campus/water/service/UserService.java index c2c5826..67b9667 100644 --- a/src/main/java/com/campus/water/service/UserService.java +++ b/src/main/java/com/campus/water/service/UserService.java @@ -4,8 +4,9 @@ import com.campus.water.entity.User; import com.campus.water.mapper.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; - +import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -17,10 +18,10 @@ public class UserService { * 获取学生用户列表(支持按姓名和状态筛选) */ public List getUserList(String studentName, User.UserStatus status) { - if (studentName != null && status != null) { + if (studentName != null && !studentName.isEmpty() && status != null) { // 按姓名模糊查询和状态筛选 return userRepository.findByStudentNameContainingAndStatus(studentName, status); - } else if (studentName != null) { + } else if (studentName != null && !studentName.isEmpty()) { // 仅按姓名模糊查询 return userRepository.findByStudentNameContaining(studentName); } else if (status != null) { @@ -31,4 +32,37 @@ public class UserService { return userRepository.findAll(); } } + + /** + * 新增/编辑学生 + */ + public User saveUser(User user) { + // 新增时设置默认值 + if (user.getCreateTime() == null) { + user.setCreateTime(LocalDateTime.now()); + } + // 新增学生默认状态为激活 + if (user.getStudentId() == null && user.getStatus() == null) { + user.setStatus(User.UserStatus.active); + } + user.setUpdatedTime(LocalDateTime.now()); + return userRepository.save(user); + } + + /** + * 删除学生 + */ + public void deleteUser(String studentId) { + if (!userRepository.existsById(studentId)) { + throw new RuntimeException("学生不存在:" + studentId); + } + userRepository.deleteById(studentId); + } + + /** + * 根据学号查询学生 + */ + public Optional getUserById(String studentId) { + return userRepository.findById(studentId); + } } \ No newline at end of file -- 2.34.1