wuxw7
2018-05-22 9e0fa31a2b81a9d2fd2588b78b6de637f0352639
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.java110.core.factory;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.common.constant.MappingConstant;
import com.java110.common.constant.OrderTypeCdConstant;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.constant.StatusConstant;
import com.java110.common.util.Assert;
import com.java110.common.util.DateUtil;
import com.java110.common.util.SequenceUtil;
import com.java110.common.util.StringUtil;
import com.java110.core.context.DataFlow;
import com.java110.core.context.DataFlowContext;
import com.java110.entity.service.PageData;
 
import java.util.Date;
import java.util.Map;
 
/**
 * 请求信息封装工厂类
 * Created by wuxw on 2018/4/28.
 */
public class DataTransactionFactory {
 
 
    /**
     * 创建 通用返回结果模板
     * @param transactionId 交易流水
     * @param code 错误
     * @param message 错误信息
     * @param business 业务信息
     * @return
     */
    public static JSONObject createCommonResponseJson(String transactionId,
                                                      String code,
                                                      String message,
                                                      JSONArray business){
 
        JSONObject responseInfo = JSONObject.parseObject("{\"orders\":{\"response\":{}}}");
        JSONObject orderInfo = responseInfo.getJSONObject("orders");
        orderInfo.put("transactionId",transactionId);
        orderInfo.put("responseTime",DateUtil.getDefaultFormateTimeString(new Date()));
        JSONObject orderResponseInfo = orderInfo.getJSONObject("response");
        orderResponseInfo.put("code",code);
        orderResponseInfo.put("message",message);
        if (business != null && business.size() > 0){
            responseInfo.put("business",business);
        }
        return responseInfo;
    }
 
    /**
     * 返回模板 只有Order信息
     * @param transactionId
     * @param code
     * @param message
     * @return
     */
    public static JSONObject createOrderResponseJson(String transactionId,String code,String message){
        return createCommonResponseJson(transactionId,code,message,null);
    }
 
    /**
     * 组装返回报文
     * @param dataFlow 数据流
     * @return
     */
    public static JSONObject createCommonResponseJson(DataFlow dataFlow){
        return dataFlow.getResponseBusinessJson();
    }
 
    /**
     * 业务系统返回报文模板
     * @param code
     * @param message
     * @param
     * @return
     */
    public static JSONObject createBusinessResponseJson(String code, String message){
        return   createBusinessResponseJson(code, message,null);
    }
 
    /**
     * 在 BusinessServiceDataFlow 中获取不到 bId 和 businessType 时
     * 从报文中获取,如果报文中也获取不到,说明 报文不对 返回空,也无妨
     * @param req
     * @param code
     * @param message
     * @param info
     * @return
     */
    public static JSONObject createNoBusinessTypeBusinessResponseJson(String req,String code, String message, JSONObject info){
        String bId = "";
        String businessType = "";
        String transactionId = "";
        String orderTypeCd = "";
        String dataFlowId = "";
        String serviceCode = "";
        try{
            JSONObject reqJson = JSONObject.parseObject(req);
            businessType = reqJson.getJSONObject("orders").getString("businessType");
            transactionId = reqJson.getJSONObject("orders").getString("transactionId");
            orderTypeCd = reqJson.getJSONObject("orders").getString("orderTypeCd");
            dataFlowId = reqJson.getJSONObject("orders").getString("dataFlowId");
            bId = reqJson.getJSONObject("business").getString("bId");
            serviceCode = reqJson.getJSONObject("business").getString("serviceCode");
        }catch (Exception e){
            bId = "-1";
            businessType = "";
            transactionId = "-1";
            orderTypeCd ="Q";
            dataFlowId ="-1";
        }
 
        return createBusinessResponseJson(code,message,transactionId,bId,businessType,orderTypeCd,dataFlowId,serviceCode,info);
    }
    /**
     * 业务系统返回报文模板
     * @param code
     * @param message
     * @param info
     * @return
     */
    public static JSONObject createBusinessResponseJson(String code, String message, JSONObject info){
        return createBusinessResponseJson(code,message,"-1","-1", "",StatusConstant.REQUEST_BUSINESS_TYPE,"-1","",info);
    }
 
