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
|
@Service @Slf4j public class OrderService {
@Autowired private OrderMapper orderMapper; @Autowired private DeviceServiceClient deviceServiceClient; @Autowired private RedissonClient redissonClient; @Autowired private SeataTransactionManager seataTransactionManager;
@GlobalTransactional(rollbackFor = Exception.class) public BatteryBindResult bindBatteryToOrder(Long orderId, Long batteryId, Long adminId) { String lockKey = "order:bind:battery:" + orderId + ":" + batteryId; RLock lock = redissonClient.getLock(lockKey); try { boolean lockAcquired = lock.tryLock(3, 30, TimeUnit.SECONDS); if (!lockAcquired) { throw new BusinessException("系统繁忙,请稍后重试"); } Order order = validateOrder(orderId); BatteryAsset batteryAsset = deviceServiceClient.validateBatteryAsset(batteryId); updateOrderWithBattery(orderId, batteryId, adminId); Long operationId = deviceServiceClient.updateBatteryAssetAndRecordOperation( batteryId, orderId, adminId); BatteryBindResult result = new BatteryBindResult(); result.setOperationId(operationId); result.setOrderId(orderId); result.setBatteryId(batteryId); result.setBindStatus("BOUND"); result.setBindTime(LocalDateTime.now()); result.setMessage("绑电解电成功"); log.info("绑电解电成功: orderId={}, batteryId={}, adminId={}, operationId={}", orderId, batteryId, adminId, operationId); return result; } catch (InterruptedException e) { Thread.currentThread().interrupt(); log.error("获取分布式锁被中断: orderId={}, batteryId={}", orderId, batteryId, e); throw new BusinessException("系统繁忙,请稍后重试"); } catch (Exception e) { log.error("绑电解电失败: orderId={}, batteryId={}, adminId={}, error={}", orderId, batteryId, adminId, e.getMessage(), e); throw new BusinessException("绑电解电失败: " + e.getMessage()); } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }
private Order validateOrder(Long orderId) { Order order = orderMapper.selectById(orderId); if (order == null) { throw new BusinessException("订单不存在"); } if (!"ACTIVE".equals(order.getStatus()) && !"PAID".equals(order.getStatus())) { throw new BusinessException("订单状态不允许绑定电池"); } if (order.getBatteryId() != null) { throw new BusinessException("订单已绑定电池,无法重复绑定"); } return order; }
private void updateOrderWithBattery(Long orderId, Long batteryId, Long adminId) { Order order = new Order(); order.setId(orderId); order.setBatteryId(batteryId); order.setBatteryBindTime(LocalDateTime.now()); order.setBatteryBindAdminId(adminId); order.setUpdateTime(LocalDateTime.now()); int updateCount = orderMapper.updateById(order); if (updateCount <= 0) { throw new BusinessException("更新订单信息失败"); } log.info("订单信息更新成功: orderId={}, batteryId={}, adminId={}", orderId, batteryId, adminId); } }
|