修改终端机代码 #134

Merged
hnu202326010106 merged 1 commits from wanglei_branch into develop 2 weeks ago

@ -203,4 +203,34 @@ public class DeviceController {
}
}*/
// ========== 新增1获取所有片区列表假设从Device表中提取唯一片区ID若有Area实体可直接查询 ==========
@GetMapping("/areas")
@Operation(summary = "获取所有片区列表", description = "返回系统中所有已配置的片区ID和相关信息")
public ResponseEntity<ResultVO<List<String>>> getAllAreas() {
try {
// 从设备表中提取唯一的片区ID若有独立Area表可替换为AreaRepository查询
List<Device> allDevices = deviceService.queryDevices(null, null, null);
List<String> areaList = allDevices.stream()
.map(Device::getAreaId)
.filter(areaId -> areaId != null && !areaId.trim().isEmpty())
.distinct()
.toList();
return ResponseEntity.ok(ResultVO.success(areaList, "片区列表查询成功"));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "片区列表查询失败: " + e.getMessage()));
}
}
// ========== 新增2根据片区ID查询该片区的供水机列表 ==========
@GetMapping("/area/{areaId}/water-supplies")
@Operation(summary = "查询片区内供水机", description = "根据片区ID获取该片区下所有可用的供水机")
public ResponseEntity<ResultVO<List<Device>>> getWaterSuppliesByArea(@PathVariable String areaId) {
try {
List<Device> waterSupplies = deviceService.getWaterSuppliesByArea(areaId);
return ResponseEntity.ok(ResultVO.success(waterSupplies, "片区供水机查询成功"));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "片区供水机查询失败: " + e.getMessage()));
}
}
}

@ -29,80 +29,86 @@ public class TerminalController {
@PostMapping("/add")
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
@Operation(summary = "新增终端", description = "同时保存终端位置和基础映射信息")
// 泛型改为?兼容TerminalManageVO和Map
public ResponseEntity<?> addTerminal(@Valid @RequestBody TerminalManageVO terminalVO) {
@Operation(summary = "新增终端", description = "同时保存终端位置、基础映射信息和片区信息")
public ResponseEntity<ResultVO<TerminalManageVO>> addTerminal(@Valid @RequestBody TerminalManageVO terminalVO) {
try {
TerminalManageVO newTerminal = terminalService.addTerminal(terminalVO);
return new ResponseEntity<>(newTerminal, HttpStatus.CREATED);
return ResponseEntity.ok(ResultVO.success(newTerminal, "终端新增成功"));
} catch (Exception e) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put("message", "终端新增失败: " + e.getMessage());
return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST);
return ResponseEntity.ok(ResultVO.error(500, "终端新增失败: " + e.getMessage()));
}
}
@PutMapping("/update")
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
@Operation(summary = "更新终端", description = "支持更新终端名称、状态、经纬度等信息")
// 泛型改为?兼容TerminalManageVO和Map
public ResponseEntity<?> updateTerminal(@Valid @RequestBody TerminalManageVO terminalVO) {
@Operation(summary = "更新终端", description = "支持更新终端名称、状态、经纬度、片区等信息")
public ResponseEntity<ResultVO<TerminalManageVO>> updateTerminal(@Valid @RequestBody TerminalManageVO terminalVO) {
try {
TerminalManageVO updated = terminalService.updateTerminal(terminalVO);
return ResponseEntity.ok(updated);
return ResponseEntity.ok(ResultVO.success(updated, "终端更新成功"));
} catch (Exception e) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put("message", "终端更新失败: " + e.getMessage());
return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST);
return ResponseEntity.ok(ResultVO.error(500, "终端更新失败: " + e.getMessage()));
}
}
/**
*
*
*/
@DeleteMapping("/delete/{terminalId}")
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
@Operation(summary = "删除终端", description = "先校验设备绑定状态,再级联删除相关数据")
// 该方法成功/失败均返回Map泛型可保留Map<String, String>(无冲突)
public ResponseEntity<Map<String, String>> deleteTerminal(@PathVariable String terminalId) {
public ResponseEntity<ResultVO<String>> deleteTerminal(@PathVariable String terminalId) {
try {
// 调用服务层删除终端
terminalService.deleteTerminal(terminalId);
Map<String, String> successMap = new HashMap<>();
successMap.put("message", "终端删除成功");
return ResponseEntity.ok(successMap);
// 用ResultVO封装成功结果返回提示信息
return ResponseEntity.ok(ResultVO.success("终端删除成功"));
} catch (Exception e) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put("message", "终端删除失败: " + e.getMessage());
return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST);
// 用ResultVO封装错误结果携带异常信息
return ResponseEntity.ok(ResultVO.error(500, "终端删除失败: " + e.getMessage()));
}
}
/**
*
* IDareaId
*/
@GetMapping("/{terminalId}")
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
@Operation(summary = "查询终端详情", description = "根据终端ID获取整合后的完整信息")
// 泛型改为?兼容TerminalManageVO和Map
public ResponseEntity<?> getTerminal(@PathVariable String terminalId) {
public ResponseEntity<ResultVO<TerminalManageVO>> getTerminal(@PathVariable String terminalId) {
try {
// 调用服务层查询终端详情返回包含areaId的VO
TerminalManageVO terminal = terminalService.getTerminalById(terminalId);
return ResponseEntity.ok(terminal);
// 用ResultVO封装成功结果自定义提示信息
return ResponseEntity.ok(ResultVO.success(terminal, "终端查询成功"));
} catch (Exception e) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put("message", "终端查询失败: " + e.getMessage());
return new ResponseEntity<>(errorMap, HttpStatus.NOT_FOUND);
// 用ResultVO封装404错误结果携带异常信息
return ResponseEntity.ok(ResultVO.notFound("终端查询失败: " + e.getMessage()));
}
}
/**
*
* areaId
*/
@GetMapping("/list")
@PreAuthorize("hasAnyRole('SUPER_ADMIN', 'AREA_ADMIN')")
@Operation(summary = "查询终端列表", description = "支持按终端名称模糊筛选")
// 泛型改为?兼容List<TerminalManageVO>和Map
public ResponseEntity<?> getTerminalList(
public ResponseEntity<ResultVO<List<TerminalManageVO>>> getTerminalList(
@RequestParam(required = false) String terminalName) {
try {
List<TerminalManageVO> terminals = terminalService.getTerminalList(terminalName);
return ResponseEntity.ok(terminals);
// 调用服务层查询终端列表返回包含areaId的VO列表
List<TerminalManageVO> terminalList = terminalService.getTerminalList(terminalName);
// 用ResultVO封装成功结果自定义提示信息
return ResponseEntity.ok(ResultVO.success(terminalList, "终端列表查询成功"));
} catch (Exception e) {
Map<String, String> errorMap = new HashMap<>();
errorMap.put("message", "终端列表查询失败: " + e.getMessage());
return new ResponseEntity<>(errorMap, HttpStatus.BAD_REQUEST);
// 用ResultVO封装错误结果携带异常信息
return ResponseEntity.ok(ResultVO.error(500, "终端列表查询失败: " + e.getMessage()));
}
}
}

