From bde0e13682ed75d2e8c0cb8a1fd6a96bb92f1dd8 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 14 三月 2022 18:34:25 +0800
Subject: [PATCH] #394

---
 src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java |  324 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 268 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
index 2f4c966..d136d5c 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
@@ -1,6 +1,10 @@
 package com.genersoft.iot.vmp.storager.impl;
 
+import com.genersoft.iot.vmp.common.StreamInfo;
+import com.genersoft.iot.vmp.conf.SipConfig;
 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.gb28181.session.VideoStreamSessionManager;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
 import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
@@ -10,6 +14,9 @@
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
 import com.genersoft.iot.vmp.storager.dao.*;
+import com.genersoft.iot.vmp.storager.dao.dto.ChannelSourceInfo;
+import com.genersoft.iot.vmp.utils.node.ForestNodeMerger;
+import com.genersoft.iot.vmp.vmanager.bean.DeviceChannelTree;
 import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
@@ -27,15 +34,21 @@
 import java.util.*;
 
 /**    
- * @description:瑙嗛璁惧鏁版嵁瀛樺偍-jdbc瀹炵幇
- * @author: swwheihei
- * @date:   2020骞�5鏈�6鏃� 涓嬪崍2:31:42
+ * 瑙嗛璁惧鏁版嵁瀛樺偍-jdbc瀹炵幇
+ * swwheihei
+ * 2020骞�5鏈�6鏃� 涓嬪崍2:31:42
  */
 @SuppressWarnings("rawtypes")
 @Component
 public class VideoManagerStoragerImpl implements IVideoManagerStorager {
 
-	private Logger logger = LoggerFactory.getLogger(VideoManagerStoragerImpl.class);
+	private final Logger logger = LoggerFactory.getLogger(VideoManagerStoragerImpl.class);
+
+	@Autowired
+	EventPublisher eventPublisher;
+
+	@Autowired
+	SipConfig sipConfig;
 
 	@Autowired
 	DataSourceTransactionManager dataSourceTransactionManager;
@@ -134,6 +147,7 @@
 			return deviceMapper.add(device) > 0;
 		}else {
 			redisCatchStorage.updateDevice(device);
+
 			return deviceMapper.update(device) > 0;
 		}
 
@@ -144,7 +158,10 @@
 	public synchronized void updateChannel(String deviceId, DeviceChannel channel) {
 		String channelId = channel.getChannelId();
 		channel.setDeviceId(deviceId);
-		channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId()));
+		StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
+		if (streamInfo != null) {
+			channel.setStreamId(streamInfo.getStream());
+		}
 		String now = this.format.format(System.currentTimeMillis());
 		channel.setUpdateTime(now);
 		DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId);
@@ -154,6 +171,7 @@
 		}else {
 			deviceChannelMapper.update(channel);
 		}
+		deviceChannelMapper.updateChannelSubCount(deviceId,channel.getParentId());
 	}
 
 	@Override
@@ -162,11 +180,14 @@
 		List<DeviceChannel> updateChannels = new ArrayList<>();
 		HashMap<String, DeviceChannel> channelsInStore = new HashMap<>();
 		if (channels != null && channels.size() > 0) {
-			List<DeviceChannel> channelList = deviceChannelMapper.queryChannelsByDeviceId(deviceId);
+			List<DeviceChannel> channelList = deviceChannelMapper.queryChannels(deviceId, null, null, null, null);
 			if (channelList.size() == 0) {
 				for (DeviceChannel channel : channels) {
 					channel.setDeviceId(deviceId);
-					channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId()));
+					StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channel.getChannelId());
+					if (streamInfo != null) {
+						channel.setStreamId(streamInfo.getStream());
+					}
 					String now = this.format.format(System.currentTimeMillis());
 					channel.setUpdateTime(now);
 					channel.setCreateTime(now);
