From ba8fffd907b5c3a7acfd3348c80e557b816cec98 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 03 七月 2023 16:53:03 +0800
Subject: [PATCH] 增加与第三方对接的接口

---
 src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java |  100 ++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 80 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java
index d024550..eeea29a 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java
@@ -1,9 +1,6 @@
 package com.genersoft.iot.vmp.service.impl;
 
-import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
-import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
-import com.genersoft.iot.vmp.gb28181.bean.PlatformCatalog;
-import com.genersoft.iot.vmp.gb28181.bean.TreeType;
+import com.genersoft.iot.vmp.gb28181.bean.*;
 import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
 import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
 import com.genersoft.iot.vmp.service.IPlatformChannelService;
@@ -15,7 +12,11 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.util.ObjectUtils;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -32,6 +33,16 @@
 
     @Autowired
     private PlatformChannelMapper platformChannelMapper;
+
+    @Autowired
+    TransactionDefinition transactionDefinition;
+
+    @Autowired
+    DataSourceTransactionManager dataSourceTransactionManager;
+
+    @Autowired
+    private SubscribeHolder subscribeHolder;
+
 
     @Autowired
     private DeviceChannelMapper deviceChannelMapper;
@@ -68,39 +79,88 @@
         }
         List<ChannelReduce> channelReducesToAdd = new ArrayList<>(deviceAndChannels.values());
         // 瀵瑰墿涓嬬殑鏁版嵁杩涜瀛樺偍
-        int result = 0;
+        int allCount = 0;
+        boolean result = false;
+        TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
+        int limitCount = 300;
         if (channelReducesToAdd.size() > 0) {
-            result = platformChannelMapper.addChannels(platformId, channelReducesToAdd);
-            // TODO 鍚庣画缁欏钩鍙板鍔犳帶鍒跺紑鍏充互鎺у埗鏄惁鍝嶅簲鐩綍璁㈤槄
-            List<DeviceChannel> deviceChannelList = getDeviceChannelListByChannelReduceList(channelReducesToAdd, catalogId, platform);
-            eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
-        }
+            if (channelReducesToAdd.size() > limitCount) {
+                for (int i = 0; i < channelReducesToAdd.size(); i += limitCount) {
+                    int toIndex = i + limitCount;
+                    if (i + limitCount > channelReducesToAdd.size()) {
+                        toIndex = channelReducesToAdd.size();
+                    }
+                    int count = platformChannelMapper.addChannels(platformId, channelReducesToAdd.subList(i, toIndex));
+                    result = result || count < 0;
+                    allCount += count;
+                    logger.info("[鍏宠仈閫氶亾]鍥芥爣閫氶亾 骞冲彴锛歿}, 鍏遍渶鍏宠仈閫氶亾鏁�:{}, 宸插叧鑱旓細{}", platformId, channelReducesToAdd.size(), toIndex);
+                }
+            }else {
+                allCount = platformChannelMapper.addChannels(platformId, channelReducesToAdd);
+                result = result || allCount < 0;
+                logger.info("[鍏宠仈閫氶亾]鍥芥爣閫氶亾 骞冲彴锛歿}, 鍏宠仈閫氶亾鏁�:{}", platformId, channelReducesToAdd.size());
+            }
 
-        return result;
+            if (result) {
+                //浜嬪姟鍥炴粴
+                dataSourceTransactionManager.rollback(transactionStatus);
+                allCount = 0;
+            }else {
+                logger.info("[鍏宠仈閫氶亾]鍥芥爣閫氶亾 骞冲彴锛歿}, 姝e湪瀛樺叆鏁版嵁搴�", platformId);
+                dataSourceTransactionManager.commit(transactionStatus);
+
+            }
+            SubscribeInfo catalogSubscribe = subscribeHolder.getCatalogSubscribe(platformId);
+            if (catalogSubscribe != null) {
+                List<DeviceChannel> deviceChannelList = getDeviceChannelListByChannelReduceList(channelReducesToAdd, catalogId, platform);
+                if (deviceChannelList != null) {
+                    eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
+                }
+            }
+            logger.info("[鍏宠仈閫氶亾]鍥芥爣閫氶亾 骞冲彴锛歿}, 瀛樺叆鏁版嵁搴撴垚鍔�", platformId);
+        }
+        return allCount;
     }
 
     private List<DeviceChannel> getDeviceChannelListByChannelReduceList(List<ChannelReduce> channelReduces, String catalogId, ParentPlatform platform) {
         List<DeviceChannel> deviceChannelList = new ArrayList<>();
         if (channelReduces.size() > 0){
             PlatformCatalog catalog = catalogManager.select(catalogId);
-            if (catalog == null && !catalogId.equals(platform.getServerGBId())) {
+            if (catalog == null || !catalogId.equals(platform.getDeviceGBId())) {
                 logger.warn("鏈煡璇㈠埌鐩綍{}鐨勪俊鎭�", catalogId);
                 return null;
             }
             for (ChannelReduce channelReduce : channelReduces) {
                 DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(channelReduce.getDeviceId(), channelReduce.getChannelId());
                 deviceChannel.setParental(0);
+                deviceChannel.setCivilCode(catalog.getCivilCode());
+                deviceChannel.setParentId(catalog.getParentId());
+                deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
                 deviceChannelList.add(deviceChannel);
-                if (platform.getTreeType().equals(TreeType.CIVIL_CODE)){
-                    deviceChannel.setCivilCode(catalogId);
-                }else if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)){
-                    deviceChannel.setParentId(catalogId);
-                    if (catalog != null) {
-                        deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
-                    }
-                }
             }
         }
         return deviceChannelList;
     }
+
+    @Override
+    public int delAllChannelForGB(String platformId, String catalogId) {
+
+        int result;
+        if (platformId == null) {
+            return 0;
+        }
+        ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformId);
+        if (platform == null) {
+            return 0;
+        }
+        if (ObjectUtils.isEmpty(catalogId)) {
+           catalogId = platform.getDeviceGBId();
+        }
+
+        if ((result = platformChannelMapper.delChannelForGBByCatalogId(platformId, catalogId)) > 0) {
+            List<DeviceChannel> deviceChannels = platformChannelMapper.queryAllChannelInCatalog(platformId, catalogId);
+            eventPublisher.catalogEventPublish(platformId, deviceChannels, CatalogEvent.DEL);
+        }
+        return result;
+    }
 }

--
Gitblit v1.8.0