luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
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
package com.zy.asrs.framework.common;
 
 
import java.io.Serializable;
 
/**
 * common result
 * Created by vincent on 2020-04-09
 */
public class Protocol<T> implements Serializable {
 
    private static final long serialVersionUID = 4893280118017319089L;
 
    private int code;
 
    private String msg;
 
    private T data;
 
    public Protocol() {
    }
 
    public Protocol(int code, String msg, T data) {
        super();
        setCode(code);
        setMsg(msg);
        setData(data);
    }
 
    public Protocol(int code, String message) {
        this(code, message, null);
    }
 
    public static <T> Protocol<T> ok(){
        return parse(BaseRes.OK);
    }
 
    public static <T> Protocol<T> ok(T result){
        Protocol<T> protocol = parse(BaseRes.OK);
        protocol.setData(result);
        return protocol;
    }
 
    public static <T> Protocol<T> error(){
        return parse(BaseRes.ERROR);
    }
 
    public static <T> Protocol<T> error(String message) {
        Protocol<T> protocol = parse(BaseRes.ERROR);
        protocol.setMsg(message);
        return protocol;
    }
 
    public static <T> Protocol<T> parse(String str) {
        if(Cools.isEmpty(str)){
            return parse(BaseRes.ERROR);
        }
        String[] msg = str.split("-");
        if(msg.length==2){
            return new Protocol<>(Integer.parseInt(msg[0]),msg[1]);
        }else{
            return parse("500-".concat(str));
        }
    }
 
    public int getCode() {
        return code;
    }
 
    public void setCode(int code) {
        this.code = code;
    }
 
    public String getMsg() {
        return msg;
    }
 
    public void setMsg(String msg) {
        this.msg = msg;
    }
 
    public T getData() {
        return data;
    }
 
    public void setData(T data) {
        this.data = data;
    }
}