package com.ruixuan.common.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回数据格式
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回处理消息
     */
    private String message = "success";

    /**
     * 返回代码
     */
    private Integer code = 0;

    /**
     * 返回数据对象 data
     */
    private T result;


    public Result() {

    }

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

    public static <T> Result<T> success() {
        Result<T> r = new Result<T>();
        r.setCode(0);
        r.setMessage("success");
        return r;
    }

    public static <T> Result<T> success(T data) {
        Result<T> r = new Result<T>();
        r.setCode(0);
        r.setMessage("success");
        r.setResult(data);
        return r;
    }

    public static <T> Result<T> success(String msg, T data) {
        Result<T> r = new Result<T>();
        r.setCode(0);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    /**
     * ================================================================================
     */

    public static <T> Result<T> error(String msg) {
        Result<T> r = new Result<T>();
        r.setCode(-1);
        r.setMessage(msg);
        return r;
    }

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

    public static Result<Object> error500(String msg) {
        return error(500, msg);
    }

    public static Result<Object> error(int code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

    /**
     * 无权限访问返回结果
     */
    public static Result<Object> noauth(String msg) {
        return error(510, msg);
    }

}