From bf47d41dddf76c58a41692e492014499f9d433ae Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期五, 09 八月 2019 23:13:48 +0800
Subject: [PATCH] 服务绑定 及 代码生成器开发测试完成
---
CodeService/src/main/java/com/java110/code/api/CodeApi.java | 119 +++++++++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 95 insertions(+), 24 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..d67a139 100644
--- a/CodeService/src/main/java/com/java110/code/api/CodeApi.java
+++ b/CodeService/src/main/java/com/java110/code/api/CodeApi.java
@@ -1,79 +1,105 @@
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.core.smo.code.ICodeApi;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
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 {
+
+ protected static Logger logger = LoggerFactory.getLogger(CodeApi.class);
+
@Autowired
IPrimaryKeyServiceSMO primaryKeyServiceSMOImpl;
- @RequestMapping(path = "/codeApi/generate",method= RequestMethod.GET)
+ /**
+ * 鐢熸垚缂栫爜鏈嶅姟鍣� 涓嶆敮鎸丟et鏂规硶璇锋眰
+ * @param request http 璇锋眰瀵硅薄
+ * @return 涓嶆敮鎸丟et鏂规硶璇锋眰
+ */
+ @Deprecated
+ @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) {
+ /**
+ * 鐢熸垚缂栫爜鏈嶅姟鍣� 涓嶆敮鎸丟et鏂规硶璇锋眰
+ * @param orderInfo 璇锋眰淇℃伅
+ * @param request http 璇锋眰瀵硅薄
+ * @return 涓嶆敮鎸丟et鏂规硶璇锋眰
+ */
+ @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);
+ preValidateOrderInfo(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
+ *
+ * @param request 璇锋眰淇℃伅灏佽
+ * @param headers 璇锋眰澶翠俊鎭�
+ * @throws Exception 澶勭悊鏁版嵁澶辫触浼氳繑鍥濫xception寮傚父
*/
- 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
+ *
+ * @param orderInfo 璇锋眰淇℃伅灏佽
+ * @param headers 璇锋眰澶翠俊鎭�
*/
- private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
+ private void preValidateOrderInfo(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 +111,49 @@
public void setPrimaryKeyServiceSMOImpl(IPrimaryKeyServiceSMO primaryKeyServiceSMOImpl) {
this.primaryKeyServiceSMOImpl = primaryKeyServiceSMOImpl;
}
+
+ /**
+ * 鐢熸垚 缂栫爜
+ *
+ * @param prefix 鍓嶇紑
+ * @return
+ */
+ @Override
+ @RequestMapping(value = "/codeApi/generateCode", method = RequestMethod.POST)
+ public String generateCode(@RequestParam("prefix") 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, "鐢熸垚缂栫爜澶辫触 "
+ + 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