@@ -177,9 +198,11 @@
 					channelsInStore.put(deviceChannel.getChannelId(), deviceChannel);
 				}
 				for (DeviceChannel channel : channels) {
-					String channelId = channel.getChannelId();
 					channel.setDeviceId(deviceId);
-					channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId()));
+					StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channel.getChannelId());
+					if (streamInfo != null) {
+						channel.setStreamId(streamInfo.getStream());
+					}
 					String now = this.format.format(System.currentTimeMillis());
 					channel.setUpdateTime(now);
 					if (channelsInStore.get(channel.getChannelId()) != null) {
@@ -227,6 +250,7 @@
 		// 鏁版嵁鍘婚噸
 		List<DeviceChannel> channels = new ArrayList<>();
 		StringBuilder stringBuilder = new StringBuilder();
+		Map<String, Integer> subContMap = new HashMap<>();
 		if (deviceChannelList.size() > 1) {
 			// 鏁版嵁鍘婚噸
 			Set<String> gbIdSet = new HashSet<>();
@@ -234,10 +258,26 @@
 				if (!gbIdSet.contains(deviceChannel.getChannelId())) {
 					gbIdSet.add(deviceChannel.getChannelId());
 					channels.add(deviceChannel);
+					if (!StringUtils.isEmpty(deviceChannel.getParentId())) {
+						if (subContMap.get(deviceChannel.getParentId()) == null) {
+							subContMap.put(deviceChannel.getParentId(), 1);
+						}else {
+							Integer count = subContMap.get(deviceChannel.getParentId());
+							subContMap.put(deviceChannel.getParentId(), count++);
+						}
+					}
 				}else {
 					stringBuilder.append(deviceChannel.getChannelId() + ",");
 				}
 			}
+			if (channels.size() > 0) {
+				for (DeviceChannel channel : channels) {
+					if (subContMap.get(channel.getChannelId()) != null){
+						channel.setSubCount(subContMap.get(channel.getChannelId()));
+					}
+				}
+			}
+
 		}else {
 			channels = deviceChannelList;
 		}
@@ -245,7 +285,8 @@
 			logger.debug("[鐩綍鏌ヨ]鏀跺埌鐨勬暟鎹瓨鍦ㄩ噸澶嶏細 {}" , stringBuilder);
 		}
 		try {
-			int cleanChannelsResult = deviceChannelMapper.cleanChannelsByDeviceId(deviceId);
+//			int cleanChannelsResult = deviceChannelMapper.cleanChannelsByDeviceId(deviceId);
+			int cleanChannelsResult = deviceChannelMapper.cleanChannelsNotInList(deviceId, channels);
 			int limitCount = 300;
 			boolean result = cleanChannelsResult < 0;
 			if (!result && channels.size() > 0) {
@@ -316,6 +357,11 @@
 	@Override
 	public List<DeviceChannel> queryChannelsByDeviceIdWithStartAndLimit(String deviceId, String query, Boolean hasSubChannel, Boolean online, int start, int limit) {
 		return deviceChannelMapper.queryChannelsByDeviceIdWithStartAndLimit(deviceId, null, query, hasSubChannel, online, start, limit);
+	}
+
+	@Override
+	public List<DeviceChannelTree> tree(String deviceId) {
+		return ForestNodeMerger.merge(deviceChannelMapper.tree(deviceId));
 	}
 
 	@Override
@@ -408,6 +454,8 @@
 		device.setOnline(1);
 		logger.info("鏇存柊璁惧鍦ㄧ嚎: " + deviceId);
 		redisCatchStorage.updateDevice(device);
+		List<DeviceChannel> deviceChannelList = deviceChannelMapper.queryOnlineChannelsByDeviceId(deviceId);
+		eventPublisher.catalogEventPublish(null, deviceChannelList, CatalogEvent.ON);
 		return deviceMapper.update(device) > 0;
 	}
 
@@ -514,7 +562,7 @@
 				if (parentPlatform.isShareAllLiveStream()) {
 					gbStreamService.addPlatformInfo(gbStreams, parentPlatform.getServerGBId(), parentPlatform.getCatalogId());
 				}else {
-					gbStreamService.delPlatformInfo(gbStreams);
+					gbStreamService.delPlatformInfo(parentPlatform.getServerGBId(), gbStreams);
 				}
 			}
 		}
