From 7afa32638bd4c1eec05a19a6586af3f4b1ce8ccb Mon Sep 17 00:00:00 2001
From: wuxw7 <wuxw7@asiainfo.com>
Date: 星期六, 16 六月 2018 01:25:43 +0800
Subject: [PATCH] 日志服务实现

---
 CodeService/src/main/java/com/java110/code/api/CodeApi.java |   91 +++++++++++++++++++++++++++++++++++----------
 1 files changed, 71 insertions(+), 20 deletions(-)

diff --git a/CodeService/src/main/java/com/java110/code/api/CodeApi.java b/CodeService/src/main/java/com/java110/code/api/CodeApi.java
index e17f3f4..49268ca 100644
--- a/CodeService/src/main/java/com/java110/code/api/CodeApi.java
+++ b/CodeService/src/main/java/com/java110/code/api/CodeApi.java
@@ -1,12 +1,16 @@
 package com.java110.code.api;
 
+import com.alibaba.fastjson.JSONObject;
 import com.java110.code.smo.IPrimaryKeyServiceSMO;
 import com.java110.common.constant.ResponseConstant;
+import com.java110.common.exception.ResponseErrorException;
 import com.java110.common.util.Assert;
+import com.java110.common.util.DateUtil;
 import com.java110.core.base.controller.BaseController;
 import com.java110.core.context.CodeDataFlow;
 import com.java110.core.factory.DataFlowFactory;
 import com.java110.core.factory.DataTransactionFactory;
+import com.java110.feign.code.ICodeApi;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -16,64 +20,67 @@
 import javax.servlet.http.HttpServletRequest;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
 
 /**
  * ID 鐢熸垚
  * Created by wuxw on 2018/6/3.
  */
 @RestController
