package com.om.utils;

import lombok.Data;

/**
 * 全局统一返回结果类
 */
@Data
public class Result<T> {
    private Integer code;//状态码
    private String msg;//返回消息
    private T data;//返回数据
    /**
     * 私有化构造方法，禁止在其它类创建对象
     */
    private Result(){}
    /**
     * 成功执行，不返回数据
     * @return
     */
    public static<T> Result<T> ok(){
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS);
        result.setMsg("执行成功");
        return result;
    }
    /**
     * 成功执行，并返回数据
     * @param data
     * @param <T>
     * @return
     */
    public static<T> Result<T> ok(T data){
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.SUCCESS);
        result.setMsg("执行成功");
        result.setData(data);
        return result;
    }
    /**
     * 失败
     * @return
     */
    public static<T> Result<T> error(){
        Result<T> result = new Result<T>();
        result.setCode(ResultCode.ERROR);
        result.setMsg("执行失败");
        return result;
    }

    /**
     * 设置状态码
     * @param code
     * @return
     */
    public Result<T> result(Integer code){
        this.setCode(code);
        return this;
    }
    /**
     * 设置返回消息
     * @param message
     * @return
     */
    public Result<T> message(String message){
        this.setMsg(message);
        return this;
    }

    // Added By Wxy
    public static Result<Object> fail(String msg) {
        return fail(400, msg, null);
    }
    public static Result<Object> fail(String msg, Object data) {
        return fail(400, msg, data);
    }

    public static Result<Object> fail(int code, String msg, Object data) {
        Result<Object> rs = new Result<>();
        rs.setCode(code);
        rs.setMsg(msg);
        rs.setData(data);

        return rs;
    }

    public static Result<Object> succ(Object data) {
        return succ(200, "操作成功", data);
    }

    public static Result<Object> succ(int code, String msg, Object data) {
        Result<Object> rs = new Result<>();
        rs.setCode(code);
        rs.setMsg(msg);
        rs.setData(data);

        return rs;
    }
}
