package com.ssi.response; import com.ssi.constant.enums.Status; import lombok.Setter; import java.io.Serializable; /** * 返回数据 */ public class SSIResponse implements Serializable { private Status status; @Setter private String msg; private T data; public T getData() { return data; } public SSIResponse setData(T data) { this.data = data; return this; } public int getCode() { return status.getCode(); } public String getMsg() { if (msg == null) { return status.getDescript(); } return msg; } protected SSIResponse(Status status) { this.status = status; } protected SSIResponse() { } // ----------------- 静态方法 ----------------------- public static SSIResponse ok() { return ok(null); } public static SSIResponse ok(T data) { final SSIResponse ok = new SSIResponse<>(Status.SUCCESS); ok.setData(data); return ok; } public static SSIResponse haveMsgOk(String msg) { final SSIResponse ok = new SSIResponse<>(Status.SUCCESS); ok.setMsg(msg); return ok; } public static SSIResponse no(Status status) { if (status.equals(Status.SUCCESS)) { throw new IllegalArgumentException("只能传递异常的Status"); } return new SSIResponse<>(status); } public static SSIResponse no(Status status, String message) { final SSIResponse no = SSIResponse.no(status); no.setMsg(message); return no; } public static SSIResponse no(String message) { // 使用什么status实例不影响,除了code值不一样 return SSIResponse.no(Status.ERROR, message); } }