@@ -556,40 +604,43 @@
 
 	@Override
 	public PageInfo<ChannelReduce> queryAllChannelList(int page, int count, String query, Boolean online,
-													   Boolean channelType, String platformId, Boolean inPlatform) {
+													   Boolean channelType, String platformId, String catalogId) {
 		PageHelper.startPage(page, count);
-		List<ChannelReduce> all = deviceChannelMapper.queryChannelListInAll(query, online, channelType, platformId, inPlatform);
+		List<ChannelReduce> all = deviceChannelMapper.queryChannelListInAll(query, online, channelType, platformId, catalogId);
 		return new PageInfo<>(all);
 	}
 
 	@Override
 	public List<ChannelReduce> queryChannelListInParentPlatform(String platformId) {
 
-		return deviceChannelMapper.queryChannelListInAll(null, null, null, platformId, true);
+		return deviceChannelMapper.queryChannelListInAll(null, null, null, platformId, platformId);
 	}
 
 	@Override
 	public int updateChannelForGB(String platformId, List<ChannelReduce> channelReduces, String catalogId) {
 
-		Map<String, ChannelReduce> deviceAndChannels = new HashMap<>();
+		Map<Integer, ChannelReduce> deviceAndChannels = new HashMap<>();
 		for (ChannelReduce channelReduce : channelReduces) {
 			channelReduce.setCatalogId(catalogId);
-			deviceAndChannels.put(channelReduce.getDeviceId() + "_" + channelReduce.getChannelId(), channelReduce);
+			deviceAndChannels.put(channelReduce.getId(), channelReduce);
 		}
-		List<String> deviceAndChannelList = new ArrayList<>(deviceAndChannels.keySet());
+		List<Integer> deviceAndChannelList = new ArrayList<>(deviceAndChannels.keySet());
 		// 鏌ヨ褰撳墠宸茬粡瀛樺湪鐨�
-		List<String> relatedPlatformchannels = platformChannelMapper.findChannelRelatedPlatform(platformId, deviceAndChannelList);
-		if (relatedPlatformchannels != null) {
-			deviceAndChannelList.removeAll(relatedPlatformchannels);
+		List<Integer> channelIds = platformChannelMapper.findChannelRelatedPlatform(platformId, channelReduces);
+		if (deviceAndChannelList != null) {
+			deviceAndChannelList.removeAll(channelIds);
 		}
-		for (String relatedPlatformchannel : relatedPlatformchannels) {
-			deviceAndChannels.remove(relatedPlatformchannel);
+		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);
+			eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
 		}
 
 		return result;
@@ -600,14 +651,28 @@
 	public int delChannelForGB(String platformId, List<ChannelReduce> channelReduces) {
 
 		int result = platformChannelMapper.delChannelForGB(platformId, channelReduces);
-
+		List<DeviceChannel> deviceChannelList = new ArrayList<>();
+		for (ChannelReduce channelReduce : channelReduces) {
+			DeviceChannel deviceChannel = new DeviceChannel();
+			deviceChannel.setChannelId(channelReduce.getChannelId());
+			deviceChannelList.add(deviceChannel);
+		}
+		eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
 		return result;
 	}
 
 	@Override
 	public DeviceChannel queryChannelInParentPlatform(String platformId, String channelId) {
-		DeviceChannel channel = platformChannelMapper.queryChannelInParentPlatform(platformId, channelId);
-		return channel;
+		List<DeviceChannel> channels = platformChannelMapper.queryChannelInParentPlatform(platformId, channelId);
+		if (channels.size() > 1) {
+			// 鍑虹幇闀垮害澶т簬0鐨勬椂鍊欒偗瀹氭槸鍥芥爣閫氶亾鐨処D閲嶅浜�
+			logger.warn("鍥芥爣ID瀛樺湪閲嶅锛歿}", channelId);
+		}
+		if (channels.size() == 0) {
+			return null;
+		}else {
+			return channels.get(0);
+		}
 	}
 
 	@Override
@@ -618,14 +683,24 @@
 
 	@Override
 	public List<PlatformCatalog> queryStreamInParentPlatformAndCatalog(String platformId, String catalogId) {
-		List<PlatformCatalog> catalogs = platformGbStreamMapper.queryChannelInParentPlatformAndCatalogForCatlog(platformId, catalogId);
+		List<PlatformCatalog> catalogs = platformGbStreamMapper.queryChannelInParentPlatformAndCatalogForCatalog(platformId, catalogId);
 		return catalogs;
 	}
 
 	@Override
 	public Device queryVideoDeviceByPlatformIdAndChannelId(String platformId, String channelId) {
-		Device device = platformChannelMapper.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId);
-		return device;
+		List<Device> devices = platformChannelMapper.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId);
+		if (devices.size() > 1) {
+			// 鍑虹幇闀垮害澶т簬0鐨勬椂鍊欒偗瀹氭槸鍥芥爣閫氶亾鐨処D閲嶅浜�
+			logger.warn("鍥芥爣ID瀛樺湪閲嶅锛歿}", channelId);
+		}
+		if (devices.size() == 0) {
+			return null;
+		}else {
+			return devices.get(0);
+		}
+
+
 	}
 
 	/**
@@ -658,10 +733,20 @@
 		streamProxyItem.setStatus(true);
 		String now = this.format.format(System.currentTimeMillis());
 		streamProxyItem.setCreateTime(now);
+		streamProxyItem.setCreateStamp(System.currentTimeMillis());
 		try {
-			if (gbStreamMapper.add(streamProxyItem)<0 || streamProxyMapper.add(streamProxyItem) < 0) {
+			if (streamProxyMapper.add(streamProxyItem) > 0) {
+				if (!StringUtils.isEmpty(streamProxyItem.getGbId())) {
+					if (gbStreamMapper.add(streamProxyItem) < 0) {
+						//浜嬪姟鍥炴粴
+						dataSourceTransactionManager.rollback(transactionStatus);
+						return false;
+					}
+				}
+			}else {
 				//浜嬪姟鍥炴粴
 				dataSourceTransactionManager.rollback(transactionStatus);
+				return false;
 			}
 			result = true;
 			dataSourceTransactionManager.commit(transactionStatus);     //鎵嬪姩鎻愪氦
@@ -685,10 +770,20 @@
 		boolean result = false;
 		streamProxyItem.setStreamType("proxy");
 		try {
-			if (gbStreamMapper.update(streamProxyItem)<0 || streamProxyMapper.update(streamProxyItem) < 0) {
+			if (streamProxyMapper.update(streamProxyItem) > 0) {
+				if (!StringUtils.isEmpty(streamProxyItem.getGbId())) {
+					if (gbStreamMapper.update(streamProxyItem) > 0) {
+						//浜嬪姟鍥炴粴
+						dataSourceTransactionManager.rollback(transactionStatus);
+						return false;
+					}
+				}
+			} else {
 				//浜嬪姟鍥炴粴
 				dataSourceTransactionManager.rollback(transactionStatus);
+				return false;
 			}
+
 			dataSourceTransactionManager.commit(transactionStatus);     //鎵嬪姩鎻愪氦
 			result = true;
 		}catch (Exception e) {
@@ -739,7 +834,7 @@
 	 * @return
 	 */
 	@Override
