Explorar o código

fix: Long精度处理 + 禁止绑定时先解绑已关联设备

- Controller 使用 toLong() 统一转换前端传来的 Number/String
- batchUpdateBindEnabled 当 bindEnabled=0 时先解绑关联的设备
- bind/unbind 端点同样使用 Map<String,Object>+toLong 防精度丢失

Co-Authored-By: Claude <noreply@anthropic.com>
skyline hai 1 día
pai
achega
2e158ee417

+ 16 - 7
haha-admin/src/main/java/com/haha/admin/controller/ErpShopController.java

@@ -56,9 +56,9 @@ public class ErpShopController {
     @RequirePermission("erp:shop:bind")
     @Log(module = "ERP店铺管理", operation = OperationType.UPDATE, summary = "绑定ERP店铺")
     @PostMapping("/bind")
-    public Result<Void> bind(@RequestBody Map<String, Long> params) {
-        Long deviceId = params.get("deviceId");
-        Long erpShopId = params.get("erpShopId");
+    public Result<Void> bind(@RequestBody Map<String, Object> params) {
+        Long deviceId = toLong(params.get("deviceId"));
+        Long erpShopId = toLong(params.get("erpShopId"));
         if (deviceId == null || erpShopId == null) {
             return Result.error("deviceId 和 erpShopId 不能为空");
         }
@@ -72,8 +72,8 @@ public class ErpShopController {
     @RequirePermission("erp:shop:bind")
     @Log(module = "ERP店铺管理", operation = OperationType.UPDATE, summary = "解绑ERP店铺")
     @PostMapping("/unbind")
-    public Result<Void> unbind(@RequestBody Map<String, Long> params) {
-        Long deviceId = params.get("deviceId");
+    public Result<Void> unbind(@RequestBody Map<String, Object> params) {
+        Long deviceId = toLong(params.get("deviceId"));
         if (deviceId == null) {
             return Result.error("deviceId 不能为空");
         }
@@ -81,6 +81,13 @@ public class ErpShopController {
         return Result.success("解绑成功");
     }
 
+    /** Long 精度统一处理:前端可能传 Number 或 String */
+    private Long toLong(Object val) {
+        if (val == null) return null;
+        if (val instanceof Number) return ((Number) val).longValue();
+        return Long.parseLong(val.toString());
+    }
+
     /**
      * 批量修改允许绑定标识
      */
@@ -88,9 +95,11 @@ public class ErpShopController {
     @Log(module = "ERP店铺管理", operation = OperationType.UPDATE, summary = "批量修改允许绑定标识")
     @PostMapping("/batch-bind-enabled")
     public Result<Integer> batchUpdateBindEnabled(@RequestBody Map<String, Object> params) {
+        // erpShopId 是 Long 类型,前端 JSON 可能以字符串传递(精度问题),统一转 Long
         @SuppressWarnings("unchecked")
-        List<Long> erpShopIds = ((List<Number>) params.get("erpShopIds")).stream()
-                .map(Number::longValue).toList();
+        List<Long> erpShopIds = ((List<?>) params.get("erpShopIds")).stream()
+                .map(o -> o instanceof Number ? ((Number) o).longValue() : Long.parseLong(o.toString()))
+                .toList();
         Integer bindEnabled = (Integer) params.get("bindEnabled");
         if (erpShopIds == null || erpShopIds.isEmpty() || bindEnabled == null) {
             return Result.error("参数不能为空");

+ 8 - 0
haha-service/src/main/java/com/haha/service/impl/ErpShopServiceImpl.java

@@ -197,6 +197,14 @@ public class ErpShopServiceImpl extends ServiceImpl<ErpShopMapper, ErpShop> impl
         if (erpShopIds == null || erpShopIds.isEmpty()) {
             return 0;
         }
+        // 禁止绑定时,先解绑已关联的设备
+        if (bindEnabled == 0) {
+            int unboundCount = deviceMapper.update(null, new LambdaUpdateWrapper<Device>()
+                    .in(Device::getErpShopId, erpShopIds)
+                    .set(Device::getErpShopId, null)
+                    .set(Device::getUpdateTime, LocalDateTime.now()));
+            log.info("禁止绑定前解绑设备: {} 台", unboundCount);
+        }
         boolean updated = update(new LambdaUpdateWrapper<ErpShop>()
                 .in(ErpShop::getErpShopId, erpShopIds)
                 .set(ErpShop::getBindEnabled, bindEnabled)