package com.mosty.common.base.domain;

import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 *  基础请求返回类
 * @author kevin
 * @date 2022/1/22 8:29 PM
 * @since 1.0.0
 */
@Data
@SuppressWarnings("unused")
public class ResponseResult<T> implements Serializable {

    /** 调用成功编码 */
    private static final int SUCCESS_CODE = 10000;

    /** 调用失败编码 */
    private static final int FAIL_CODE = -1;

    /** 编号 */
    private int code = SUCCESS_CODE;

    /** 消息 */
    private String message;

    /** 数据 */
    private T data;

    public ResponseResult() {
    }

    public ResponseResult(T data) {
        this.message = "OK";
        this.data = data;
    }

    public ResponseResult(int code, String msg) {
        this.code = code;
        this.message = msg;
        this.data = null;
    }

    public ResponseResult(int code, String msg, T data) {
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public static <T> ResponseResult<T> success() {
        return new ResponseResult(SUCCESS_CODE, "OK", null);
    }

    public static <T> ResponseResult<T> success(int code, String message, T data) {
        return new ResponseResult(code, message, data);
    }

    public static <T> ResponseResult<T> success(String message, T data) {
        return new ResponseResult(SUCCESS_CODE, message, data);
    }

    public static <T> ResponseResult<T> success(T data) {
        return new ResponseResult(SUCCESS_CODE, "OK", data);
    }

    public static <T> ResponseResult<T> fail(String message) {
        return new ResponseResult(FAIL_CODE, message, (Object)null);
    }

    public static <T> ResponseResult<T> fail(int code, String message) {
        return new ResponseResult(code, message, (Object)null);
    }

    public static <T> ResponseResult<T> fail(int code, String message, T t) {
        return new ResponseResult(code, message, t);
    }

    public boolean isSuccess() {
        return SUCCESS_CODE == code;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass())  {
            return false;
        }
        ResponseResult<?> that = (ResponseResult<?>) o;
        return Objects.equals(code, that.code) && Objects.equals(message, that.message) && Objects.equals(data, that.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, message, data);
    }
}