From 8b0ff3767b23f9479a0b3ebe8e343ed6470bd194 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 09 八月 2022 14:41:21 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0'

---
 src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java |  106 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 106 insertions(+), 0 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
new file mode 100644
index 0000000..d024550
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformChannelServiceImpl.java
@@ -0,0 +1,106 @@
+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.event.EventPublisher;
+import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
+import com.genersoft.iot.vmp.service.IPlatformChannelService;
+import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
+import com.genersoft.iot.vmp.storager.dao.ParentPlatformMapper;
+import com.genersoft.iot.vmp.storager.dao.PlatformCatalogMapper;
+import com.genersoft.iot.vmp.storager.dao.PlatformChannelMapper;
+import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author lin
+ */
+@Service
+public class PlatformChannelServiceImpl implements IPlatformChannelService {
+
+    private final static Logger logger = LoggerFactory.getLogger(PlatformChannelServiceImpl.class);
+
+    @Autowired
+    private PlatformChannelMapper platformChannelMapper;
+
+    @Autowired
+    private DeviceChannelMapper deviceChannelMapper;
+
+    @Autowired
+    private PlatformCatalogMapper catalogManager;
+
+    @Autowired
+    private ParentPlatformMapper platformMapper;
+
+    @Autowired
+    EventPublisher eventPublisher;
+
+    @Override
+    public int updateChannelForGB(String platformId, List<ChannelReduce> channelReduces, String catalogId) {
+        ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformId);
+        if (platform == null) {
+            logger.warn("鏇存柊绾ц仈閫氶亾淇℃伅鏃舵湭鎵惧埌骞冲彴{}鐨勪俊鎭�", platformId);
+            return 0;
+        }
+        Map<Integer, ChannelReduce> deviceAndChannels = new HashMap<>();
+        for (ChannelReduce channelReduce : channelReduces) {
+            channelReduce.setCatalogId(catalogId);
+            deviceAndChannels.put(channelReduce.getId(), channelReduce);
+        }
+        List<Integer> deviceAndChannelList = new ArrayList<>(deviceAndChannels.keySet());
+        // 鏌ヨ褰撳墠宸茬粡瀛樺湪鐨�
+        List<Integer> channelIds = platformChannelMapper.findChannelRelatedPlatform(platformId, channelReduces);
+        if (deviceAndChannelList != null) {
+            deviceAndChannelList.removeAll(channelIds);
+        }
+        for (Integer channelId : channelIds) {
+            deviceAndChannels.remove(channelId);
+        }
+        List<ChannelReduce> channelReducesToAdd = new ArrayList<>(deviceAndChannels.values());
+        // 瀵瑰墿涓嬬殑鏁版嵁杩涜瀛樺偍
+        int result = 0;
+        if (channelReducesToAdd.size() > 0) {
+            result = platformChannelMapper.addChannels(platformId, channelReducesToAdd);
+            // TODO 鍚庣画缁欏钩鍙板鍔犳帶鍒跺紑鍏充互鎺у埗鏄惁鍝嶅簲鐩綍璁㈤槄
+            List<DeviceChannel> deviceChannelList = getDeviceChannelListByChannelReduceList(channelReducesToAdd, catalogId, platform);
+            eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
+        }
+
+        return result;
+    }
+
+    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())) {
+                logger.warn("鏈煡璇㈠埌鐩綍{}鐨勪俊鎭�", catalogId);
+                return null;
+            }
+            for (ChannelReduce channelReduce : channelReduces) {
+                DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(channelReduce.getDeviceId(), channelReduce.getChannelId());
+                deviceChannel.setParental(0);
+                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;
+    }
+}

--
Gitblit v1.8.0