GbStreamServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.gb28181.bean.*;
  3. import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
  4. import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
  5. import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem;
  6. import com.genersoft.iot.vmp.service.IGbStreamService;
  7. import com.genersoft.iot.vmp.storager.dao.GbStreamMapper;
  8. import com.genersoft.iot.vmp.storager.dao.ParentPlatformMapper;
  9. import com.genersoft.iot.vmp.storager.dao.PlatformCatalogMapper;
  10. import com.genersoft.iot.vmp.storager.dao.PlatformGbStreamMapper;
  11. import com.github.pagehelper.PageHelper;
  12. import com.github.pagehelper.PageInfo;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.TransactionDefinition;
  19. import org.springframework.transaction.TransactionStatus;
  20. import org.springframework.util.ObjectUtils;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. @Service
  24. public class GbStreamServiceImpl implements IGbStreamService {
  25. private final static Logger logger = LoggerFactory.getLogger(GbStreamServiceImpl.class);
  26. @Autowired
  27. DataSourceTransactionManager dataSourceTransactionManager;
  28. @Autowired
  29. TransactionDefinition transactionDefinition;
  30. @Autowired
  31. private GbStreamMapper gbStreamMapper;
  32. @Autowired
  33. private PlatformGbStreamMapper platformGbStreamMapper;
  34. @Autowired
  35. private ParentPlatformMapper platformMapper;
  36. @Autowired
  37. private PlatformCatalogMapper catalogMapper;
  38. @Autowired
  39. private EventPublisher eventPublisher;
  40. @Override
  41. public PageInfo<GbStream> getAll(Integer page, Integer count, String platFormId, String catalogId, String query, String mediaServerId) {
  42. PageHelper.startPage(page, count);
  43. List<GbStream> all = gbStreamMapper.selectAll(platFormId, catalogId, query, mediaServerId);
  44. return new PageInfo<>(all);
  45. }
  46. @Override
  47. public void del(String app, String stream) {
  48. gbStreamMapper.del(app, stream);
  49. }
  50. @Override
  51. public boolean addPlatformInfo(List<GbStream> gbStreams, String platformId, String catalogId) {
  52. // 放在事务内执行
  53. boolean result = false;
  54. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  55. ParentPlatform parentPlatform = platformMapper.getParentPlatByServerGBId(platformId);
  56. if (catalogId == null) {
  57. catalogId = parentPlatform.getCatalogId();
  58. }
  59. try {
  60. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  61. for (GbStream gbStream : gbStreams) {
  62. gbStream.setCatalogId(catalogId);
  63. gbStream.setPlatformId(platformId);
  64. // TODO 修改为批量提交
  65. platformGbStreamMapper.add(gbStream);
  66. DeviceChannel deviceChannelListByStream = getDeviceChannelListByStreamWithStatus(gbStream, catalogId, parentPlatform);
  67. deviceChannelList.add(deviceChannelListByStream);
  68. }
  69. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  70. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
  71. result = true;
  72. }catch (Exception e) {
  73. logger.error("批量保存流与平台的关系时错误", e);
  74. dataSourceTransactionManager.rollback(transactionStatus);
  75. }
  76. return result;
  77. }
  78. @Override
  79. public DeviceChannel getDeviceChannelListByStream(GbStream gbStream, String catalogId, ParentPlatform platform) {
  80. DeviceChannel deviceChannel = new DeviceChannel();
  81. deviceChannel.setChannelId(gbStream.getGbId());
  82. deviceChannel.setName(gbStream.getName());
  83. deviceChannel.setLongitude(gbStream.getLongitude());
  84. deviceChannel.setLatitude(gbStream.getLatitude());
  85. deviceChannel.setDeviceId(platform.getDeviceGBId());
  86. deviceChannel.setManufacture("wvp-pro");
  87. deviceChannel.setStatus(gbStream.isStatus()?1:0);
  88. deviceChannel.setRegisterWay(1);
  89. deviceChannel.setCivilCode(platform.getAdministrativeDivision());
  90. if (platform.getTreeType().equals(TreeType.CIVIL_CODE)){
  91. deviceChannel.setCivilCode(catalogId);
  92. }else if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)){
  93. PlatformCatalog catalog = catalogMapper.select(catalogId);
  94. if (catalog == null) {
  95. deviceChannel.setParentId(platform.getDeviceGBId());
  96. deviceChannel.setBusinessGroupId(null);
  97. }else {
  98. deviceChannel.setParentId(catalog.getId());
  99. deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
  100. }
  101. }
  102. deviceChannel.setModel("live");
  103. deviceChannel.setOwner("wvp-pro");
  104. deviceChannel.setParental(0);
  105. deviceChannel.setSecrecy("0");
  106. return deviceChannel;
  107. }
  108. @Override
  109. public boolean delPlatformInfo(String platformId, List<GbStream> gbStreams) {
  110. // 放在事务内执行
  111. boolean result = false;
  112. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  113. try {
  114. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  115. platformGbStreamMapper.delByAppAndStreamsByPlatformId(gbStreams, platformId);
  116. for (GbStream gbStream : gbStreams) {
  117. DeviceChannel deviceChannel = new DeviceChannel();
  118. deviceChannel.setChannelId(gbStream.getGbId());
  119. deviceChannelList.add(deviceChannel);
  120. }
  121. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  122. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  123. result = true;
  124. }catch (Exception e) {
  125. logger.error("批量移除流与平台的关系时错误", e);
  126. dataSourceTransactionManager.rollback(transactionStatus);
  127. }
  128. return result;
  129. }
  130. @Override
  131. public void sendCatalogMsg(GbStream gbStream, String type) {
  132. if (gbStream == null || type == null) {
  133. logger.warn("[发送目录订阅]类型:流信息或类型为NULL");
  134. return;
  135. }
  136. List<GbStream> gbStreams = new ArrayList<>();
  137. if (gbStream.getGbId() != null) {
  138. gbStreams.add(gbStream);
  139. }else {
  140. GbStream gbStreamIndb = gbStreamMapper.selectOne(gbStream.getApp(), gbStream.getStream());
  141. if (gbStreamIndb != null && gbStreamIndb.getGbId() != null){
  142. gbStreams.add(gbStreamIndb);
  143. }
  144. }
  145. sendCatalogMsgs(gbStreams, type);
  146. }
  147. @Override
  148. public void sendCatalogMsgs(List<GbStream> gbStreams, String type) {
  149. if (gbStreams.size() > 0) {
  150. for (GbStream gs : gbStreams) {
  151. if (ObjectUtils.isEmpty(gs.getGbId())){
  152. continue;
  153. }
  154. List<ParentPlatform> parentPlatforms = platformGbStreamMapper.selectByAppAndStream(gs.getApp(), gs.getStream());
  155. if (parentPlatforms.size() > 0) {
  156. for (ParentPlatform parentPlatform : parentPlatforms) {
  157. if (parentPlatform != null) {
  158. eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), gs, type);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. @Override
  166. public int updateGbIdOrName(List<StreamPushItem> streamPushItemForUpdate) {
  167. return gbStreamMapper.updateGbIdOrName(streamPushItemForUpdate);
  168. }
  169. @Override
  170. public DeviceChannel getDeviceChannelListByStreamWithStatus(GbStream gbStream, String catalogId, ParentPlatform platform) {
  171. DeviceChannel deviceChannel = new DeviceChannel();
  172. deviceChannel.setChannelId(gbStream.getGbId());
  173. deviceChannel.setName(gbStream.getName());
  174. deviceChannel.setLongitude(gbStream.getLongitude());
  175. deviceChannel.setLatitude(gbStream.getLatitude());
  176. deviceChannel.setDeviceId(platform.getDeviceGBId());
  177. deviceChannel.setManufacture("wvp-pro");
  178. // todo 目前是每一条查询一次,需要优化
  179. Boolean status = null;
  180. if ("proxy".equals(gbStream.getStreamType())) {
  181. status = gbStreamMapper.selectStatusForProxy(gbStream.getApp(), gbStream.getStream());
  182. }else {
  183. status = gbStreamMapper.selectStatusForPush(gbStream.getApp(), gbStream.getStream());
  184. }
  185. deviceChannel.setStatus((status != null && status )?1:0);
  186. deviceChannel.setRegisterWay(1);
  187. deviceChannel.setCivilCode(platform.getAdministrativeDivision());
  188. if (platform.getTreeType().equals(TreeType.CIVIL_CODE)){
  189. deviceChannel.setCivilCode(catalogId);
  190. }else if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)){
  191. PlatformCatalog catalog = catalogMapper.select(catalogId);
  192. if (catalog == null) {
  193. deviceChannel.setParentId(platform.getDeviceGBId());
  194. deviceChannel.setBusinessGroupId(null);
  195. }else {
  196. deviceChannel.setParentId(catalog.getId());
  197. deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
  198. }
  199. }
  200. deviceChannel.setModel("live");
  201. deviceChannel.setOwner("wvp-pro");
  202. deviceChannel.setParental(0);
  203. deviceChannel.setSecrecy("0");
  204. return deviceChannel;
  205. }
  206. @Override
  207. public List<GbStream> getAllGBChannels(String platformId) {
  208. return gbStreamMapper.selectAll(platformId, null, null, null);
  209. }
  210. @Override
  211. public void delAllPlatformInfo(String platformId, String catalogId) {
  212. if (platformId == null) {
  213. return ;
  214. }
  215. ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformId);
  216. if (platform == null) {
  217. return ;
  218. }
  219. if (ObjectUtils.isEmpty(catalogId)) {
  220. catalogId = platform.getDeviceGBId();
  221. }
  222. if (platformGbStreamMapper.delByPlatformAndCatalogId(platformId, catalogId) > 0) {
  223. List<GbStream> gbStreams = platformGbStreamMapper.queryChannelInParentPlatformAndCatalog(platformId, catalogId);
  224. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  225. for (GbStream gbStream : gbStreams) {
  226. DeviceChannel deviceChannel = new DeviceChannel();
  227. deviceChannel.setChannelId(gbStream.getGbId());
  228. deviceChannelList.add(deviceChannel);
  229. }
  230. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  231. }
  232. }
  233. }