-	public List<GbStream> queryStreamInParentPlatform(String platformId, String gbId) {
+	public GbStream queryStreamInParentPlatform(String platformId, String gbId) {
 		return gbStreamMapper.queryStreamInPlatform(platformId, gbId);
 	}
 
@@ -771,7 +866,11 @@
 		streamPushMapper.addAll(streamPushItems);
 		// TODO 寰呬紭鍖�
 		for (int i = 0; i < streamPushItems.size(); i++) {
-			gbStreamMapper.setStatus(streamPushItems.get(i).getApp(), streamPushItems.get(i).getStream(), true);
+			int onlineResult = gbStreamMapper.setStatus(streamPushItems.get(i).getApp(), streamPushItems.get(i).getStream(), true);
+			if (onlineResult > 0) {
+				// 鍙戦�佷笂绾块�氱煡
+				eventPublisher.catalogEventPublishForStream(null, streamPushItems.get(i), CatalogEvent.ON);
+			}
 		}
 	}
 
@@ -780,6 +879,7 @@
 		streamPushMapper.del(streamPushItem.getApp(), streamPushItem.getStream());
 		streamPushMapper.add(streamPushItem);
 		gbStreamMapper.setStatus(streamPushItem.getApp(), streamPushItem.getStream(), true);
+
 		if(!StringUtils.isEmpty(streamPushItem.getGbId() )){
 			// 鏌ユ壘寮�鍚簡鍏ㄩ儴鐩存挱娴佸叡浜殑涓婄骇骞冲彴
 			List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream();
@@ -788,9 +888,11 @@
 					streamPushItem.setCatalogId(parentPlatform.getCatalogId());
 					streamPushItem.setPlatformId(parentPlatform.getServerGBId());
 					String stream = streamPushItem.getStream();
-					StreamProxyItem streamProxyItems = platformGbStreamMapper.selectOne(streamPushItem.getApp(), stream, parentPlatform.getServerGBId());
+					StreamProxyItem streamProxyItems = platformGbStreamMapper.selectOne(streamPushItem.getApp(), stream,
+							parentPlatform.getServerGBId());
 					if (streamProxyItems == null) {
 						platformGbStreamMapper.add(streamPushItem);
+						eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), streamPushItem, CatalogEvent.ADD);
 					}
 				}
 			}