-public class CodeApi extends BaseController {
+public class CodeApi extends BaseController implements ICodeApi {
 
     @Autowired
     IPrimaryKeyServiceSMO primaryKeyServiceSMOImpl;
 
-    @RequestMapping(path = "/codeApi/generate",method= RequestMethod.GET)
+    @RequestMapping(path = "/codeApi/generate", method = RequestMethod.GET)
     public String generateGet(HttpServletRequest request) {
-        return DataTransactionFactory.createCodeResponseJson(ResponseConstant.NO_TRANSACTION_ID,"-1",
-                ResponseConstant.RESULT_CODE_ERROR,"涓嶆敮鎸丟et鏂规硶璇锋眰").toJSONString();
+        return DataTransactionFactory.createCodeResponseJson(ResponseConstant.NO_TRANSACTION_ID, "-1",
+                ResponseConstant.RESULT_CODE_ERROR, "涓嶆敮鎸丟et鏂规硶璇锋眰").toJSONString();
     }
 
-    @RequestMapping(path = "/codeApi/generate",method= RequestMethod.POST)
-    public String generatePost(@RequestBody String orderInfo,HttpServletRequest request) {
+    @RequestMapping(path = "/codeApi/generate", method = RequestMethod.POST)
+    public String generatePost(@RequestBody String orderInfo, HttpServletRequest request) {
         Map<String, String> headers = new HashMap<String, String>();
         try {
             getRequestInfo(request, headers);
             //棰勬牎楠�
-            preValiateOrderInfo(orderInfo,headers);
-            CodeDataFlow dataFlow = DataFlowFactory.newInstance(CodeDataFlow.class).builder(orderInfo,null);
+            preValiateOrderInfo(orderInfo, headers);
+            CodeDataFlow dataFlow = DataFlowFactory.newInstance(CodeDataFlow.class).builder(orderInfo, null);
             primaryKeyServiceSMOImpl.generateCode(dataFlow);
             return dataFlow.getResJson().toJSONString();
-        }catch (Exception e){
-            logger.error("璇锋眰璁㈠崟寮傚父",e);
-            return DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e).toJSONString();
+        } catch (Exception e) {
+            logger.error("璇锋眰璁㈠崟寮傚父", e);
+            return DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR, e.getMessage() + e).toJSONString();
         }
     }
 
     /**
      * 鑾峰彇璇锋眰淇℃伅
+     *
      * @param request
      * @param headers
      * @throws RuntimeException
      */
-    private void getRequestInfo(HttpServletRequest request,Map headers) throws Exception{
-        try{
-            super.initHeadParam(request,headers);
-            super.initUrlParam(request,headers);
-        }catch (Exception e){
-            logger.error("鍔犺浇澶翠俊鎭け璐�",e);
+    private void getRequestInfo(HttpServletRequest request, Map headers) throws Exception {
+        try {
+            super.initHeadParam(request, headers);
+            super.initUrlParam(request, headers);
+        } catch (Exception e) {
+            logger.error("鍔犺浇澶翠俊鎭け璐�", e);
             throw e;
         }
     }
 
     /**
      * 杩欓噷棰勬牎楠岋紝璇锋眰鎶ユ枃涓笉鑳芥湁 dataFlowId
+     *
      * @param orderInfo
      */
-    private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
+    private void preValiateOrderInfo(String orderInfo, Map<String, String> headers) {
 
-        Assert.jsonObjectHaveKey(orderInfo,"prefix","娌℃湁鍖呭惈prefix");
+        Assert.jsonObjectHaveKey(orderInfo, "prefix", "娌℃湁鍖呭惈prefix");
 
-        Assert.jsonObjectHaveKey(orderInfo,"transactionId","娌℃湁鍖呭惈transactionId");
+        Assert.jsonObjectHaveKey(orderInfo, "transactionId", "娌℃湁鍖呭惈transactionId");
 
     }
 
@@ -85,4 +92,48 @@
     public void setPrimaryKeyServiceSMOImpl(IPrimaryKeyServiceSMO primaryKeyServiceSMOImpl) {
         this.primaryKeyServiceSMOImpl = primaryKeyServiceSMOImpl;
     }
+
+    /**
+     * 鐢熸垚 缂栫爜
+     *
+     * @param prefix 鍓嶇紑
+     * @return
+     */
+    @Override
+    public String generateCode(String prefix) {
+
+        try {
+            JSONObject requestInfo = new JSONObject();
+
+            //灏佽绗﹀悎鏋勫缓CodeDataFlow瀵硅薄鐨凧SON瀵硅薄鍙傛暟
+            builderRequestInfo(prefix, requestInfo);
+
+            CodeDataFlow dataFlow = DataFlowFactory.newInstance(CodeDataFlow.class).builder(requestInfo.toJSONString(), null);
+
+            //鐢熸垚缂栫爜
+            primaryKeyServiceSMOImpl.generateCode(dataFlow);
+
+            if (!ResponseConstant.RESULT_CODE_SUCCESS.equals(dataFlow.getResJson().getString("code"))) {
+                throw new ResponseErrorException(ResponseConstant.RESULT_CODE_ERROR, "鐢熸垚oId缂栫爜澶辫触 "
+                        + dataFlow.getResJson().getString("message"));
+            }
+
+            return dataFlow.getResJson().getString("id");
+        } catch (Exception e) {
+            logger.error("璇锋眰璁㈠崟寮傚父", e);
+            return ResponseConstant.RESULT_CODE_ERROR;
+        }
+    }
+
+    /**
+     * 灏佽绗﹀悎鏋勫缓CodeDataFlow瀵硅薄鐨凧SON瀵硅薄鍙傛暟
+     *
+     * @param prefix      鍓嶇紑
+     * @param requestInfo 鏋勫缓鐨勮姹侸SON瀵硅薄
+     */
+    private void builderRequestInfo(String prefix, JSONObject requestInfo) {
+        requestInfo.put("transactionId", UUID.randomUUID().toString().replace("-", ""));
+        requestInfo.put("prefix", prefix);
+        requestInfo.put("requestTime", DateUtil.getNowDefault());
+    }
 }

--
Gitblit v1.8.0