diff --git a/src/main/java/com/campus/water/controller/web/DeviceController.java b/src/main/java/com/campus/water/controller/web/DeviceController.java index 660f043..97542f6 100644 --- a/src/main/java/com/campus/water/controller/web/DeviceController.java +++ b/src/main/java/com/campus/water/controller/web/DeviceController.java @@ -2,6 +2,8 @@ package com.campus.water.controller.web; import com.campus.water.entity.Device; import com.campus.water.service.DeviceService; +import com.campus.water.entity.Repairman; +import com.campus.water.mapper.RepairmanRepository; import com.campus.water.util.ResultVO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -9,6 +11,9 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.security.core.Authentication; +import org.springframework.security.access.prepost.PreAuthorize; + import java.util.List; // 新增List的导入语句 @RestController @@ -18,7 +23,7 @@ import java.util.List; // 新增List的导入语句 public class DeviceController { private final DeviceService deviceService; - + private final RepairmanRepository repairmanRepository; /** * 新增设备 */ @@ -74,4 +79,39 @@ public class DeviceController { List suppliers = deviceService.getSuppliersByMaker(makerId); return ResponseEntity.ok(ResultVO.success(suppliers)); } + + /** + * 维修人员查询本辖区设备(按类型筛选) + */ + @GetMapping("/repairman/area-devices-by-type") + @PreAuthorize("hasRole('REPAIRMAN')") // 仅维修人员角色可访问 + @Operation(summary = "维修人员查询辖区设备(按类型)", description = "维修人员查看本辖区内指定类型的设备列表") + public ResponseEntity>> getAreaDevicesByTypeForRepairman( + @RequestParam String deviceType, // 必选参数:设备类型(water_maker/water_supply) + Authentication authentication) { + try { + // 1. 获取当前登录维修人员ID(从Spring Security上下文) + String repairmanId = authentication.getName(); + + // 2. 查询维修人员所属区域ID + Repairman repairman = repairmanRepository.findById(repairmanId) + .orElseThrow(() -> new RuntimeException("维修人员信息不存在")); + String areaId = repairman.getAreaId(); + if (areaId == null || areaId.isEmpty()) { + return ResponseEntity.ok(ResultVO.error(400, "维修人员未分配辖区")); + } + + // 3. 转换设备类型参数为枚举 + Device.DeviceType type = Device.DeviceType.valueOf(deviceType); + + // 4. 调用现有Service方法查询(仅传areaId和deviceType,status传null) + List devices = deviceService.queryDevices(areaId, type, null); + + return ResponseEntity.ok(ResultVO.success(devices)); + } catch (IllegalArgumentException e) { + return ResponseEntity.ok(ResultVO.error(400, "无效的设备类型: " + deviceType)); + } catch (Exception e) { + return ResponseEntity.ok(ResultVO.error(500, "查询设备失败: " + e.getMessage())); + } + } } \ No newline at end of file