Quellcode durchsuchen

fix: 库存扣减失败 — items JSON 只有 code 无 productId 导致跳过

AI识别返回的items格式为 [{"code":"xxx","number":1}],有code但没有
productId。decreaseStockOnPaid 和 checkStockAvailable 在 productId
为 null 时直接 continue,从未用已提取的 productCode 反查本地商品库。

修复:productId为空但productCode有值时,通过 ProductService.getProductByCode
查找本地商品获取 productId,再执行扣减。

同时修复了 checkStockAvailable 中的相同问题。

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
skyline vor 2 Wochen
Ursprung
Commit
6390eb8eb5

+ 23 - 1
haha-service/src/main/java/com/haha/service/impl/OrderInventoryServiceImpl.java

@@ -5,10 +5,12 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import com.haha.entity.DeviceInventory;
 import com.haha.entity.InventoryLog;
 import com.haha.entity.Order;
+import com.haha.entity.Product;
 import com.haha.entity.RefundItem;
 import com.haha.service.DeviceInventoryService;
 import com.haha.service.InventoryLogService;
 import com.haha.service.OrderInventoryService;
+import com.haha.service.ProductService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -30,6 +32,9 @@ public class OrderInventoryServiceImpl implements OrderInventoryService {
     @Autowired
     private InventoryLogService inventoryLogService;
 
+    @Autowired
+    private ProductService productService;
+
     @Autowired
     private ObjectMapper objectMapper;
 
@@ -63,8 +68,16 @@ public class OrderInventoryServiceImpl implements OrderInventoryService {
                 Integer quantity = extractQuantity(item);
                 String productCode = extractProductCode(item);
 
+                // AI识别返回的items只有code没有productId,需要通过code反查本地商品库
+                if (productId == null && productCode != null) {
+                    Product product = productService.getProductByCode(productCode);
+                    if (product != null) {
+                        productId = product.getId();
+                    }
+                }
+
                 if (productId == null) {
-                    log.warn("商品ID为空,跳过扣减: orderNo={}, item={}", orderNo, item);
+                    log.warn("无法确定商品ID,跳过扣减: orderNo={}, productCode={}", orderNo, productCode);
                     continue;
                 }
 
@@ -147,6 +160,15 @@ public class OrderInventoryServiceImpl implements OrderInventoryService {
             for (Map<String, Object> item : itemsList) {
                 Long productId = extractProductId(item);
                 Integer quantity = extractQuantity(item);
+                String productCode = extractProductCode(item);
+
+                // AI识别返回的items只有code没有productId,需要通过code反查本地商品库
+                if (productId == null && productCode != null) {
+                    Product product = productService.getProductByCode(productCode);
+                    if (product != null) {
+                        productId = product.getId();
+                    }
+                }
 
                 if (productId != null) {
                     DeviceInventory inventory = deviceInventoryService.getByDeviceAndProduct(deviceId, productId);