package com.ruoyi.ibs.app.util;


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * 统一返回类型
 *
 * @author yjb
 */
@Data
@ToString
public class JsonResult<T> implements Serializable {
    private static final long serialVersionUID = -2792556188993845048L;

    @ApiModelProperty(value = "状态码,00000000:成功,99999999:异常", name = "code")
    private String code;

    @ApiModelProperty(value = "异常信息", name = "message")
    private String message;

    private T data;

    public JsonResult() {
    }

    public JsonResult(final String code, final String message, final T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> JsonResult<T> success() {
        return success( "执行完成", null );
    }

    public static <T> JsonResult<T> success(final T data) {
        return success( null, data );
    }

    public static <T> JsonResult<T> success(final String msg, final T data) {
        return get( Constants.OK, msg, data );
    }

    public static <T> JsonResult<T> error(final String code, final String msg) {
        return get( code, msg, null );
    }

    public static <T> JsonResult<T> error(final String msg) {
        return get( Constants.FAILURE, msg, null );
    }

    public static <T> JsonResult<T> error(final String msg, final T data) {
        return get( Constants.FAILURE, msg, data );
    }
    private static <T> JsonResult<T> get(final String code, final String msg, final T data) {
        return new JsonResult<T>( code, msg, data );
    }

    @Override
    public String toString() {
        return "JsonResult{" + "code=" + code + ", message='" + message + '\'' + ", data=" + data + '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        JsonResult<?> that = (JsonResult<?>) o;

        if (code != null ? !code.equals( that.code ) : that.code != null) {
            return false;
        }
        if (message != null ? !message.equals( that.message ) : that.message != null) {
            return false;
        }
        return data != null ? data.equals( that.data ) : that.data == null;
    }

    @Override
    public int hashCode() {
        int result = code != null ? code.hashCode() : 0;
        result = 31 * result + ( message != null ? message.hashCode() : 0 );
        result = 31 * result + ( data != null ? data.hashCode() : 0 );
        return result;
    }
}