    public static JSONObject createBusinessResponseJson(DataFlowContext dataFlowContext,
                                                        String code,String message,Map<String,Object> info){
        return createBusinessResponseJson(code,message,dataFlowContext.getOrder().getTransactionId(),
                dataFlowContext.getbId(),dataFlowContext.getOrder().getBusinessType(),dataFlowContext.getOrder().getOrderTypeCd(),
                dataFlowContext.getOrder().getDataFlowId(),dataFlowContext.getCurrentBusiness().getServiceCode(),info);
    }
 
    public static JSONObject createBusinessResponseJson(String code, String message,String transactionId,
                                                        String bId,String businessType,
                                                        String orderTypeCd,
                                                        String dataFlowId,
                                                        String serviceCode,
                                                        Map<String,Object> info){
        JSONObject responseMessage = JSONObject.parseObject("{\"response\":{}}");
 
        JSONObject response = responseMessage.getJSONObject("response");
 
        response.put("code",code);
        response.put("message",message);
        if(info != null) {
            responseMessage.putAll(info);
        }
        responseMessage.put("bId",bId);
        responseMessage.put("businessType",businessType);
        responseMessage.put("responseTime",DateUtil.getDefaultFormateTimeString(new Date()));
        responseMessage.put("transactionId",transactionId);
        responseMessage.put("orderTypeCd",orderTypeCd);
        responseMessage.put("dataFlowId",dataFlowId);
        responseMessage.put("serviceCode",serviceCode);
 
        return responseMessage;
    }
 
    /*public static JSONObject createBusinessResponseJson(String code, String message,String bId,String businessType, JSONObject info){
        return createBusinessResponseJson(code,message,bId, businessType,info);
    }*/
 
    /**
     * 创建查询 请求 centerService json 报文
     * @param appId
     * @param userId
     * @param sign
     * @param business
     * @return
     */
    public static String createQueryOneCenterServiceRequestJson(String appId,String userId,String sign,
                                                             JSONObject business){
        JSONArray businesses = new JSONArray();
        businesses.add(business);
        return createCenterServiceRequestJson(appId,userId, OrderTypeCdConstant.ORDER_TYPE_CD_QUERY,sign,"",null,businesses);
    }
 
 
    /**
     * 创建查询 请求 centerService json 报文
     * @param appId
     * @param userId
     * @param sign
     * @param businesses
     * @return
     */
    public static String createQueryCenterServiceRequestJson(String appId,String userId,String sign,
                                                        JSONArray businesses){
        return createCenterServiceRequestJson(appId,userId, OrderTypeCdConstant.ORDER_TYPE_CD_QUERY,sign,"",null,businesses);
    }
 
 
    /**
     * 查询一个服务请求报文
     * @param serviceCode
     * @param serviceName
     * @param paramIn
     * @return
     */
    public static JSONObject createQueryOneBusinessRequestJson(String serviceCode,String serviceName,Map paramIn){
        JSONObject business = JSONObject.parseObject("{\"datas\":{}}");
        business.put("serviceCode",serviceCode);
        business.put("serviceName",serviceName);
        business.getJSONObject("datas").put("params",paramIn);
        return business;
    }
 
 
 
    /**
     * 创建请求 center 报文 分装成JSON
     * @param appId
     * @return
     */
    public static String createCenterServiceRequestJson(String appId,String userId,String orderTypeCd,String sign,String remark,
                                                        JSONArray orderAttrs,JSONArray businesses){
        JSONObject paramIn = JSONObject.parseObject("{\"orders\":{},\"business\":[]}");
        JSONObject orders = paramIn.getJSONObject("orders");
        orders.put("appId",appId);
        orders.put("transactionId", SequenceUtil.getTransactionId());
        orders.put("userId",userId);
        orders.put("orderTypeCd",orderTypeCd);
        orders.put("requestTime",DateUtil.getNowDefault());
        orders.put("remark",remark);
        if(orderAttrs != null && orderAttrs.size()>0) {
            orders.put("attrs", orderAttrs);
        }
        JSONArray businessArray = paramIn.getJSONArray("business");
        businessArray.addAll(businesses);
 
        if(!StringUtil.isNullOrNone(sign)) {
            orders.put("sign", AuthenticationFactory.md5(orders.getString("transactionId"), orders.getString("appId"),
                    businessArray.toJSONString(), sign));
        }
        return paramIn.toJSONString();
    }
 
