From 5461b8ebf2f8bf66a9d34351eb8783484304958a Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 22 八月 2022 16:17:28 +0800
Subject: [PATCH] 支持全局异常和统一返回结果,未完待续

---
 src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java |   81 +++++++++++++++++++++-------------------
 1 files changed, 42 insertions(+), 39 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
index 40c37c2..8fabbda 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
@@ -4,6 +4,7 @@
 import com.alibaba.fastjson.JSONObject;
 import com.genersoft.iot.vmp.common.StreamInfo;
 import com.genersoft.iot.vmp.conf.UserSetting;
+import com.genersoft.iot.vmp.conf.exception.ControllerException;
 import com.genersoft.iot.vmp.gb28181.bean.GbStream;
 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
 import com.genersoft.iot.vmp.gb28181.bean.TreeType;
@@ -24,6 +25,7 @@
 import com.genersoft.iot.vmp.storager.dao.StreamProxyMapper;
 import com.genersoft.iot.vmp.service.IStreamProxyService;
 import com.genersoft.iot.vmp.utils.DateUtil;
+import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
 import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
 import com.github.pagehelper.PageInfo;
 import org.slf4j.Logger;
@@ -33,6 +35,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.TransactionDefinition;
 import org.springframework.transaction.TransactionStatus;
+import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import java.net.InetAddress;
@@ -93,10 +96,8 @@
 
 
     @Override
-    public WVPResult<StreamInfo> save(StreamProxyItem param) {
+    public StreamInfo save(StreamProxyItem param) {
         MediaServerItem mediaInfo;
-        WVPResult<StreamInfo> wvpResult = new WVPResult<>();
-        wvpResult.setCode(0);
         if (param.getMediaServerId() == null || "auto".equals(param.getMediaServerId())){
             mediaInfo = mediaServerService.getMediaServerForMinimumLoad();
         }else {
@@ -104,14 +105,12 @@
         }
         if (mediaInfo == null) {
             logger.warn("淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM...");
-            wvpResult.setMsg("淇濆瓨澶辫触");
-            return wvpResult;
+            throw new ControllerException(ErrorCode.ERROR100.getCode(), "淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM");
         }
-
         String dstUrl = String.format("rtmp://%s:%s/%s/%s", "127.0.0.1", mediaInfo.getRtmpPort(), param.getApp(),
                 param.getStream() );
         param.setDst_url(dstUrl);
-        StringBuffer result = new StringBuffer();
+        StringBuffer resultMsg = new StringBuffer();
         boolean streamLive = false;
         param.setMediaServerId(mediaInfo.getId());
         boolean saveResult;
@@ -121,43 +120,47 @@
         }else { // 鏂板
             saveResult = addStreamProxy(param);
         }
-        if (saveResult) {
-            result.append("淇濆瓨鎴愬姛");
-            if (param.isEnable()) {
-                JSONObject jsonObject = addStreamProxyToZlm(param);
-                if (jsonObject == null || jsonObject.getInteger("code") != 0) {
-                    streamLive = false;
-                    result.append(", 浣嗘槸鍚敤澶辫触锛岃妫�鏌ユ祦鍦板潃鏄惁鍙敤");
-                    param.setEnable(false);
-                    // 鐩存帴绉婚櫎
-                    if (param.isEnable_remove_none_reader()) {
-                        del(param.getApp(), param.getStream());
-                    }else {
-                        updateStreamProxy(param);
-                    }
-
-                }else {
-                    streamLive = true;
-                    StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
-                            mediaInfo, param.getApp(), param.getStream(), null, null);
-                    wvpResult.setData(streamInfo);
-
-                }
-            }
-        }else {
-            result.append("淇濆瓨澶辫触");
+        if (!saveResult) {
+            throw new ControllerException(ErrorCode.ERROR100.getCode(),"淇濆瓨澶辫触");
         }
-        if ( !StringUtils.isEmpty(param.getPlatformGbId()) && streamLive) {
+        StreamInfo resultForStreamInfo = null;
+        resultMsg.append("淇濆瓨鎴愬姛");
+        if (param.isEnable()) {
+            JSONObject jsonObject = addStreamProxyToZlm(param);
+            if (jsonObject == null || jsonObject.getInteger("code") != 0) {
+                streamLive = false;
+                resultMsg.append(", 浣嗘槸鍚敤澶辫触锛岃妫�鏌ユ祦鍦板潃鏄惁鍙敤");
+                param.setEnable(false);
+                // 鐩存帴绉婚櫎
+                if (param.isEnable_remove_none_reader()) {
+                    del(param.getApp(), param.getStream());
+                }else {
+                    updateStreamProxy(param);
+                }
+
+
+            }else {
+                streamLive = true;
+                resultForStreamInfo = mediaService.getStreamInfoByAppAndStream(
+                        mediaInfo, param.getApp(), param.getStream(), null, null);
+
+            }
+        }
+        if ( !ObjectUtils.isEmpty(param.getPlatformGbId()) && streamLive) {
             List<GbStream> gbStreams = new ArrayList<>();
             gbStreams.add(param);
             if (gbStreamService.addPlatformInfo(gbStreams, param.getPlatformGbId(), param.getCatalogId())){
-                result.append(",  鍏宠仈鍥芥爣骞冲彴[ " + param.getPlatformGbId() + " ]鎴愬姛");
+                return resultForStreamInfo;
             }else {
-                result.append(",  鍏宠仈鍥芥爣骞冲彴[ " + param.getPlatformGbId() + " ]澶辫触");
+                resultMsg.append(",  鍏宠仈鍥芥爣骞冲彴[ " + param.getPlatformGbId() + " ]澶辫触");
+                throw new ControllerException(ErrorCode.ERROR100.getCode(), resultMsg.toString());
+            }
+        }else {
+            if (!streamLive) {
+                throw new ControllerException(ErrorCode.ERROR100.getCode(), resultMsg.toString());
             }
         }
-        wvpResult.setMsg(result.toString());
-        return wvpResult;
+        return resultForStreamInfo;
     }
 
     /**
@@ -174,7 +177,7 @@
         streamProxyItem.setCreateTime(now);
         try {
             if (streamProxyMapper.add(streamProxyItem) > 0) {
-                if (!StringUtils.isEmpty(streamProxyItem.getGbId())) {
+                if (!ObjectUtils.isEmpty(streamProxyItem.getGbId())) {
                     if (gbStreamMapper.add(streamProxyItem) < 0) {
                         //浜嬪姟鍥炴粴
                         dataSourceTransactionManager.rollback(transactionStatus);
@@ -209,7 +212,7 @@
         streamProxyItem.setStreamType("proxy");
         try {
             if (streamProxyMapper.update(streamProxyItem) > 0) {
-                if (!StringUtils.isEmpty(streamProxyItem.getGbId())) {
+                if (!ObjectUtils.isEmpty(streamProxyItem.getGbId())) {
                     if (gbStreamMapper.updateByAppAndStream(streamProxyItem) == 0) {
                         //浜嬪姟鍥炴粴
                         dataSourceTransactionManager.rollback(transactionStatus);

--
Gitblit v1.8.0