@@ -819,20 +921,8 @@
 	}
 
 	@Override
-	public void updateMediaServer(MediaServerItem mediaServerItem) {
-		String now = this.format.format(System.currentTimeMillis());
-		mediaServerItem.setUpdateTime(now);
-		if (mediaServerMapper.queryOne(mediaServerItem.getId()) != null) {
-			mediaServerMapper.update(mediaServerItem);
-		}else {
-			mediaServerItem.setCreateTime(now);
-			mediaServerMapper.add(mediaServerItem);
-		}
-	}
-
-	@Override
-	public List<StreamProxyItem> getStreamProxyListForEnableInMediaServer(String id, boolean enable) {
-		return streamProxyMapper.selectForEnableInMediaServer(id, enable);
+	public List<StreamProxyItem> getStreamProxyListForEnableInMediaServer(String id, boolean enable, boolean status) {
+		return streamProxyMapper.selectForEnableInMediaServer(id, enable, status);
 	}
 
 
@@ -858,7 +948,12 @@
 
 	@Override
 	public int addCatalog(PlatformCatalog platformCatalog) {
-		return catalogMapper.add(platformCatalog);
+		int result = catalogMapper.add(platformCatalog);
+		if (result > 0) {
+			DeviceChannel deviceChannel = getDeviceChannelByCatalog(platformCatalog);
+			eventPublisher.catalogEventPublish(platformCatalog.getPlatformId(), deviceChannel, CatalogEvent.ADD);
+		}
+		return result;
 	}
 
 	@Override
@@ -873,23 +968,56 @@
 			List<PlatformCatalog> platformCatalogList = catalogMapper.selectByParentId(platformCatalog.getPlatformId(), platformCatalog.getId());
 			for (PlatformCatalog catalog : platformCatalogList) {
 				if (catalog.getChildrenCount() == 0) {
-					catalogMapper.del(catalog.getId());
-					platformGbStreamMapper.delByCatalogId(catalog.getId());
-					platformChannelMapper.delByCatalogId(catalog.getId());
+					delCatalogExecute(catalog.getId(), catalog.getPlatformId());
 				}else {
 					delCatalog(catalog.getId());
 				}
 			}
 		}
