repair人员用户列表接口和User用户列表接口的增删改查添加 #82

Merged
hnu202326010122 merged 1 commits from jingyou_branch into develop 4 weeks ago

@ -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<ResultVO<Repairman>> 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<ResultVO<Void>> 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<ResultVO<Repairman>> getRepairmanById(@PathVariable String repairmanId) {
try {
Optional<Repairman> 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()));
}
}
}

@ -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<ResultVO<List<User>>> 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<User> userList = userService.getUserList(studentName, userStatus);
List<User> 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<ResultVO<User.UserStatus[]>> 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()));
}
}
/**
* /RepositoryService
*/
@PostMapping("/save") // 已补充@PostMapping的导入
@PreAuthorize("hasRole('SUPER_ADMIN')")
@Operation(summary = "保存学生信息", description = "新增/编辑学生,支持指定状态")
public ResponseEntity<ResultVO<User>> 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<ResultVO<Void>> 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<ResultVO<User>> getUserById(@PathVariable String studentId) {
try {
Optional<User> 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()));
}
}
}

@ -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<Repairman> 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<Repairman> getRepairmanById(String repairmanId) {
return repairmanRepository.findById(repairmanId);
}
}

@ -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<User> 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<User> getUserById(String studentId) {
return userRepository.findById(studentId);
}
}
Loading…
Cancel
Save