zjh
2025-02-21 2c785c3d4513daea9deb5c7edbb17a9f17111d25
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.ltkj.tduck.utils;
 
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ltkj.tduck.constant.ResponseCodeConstants;
import lombok.*;
import lombok.experimental.Accessors;
 
import java.io.Serializable;
 
/**
 * 响应信息主体
 *
 * @param <T>
 * @author smalljop
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
//@Schema(description = "响应信息主体")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
 
    @Getter
    @Setter
//    @Schema(description = "返回标记:成功标记=200,失败标记=500")
    private int code = ResponseCodeConstants.SUCCESS;
 
    @Getter
    @Setter
//    @Schema(description = "返回信息")
    private String msg;
 
 
    @Getter
    @Setter
//    @Schema(description = "数据")
    private T data;
 
    public static <T> Result<T> success() {
        return restResult(null, ResponseCodeConstants.SUCCESS, null);
    }
 
    public static <T> Result<T> success(T data) {
        return restResult(data, ResponseCodeConstants.SUCCESS, null);
    }
 
    public static <T> Result<T> success(T data, String msg) {
        return restResult(data, ResponseCodeConstants.SUCCESS, msg);
    }
 
    public static <T> Result<T> isSuccess(boolean flag) {
        return flag ? success() : failed();
    }
 
    public static <T> Result<T> failed() {
        return restResult(null, ResponseCodeConstants.FAIL, null);
    }
 
 
    public static <T> Result<T> failed(int code, String msg) {
        return restResult(null, code, msg);
    }
 
    public static <T> Result<T> failed(String msg) {
        return restResult(null, ResponseCodeConstants.FAIL, msg);
    }
 
    public static <T> Result<T> failed(String msg, T data) {
        return restResult(data, ResponseCodeConstants.FAIL, msg);
    }
 
 
    public static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
 
 
    @JsonIgnore
    public Boolean isDataNull() {
        return ObjectUtil.isNull(data);
    }
 
}