wuxw7
2018-04-17 fbe3ee1eee42e4f0f0e3b4355f22eba8c1f7bdf1
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
package com.java110.common.util;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.common.constant.ResponseConstant;
import com.java110.entity.center.DataFlow;
 
import java.util.Date;
 
/**
 * 接口返回模板类
 *
 * {
     "orders": {
         "TransactionId": "100000000020180409224736000001",
         "ResponseTime": "20180409224736",
         "sign": "这个服务是否要求MD5签名",
         "response": {//这个是centerOrder 返回的状态结果
         "code": "1999",
         "message": "具体值"
         }
     },
     "business":[{//这个是相应的业务系统返回的结果,(受理为空,查询时不为空)
         "response": {
         "code": "1999",
         "message": "具体值"
         }
     //相应内容
     }]
 }
 * Created by wuxw on 2018/4/13.
 */
public class ResponseTemplateUtil {
 
    /**
     * 创建 通用返回结果模板
     * @param transactionId 交易流水
     * @param sign 签名
     * @param code 错误
     * @param message 错误信息
     * @param business 业务信息
     * @return
     */
    public static String createCommonResponseJson(String transactionId,
                                                  String sign,
                                                  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()));
        orderInfo.put("sign",sign);
        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.toJSONString();
    }
 
    /**
     * 返回模板 只有Order信息
     * @param transactionId
     * @param sign
     * @param code
     * @param message
     * @return
     */
    public static String createOrderResponseJson(String transactionId, String sign,String code,String message){
        return createCommonResponseJson(transactionId,sign,code,message,null);
    }
 
    /**
     * 组装返回报文
     * @param dataFlow 数据流
     * @return
     */
    public static String createCommonResponseJson(DataFlow dataFlow){
        return dataFlow.getResponseBusinessJson().toJSONString();
    }
}