package com.ms.biz.common;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@Accessors(chain = true)
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private String result;

    private T data;

    private String msg;

    public ApiResult() {
        this.result = "success";
        this.msg = "";
        this.data = null;
    }

    public ApiResult(T data) {
        this.result = "success";
        this.data = data;
    }

    public ApiResult(String result, String msg) {
        this.result = result;
        this.msg = msg;
        this.data = null;
    }

    public ApiResult(String result, String msg, T data) {
        this.result = result;
        this.msg = msg;
        this.data = data;
    }

    public static <T> ApiResult<T> ok() {
        return new ApiResult<T>(null);
    }

    public static <T> ApiResult<T> ok(T data) {
        return new ApiResult<T>(data);
    }

    public static <T> ApiResult<T> fail(String msg) {
        return new ApiResult<T>("fail", msg);
    }

    public static <T> ApiResult<T> fail(String msg, T data) {
        return new ApiResult<T>("fail", msg, data);

    }

    public boolean isSuccess() {
        return "success".equals(this.result);
    }
}