    /**
     * 获取Business 内容
     * @return
     */
    public static JSONArray getBusinessFromCenterServiceResponseJson(String resJson){
 
        JSONObject paramOut = JSONObject.parseObject(resJson);
        return paramOut.getJSONArray("business");
    }
 
    public static JSONObject getOneBusinessFromCenterServiceResponseJson(String resJson){
        JSONArray paramOut = getBusinessFromCenterServiceResponseJson(resJson);
 
        if(paramOut != null && paramOut.size() > 0){
           JSONObject business =  paramOut.getJSONObject(0);
           if(!"0000".equals(business.getJSONObject("response").getString("code"))){
               return null;
            }
 
            return business;
        }
 
        return null;
    }
 
    /**
     * 数据加密
     * @param reqInfo
     * @param keySize
     * @return
     * @throws Exception
     */
    public static String encrypt(String reqInfo,int keySize) throws Exception {
        return new String(AuthenticationFactory.encrypt(reqInfo.getBytes("UTF-8"), AuthenticationFactory.loadPubKey(MappingConstant.KEY_OUT_PUBLIC_STRING)
                ,keySize),"UTF-8");
    }
 
    /**
     * 数据解密
     * @param resInfo
     * @param keySize
     * @return
     * @throws Exception
     */
    public static String decrypt(String resInfo,int keySize) throws Exception{
        return new String(AuthenticationFactory.decrypt(resInfo.getBytes("UTF-8"), AuthenticationFactory.loadPrivateKey(MappingConstant.KEY_OUT_PRIVATE_STRING)
                ,keySize),"UTF-8");
    }
 
 
    /**
     * 页面返回报文封装
     *
     * {
     "meta":{
     "code":"",//主要用于,日志记录
     "message":"",
     "responseTime":"",
     "transactionId":"请求流水" //由系统返回
     },
     "data":{
     //这里是返回参数
     }
     }
     * @return
     * @throws Exception
     */
    public static String pageResponseJson(String transactionId,String code,String message,JSONObject data){
        JSONObject paramOut = JSONObject.parseObject("{\"meta\":{}}");
        JSONObject metaObj = paramOut.getJSONObject("meta");
        metaObj.put("transactionId",transactionId);
        metaObj.put("code",code);
        metaObj.put("message",message);
        metaObj.put("responseTime",DateUtil.getNowDefault());
        if(data != null) {
            paramOut.put("data", data);
        }
        return paramOut.toJSONString();
    }
 
    /**
     * 页面返回封装
     * @param code 编码
     * @param message 信息
     * @param data 数据
     * @return
     * @throws Exception
     */
    public static String pageResponseJson(String code,String message,JSONObject data){
        return pageResponseJson(ResponseConstant.NO_TRANSACTION_ID,code,message,data);
    }
 
    /**
     * 页面返回封装
     * @param code 编码
     * @param message 信息
     * @param data 数据
     * @return
     * @throws Exception
     */
    public static String pageResponseJson(PageData pd,String code, String message, JSONObject data){
        return pageResponseJson(pd == null || StringUtil.isNullOrNone(pd.getTransactionId()) ? ResponseConstant.NO_TRANSACTION_ID:pd.getTransactionId(),code,message,data);
    }
 
    /**
     * 页面处理成功信息封装
     * @param transactionId
     * @return
     * @throws Exception
     */
    public static String pageResponseJson(String transactionId){
        return pageResponseJson(transactionId,ResponseConstant.RESULT_CODE_SUCCESS,"成功",null);
    }
 
}