Преглед изворни кода

fix: 补货员库存修正接口 productId 类型转换兼容 String

前端传雪花ID时因数值过大JS自动转String,后端强转Number导致ClassCastException。
新增 toLong/toInt 方法兼容 String 和 Number 两种类型。

Co-Authored-By: Claude <noreply@anthropic.com>
skyline пре 2 дана
родитељ
комит
84c9e3bc9b

+ 41 - 5
haha-admin/src/main/java/com/haha/admin/controller/ReplenisherOperationController.java

@@ -455,11 +455,11 @@ public class ReplenisherOperationController {
         Replenisher replenisher = getCurrentReplenisher();
 
         String deviceId = (String) body.get("deviceId");
-        Number productIdNum = (Number) body.get("productId");
-        Number newStockNum = (Number) body.get("newStock");
+        Object productIdObj = body.get("productId");
+        Object newStockObj = body.get("newStock");
         String remark = (String) body.getOrDefault("remark", "补货员盘点修正");
 
-        if (deviceId == null || productIdNum == null || newStockNum == null) {
+        if (deviceId == null || productIdObj == null || newStockObj == null) {
             return Result.error(400, "deviceId, productId, newStock 不能为空");
         }
 
@@ -467,8 +467,12 @@ public class ReplenisherOperationController {
             return Result.error(403, "您无权对此设备进行操作");
         }
 
-        Long productId = productIdNum.longValue();
-        int newStock = newStockNum.intValue();
+        // 兼容前端传 String(雪花ID过长JS自动转字符串)或 Number
+        Long productId = toLong(productIdObj);
+        Integer newStock = toInt(newStockObj);
+        if (productId == null || newStock == null) {
+            return Result.error(400, "productId, newStock 格式不正确");
+        }
 
         DeviceInventory inventory = deviceInventoryService.adjustStock(
                 deviceId, productId, newStock, remark,
@@ -594,4 +598,36 @@ public class ReplenisherOperationController {
             default: return "未知";
         }
     }
+
+    /**
+     * 安全转 Long,兼容前端传 String(雪花ID过长JS自动转字符串)或 Number
+     */
+    private Long toLong(Object obj) {
+        if (obj == null) return null;
+        if (obj instanceof Number) return ((Number) obj).longValue();
+        if (obj instanceof String) {
+            try {
+                return Long.parseLong((String) obj);
+            } catch (NumberFormatException e) {
+                return null;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * 安全转 Integer,兼容前端传 String 或 Number
+     */
+    private Integer toInt(Object obj) {
+        if (obj == null) return null;
+        if (obj instanceof Number) return ((Number) obj).intValue();
+        if (obj instanceof String) {
+            try {
+                return Integer.parseInt((String) obj);
+            } catch (NumberFormatException e) {
+                return null;
+            }
+        }
+        return null;
+    }
 }