@ -22,6 +22,9 @@ public class DeviceTerminalMapping {
@Column(name = "device_id", length = 20)
private String deviceId;
@Column(name = "area_id", length = 36)
private String areaId;
@Column(name = "terminal_id", length = 20)
private String terminalId;

@ -31,4 +31,7 @@ public class TerminalManageVO {
// 设备ID关联的设备来自映射表
private String deviceId;
// ========== 新增片区ID字段前端传递选中的片区ID ==========
private String areaId;
}

@ -12,6 +12,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@ -112,23 +113,28 @@ public class DeviceService {
/**
*
*/
// ========== 改造原有bindTerminal方法添加areaId参数和校验 ==========
@Transactional
public DeviceTerminalMapping bindTerminal(String deviceId, String terminalId, String terminalName) {
// 校验设备是否存在
getDeviceById(deviceId);
// 检查终端是否已绑定
public DeviceTerminalMapping bindTerminal(String deviceId, String terminalId, String terminalName, String areaId) {
// 1. 校验片区非空(终端必须归属片区)
if (areaId == null || areaId.trim().isEmpty()) {
throw new RuntimeException("片区ID不能为空请先选择片区");
}
// 2. 校验设备存在且属于该片区的供水机
validateDeviceBelongsToArea(deviceId, areaId);
// 3. 检查终端是否已绑定(原有逻辑保留)
Optional<DeviceTerminalMapping> existing = terminalMappingRepository.findByTerminalId(terminalId);
if (existing.isPresent()) {
throw new RuntimeException("终端已绑定设备:" + existing.get().getDeviceId());
}
// 4. 构建映射对象新增设置areaId
DeviceTerminalMapping mapping = new DeviceTerminalMapping();
mapping.setDeviceId(deviceId);
mapping.setTerminalId(terminalId);
mapping.setTerminalName(terminalName);
mapping.setTerminalStatus(TerminalStatus.active);
mapping.setInstallDate(java.time.LocalDate.now());
mapping.setTerminalStatus(DeviceTerminalMapping.TerminalStatus.active);
mapping.setInstallDate(LocalDate.now());
mapping.setAreaId(areaId); // 保存终端所属片区
return terminalMappingRepository.save(mapping);
}
@ -222,6 +228,29 @@ public class DeviceService {
return deviceRepository.findById(supplier.getParentMakerId()).orElse(null);
}
// ========== 新增1查询指定片区的所有供水机 ==========
public List<Device> getWaterSuppliesByArea(String areaId) {
// 1. 校验片区ID非空
if (areaId == null || areaId.trim().isEmpty()) {
throw new RuntimeException("片区ID不能为空");
}
// 2. 查询该片区下类型为供水机的设备
return deviceRepository.findByAreaIdAndDeviceType(areaId, Device.DeviceType.water_supply);
}
// ========== 新增2校验设备是否属于指定片区 ==========
public void validateDeviceBelongsToArea(String deviceId, String areaId) {
// 1. 校验设备存在
Device device = getDeviceById(deviceId);
// 2. 校验设备是供水机
if (!Device.DeviceType.water_supply.equals(device.getDeviceType())) {
throw new RuntimeException("只能关联供水机设备,当前设备类型不合法");
}
// 3. 校验设备所属片区与选中片区一致
if (!areaId.equals(device.getAreaId())) {
throw new RuntimeException("该供水机不属于所选片区(设备所属片区:" + device.getAreaId() + "");
}
}
}

@ -21,6 +21,8 @@ public class TerminalServiceImpl implements TerminalService {
private final WaterTerminalLocationRepository locationRepository;
private final DeviceTerminalMappingRepository mappingRepository;
// ========== 新增注入DeviceService用于片区和供水机校验 ==========
private final DeviceService deviceService;
@Override
@Transactional
@ -34,6 +36,14 @@ public class TerminalServiceImpl implements TerminalService {
if (terminalVO.getLongitude() == null || terminalVO.getLatitude() == null) {
throw new RuntimeException("终端经度和纬度为必填项,不可为空");
}
// ========== 新增校验片区ID非空 ==========
if (terminalVO.getAreaId() == null || terminalVO.getAreaId().trim().isEmpty()) {
throw new RuntimeException("片区ID不能为空请先选择片区");
}
// ========== 新增若传递了deviceId校验供水机是否属于该片区 ==========
if (terminalVO.getDeviceId() != null && !terminalVO.getDeviceId().trim().isEmpty()) {
deviceService.validateDeviceBelongsToArea(terminalVO.getDeviceId(), terminalVO.getAreaId());
}
WaterTerminalLocation location = new WaterTerminalLocation();
location.setTerminalId(terminalVO.getTerminalId());
location.setLongitude(terminalVO.getLongitude());
@ -49,6 +59,7 @@ public class TerminalServiceImpl implements TerminalService {
: terminalVO.getTerminalStatus());
mapping.setDeviceId(terminalVO.getDeviceId());
mapping.setInstallDate(terminalVO.getInstallDate());
mapping.setAreaId(terminalVO.getAreaId()); // 保存片区ID
mappingRepository.save(mapping);
// 4. 封装返回结果
@ -61,7 +72,12 @@ public class TerminalServiceImpl implements TerminalService {
// 1. 校验终端是否存在通过位置表查询复用原有findById方法
WaterTerminalLocation existingLocation = locationRepository.findById(terminalVO.getTerminalId())
.orElseThrow(() -> new RuntimeException("终端不存在,无法更新:" + terminalVO.getTerminalId()));
// ========== 新增:若更新片区/设备,校验供水机与片区的关联 ==========
if (terminalVO.getAreaId() != null && !terminalVO.getAreaId().trim().isEmpty()) {
if (terminalVO.getDeviceId() != null && !terminalVO.getDeviceId().trim().isEmpty()) {
deviceService.validateDeviceBelongsToArea(terminalVO.getDeviceId(), terminalVO.getAreaId());
}
}
// 2. 更新终端位置信息仅更新有值字段复用原有save方法
if (terminalVO.getLongitude() != null) {
existingLocation.setLongitude(terminalVO.getLongitude());
@ -87,6 +103,9 @@ public class TerminalServiceImpl implements TerminalService {
if (terminalVO.getInstallDate() != null) {
existingMapping.setInstallDate(terminalVO.getInstallDate());
}
if (terminalVO.getAreaId() != null && !terminalVO.getAreaId().trim().isEmpty()) {
existingMapping.setAreaId(terminalVO.getAreaId()); // 更新片区ID
}
mappingRepository.save(existingMapping);
// 4. 封装返回结果
@ -166,6 +185,7 @@ public void deleteTerminal(String terminalId) {
vo.setTerminalStatus(mapping.getTerminalStatus());
vo.setDeviceId(mapping.getDeviceId());
vo.setInstallDate(mapping.getInstallDate());
vo.setAreaId(mapping.getAreaId()); // 封装片区ID返回给前端
return vo;
}
}
Loading…
Cancel
Save