package net.northking.cctp.upperComputer.entity;

import java.io.Serializable;
import java.util.Date;

public class ResultWrapper<T> implements Serializable {

    private boolean success = false;

    private String code;

    private String message;

    private Long timestamp = new Date().getTime();

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    private T data;

    public T getData()
    {
        return data;
    }

    public void setData(T data)
    {
        this.data = data;
    }

    public boolean isSuccess()
    {
        return success;
    }

    public void setSuccess(boolean success)
    {
        this.success = success;
    }

    public long getTimestamp()
    {
        return timestamp;
    }

    public void setTimestamp(long timestamp)
    {
        this.timestamp = timestamp;
    }

    /**
     * 成功且无返回数据
     *
     * @return 接口返回对象
     */
    public ResultWrapper<T> success()
    {
        this.setSuccess(true);
        this.setCode("000000");
        this.setMessage("接口调用成功");
        return this;
    }

    /**
     * 成功
     *
     * @param data 返回的数据
     * @return 接口返回对象
     */
    public ResultWrapper<T> success(T data)
    {
        this.setData(data);
        return this.success();
    }

    /**
     * 失败
     *
     * @param code  返回码
     * @param error 错误信息
     * @return 接口返回对象
     */
    public ResultWrapper<T> fail(String code, String error)
    {
        this.setSuccess(false);
        this.code = code;
        this.message = error;

        if (this.code == null || "".equals(this.code))
        {
            this.code = "100000";
        }
        if (this.message == null || "".equals(this.message))
        {
            this.message = "接口调用失败";
        }
        return this;
    }

    /**
     * 失敗
     *
     * @param code  返回码
     * @param error 错误信息
     * @param data  返回的数据
     * @return 接口返回对象
     */
    public ResultWrapper<T> fail(String code, String error, T data)
    {
        this.setData(data);
        return fail(code, error);
    }


    /**
     * 失败，请求的数据不存在
     *
     * @return 接口返回对象
     */
    public ResultWrapper<T> failNothingness()
    {
        return fail("900001", "请求的数据不存在");
    }
}
