package com.tofly.common.core.entity;


import com.tofly.common.core.constant.ResponeStatus;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * @author Administrator
 * @date 2019/8/30  15:27
 */
@ToString
public class ResultRespone<T> implements Serializable {


    private static final long serialVersionUID = -7275852267551207696L;
    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String message;

    @Getter
    @Setter
    private T result;

    public static <T> ResultRespone<T> success() {
        return restResult(null, ResponeStatus.SUCCESS.getCode(), null);
    }

    public static <T> ResultRespone<T> success(T result) {
        return restResult(result, ResponeStatus.SUCCESS.getCode(), null);
    }

    public static <T> ResultRespone<T> success(T result, String message) {
        return restResult(result, ResponeStatus.SUCCESS.getCode(), message);
    }

    public static <T> ResultRespone<T> failed() {
        return restResult(null, ResponeStatus.FAIL.getCode(), null);
    }

    public static <T> ResultRespone<T> failed(String message) {
        return restResult(null, ResponeStatus.FAIL.getCode(), message);
    }

    public static <T> ResultRespone<T> failed(T result) {
        return restResult(result, ResponeStatus.FAIL.getCode(), null);
    }

    public static <T> ResultRespone<T> failed(T result, String message) {
        return restResult(result, ResponeStatus.FAIL.getCode(), message);
    }

    private static <T> ResultRespone<T> restResult(T result, int code, String message) {
        ResultRespone<T> apiResult = new ResultRespone<>();
        apiResult.setCode(code);
        apiResult.setResult(result);
        apiResult.setMessage(message);
        return apiResult;
    }
}
