package com.base.orbit.web;

import lombok.Data;

import java.util.HashMap;

/**
 * @author shuyy
 * @date 2022-01-13
 */
@Data
public class Result<T> {


    private int code = 200;
    private String message = "";
    private T resp = null;

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T resp) {
        Result<T> res = new Result<T>();
        res.setCode(Code.SUCCESS);
        res.setMessage(Code.getInfo(Code.SUCCESS));
        res.setResp(resp);
        return res;
    }

    public static <T> Result<T> error(int code) {
        Result<T> res = new Result<T>();
        res.setCode(code);
        res.setMessage(Code.getInfo(code));
        res.setResp(null);
        return res;
    }

    public static <T> Result<T> error(String info) {
        Result<T> res = new Result<T>();
        res.setCode(Code.ERROR);
        res.setMessage(info);
        res.setResp(null);
        return res;
    }

    public static <T>  Result<T>  error(int code, String info) {
        Result<T> res = new Result<T>();
        res.setCode(code);
        res.setMessage(info);
        res.setResp(null);
        return res;
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static  <T> Result<T> error()
    {
        return Result.error("操作失败");
    }

    public Result<T> errorInfo(int code, String info) {
        if (this.code != code) {
            this.code = code;
        }
        this.message = info;
        return this;
    }
}
