From 71bc98161c605c585fb9fd62ae84a735a3650e8b Mon Sep 17 00:00:00 2001
From: 吴学文 <wuxuewen@wuxuewendeMacBook-Pro.local>
Date: 星期三, 17 七月 2019 21:41:10 +0800
Subject: [PATCH] 服务绑定开发选择服务环节

---
 java110-logAgent/src/main/java/com/java110/log/agent/LogAgent.java |  101 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 96 insertions(+), 5 deletions(-)

diff --git a/java110-logAgent/src/main/java/com/java110/log/agent/LogAgent.java b/java110-logAgent/src/main/java/com/java110/log/agent/LogAgent.java
index 4bc2a28..a70bd5c 100644
--- a/java110-logAgent/src/main/java/com/java110/log/agent/LogAgent.java
+++ b/java110-logAgent/src/main/java/com/java110/log/agent/LogAgent.java
@@ -1,13 +1,20 @@
 package com.java110.log.agent;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.JSONPath;
 import com.java110.common.cache.MappingCache;
 import com.java110.common.constant.KafkaConstant;
 import com.java110.common.constant.MappingConstant;
+import com.java110.common.constant.ResponseConstant;
 import com.java110.common.factory.ApplicationContextFactory;
 import com.java110.common.kafka.KafkaFactory;
 import com.java110.common.log.LoggerEngine;
+import com.java110.common.util.Assert;
 import com.java110.core.context.DataFlow;
 import com.java110.core.context.TransactionLog;
+
+import java.util.Map;
 
 /**
  * 鏃ュ織浠g悊
@@ -18,8 +25,6 @@
 
     public static final String LOG_STATUS_S = "S";
     public static final String LOG_STATUS_F = "F";
-    public static final String LOG_TYPE_S = "S";
-    public static final String LOG_TYPE_C = "C";
 
 
     /**
@@ -40,10 +45,96 @@
     }
 
 
-    public static boolean sendLog(DataFlow dataFlow){
-        return sendLog(dataFlow);
-
+    /**
+     * 鍙戦�佷氦浜掓棩蹇�
+     * @param dataFlow 鏁版嵁娴佸璞�
+     * @param requestMessage 璇锋眰鏁版嵁
+     * @param responseMessage 杩斿洖鏁版嵁
+     * @param logStatus 鏃ュ織鐘舵��
+     * @return
+     */
+    public static boolean sendLog(DataFlow dataFlow,String requestMessage,String responseMessage,String logStatus,long costTime){
+        return sendLog(dataFlow.reBuilder(requestMessage,responseMessage,logStatus,costTime));
     }
 
+    /**
+     * 鍙戦�佷氦浜掓棩蹇�
+     * 璇锋眰鎶ユ枃鍜岃繑鍥炴姤鏂囧繀椤荤粍瑁呮垚
+     * {"headers":"",
+     * "body":""
+     * }
+     * @param dataFlow 鏁版嵁娴佸璞�
+     * @param requestMessage 璇锋眰鏁版嵁
+     * @param responseMessage 杩斿洖鏁版嵁
+     * @return
+     */
+    public static boolean sendLog(DataFlow dataFlow, JSONObject requestMessage, JSONObject responseMessage,long costTime){
+        Assert.hasKey(responseMessage,"body","杩斿洖鎶ユ枃涓嶆弧瓒� 鏃ュ織鍗忚瑕佹眰"+responseMessage.toJSONString());
+
+        String body = responseMessage.getString("body");
+        String logStatus = LOG_STATUS_F;
+        //濡傛灉鏄疛SONObject
+        if(Assert.isJsonObject(body)){
+            JSONObject bodyObj = JSONObject.parseObject(body);
+
+            Object codeNode = JSONPath.eval(bodyObj,"$.orders.response.code");
+            //鍒ゆ柇璁㈠崟鏄惁鎴愬姛
+            if(codeNode != null && ResponseConstant.RESULT_CODE_SUCCESS.equals(codeNode.toString())){
+                //鍒ゆ柇涓氬姟鏄惁鍙楃悊鎴愬姛涓紝濡傛灉鏈変竴涓笟鍔″彈鐞嗗け璐ワ紝鍒欒涓哄け璐�
+                if(!bodyObj.containsKey("business")){
+                    return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S,costTime);
+                }
+
+                if(bodyObj.get("business") instanceof JSONObject){
+                    JSONObject businessObj = bodyObj.getJSONObject("business");
+                    if(businessObj.containsKey("response")&&
+                            ResponseConstant.RESULT_CODE_SUCCESS.equals(businessObj.getJSONObject("response").getString("code"))){
+                        return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S,costTime);
+                    }
+                }
+
+                if(bodyObj.get("business") instanceof JSONArray){
+                    JSONArray businessArrays = bodyObj.getJSONArray("business");
+                    if(businessArrays == null || businessArrays.size() == 0){
+                        return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),LOG_STATUS_S,costTime);
+                    }
+
+                    logStatus = LOG_STATUS_S;
+                    for(int businessIndex = 0; businessIndex < businessArrays.size();businessIndex ++){
+                        JSONObject businessObj = businessArrays.getJSONObject(businessIndex);
+                        if(!businessObj.containsKey("response") ||
+                                !ResponseConstant.RESULT_CODE_SUCCESS.equals(businessObj.getJSONObject("response").getString("code"))){
+                            logStatus = LOG_STATUS_F;
+                        }
+                    }
+                }
+            }
+        }
+        //濡傛灉鏈墄ml浜や簰锛屽垯鎵╁睍
+
+        //鍏煎kafka 浼犻�掓秷鎭�
+        if(ResponseConstant.RESULT_CODE_SUCCESS.equals(body)){
+            logStatus = LOG_STATUS_S;
+        }
+        return sendLog(dataFlow,requestMessage.toJSONString(),responseMessage.toJSONString(),logStatus,costTime);
+    }
+
+
+    /**
+     * 灏佽澶翠俊鎭拰 娑堟伅淇℃伅鑷砨ody涓�
+     * {"headers":"",
+     * "body":""
+     * }
+     * @param headers 澶翠俊鎭�
+     * @param dataInfo 鏁版嵁淇℃伅
+     * @return
+     */
+    public static JSONObject createLogMessage(Map<String,String> headers, String dataInfo){
+        JSONObject message = new JSONObject();
+        String headerMessage = (headers == null || headers.isEmpty())?"":JSONObject.toJSONString(headers);
+        message.put("headers",headerMessage);
+        message.put("body",dataInfo);
+        return message;
+    }
 
 }

--
Gitblit v1.8.0