|
|
|
|
@ -5,6 +5,8 @@ import com.campus.water.service.WorkOrderService;
|
|
|
|
|
import com.campus.water.util.ResultVO;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
@ -86,9 +88,36 @@ public class WorkOrderController {
|
|
|
|
|
// 获取可抢工单列表 - 维修人员和管理员可访问
|
|
|
|
|
@GetMapping("/available")
|
|
|
|
|
@PreAuthorize("hasAnyRole('REPAIRMAN','SUPER_ADMIN', 'AREA_ADMIN')")
|
|
|
|
|
public ResultVO<List<WorkOrder>> getAvailableOrders(@RequestParam String areaId) {
|
|
|
|
|
public ResultVO<List<WorkOrder>> getAvailableOrders(
|
|
|
|
|
@RequestParam(required = false) String areaId, // 改为非必填
|
|
|
|
|
Authentication authentication) { // 获取当前登录用户的认证信息
|
|
|
|
|
try {
|
|
|
|
|
List<WorkOrder> orders = workOrderService.getAvailableOrders(areaId);
|
|
|
|
|
// 1. 判断当前用户角色
|
|
|
|
|
boolean isRepairman = authentication.getAuthorities().contains(
|
|
|
|
|
new SimpleGrantedAuthority("ROLE_REPAIRMAN")
|
|
|
|
|
);
|
|
|
|
|
boolean isAdmin = authentication.getAuthorities().stream()
|
|
|
|
|
.anyMatch(auth -> auth.getAuthority().equals("ROLE_SUPER_ADMIN")
|
|
|
|
|
|| auth.getAuthority().equals("ROLE_AREA_ADMIN"));
|
|
|
|
|
|
|
|
|
|
// 2. 角色逻辑校验
|
|
|
|
|
List<WorkOrder> orders;
|
|
|
|
|
if (isRepairman) {
|
|
|
|
|
// 维修人员:必须传areaId,否则抛异常
|
|
|
|
|
if (areaId == null || areaId.trim().isEmpty()) {
|
|
|
|
|
return ResultVO.error(400, "维修人员查询可抢工单必须传入区域ID");
|
|
|
|
|
}
|
|
|
|
|
// 维修人员:查指定区域的可抢工单
|
|
|
|
|
orders = workOrderService.getAvailableOrders(areaId);
|
|
|
|
|
} else if (isAdmin) {
|
|
|
|
|
// 管理员:无需传areaId,查所有区域的可抢工单
|
|
|
|
|
// 给service层传null,让service层识别为"查所有"
|
|
|
|
|
orders = workOrderService.getAvailableOrders(null);
|
|
|
|
|
} else {
|
|
|
|
|
// 非授权角色(理论上被@PreAuthorize拦截,不会走到这)
|
|
|
|
|
return ResultVO.error(403, "无权限访问");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ResultVO.success(orders);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ResultVO.error(500, "获取工单列表失败:" + e.getMessage());
|
|
|
|
|
|