TRX 1 tahun lalu
induk
melakukan
62f14d3b3f

+ 10 - 9
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotTemplateManagerService.java

@@ -138,13 +138,15 @@ public class IotTemplateManagerService extends SuperService {
         List<IotMain> saveList = new ArrayList<>();
         // 模版对应的数据
         List<IotMain> list = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(iotTemplate);
+
         // 以前绑定的数据
         List<IotMain> oldList = iotMainDao.findByIotTemplateOrderByCreateTimeAsc(newIotTemplate);
-
+        List<String> oldIds = new ArrayList<>();
         HashMap<String, String> map = new HashMap<>();
         if (ObjectUtils.isNotEmpty(oldList)) {
             oldList.stream().forEach(it -> {
                 map.put(it.getIotMainTemplateId(), it.getId());
+                oldIds.add(it.getId());
             });
         }
         DeviceInfo gateWayInfo = gateWayInfoService.getDeviceGateWayInfo(deviceInfo);
@@ -153,12 +155,12 @@ public class IotTemplateManagerService extends SuperService {
             IotTemplate finalNewIotTemplate = newIotTemplate;
             list.stream().forEach(it -> {
                 IotMain main = new IotMain();
-                BeanUtils.copyProperties(it, main, "id", "createTime", "updateTime");
-                if (oldList.contains(it.getId())) {
-                    main.setId(map.get(it.getId()));
-
-                    map.remove(it.getId());
+                if (map.containsKey(it.getId())) {
+                    String oldId = map.get(it.getId());
+                    main = iotMainDao.findTopById(oldId);
+                    oldIds.remove(oldId);
                 }
+                BeanUtils.copyProperties(it, main, "id", "createTime", "updateTime");
                 main.setIotThingId(iotThingId);
 
                 // 所属产品副本
@@ -182,9 +184,8 @@ public class IotTemplateManagerService extends SuperService {
             });
         }
         iotMainDao.saveAll(saveList);
-        if (!map.isEmpty()) {
-            List<String> ids = new ArrayList<>(map.values());
-            iotMainDao.deleteAllById(ids);
+        if (ObjectUtils.isNotEmpty(oldIds)) {
+            iotMainDao.deleteAllById(oldIds);
         }
         return ResultContent.buildSuccess();
     }