wuxw7
2018-04-20 274d91f5c1d85c6ae20a4ba932d6e245f1eb7f52
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
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 JSONObject 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;
    }
 
    /**
     * 返回模板 只有Order信息
     * @param transactionId
     * @param sign
     * @param code
     * @param message
     * @return
     */
    public static JSONObject createOrderResponseJson(String transactionId, String sign,String code,String message){
        return createCommonResponseJson(transactionId,sign,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);
    }
    /**
     * 业务系统返回报文模板
     * @param code
     * @param message
     * @param info
     * @return
     */
    public static JSONObject createBusinessResponseJson(String code, String message, JSONObject 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);
        }
 
        return responseMessage;
    }
}