package com.ytpm.general;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author marx
 * @date 2022/4/8 9:43
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    /**
     * 返回success
     *
     * @return
     */
    public static <T> Result<T> resultOk(String msg) {
        return resultOk(msg, null);
    }
    /**
     * 返回成功，携带数据
     *
     * @return
     */
    public static <T> Result<T> resultObjOk(T data) {
        return resultOk("SUCCESS", data);
    }

    /**
     * 返回success
     *
     * @return
     */
    public static <T> Result<T> resultOk() {
        return resultOk("SUCCESS", null);
    }

    /**
     * 返回success 含参数
     *
     * @return
     */
    public static <T> Result<T> resultOk(String msg, T t) {
        return new Result<T>(StatusCode.OK, msg, t);
    }

    /**
     * 返回fail 自定义code
     *
     * @return
     */
    public static <T> Result<T> resultFail(int code, String msg) {
        return new Result<T>(code, msg, null);
    }

    /**
     * 返回ERROR  code = 801
     *
     * @return
     */
    public static <T> Result<T> resultErr(String msg) {
        return resultFail(StatusCode.ERROR, msg);
    }

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

}