-		int delresult =  catalogMapper.del(id);
-		int delStreamresult = platformGbStreamMapper.delByCatalogId(id);
-		int delChanneresult = platformChannelMapper.delByCatalogId(id);
-		return delresult + delChanneresult + delStreamresult;
+		return delCatalogExecute(id, platformCatalog.getPlatformId());
 	}
+	private int delCatalogExecute(String id, String platformId) {
+		int delresult =  catalogMapper.del(id);
+		DeviceChannel deviceChannelForCatalog = new DeviceChannel();
+		if (delresult > 0){
+			deviceChannelForCatalog.setChannelId(id);
+			eventPublisher.catalogEventPublish(platformId, deviceChannelForCatalog, CatalogEvent.DEL);
+		}
+
+		List<GbStream> gbStreams = platformGbStreamMapper.queryChannelInParentPlatformAndCatalog(platformId, id);
+		if (gbStreams.size() > 0){
+			List<DeviceChannel> deviceChannelList = new ArrayList<>();
+			for (GbStream gbStream : gbStreams) {
+				DeviceChannel deviceChannel = new DeviceChannel();
+				deviceChannel.setChannelId(gbStream.getGbId());
+				deviceChannelList.add(deviceChannel);
+			}
+			eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
+		}
+		int delStreamresult = platformGbStreamMapper.delByCatalogId(id);
+		List<PlatformCatalog> platformCatalogs = platformChannelMapper.queryChannelInParentPlatformAndCatalog(platformId, id);
+		if (platformCatalogs.size() > 0){
+			List<DeviceChannel> deviceChannelList = new ArrayList<>();
+			for (PlatformCatalog platformCatalog : platformCatalogs) {
+				DeviceChannel deviceChannel = new DeviceChannel();
+				deviceChannel.setChannelId(platformCatalog.getId());
+				deviceChannelList.add(deviceChannel);
+			}
+			eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
+		}
+		int delChannelresult = platformChannelMapper.delByCatalogId(id);
+		return delresult + delChannelresult + delStreamresult;
+	}
+
 
 	@Override
 	public int updateCatalog(PlatformCatalog platformCatalog) {
-		return catalogMapper.update(platformCatalog);
+		int result = catalogMapper.update(platformCatalog);
+		if (result > 0) {
+			DeviceChannel deviceChannel = getDeviceChannelByCatalog(platformCatalog);
+			eventPublisher.catalogEventPublish(platformCatalog.getPlatformId(), deviceChannel, CatalogEvent.UPDATE);
+		}
+		return result;
 	}
 
 	@Override
