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
| @Service @Transactional public class InventoryService { @Autowired private InventoryRepository inventoryRepository; @Autowired private InventoryOperationRepository operationRepository; @Autowired private RedisTemplate<String, Object> redisTemplate;
public boolean deductInventory(InventoryDeductionMessage message) { if (StringUtils.hasText(message.getIdempotencyKey())) { InventoryOperation existingOperation = operationRepository .findByIdempotencyKey(message.getIdempotencyKey()); if (existingOperation != null) { log.info("库存操作已存在,返回现有结果: idempotencyKey={}, status={}", message.getIdempotencyKey(), existingOperation.getStatus()); return "SUCCESS".equals(existingOperation.getStatus()); } } try { Inventory inventory = inventoryRepository.findByProductId(message.getProductId()) .orElseThrow(() -> new InventoryNotFoundException("商品库存不存在: " + message.getProductId())); if (inventory.getAvailableStock() < message.getQuantity()) { log.warn("库存不足: productId={}, available={}, required={}", message.getProductId(), inventory.getAvailableStock(), message.getQuantity()); recordInventoryOperation(message, "FAILED"); return false; } int updatedRows = inventoryRepository.deductStock( message.getProductId(), message.getQuantity(), inventory.getVersion() ); if (updatedRows == 0) { log.warn("库存扣减失败,可能被其他操作修改: productId={}", message.getProductId()); recordInventoryOperation(message, "FAILED"); return false; } recordInventoryOperation(message, "SUCCESS"); updateInventoryCache(message.getProductId()); log.info("库存扣减成功: productId={}, quantity={}, orderId={}", message.getProductId(), message.getQuantity(), message.getOrderId()); return true; } catch (Exception e) { log.error("库存扣减异常: message={}", message, e); recordInventoryOperation(message, "FAILED"); return false; } }
public boolean restoreInventory(Long orderId, Long productId, Integer quantity) { try { Inventory inventory = inventoryRepository.findByProductId(productId) .orElseThrow(() -> new InventoryNotFoundException("商品库存不存在: " + productId)); int updatedRows = inventoryRepository.restoreStock( productId, quantity, inventory.getVersion() ); if (updatedRows == 0) { log.warn("库存恢复失败: productId={}", productId); return false; } updateInventoryCache(productId); log.info("库存恢复成功: productId={}, quantity={}, orderId={}", productId, quantity, orderId); return true; } catch (Exception e) { log.error("库存恢复异常: orderId={}, productId={}, quantity={}", orderId, productId, quantity, e); return false; } }
private void recordInventoryOperation(InventoryDeductionMessage message, String status) { InventoryOperation operation = InventoryOperation.builder() .orderId(message.getOrderId()) .productId(message.getProductId()) .operationType("DEDUCT") .quantity(message.getQuantity()) .idempotencyKey(message.getIdempotencyKey()) .status(status) .createdTime(LocalDateTime.now()) .build(); operationRepository.save(operation); }
private void updateInventoryCache(Long productId) { Inventory inventory = inventoryRepository.findByProductId(productId).orElse(null); if (inventory != null) { String cacheKey = "inventory:" + productId; redisTemplate.opsForValue().set(cacheKey, inventory, Duration.ofMinutes(30)); } }
public Inventory getInventory(Long productId) { String cacheKey = "inventory:" + productId; Object cachedInventory = redisTemplate.opsForValue().get(cacheKey); if (cachedInventory != null) { return (Inventory) cachedInventory; } Inventory inventory = inventoryRepository.findByProductId(productId).orElse(null); if (inventory != null) { redisTemplate.opsForValue().set(cacheKey, inventory, Duration.ofMinutes(30)); } return inventory; } }
|