package com.m2pool.lease.dto;

import com.m2pool.lease.constant.ResponseConstant;
import lombok.Data;

import java.io.Serializable;

/**
 * @Description 响应对象封装类
 * @Date 2024/6/11 9:18
 * @Author yyb
 */
@Data
public class Result<T> implements Serializable
{
    private static final long serialVersionUID = 3276842119125241681L;

    /** 成功 */
    public static final int SUCCESS = ResponseConstant.SUCCESS;

    /** 失败 */
    public static final int FAIL = ResponseConstant.FAIL;

    private int code;

    private String msg;

    private T data;

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

    public static <T> Result<T> success(T data)
    {
        return restResult(data, SUCCESS, null);
    }

    public static <T> Result<T> success(T data, String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> Result<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> Result<T> fail(T data)
    {
        return restResult(data, FAIL, null);
    }

    public static <T> Result<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg);
    }

    public static <T> Result<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> Result<T> restResult(T data, int code, String msg)
    {
        Result<T> apiResult = new Result<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