@@ -905,11 +1033,17 @@
 	@Override
 	public int delRelation(PlatformCatalog platformCatalog) {
 		if (platformCatalog.getType() == 1) {
+			DeviceChannel deviceChannel = new DeviceChannel();
+			deviceChannel.setChannelId(platformCatalog.getId());
+			eventPublisher.catalogEventPublish(platformCatalog.getPlatformId(), deviceChannel, CatalogEvent.DEL);
 			return platformChannelMapper.delByCatalogIdAndChannelIdAndPlatformId(platformCatalog);
 		}else if (platformCatalog.getType() == 2) {
 			List<GbStream> gbStreams = platformGbStreamMapper.queryChannelInParentPlatformAndCatalog(platformCatalog.getPlatformId(), platformCatalog.getParentId());
 			for (GbStream gbStream : gbStreams) {
 				if (gbStream.getGbId().equals(platformCatalog.getId())) {
+					DeviceChannel deviceChannel = new DeviceChannel();
+					deviceChannel.setChannelId(gbStream.getGbId());
+					eventPublisher.catalogEventPublish(platformCatalog.getPlatformId(), deviceChannel, CatalogEvent.DEL);
 					return platformGbStreamMapper.delByAppAndStream(gbStream.getApp(), gbStream.getStream());
 				}
 			}
@@ -921,4 +1055,82 @@
 	public int updateStreamGPS(List<GPSMsgInfo> gpsMsgInfos) {
 		return gbStreamMapper.updateStreamGPS(gpsMsgInfos);
 	}
+
+	private List<DeviceChannel> getDeviceChannelListByChannelReduceList(List<ChannelReduce> channelReduces, String catalogId) {
+		List<DeviceChannel> deviceChannelList = new ArrayList<>();
+		if (channelReduces.size() > 0){
+			for (ChannelReduce channelReduce : channelReduces) {
+				DeviceChannel deviceChannel = queryChannel(channelReduce.getDeviceId(), channelReduce.getChannelId());
+				deviceChannel.setParental(1);
+				deviceChannel.setParentId(catalogId);
+				deviceChannelList.add(deviceChannel);
+			}
+		}
+		return deviceChannelList;
+	}
+
+	private DeviceChannel getDeviceChannelByCatalog(PlatformCatalog catalog) {
+		ParentPlatform parentPlatByServerGBId = platformMapper.getParentPlatByServerGBId(catalog.getPlatformId());
+		DeviceChannel deviceChannel = new DeviceChannel();
+		deviceChannel.setChannelId(catalog.getId());
+		deviceChannel.setName(catalog.getName());
+		deviceChannel.setLongitude(0.0);
+		deviceChannel.setLatitude(0.0);
+		deviceChannel.setDeviceId(parentPlatByServerGBId.getDeviceGBId());
+		deviceChannel.setManufacture("wvp-pro");
+		deviceChannel.setStatus(1);
+		deviceChannel.setParental(1);
+		deviceChannel.setParentId(catalog.getParentId());
+		deviceChannel.setRegisterWay(1);
+		// 琛屾斂鍖哄垝搴旇鏄疍omain鐨勫墠鍏綅
+		deviceChannel.setCivilCode(sipConfig.getDomain().substring(0, sipConfig.getDomain().length() - 2));
+		deviceChannel.setModel("live");
+		deviceChannel.setOwner("wvp-pro");
+		deviceChannel.setSecrecy("0");
+		return deviceChannel;
+	}
+
+	@Override
+	public List<DeviceChannel> queryOnlineChannelsByDeviceId(String deviceId) {
+		return deviceChannelMapper.queryOnlineChannelsByDeviceId(deviceId);
+	}
+
+	@Override
+	public List<ParentPlatform> queryPlatFormListForGBWithGBId(String channelId, List<String> platforms) {
+		return platformChannelMapper.queryPlatFormListForGBWithGBId(channelId, platforms);
+	}
+
+	@Override
+	public List<ParentPlatform> queryPlatFormListForStreamWithGBId(String app, String stream, List<String> platforms) {
+		if (platforms == null || platforms.size() == 0) {
+			return new ArrayList<>();
+		}
+		return platformGbStreamMapper.queryPlatFormListForGBWithGBId(app, stream, platforms);
+	}
+
+	@Override
+	public GbStream getGbStream(String app, String streamId) {
+		return gbStreamMapper.selectOne(app, streamId);
+	}
+
+	@Override
+	public void delCatalogByPlatformId(String serverGBId) {
+		catalogMapper.delByPlatformId(serverGBId);
+	}
+
+	@Override
+	public void delRelationByPlatformId(String serverGBId) {
+		platformGbStreamMapper.delByPlatformId(serverGBId);
+		platformChannelMapper.delByPlatformId(serverGBId);
+	}
+
+	@Override
+	public PlatformCatalog queryDefaultCatalogInPlatform(String platformId) {
+		return catalogMapper.selectDefaultByPlatFormId(platformId);
+	}
+
+	@Override
+	public List<ChannelSourceInfo> getChannelSource(String platformId, String gbId) {
+		return platformMapper.getChannelSource(platformId, gbId);
+	}
 }

--
Gitblit v1.8.0