wuxw7
2018-06-16 7afa32638bd4c1eec05a19a6586af3f4b1ce8ccb
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,13 +20,14 @@
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;
@@ -51,6 +56,7 @@
    /**
     * 获取请求信息
     *
     * @param request
     * @param headers
     * @throws RuntimeException
@@ -67,6 +73,7 @@
    /**
     * 这里预校验,请求报文中不能有 dataFlowId
     *
     * @param orderInfo
     */
    private void preValiateOrderInfo(String orderInfo,Map<String, String> headers) {
@@ -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对象的JSON对象参数
            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对象的JSON对象参数
     *
     * @param prefix      前缀
     * @param requestInfo 构建的请求JSON对象
     */
    private void builderRequestInfo(String prefix, JSONObject requestInfo) {
        requestInfo.put("transactionId", UUID.randomUUID().toString().replace("-", ""));
        requestInfo.put("prefix", prefix);
        requestInfo.put("requestTime", DateUtil.getNowDefault());
    }
}