wuxw7
2018-04-14 f3bf189c4ffd4fa43f5384dce87b8120a80ed428
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.java110.center.smo.impl;
 
import com.java110.common.cache.AppRouteCache;
import com.java110.center.smo.ICenterServiceSMO;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.exception.NoAuthorityException;
import com.java110.common.factory.DataFlowFactory;
import com.java110.common.util.DateUtil;
import com.java110.common.util.ResponseTemplateUtil;
import com.java110.common.util.StringUtil;
import com.java110.entity.center.AppRoute;
import com.java110.entity.center.AppService;
import com.java110.entity.center.Business;
import com.java110.entity.center.DataFlow;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.Map;
 
/**
 * 中心服务处理类
 * Created by wuxw on 2018/4/13.
 */
@Service("centerServiceSMOImpl")
@Transactional
public class CenterServiceSMOImpl implements ICenterServiceSMO {
 
    @Override
    public String service(String reqJson, Map<String,String> headers) {
 
        DataFlow dataFlow = null;
 
        try {
            //1.0 创建数据流
            dataFlow = DataFlowFactory.newInstance().builder(reqJson, headers);
            //2.0 加载配置信息
            initConfigData(dataFlow);
            //2.0 校验 APPID是否有权限操作serviceCode
            judgeAuthority(dataFlow);
 
 
        }catch(NoAuthorityException e){
 
            return ResponseTemplateUtil.createOrderResponseJson(dataFlow.getTransactionId(),
                    ResponseConstant.NO_NEED_SIGN,e.getResult().getCode(),e.getMessage());
        }catch (Exception e){
            return ResponseTemplateUtil.createOrderResponseJson(dataFlow == null
                            ?ResponseConstant.NO_TRANSACTION_ID
                            :dataFlow.getTransactionId(),
                    ResponseConstant.NO_NEED_SIGN,ResponseConstant.RESULT_CODE_INNER_ERROR,"内部异常了:"+e.getMessage()+e.getLocalizedMessage());
        }finally {
            //这里记录日志
            Date endDate = DateUtil.getCurrentDate();
            if(dataFlow == null){ //说明异常了
                return null;
            }
            dataFlow.setEndDate(endDate);
            //添加耗时
            DataFlowFactory.addCostTime(dataFlow,"service","业务处理总耗时",dataFlow.getStartDate(),dataFlow.getEndDate());
 
            //这里保存耗时,以及日志
 
            return ResponseTemplateUtil.createCommonResponseJson(dataFlow);
        }
 
    }
 
    /**
     * 初始化配置信息
     * @param dataFlow
     */
    private void initConfigData(DataFlow dataFlow){
 
        //查询配置信息,并将配置信息封装到 dataFlow 对象中
        AppRoute appRoute = AppRouteCache.getAppRoute(dataFlow.getAppId());
 
        if(appRoute == null){
            throw new RuntimeException("当前没有获取到AppId对应的信息");
        }
        dataFlow.setAppRoute(appRoute);
    }
    /**
     * 判断 AppId 是否 有serviceCode权限
     * @param dataFlow
     * @throws RuntimeException
     */
    private void judgeAuthority(DataFlow dataFlow) throws NoAuthorityException{
 
 
        if(StringUtil.isNullOrNone(dataFlow.getAppId()) || dataFlow.getAppRoute() == null){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"appId 为空或不正确");
        }
 
        if(StringUtil.isNullOrNone(dataFlow.getTransactionId())){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"transactionId 不能为空");
        }
 
        if(StringUtil.isNullOrNone(dataFlow.getUserId())){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"userId 不能为空");
        }
 
        if(StringUtil.isNullOrNone(dataFlow.getRequestTime()) || DateUtil.judgeDate(dataFlow.getRequestTime(),DateUtil.DATE_FORMATE_STRING_DEFAULT)){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"requestTime 格式不对,遵循yyyyMMddHHmmss格式");
        }
 
        if(StringUtil.isNullOrNone(dataFlow.getOrderTypeCd())){
            throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"orderTypeCd 不能为空");
        }
 
        //判断 AppId 是否有权限操作相应的服务
        if(dataFlow.getBusinesses() != null && dataFlow.getBusinesses().size() > 0){
            for (Business business : dataFlow.getBusinesses()){
 
               AppService appService = DataFlowFactory.getService(dataFlow,business.getServiceCode());
 
                //这里调用缓存 查询缓存信息
                if(appService == null){
                    throw new NoAuthorityException(ResponseConstant.RESULT_CODE_NO_AUTHORITY_ERROR,"AppId 没有权限访问 serviceCod = "+business.getServiceCode());
                }
            }
        }
    }
 
}