增加制水机、供水机关联相关代码 #72

Merged
hnu202326010106 merged 1 commits from wanglei_branch into develop 1 month ago

@ -9,6 +9,7 @@ import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List; // 新增List的导入语句
@RestController
@RequestMapping("/api/web/device")
@ -45,4 +46,32 @@ public class DeviceController {
return ResponseEntity.ok(ResultVO.error(500, "设备删除失败: " + e.getMessage()));
}
}
@PostMapping("/relate")
public ResponseEntity<ResultVO<Void>> relateSupplier(@RequestParam String supplierId, @RequestParam String makerId) {
try {
deviceService.relateSupplierToMaker(supplierId, makerId);
return ResponseEntity.ok(ResultVO.success(null, "关联成功"));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, e.getMessage()));
}
}
// 解除关联
@PostMapping("/unrelate")
public ResponseEntity<ResultVO<Void>> unrelateSupplier(@RequestParam String supplierId) {
try {
deviceService.unrelateSupplierFromMaker(supplierId);
return ResponseEntity.ok(ResultVO.success(null, "解除关联成功"));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, e.getMessage()));
}
}
// 查询制水机关联的供水机
@GetMapping("/maker/{makerId}/suppliers")
public ResponseEntity<ResultVO<List<Device>>> getSuppliers(@PathVariable String makerId) {
List<Device> suppliers = deviceService.getSuppliersByMaker(makerId);
return ResponseEntity.ok(ResultVO.success(suppliers));
}
}

@ -40,6 +40,10 @@ public class Device {
@Column(name = "create_time")
private LocalDateTime createTime = LocalDateTime.now();
// 新增关联的制水机ID仅供水机有值
@Column(name = "parent_maker_id", length = 20)
private String parentMakerId;
// 保留原有的remark方法若表中有该字段可直接映射无则忽略
private String remark;
public void setRemark(String remark) {

@ -38,4 +38,7 @@ public interface DeviceRepository extends JpaRepository<Device, String> {
);
List<Device> findByAreaIdAndStatus(String areaId, Device.DeviceStatus deviceStatus);
// 根据制水机ID查询关联的供水机
List<Device> findByParentMakerIdAndDeviceType(String parentMakerId, Device.DeviceType deviceType);
}

@ -165,4 +165,58 @@ public class DeviceService {
// 3. 执行删除操作
deviceRepository.delete(device);
}
// 新增:关联供水机到制水机
@Transactional
public void relateSupplierToMaker(String supplierId, String makerId) {
// 校验制水机是否存在且类型正确
Device maker = deviceRepository.findById(makerId)
.orElseThrow(() -> new RuntimeException("制水机不存在:" + makerId));
if (maker.getDeviceType() != DeviceType.water_maker) {
throw new RuntimeException("目标设备不是制水机:" + makerId);
}
// 校验供水机是否存在且类型正确
Device supplier = deviceRepository.findById(supplierId)
.orElseThrow(() -> new RuntimeException("供水机不存在:" + supplierId));
if (supplier.getDeviceType() != DeviceType.water_supply) {
throw new RuntimeException("目标设备不是供水机:" + supplierId);
}
// 检查供水机是否已关联其他制水机
if (supplier.getParentMakerId() != null) {
throw new RuntimeException("供水机已关联制水机:" + supplier.getParentMakerId());
}
// 建立关联
supplier.setParentMakerId(makerId);
deviceRepository.save(supplier);
}
// 新增:解除供水机与制水机的关联
@Transactional
public void unrelateSupplierFromMaker(String supplierId) {
Device supplier = deviceRepository.findById(supplierId)
.orElseThrow(() -> new RuntimeException("供水机不存在:" + supplierId));
if (supplier.getDeviceType() != DeviceType.water_supply) {
throw new RuntimeException("目标设备不是供水机:" + supplierId);
}
supplier.setParentMakerId(null);
deviceRepository.save(supplier);
}
// 新增:查询制水机关联的所有供水机
public List<Device> getSuppliersByMaker(String makerId) {
return deviceRepository.findByParentMakerIdAndDeviceType(makerId, DeviceType.water_supply);
}
// 新增:查询供水机所属的制水机
public Device getMakerBySupplier(String supplierId) {
Device supplier = deviceRepository.findById(supplierId)
.orElseThrow(() -> new RuntimeException("供水机不存在:" + supplierId));
if (supplier.getParentMakerId() == null) {
return null;
}
return deviceRepository.findById(supplier.getParentMakerId()).orElse(null);
}
}
Loading…
Cancel
Save