[测评系统]--测评系统核心代码库
zhijie
2023-10-22 3ac0b7f54a4a3d747ff43eb9e7ca619e5ef0f8d0
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
package com.ots.framework.web.domain;
import com.ots.common.utils.MessageUtils;
import com.ots.common.utils.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.util.HashMap;
 
public class AjaxResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;
    public static final String CODE_TAG = "code";
    public static final String MSG_TAG = "msg";
    public static final String DATA_TAG = "data";
    
    public enum Type {
        
        SUCCESS(0),
        
        WARN(301),
        
        ERROR(500);
        private final int value;
        Type(int value) {
            this.value = value;
        }
        public int value() {
            return this.value;
        }
    }
    
    private Type type;
    
    private int code;
    
    private String msg;
    
    private Object data;
    
    public AjaxResult() {
    }
    
    public AjaxResult(Type type, String msg) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
    }
    
    public AjaxResult(Type type, String msg, Object data) {
        super.put(CODE_TAG, type.value);
        super.put(MSG_TAG, msg);
        if (StringUtils.isNotNull(data)) {
            super.put(DATA_TAG, data);
        }
    }
    
    public static AjaxResult success() {
        return AjaxResult.success(MessageUtils.message("basis.successfulOperation"));
    }
    
    public static AjaxResult success(Object data) {
        return AjaxResult.success(MessageUtils.message("basis.successfulOperation"), data);
    }
    
    public static AjaxResult success(String msg) {
        return AjaxResult.success(msg, null);
    }
    
    public static AjaxResult success(String msg, Object data) {
        return new AjaxResult(Type.SUCCESS, msg, data);
    }
    
    public static AjaxResult warn(String msg) {
        return AjaxResult.warn(msg, null);
    }
    
    public static AjaxResult warn(String msg, Object data) {
        return new AjaxResult(Type.WARN, msg, data);
    }
    
    public static AjaxResult error() {
        return AjaxResult.error("操作失败");
    }
    
    public static AjaxResult error(String msg) {
        return AjaxResult.error(msg, null);
    }
    
    public static AjaxResult error(String msg, Object data) {
        return new AjaxResult(Type.ERROR, msg, data);
    }
    public Type getType() {
        return type;
    }
    public void setType(Type type) {
        this.type = type;
    }
    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 Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("code", getCode())
                .append("msg", getMsg())
                .append("data", getData())
                .toString();
    }
}