From 2b1f7a47394363e95deb4dfa0f1c67d41e747f7f Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 01 二月 2023 10:56:40 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0' into fix-269

---
 src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStorageImpl.java |  992 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 992 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStorageImpl.java
new file mode 100644
index 0000000..41cabad
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStorageImpl.java
@@ -0,0 +1,992 @@
+package com.genersoft.iot.vmp.storager.impl;
+
+import com.genersoft.iot.vmp.conf.SipConfig;
+import com.genersoft.iot.vmp.conf.UserSetting;
+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.media.zlm.dto.StreamProxyItem;
+import com.genersoft.iot.vmp.service.IGbStreamService;
+import com.genersoft.iot.vmp.service.bean.GPSMsgInfo;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import com.genersoft.iot.vmp.storager.dao.*;
+import com.genersoft.iot.vmp.storager.dao.dto.ChannelSourceInfo;
+import com.genersoft.iot.vmp.utils.DateUtil;
+import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+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.Component;
+import org.springframework.transaction.TransactionDefinition;
+import org.springframework.transaction.TransactionStatus;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * 瑙嗛璁惧鏁版嵁瀛樺偍-jdbc瀹炵幇
+ * swwheihei
+ * 2020骞�5鏈�6鏃� 涓嬪崍2:31:42
+ */
+@SuppressWarnings("rawtypes")
+@Component
+public class VideoManagerStorageImpl implements IVideoManagerStorage {
+
+	private final Logger logger = LoggerFactory.getLogger(VideoManagerStorageImpl.class);
+
+	@Autowired
+	EventPublisher eventPublisher;
+
+	@Autowired
+	SipConfig sipConfig;
+
+
+	@Autowired
+	TransactionDefinition transactionDefinition;
+
+	@Autowired
+	DataSourceTransactionManager dataSourceTransactionManager;
+
+	@Autowired
+    private DeviceMapper deviceMapper;
+
+	@Autowired
+	private DeviceChannelMapper deviceChannelMapper;
+
+	@Autowired
+	private DeviceMobilePositionMapper deviceMobilePositionMapper;
+
+	@Autowired
+    private ParentPlatformMapper platformMapper;
+
+	@Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+	@Autowired
+    private PlatformChannelMapper platformChannelMapper;
+
+	@Autowired
+    private StreamProxyMapper streamProxyMapper;
+
+	@Autowired
+    private StreamPushMapper streamPushMapper;
+
+	@Autowired
+    private GbStreamMapper gbStreamMapper;
+
+	@Autowired
+    private UserSetting userSetting;
+
+	@Autowired
+    private PlatformCatalogMapper catalogMapper;
+
+	@Autowired
+    private PlatformGbStreamMapper platformGbStreamMapper;
+
+	@Autowired
+    private IGbStreamService gbStreamService;
+
+	@Autowired
+    private ParentPlatformMapper parentPlatformMapper;
+
+	/**
+	 * 鏍规嵁璁惧ID鍒ゆ柇璁惧鏄惁瀛樺湪
+	 *
+	 * @param deviceId 璁惧ID
+	 * @return true:瀛樺湪  false锛氫笉瀛樺湪
+	 */
+	@Override
+	public boolean exists(String deviceId) {
+		return deviceMapper.getDeviceByDeviceId(deviceId) != null;
+	}
+
+	@Override
+	public boolean resetChannels(String deviceId, List<DeviceChannel> deviceChannelList) {
+		if (CollectionUtils.isEmpty(deviceChannelList)) {
+			return false;
+		}
+		List<DeviceChannel> allChannels = deviceChannelMapper.queryAllChannels(deviceId);
+		Map<String,DeviceChannel> allChannelMap = new ConcurrentHashMap<>();
+		if (allChannels.size() > 0) {
+			for (DeviceChannel deviceChannel : allChannels) {
+				allChannelMap.put(deviceChannel.getChannelId(), deviceChannel);
+			}
+		}
+		TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
+		// 鏁版嵁鍘婚噸
+		List<DeviceChannel> channels = new ArrayList<>();
+		StringBuilder stringBuilder = new StringBuilder();
+		Map<String, Integer> subContMap = new HashMap<>();
+		if (deviceChannelList.size() > 0) {
+			// 鏁版嵁鍘婚噸
+			Set<String> gbIdSet = new HashSet<>();
+			for (DeviceChannel deviceChannel : deviceChannelList) {
+				if (!gbIdSet.contains(deviceChannel.getChannelId())) {
+					gbIdSet.add(deviceChannel.getChannelId());
+					if (allChannelMap.containsKey(deviceChannel.getChannelId())) {
+						deviceChannel.setStreamId(allChannelMap.get(deviceChannel.getChannelId()).getStreamId());
+						deviceChannel.setHasAudio(allChannelMap.get(deviceChannel.getChannelId()).isHasAudio());
+					}
+					channels.add(deviceChannel);
+					if (!ObjectUtils.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()).append(",");
+				}
+			}
+			if (channels.size() > 0) {
+				for (DeviceChannel channel : channels) {
+					if (subContMap.get(channel.getChannelId()) != null){
+						channel.setSubCount(subContMap.get(channel.getChannelId()));
+					}
+				}
+			}
+
+		}
+		if (stringBuilder.length() > 0) {
+			logger.info("[鐩綍鏌ヨ]鏀跺埌鐨勬暟鎹瓨鍦ㄩ噸澶嶏細 {}" , stringBuilder);
+		}
+		if(CollectionUtils.isEmpty(channels)){
+			logger.info("閫氶亾閲嶈锛屾暟鎹负绌�={}" , deviceChannelList);
+			return false;
+		}
+		try {
+			int cleanChannelsResult = deviceChannelMapper.cleanChannelsNotInList(deviceId, channels);
+			int limitCount = 300;
+			boolean result = cleanChannelsResult < 0;
+			if (!result && channels.size() > 0) {
+				if (channels.size() > limitCount) {
+					for (int i = 0; i < channels.size(); i += limitCount) {
+						int toIndex = i + limitCount;
+						if (i + limitCount > channels.size()) {
+							toIndex = channels.size();
+						}
+						result = result || deviceChannelMapper.batchAdd(channels.subList(i, toIndex)) < 0;
+					}
+				}else {
+					result = result || deviceChannelMapper.batchAdd(channels) < 0;
+				}
+			}
+			if (result) {
+				//浜嬪姟鍥炴粴
+				dataSourceTransactionManager.rollback(transactionStatus);
+			}
+			dataSourceTransactionManager.commit(transactionStatus);     //鎵嬪姩鎻愪氦
+			return true;
+		}catch (Exception e) {
+			e.printStackTrace();
+			dataSourceTransactionManager.rollback(transactionStatus);
+			return false;
+		}
+
+	}
+
+
+	@Override
+	public boolean updateChannels(String deviceId, List<DeviceChannel> deviceChannelList) {
+		if (CollectionUtils.isEmpty(deviceChannelList)) {
+			return false;
+		}
+		List<DeviceChannel> allChannels = deviceChannelMapper.queryAllChannels(deviceId);
+		Map<String,DeviceChannel> allChannelMap = new ConcurrentHashMap<>();
+		if (allChannels.size() > 0) {
+			for (DeviceChannel deviceChannel : allChannels) {
+				allChannelMap.put(deviceChannel.getChannelId(), deviceChannel);
+			}
+		}
+		List<DeviceChannel> addChannels = new ArrayList<>();
+		List<DeviceChannel> updateChannels = new ArrayList<>();
+
+
+		TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
+		// 鏁版嵁鍘婚噸
+		StringBuilder stringBuilder = new StringBuilder();
+		Map<String, Integer> subContMap = new HashMap<>();
+		if (deviceChannelList.size() > 0) {
+			// 鏁版嵁鍘婚噸
+			Set<String> gbIdSet = new HashSet<>();
+			for (DeviceChannel deviceChannel : deviceChannelList) {
+				if (!gbIdSet.contains(deviceChannel.getChannelId())) {
+					gbIdSet.add(deviceChannel.getChannelId());
+					if (allChannelMap.containsKey(deviceChannel.getChannelId())) {
+						deviceChannel.setStreamId(allChannelMap.get(deviceChannel.getChannelId()).getStreamId());
+						deviceChannel.setHasAudio(allChannelMap.get(deviceChannel.getChannelId()).isHasAudio());
+						updateChannels.add(deviceChannel);
+					}else {
+						addChannels.add(deviceChannel);
+					}
+					if (!ObjectUtils.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()).append(",");
+				}
+			}
+			if (addChannels.size() > 0) {
+				for (DeviceChannel channel : addChannels) {
+					if (subContMap.get(channel.getChannelId()) != null){
+						channel.setSubCount(subContMap.get(channel.getChannelId()));
+					}
+				}
+			}
+			if (updateChannels.size() > 0) {
+				for (DeviceChannel channel : updateChannels) {
+					if (subContMap.get(channel.getChannelId()) != null){
+						channel.setSubCount(subContMap.get(channel.getChannelId()));
+					}
+				}
+			}
+
+		}
+		if (stringBuilder.length() > 0) {
+			logger.info("[鐩綍鏌ヨ]鏀跺埌鐨勬暟鎹瓨鍦ㄩ噸澶嶏細 {}" , stringBuilder);
+		}
+		if(CollectionUtils.isEmpty(updateChannels) && CollectionUtils.isEmpty(addChannels) ){
+			logger.info("閫氶亾鏇存柊锛屾暟鎹负绌�={}" , deviceChannelList);
+			return false;
+		}
+		try {
+			int limitCount = 300;
+			boolean result = false;
+			if (addChannels.size() > 0) {
+				if (addChannels.size() > limitCount) {
+					for (int i = 0; i < addChannels.size(); i += limitCount) {
+						int toIndex = i + limitCount;
+						if (i + limitCount > addChannels.size()) {
+							toIndex = addChannels.size();
+						}
+						result = result || deviceChannelMapper.batchAdd(addChannels.subList(i, toIndex)) < 0;
+					}
+				}else {
+					result = result || deviceChannelMapper.batchAdd(addChannels) < 0;
+				}
+			}
+			if (updateChannels.size() > 0) {
+				if (updateChannels.size() > limitCount) {
+					for (int i = 0; i < updateChannels.size(); i += limitCount) {
+						int toIndex = i + limitCount;
+						if (i + limitCount > updateChannels.size()) {
+							toIndex = updateChannels.size();
+						}
+						result = result || deviceChannelMapper.batchUpdate(updateChannels.subList(i, toIndex)) < 0;
+					}
+				}else {
+					result = result || deviceChannelMapper.batchUpdate(updateChannels) < 0;
+				}
+			}
+			if (result) {
+				//浜嬪姟鍥炴粴
+				dataSourceTransactionManager.rollback(transactionStatus);
+			}else {
+				//鎵嬪姩鎻愪氦
+				dataSourceTransactionManager.commit(transactionStatus);
+			}
+			return true;
+		}catch (Exception e) {
+			e.printStackTrace();
+			dataSourceTransactionManager.rollback(transactionStatus);
+			return false;
+		}
+	}
+
+	@Override
+	public void deviceChannelOnline(String deviceId, String channelId) {
+		deviceChannelMapper.online(deviceId, channelId);
+	}
+
+	@Override
+	public void deviceChannelOffline(String deviceId, String channelId) {
+		deviceChannelMapper.offline(deviceId, channelId);
+	}
+
+	@Override
+	public void startPlay(String deviceId, String channelId, String streamId) {
+		deviceChannelMapper.startPlay(deviceId, channelId, streamId);
+	}
+
+	@Override
+	public void stopPlay(String deviceId, String channelId) {
+		deviceChannelMapper.stopPlay(deviceId, channelId);
+	}
+
+	/**
+	 * 鑾峰彇璁惧
+	 *
+	 * @param deviceId 璁惧ID
+	 * @return Device 璁惧瀵硅薄
+	 */
+	@Override
+	public Device queryVideoDevice(String deviceId) {
+		return deviceMapper.getDeviceByDeviceId(deviceId);
+	}
+
+	@Override
+	public PageInfo queryChannelsByDeviceId(String deviceId, String query, Boolean hasSubChannel, Boolean online, Boolean catalogUnderDevice, int page, int count) {
+		// 鑾峰彇鍒版墍鏈夋鍦ㄦ挱鏀剧殑娴�
+		PageHelper.startPage(page, count);
+		List<DeviceChannel> all;
+		if (catalogUnderDevice != null && catalogUnderDevice) {
+			all = deviceChannelMapper.queryChannels(deviceId, deviceId, query, hasSubChannel, online,null);
+			// 娴峰悍璁惧鐨刾arentId鏄疭IP id
+			List<DeviceChannel> deviceChannels = deviceChannelMapper.queryChannels(deviceId, sipConfig.getId(), query, hasSubChannel, online,null);
+			all.addAll(deviceChannels);
+		}else {
+			all = deviceChannelMapper.queryChannels(deviceId, null, query, hasSubChannel, online,null);
+		}
+		return new PageInfo<>(all);
+	}
+
+	@Override
+	public List<DeviceChannel> queryChannelsByDeviceIdWithStartAndLimit(String deviceId, String query, Boolean hasSubChannel, Boolean online, int start, int limit,List<String> channelIds) {
+		return deviceChannelMapper.queryChannelsByDeviceIdWithStartAndLimit(deviceId, null, query, hasSubChannel, online, start, limit,channelIds);
+	}
+
+
+	@Override
+	public List<DeviceChannel> queryChannelsByDeviceId(String deviceId,Boolean online,List<String> channelIds) {
+		return deviceChannelMapper.queryChannels(deviceId, null,null, null, online,channelIds);
+	}
+
+	@Override
+	public PageInfo<DeviceChannel> querySubChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, Boolean online, int page, int count) {
+		PageHelper.startPage(page, count);
+		List<DeviceChannel> all = deviceChannelMapper.queryChannels(deviceId, parentChannelId, query, hasSubChannel, online,null);
+		return new PageInfo<>(all);
+	}
+
+	@Override
+	public DeviceChannel queryChannel(String deviceId, String channelId) {
+		return deviceChannelMapper.queryChannel(deviceId, channelId);
+	}
+
+
+	@Override
+	public int delChannel(String deviceId, String channelId) {
+		return deviceChannelMapper.del(deviceId, channelId);
+	}
+
+	/**
+	 * 鑾峰彇澶氫釜璁惧
+	 *
+	 * @param page 褰撳墠椤垫暟
+	 * @param count 姣忛〉鏁伴噺
+	 * @return PageInfo<Device> 鍒嗛〉璁惧瀵硅薄鏁扮粍
+	 */
+	@Override
+	public PageInfo<Device> queryVideoDeviceList(int page, int count,Boolean online) {
+		PageHelper.startPage(page, count);
+		List<Device> all = deviceMapper.getDevices(online);
+		return new PageInfo<>(all);
+	}
+
+	/**
+	 * 鑾峰彇澶氫釜璁惧
+	 *
+	 * @return List<Device> 璁惧瀵硅薄鏁扮粍
+	 */
+	@Override
+	public List<Device> queryVideoDeviceList(Boolean online) {
+
+		List<Device> deviceList =  deviceMapper.getDevices(online);
+		return deviceList;
+	}
+
+	/**
+	 * 娓呯┖閫氶亾
+	 * @param deviceId
+	 */
+	@Override
+	public void cleanChannelsForDevice(String deviceId) {
+		deviceChannelMapper.cleanChannelsByDeviceId(deviceId);
+	}
+
+	/**
+	 * 娣诲姞Mobile Position璁惧绉诲姩浣嶇疆
+	 * @param mobilePosition
+	 */
+	@Override
+	public synchronized boolean insertMobilePosition(MobilePosition mobilePosition) {
+		if (mobilePosition.getDeviceId().equals(mobilePosition.getChannelId())) {
+			mobilePosition.setChannelId(null);
+		}
+		return deviceMobilePositionMapper.insertNewPosition(mobilePosition) > 0;
+	}
+
+	/**
+	 * 鏌ヨ绉诲姩浣嶇疆杞ㄨ抗
+	 * @param deviceId
+	 * @param startTime
+	 * @param endTime
+	 */
+	@Override
+	public synchronized List<MobilePosition> queryMobilePositions(String deviceId, String channelId, String startTime, String endTime) {
+		return deviceMobilePositionMapper.queryPositionByDeviceIdAndTime(deviceId, channelId, startTime, endTime);
+	}
+
+	@Override
+	public boolean addParentPlatform(ParentPlatform parentPlatform) {
+		if (parentPlatform.getCatalogId() == null) {
+			parentPlatform.setCatalogId(parentPlatform.getServerGBId());
+		}
+		int result = platformMapper.addParentPlatform(parentPlatform);
+		return result > 0;
+	}
+
+	@Override
+	public boolean updateParentPlatform(ParentPlatform parentPlatform) {
+		int result = 0;
+		if (parentPlatform.getCatalogGroup() == 0) {
+			parentPlatform.setCatalogGroup(1);
+		}
+		if (parentPlatform.getAdministrativeDivision() == null) {
+			parentPlatform.setAdministrativeDivision(parentPlatform.getAdministrativeDivision());
+		}
+		ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId()); // .getDeviceGBId());
+		if (parentPlatform.getId() == null ) {
+			if (parentPlatform.getCatalogId() == null) {
+				parentPlatform.setCatalogId(parentPlatform.getServerGBId());
+			}
+			result = platformMapper.addParentPlatform(parentPlatform);
+			if (parentPlatformCatch == null) {
+				parentPlatformCatch = new ParentPlatformCatch();
+				parentPlatformCatch.setParentPlatform(parentPlatform);
+				parentPlatformCatch.setId(parentPlatform.getServerGBId());
+			}
+		}else {
+			if (parentPlatformCatch == null) { // serverGBId 宸插彉鍖�
+				ParentPlatform parentPlatById = platformMapper.getParentPlatById(parentPlatform.getId());
+				// 浣跨敤鏃х殑鏌ュ嚭缂撳瓨ID
+				parentPlatformCatch = new ParentPlatformCatch();
+				parentPlatformCatch.setId(parentPlatform.getServerGBId());
+				redisCatchStorage.delPlatformCatchInfo(parentPlatById.getServerGBId());
+			}
+
+			result = platformMapper.updateParentPlatform(parentPlatform);
+		}
+		// 鏇存柊缂撳瓨
+		parentPlatformCatch.setParentPlatform(parentPlatform);
+		redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
+
+		return result > 0;
+	}
+
+	@Transactional
+	@Override
+	public boolean deleteParentPlatform(ParentPlatform parentPlatform) {
+		int result = platformMapper.delParentPlatform(parentPlatform);
+		// 鍒犻櫎鍏宠仈鐨勯�氶亾
+		platformChannelMapper.cleanChannelForGB(parentPlatform.getServerGBId());
+		return result > 0;
+	}
+
+	@Override
+	public ParentPlatform queryParentPlatByServerGBId(String platformGbId) {
+		return platformMapper.getParentPlatByServerGBId(platformGbId);
+	}
+
+	@Override
+	public List<ParentPlatform> queryEnableParentPlatformList(boolean enable) {
+		return platformMapper.getEnableParentPlatformList(enable);
+	}
+
+	@Override
+	public void outlineForAllParentPlatform() {
+		platformMapper.outlineForAllParentPlatform();
+	}
+
+
+	@Override
+	public PageInfo<ChannelReduce> queryAllChannelList(int page, int count, String query, Boolean online,
+													   Boolean channelType, String platformId, String catalogId) {
+		PageHelper.startPage(page, count);
+		List<ChannelReduce> all = deviceChannelMapper.queryChannelListInAll(query, online, channelType, platformId, catalogId);
+		return new PageInfo<>(all);
+	}
+
+	@Override
+	public List<DeviceChannelInPlatform> queryChannelListInParentPlatform(String platformId) {
+
+		return deviceChannelMapper.queryChannelByPlatformId(platformId);
+	}
+
+
+	@Override
+	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) {
+		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
+	public List<PlatformCatalog> queryChannelInParentPlatformAndCatalog(String platformId, String catalogId) {
+		List<PlatformCatalog> catalogs = platformChannelMapper.queryChannelInParentPlatformAndCatalog(platformId, catalogId);
+		return catalogs;
+	}
+
+	@Override
+	public List<PlatformCatalog> queryStreamInParentPlatformAndCatalog(String platformId, String catalogId) {
+		List<PlatformCatalog> catalogs = platformGbStreamMapper.queryChannelInParentPlatformAndCatalogForCatalog(platformId, catalogId);
+		return catalogs;
+	}
+
+	@Override
+	public Device queryVideoDeviceByPlatformIdAndChannelId(String platformId, String channelId) {
+		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);
+		}
+
+
+	}
+
+	@Override
+	public Device queryDeviceInfoByPlatformIdAndChannelId(String platformId, String channelId) {
+		List<Device> devices = platformChannelMapper.queryDeviceInfoByPlatformIdAndChannelId(platformId, channelId);
+		if (devices.size() > 1) {
+			// 鍑虹幇闀垮害澶т簬0鐨勬椂鍊欒偗瀹氭槸鍥芥爣閫氶亾鐨処D閲嶅浜�
+			logger.warn("鍥芥爣ID瀛樺湪閲嶅锛歿}", channelId);
+		}
+		if (devices.size() == 0) {
+			return null;
+		}else {
+			return devices.get(0);
+		}
+	}
+
+	/**
+	 * 鏌ヨ鏈�鏂扮Щ鍔ㄤ綅缃�
+	 * @param deviceId
+	 */
+	@Override
+	public MobilePosition queryLatestPosition(String deviceId) {
+		return deviceMobilePositionMapper.queryLatestPositionByDevice(deviceId);
+	}
+
+	/**
+	 * 鍒犻櫎鎸囧畾璁惧鐨勬墍鏈夌Щ鍔ㄤ綅缃�
+	 * @param deviceId
+	 */
+	@Override
+	public int clearMobilePositionsByDeviceId(String deviceId) {
+		return deviceMobilePositionMapper.clearMobilePositionsByDeviceId(deviceId);
+	}
+
+
+	/**
+	 * 绉婚櫎浠g悊娴�
+	 * @param app
+	 * @param stream
+	 * @return
+	 */
+	@Override
+	public int deleteStreamProxy(String app, String stream) {
+		return streamProxyMapper.del(app, stream);
+	}
+
+	/**
+	 * 鏍规嵁鏄惁鍚敤鑾峰彇浠g悊娴佸垪琛�
+	 * @param enable
+	 * @return
+	 */
+	@Override
+	public List<StreamProxyItem> getStreamProxyListForEnable(boolean enable) {
+		return streamProxyMapper.selectForEnable(enable);
+	}
+
+	/**
+	 * 鍒嗛〉鏌ヨ浠g悊娴佸垪琛�
+	 * @param page
+	 * @param count
+	 * @return
+	 */
+	@Override
+	public PageInfo<StreamProxyItem> queryStreamProxyList(Integer page, Integer count) {
+		PageHelper.startPage(page, count);
+		List<StreamProxyItem> all = streamProxyMapper.selectAll();
+		return new PageInfo<>(all);
+	}
+
+	/**
+	 * 鏍规嵁鍥芥爣ID鑾峰彇骞冲彴鍏宠仈鐨勭洿鎾祦
+	 * @param platformId
+	 * @param gbId
+	 * @return
+	 */
+	@Override
+	public GbStream queryStreamInParentPlatform(String platformId, String gbId) {
+		return gbStreamMapper.queryStreamInPlatform(platformId, gbId);
+	}
+
+	/**
+	 * 鑾峰彇骞冲彴鍏宠仈鐨勭洿鎾祦
+	 * @param platformId
+	 * @return
+	 */
+	@Override
+	public List<DeviceChannel> queryGbStreamListInPlatform(String platformId) {
+		return gbStreamMapper.queryGbStreamListInPlatform(platformId, userSetting.isUsePushingAsStatus());
+	}
+
+	/**
+	 * 鎸夌収鏄痑pp鍜宻tream鑾峰彇浠g悊娴�
+	 * @param app
+	 * @param stream
+	 * @return
+	 */
+	@Override
+	public StreamProxyItem queryStreamProxy(String app, String stream){
+		return streamProxyMapper.selectOne(app, stream);
+	}
+
+	@Override
+	public int removeMedia(String app, String stream) {
+		return streamPushMapper.del(app, stream);
+	}
+
+	@Override
+	public int mediaOffline(String app, String stream) {
+		GbStream gbStream = gbStreamMapper.selectOne(app, stream);
+		int result;
+		if ("proxy".equals(gbStream.getStreamType())) {
+			result = streamProxyMapper.updateStatus(app, stream, false);
+		}else {
+			result = streamPushMapper.updatePushStatus(app, stream, false);
+		}
+		return result;
+	}
+
+	@Override
+	public int mediaOnline(String app, String stream) {
+		GbStream gbStream = gbStreamMapper.selectOne(app, stream);
+		int result;
+		if ("proxy".equals(gbStream.getStreamType())) {
+			result = streamProxyMapper.updateStatus(app, stream, true);
+		}else {
+			result = streamPushMapper.updatePushStatus(app, stream, true);
+		}
+		return result;
+	}
+
+	@Override
+	public void updateParentPlatformStatus(String platformGbID, boolean online) {
+		platformMapper.updateParentPlatformStatus(platformGbID, online);
+	}
+
+	@Override
+	public List<StreamProxyItem> getStreamProxyListForEnableInMediaServer(String id, boolean enable) {
+		return streamProxyMapper.selectForEnableInMediaServer(id, enable);
+	}
+
+
+	@Override
+	public Device queryVideoDeviceByChannelId( String channelId) {
+		Device result = null;
+		List<DeviceChannel> channelList = deviceChannelMapper.queryChannelByChannelId(channelId);
+		if (channelList.size() == 1) {
+			result = deviceMapper.getDeviceByDeviceId(channelList.get(0).getDeviceId());
+		}
+		return result;
+	}
+
+	@Override
+	public StreamProxyItem getStreamProxyByAppAndStream(String app, String streamId) {
+		return streamProxyMapper.selectOne(app, streamId);
+	}
+
+	@Override
+	public List<PlatformCatalog> getChildrenCatalogByPlatform(String platformId, String parentId) {
+		return catalogMapper.selectByParentId(platformId, parentId);
+	}
+
+	@Override
+	public int addCatalog(PlatformCatalog platformCatalog) {
+		ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformCatalog.getPlatformId());
+		if (platform == null) {
+			return 0;
+		}
+		if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)) {
+			if (platform.getDeviceGBId().equals(platformCatalog.getParentId())) {
+				// 绗竴灞傝妭鐐�
+				platformCatalog.setBusinessGroupId(platformCatalog.getId());
+				platformCatalog.setParentId(platform.getDeviceGBId());
+			}else {
+				// 鑾峰彇椤跺眰鐨�
+				PlatformCatalog topCatalog = getTopCatalog(platformCatalog.getParentId(), platform.getDeviceGBId());
+				platformCatalog.setBusinessGroupId(topCatalog.getId());
+			}
+		}
+		if (platform.getTreeType().equals(TreeType.CIVIL_CODE)) {
+			platformCatalog.setCivilCode(platformCatalog.getId());
+			if (platformCatalog.getPlatformId().equals(platformCatalog.getParentId())) {
+				// 绗竴灞傝妭鐐�
+				platformCatalog.setParentId(platform.getDeviceGBId());
+			}
+		}
+
+		int result = catalogMapper.add(platformCatalog);
+		if (result > 0) {
+			DeviceChannel deviceChannel = getDeviceChannelByCatalog(platformCatalog);
+			eventPublisher.catalogEventPublish(platformCatalog.getPlatformId(), deviceChannel, CatalogEvent.ADD);
+		}
+		return result;
+	}
+
+	private PlatformCatalog getTopCatalog(String id, String platformId) {
+		PlatformCatalog catalog = catalogMapper.selectParentCatalog(id);
+		if (catalog.getParentId().equals(platformId)) {
+			return catalog;
+		}else {
+			return getTopCatalog(catalog.getParentId(), platformId);
+		}
+	}
+
+	@Override
+	public PlatformCatalog getCatalog(String id) {
+		return catalogMapper.select(id);
+	}
+
+	@Override
+	public int delCatalog(String id) {
+		PlatformCatalog platformCatalog = catalogMapper.select(id);
+		if (platformCatalog.getChildrenCount() > 0) {
+			List<PlatformCatalog> platformCatalogList = catalogMapper.selectByParentId(platformCatalog.getPlatformId(), platformCatalog.getId());
+			for (PlatformCatalog catalog : platformCatalogList) {
+				if (catalog.getChildrenCount() == 0) {
+					delCatalogExecute(catalog.getId(), catalog.getPlatformId());
+				}else {
+					delCatalog(catalog.getId());
+				}
+			}
+		}
+		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) {
+		int result = catalogMapper.update(platformCatalog);
+		if (result > 0) {
+			DeviceChannel deviceChannel = getDeviceChannelByCatalog(platformCatalog);
+			eventPublisher.catalogEventPublish(platformCatalog.getPlatformId(), deviceChannel, CatalogEvent.UPDATE);
+		}
+		return result;
+	}
+
+	@Override
+	public int setDefaultCatalog(String platformId, String catalogId) {
+		return platformMapper.setDefaultCatalog(platformId, catalogId, DateUtil.getNow());
+	}
+
+	@Override
+	public List<DeviceChannel> queryCatalogInPlatform(String platformId) {
+		return catalogMapper.queryCatalogInPlatform(platformId);
+	}
+
+	@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());
+				}
+			}
+		}
+		return 0;
+	}
+
+	@Override
+	public int updateStreamGPS(List<GPSMsgInfo> gpsMsgInfos) {
+		return gbStreamMapper.updateStreamGPS(gpsMsgInfos);
+	}
+
+
+	private DeviceChannel getDeviceChannelByCatalog(PlatformCatalog catalog) {
+		ParentPlatform platform = 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(platform.getDeviceGBId());
+		deviceChannel.setManufacture("wvp-pro");
+		deviceChannel.setStatus(1);
+		deviceChannel.setParental(1);
+
+		deviceChannel.setRegisterWay(1);
+		// 琛屾斂鍖哄垝搴旇鏄疍omain鐨勫墠鍏綅
+		if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)) {
+			deviceChannel.setParentId(catalog.getParentId());
+			deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
+		}
+
+		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);
+	}
+
+	@Override
+	public void updateChannelPosition(DeviceChannel deviceChannel) {
+		if (deviceChannel.getChannelId().equals(deviceChannel.getDeviceId())) {
+			deviceChannel.setChannelId(null);
+		}
+		if (deviceChannel.getGpsTime() == null) {
+			deviceChannel.setGpsTime(DateUtil.getNow());
+		}
+
+		deviceChannelMapper.updatePosition(deviceChannel);
+	}
+
+	@Override
+	public void cleanContentForPlatform(String serverGBId) {
+//		List<PlatformCatalog> catalogList = catalogMapper.selectByPlatForm(serverGBId);
+//		if (catalogList.size() > 0) {
+//			int result = catalogMapper.delByPlatformId(serverGBId);
+//			if (result > 0) {
+//				List<DeviceChannel> deviceChannels = new ArrayList<>();
+//				for (PlatformCatalog catalog : catalogList) {
+//					deviceChannels.add(getDeviceChannelByCatalog(catalog));
+//				}
+//				eventPublisher.catalogEventPublish(serverGBId, deviceChannels, CatalogEvent.DEL);
+//			}
+//		}
+		catalogMapper.delByPlatformId(serverGBId);
+		platformChannelMapper.delByPlatformId(serverGBId);
+		platformGbStreamMapper.delByPlatformId(serverGBId);
+	}
+
+	@Override
+	public List<DeviceChannel> queryChannelWithCatalog(String serverGBId) {
+		return deviceChannelMapper.queryChannelWithCatalog(serverGBId);
+	}
+}

--
Gitblit v1.8.0