forked from kidgrow-microservices-platform

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
package com.kidgrow.common.model;
 
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import lombok.Data;
 
import java.io.Serializable;
import java.util.Map;
import java.util.ResourceBundle;
//import io.swagger.annotations.ApiModel;
//import io.swagger.annotations.ApiModelProperty;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: <br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/4 13:41 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
//@ApiModel(value = "响应结果")
@Data
public class ResultBody<T> implements Serializable {
    private static final long serialVersionUID = -6190689122701100762L;
 
    /**
     * 响应编码
     */
//    @ApiModelProperty(value = "响应编码:0-请求处理成功")
    private int code = 0;
    /**
     * 提示消息
     */
//    @ApiModelProperty(value = "提示消息")
    private String msg;
 
    /**
     * 请求路径
     */
//    @ApiModelProperty(value = "请求路径")
    private String path;
 
    /**
     * 响应数据
     */
//    @ApiModelProperty(value = "响应数据")
    private T data;
 
    /**
     * http状态码
     */
    private int httpStatus;
 
    /**
     * 附加数据
     */
//    @ApiModelProperty(value = "附加数据")
    private Map<String, Object> extra;
 
    /**
     * 响应时间
     */
//    @ApiModelProperty(value = "响应时间")
    private long timestamp = System.currentTimeMillis();
 
    public ResultBody() {
        super();
    }
 
    public ResultBody(int code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }
 
    public int getCode() {
        return code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public String getPath() {
        return path;
    }
 
    public T getData() {
        return data;
    }
 
    public Map<String, Object> getExtra() {
        return extra;
    }
 
    public long getTimestamp() {
        return timestamp;
    }
 
    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    public int getHttpStatus() {
        return httpStatus;
    }
 
    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    public boolean isOk() {
        return this.code == ErrorCode.OK.getCode();
    }
 
 
    public static ResultBody ok() {
        return new ResultBody().code(ErrorCode.OK.getCode()).msg(ErrorCode.OK.getMessage());
    }
 
 
    public static ResultBody failed() {
        return new ResultBody().code(ErrorCode.FAIL.getCode()).msg(ErrorCode.FAIL.getMessage());
    }
 
    public static ResultBody failed(String msg) {
        return new ResultBody().code(ErrorCode.FAIL.getCode()).msg(msg);
    }
 
    public static ResultBody ok(int code,String msg) {
 
        return new ResultBody(code,msg,null);
    }
 
    public static ResultBody failed(int code,String msg) {
 
        return new ResultBody(code,msg,null);
    }
 
 
    public static <T> ResultBody<T> fail(int code, String msg) {
        return new ResultBody<>(code, msg,null);
    }
 
 
    public ResultBody code(int code) {
        this.code = code;
        return this;
    }
 
    public ResultBody msg(String msg) {
        this.msg = i18n(ErrorCode.getResultEnum(this.code).getMessage(), msg);
        return this;
    }
 
    public ResultBody data(T data) {
        this.data = data;
        return this;
    }
 
    public ResultBody path(String path) {
        this.path = path;
        return this;
    }
 
    public ResultBody httpStatus(int httpStatus) {
        this.httpStatus = httpStatus;
        return this;
    }
 
    public ResultBody put(String key, Object value) {
        if (this.extra == null) {
            this.extra = Maps.newHashMap();
        }
        this.extra.put(key, value);
        return this;
    }
 
    @Override
    public String toString() {
        return "ResultBody{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", path='" + path + '\'' +
                ", data=" + data +
                ", httpStatus=" + httpStatus +
                ", extra=" + extra +
                ", timestamp=" + timestamp +
                '}';
    }
 
    /**
     * 错误信息配置
     */
    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    private static ResourceBundle resourceBundle = ResourceBundle.getBundle("error");
 
    /**
     * 提示信息国际化
     *
     * @param msg
     * @param defaultMessage
     * @return
     */
    @JSONField(serialize = false, deserialize = false)
    @JsonIgnore
    private static String i18n(String msg, String defaultMessage) {
        return resourceBundle.containsKey(msg)?resourceBundle.getString(msg):defaultMessage;
    }
 
 
}