1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
@Service @Slf4j public class DeviceService {
@Autowired private BatteryCabinetMapper batteryCabinetMapper; @Autowired private BatteryOperationMapper batteryOperationMapper; @Autowired private AccessServiceClient accessServiceClient; @Autowired private OrderServiceClient orderServiceClient; @Autowired private KafkaTemplate<String, String> kafkaTemplate; @Autowired private RedissonClient redissonClient;
public CabinetListResult getNearbyCabinets(CabinetQueryRequest request) { try { List<BatteryCabinet> cabinets = batteryCabinetMapper.selectNearbyCabinets( request.getLatitude(), request.getLongitude(), request.getRadius()); List<CabinetInfo> cabinetInfoList = cabinets.stream() .map(this::convertToCabinetInfo) .collect(Collectors.toList()); CabinetListResult result = new CabinetListResult(); result.setSuccess(true); result.setCabinets(cabinetInfoList); result.setTotal(cabinetInfoList.size()); result.setMessage("查询成功"); return result; } catch (Exception e) { log.error("查询附近换电柜失败: error={}", e.getMessage(), e); return CabinetListResult.failed("查询失败: " + e.getMessage()); } }
public CabinetDetailResult getCabinetDetail(Long cabinetId) { try { BatteryCabinet cabinet = batteryCabinetMapper.selectById(cabinetId); if (cabinet == null) { return CabinetDetailResult.failed("换电柜不存在"); } List<CabinetSlot> slots = batteryCabinetMapper.selectCabinetSlots(cabinetId); CabinetDetailResult result = new CabinetDetailResult(); result.setSuccess(true); result.setCabinet(convertToCabinetInfo(cabinet)); result.setSlots(slots); result.setMessage("查询成功"); return result; } catch (Exception e) { log.error("查询换电柜详情失败: cabinetId={}, error={}", cabinetId, e.getMessage(), e); return CabinetDetailResult.failed("查询失败: " + e.getMessage()); } }
@Transactional(rollbackFor = Exception.class) public BatteryOperationResult processBatteryOperation( BatteryOperationProcessRequest request) { String lockKey = "battery:operation:lock:" + request.getCabinetId() + ":" + request.getSlotNo(); RLock lock = redissonClient.getLock(lockKey); try { if (lock.tryLock(10, TimeUnit.SECONDS)) { BatteryOperation operation = createBatteryOperation(request); batteryOperationMapper.insert(operation); DeviceControlCommand command = buildDeviceControlCommand(request, operation); DeviceControlResult controlResult = accessServiceClient.sendDeviceCommand(command); if (!controlResult.isSuccess()) { operation.setStatus(BatteryOperationStatus.FAILED.getCode()); operation.setErrorMessage(controlResult.getMessage()); operation.setUpdateTime(new Date()); batteryOperationMapper.updateById(operation); return BatteryOperationResult.failed("设备控制指令下发失败: " + controlResult.getMessage()); } operation.setStatus(BatteryOperationStatus.PROCESSING.getCode()); operation.setCommandId(controlResult.getCommandId()); operation.setUpdateTime(new Date()); batteryOperationMapper.updateById(operation); asyncProcessDeviceResponse(operation, controlResult); log.info("换电操作处理成功: operationId={}, operationType={}, cabinetId={}", operation.getId(), request.getOperationType(), request.getCabinetId()); BatteryOperationResult result = new BatteryOperationResult(); result.setSuccess(true); result.setOperationId(operation.getId()); result.setOperationNo(operation.getOperationNo()); result.setStatus(BatteryOperationStatus.PROCESSING.getCode()); result.setMessage("换电操作已提交,处理中"); return result; } else { return BatteryOperationResult.failed("换电操作超时,请稍后再试"); } } catch (Exception e) { log.error("换电操作处理失败: operationType={}, cabinetId={}, error={}", request.getOperationType(), request.getCabinetId(), e.getMessage(), e); return BatteryOperationResult.failed("换电操作失败: " + e.getMessage()); } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }
private BatteryOperation createBatteryOperation(BatteryOperationProcessRequest request) { BatteryOperation operation = new BatteryOperation();
operation.setOperationNo(generateOperationNo()); operation.setUserId(request.getUserId()); operation.setCabinetId(request.getCabinetId()); operation.setSlotNo(request.getSlotNo()); operation.setBatteryId(request.getBatteryId()); operation.setOperationType(request.getOperationType()); operation.setStatus(BatteryOperationStatus.PENDING.getCode()); operation.setCreateTime(new Date()); operation.setUpdateTime(new Date()); return operation; }
private DeviceControlCommand buildDeviceControlCommand( BatteryOperationProcessRequest request, BatteryOperation operation) { DeviceControlCommand command = new DeviceControlCommand();
command.setCabinetId(request.getCabinetId()); command.setSlotNo(request.getSlotNo()); command.setOperationType(request.getOperationType()); command.setOperationId(operation.getId()); command.setOperationNo(operation.getOperationNo()); command.setBatteryId(request.getBatteryId()); command.setTimestamp(new Date()); return command; }
@Async("batteryOperationExecutor") public void asyncProcessDeviceResponse(BatteryOperation operation, DeviceControlResult controlResult) { try { BatteryOperationMessage message = new BatteryOperationMessage();
message.setOperationId(operation.getId()); message.setOperationNo(operation.getOperationNo()); message.setCommandId(controlResult.getCommandId()); message.setCabinetId(operation.getCabinetId()); message.setOperationType(operation.getOperationType()); kafkaTemplate.send("battery.operation.response", operation.getOperationNo(), JSON.toJSONString(message)); } catch (Exception e) { log.error("异步处理设备响应失败: operationId={}, error={}", operation.getId(), e.getMessage(), e); } }
@KafkaListener(topics = "device.control.response", groupId = "device-control-group") public void receiveDeviceControlResponse(String message) { try { DeviceControlResponse response = JSON.parseObject(message, DeviceControlResponse.class); BatteryOperation operation = batteryOperationMapper.selectByCommandId( response.getCommandId()); if (operation == null) { log.warn("换电操作记录不存在: commandId={}", response.getCommandId()); return; } if (response.isSuccess()) { operation.setStatus(BatteryOperationStatus.SUCCESS.getCode()); } else { operation.setStatus(BatteryOperationStatus.FAILED.getCode()); operation.setErrorMessage(response.getErrorMessage()); } operation.setResponseTime(new Date()); operation.setUpdateTime(new Date()); batteryOperationMapper.updateById(operation); if (response.isSuccess()) { updateOrderAndAsset(operation, response); } log.info("设备控制指令响应处理完成: operationId={}, success={}", operation.getId(), response.isSuccess()); } catch (Exception e) { log.error("接收设备控制指令响应失败: error={}", e.getMessage(), e); } }
private void updateOrderAndAsset(BatteryOperation operation, DeviceControlResponse response) { try { OrderAssetUpdateRequest updateRequest = new OrderAssetUpdateRequest(); updateRequest.setUserId(operation.getUserId()); updateRequest.setOperationId(operation.getId()); updateRequest.setOperationType(operation.getOperationType()); updateRequest.setCabinetId(operation.getCabinetId()); updateRequest.setSlotNo(operation.getSlotNo()); updateRequest.setBatteryId(response.getBatteryId()); updateRequest.setSuccess(true); OrderAssetUpdateResult updateResult = orderServiceClient.updateOrderAndAsset(updateRequest); if (!updateResult.isSuccess()) { log.error("更新订单资产失败: operationId={}, error={}", operation.getId(), updateResult.getMessage()); } } catch (Exception e) { log.error("更新订单资产异常: operationId={}, error={}", operation.getId(), e.getMessage(), e); } }
public BatteryOperationQueryResult getBatteryOperationResult(Long operationId, Long userId) { try { BatteryOperation operation = batteryOperationMapper.selectById(operationId); if (operation == null) { return BatteryOperationQueryResult.failed("换电操作记录不存在"); } if (!operation.getUserId().equals(userId)) { return BatteryOperationQueryResult.failed("无权限查询该换电操作"); } BatteryOperationQueryResult result = new BatteryOperationQueryResult();
result.setSuccess(true); result.setOperationId(operation.getId()); result.setOperationNo(operation.getOperationNo()); result.setOperationType(operation.getOperationType()); result.setStatus(operation.getStatus()); result.setCabinetId(operation.getCabinetId()); result.setSlotNo(operation.getSlotNo()); result.setBatteryId(operation.getBatteryId()); result.setCreateTime(operation.getCreateTime()); result.setResponseTime(operation.getResponseTime()); result.setErrorMessage(operation.getErrorMessage()); result.setMessage("查询成功"); return result; } catch (Exception e) { log.error("查询换电操作结果失败: operationId={}, error={}", operationId, e.getMessage(), e); return BatteryOperationQueryResult.failed("查询失败: " + e.getMessage()); } }
private CabinetInfo convertToCabinetInfo(BatteryCabinet cabinet) { CabinetInfo info = new CabinetInfo(); info.setCabinetId(cabinet.getId()); info.setCabinetName(cabinet.getCabinetName()); info.setCabinetCode(cabinet.getCabinetCode()); info.setLatitude(cabinet.getLatitude()); info.setLongitude(cabinet.getLongitude()); info.setAddress(cabinet.getAddress()); info.setStatus(cabinet.getStatus()); info.setAvailableSlots(cabinet.getAvailableSlots()); info.setTotalSlots(cabinet.getTotalSlots()); return info; }
private String generateOperationNo() { return "OPR" + System.currentTimeMillis() + RandomUtils.nextInt(10000, 99999); } }
|