Result.java 4.28 KB
package com.huaheng.control.management.vo;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.huaheng.control.management.utils.constant.CommonConstant;

import io.swagger.v3.oas.annotations.media.Schema;

/**
 * 统一接口返回对象(OpenAPI 3 标准版)
 */
@Schema(title = "Result", description = "通用接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Schema(description = "请求是否成功", example = "true")
    private boolean success = true;

    @Schema(description = "返回状态码", example = "200")
    private Integer code = CommonConstant.SC_OK_200;

    @Schema(description = "返回消息", example = "操作成功")
    private String message = "";

    @Schema(example = "")
    private T result;

    @Schema(description = "时间戳(毫秒)", example = "1620000000000")
    private long timestamp = System.currentTimeMillis();

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    @JsonIgnore
    public void setOnlTable(String onlTable) {
        this.onlTable = onlTable;
    }

    protected boolean canEqual(Object other) {
        return other instanceof Result;
    }

    public String toString() {
        return "Result(success=" + isSuccess() + ", message=" + getMessage() + ", code=" + getCode() + ", result=" + getResult() + ", timestamp=" + getTimestamp()
            + ", onlTable=" + getOnlTable() + ")";
    }

    public boolean isSuccess() {
        return this.success;
    }

    public String getMessage() {
        return this.message;
    }

    public Integer getCode() {
        return this.code;
    }

    public T getResult() {
        return this.result;
    }

    @JsonIgnore
    private String onlTable;

    public long getTimestamp() {
        return this.timestamp;
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }

    public static <T> Result<T> OK() {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        return r;
    }

    public static <T> Result<T> OK(String msg) {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> OK(String msg, T data) {
        Result<T> r = new Result<>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> error(String msg, T data) {
        Result<T> r = new Result<>();
        r.setSuccess(false);
        r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> error(String msg) {
        return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500.intValue(), msg);
    }

    public static <T> Result<T> error(int code, String msg) {
        Result<T> r = new Result<>();
        r.setCode(Integer.valueOf(code));
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }

    public static <T> Result<T> noauth(String msg) {
        return error(CommonConstant.SC_NO_AUTHZ.intValue(), msg);
    }

    public String getOnlTable() {
        return this.onlTable;
    }

    public Result() {}
}