package com.fkzy.warn.common.response;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 请求返回对象
 *
 * @author yuyantian
 * @date 2021/09/07 14:30
 */

public class ApiResponseT<T> implements Serializable {

    private static final long serialVersionUID = 6459579728006690174L;

    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private int code;

    /**
     * 错误原因
     */
    @ApiModelProperty("错误原因")
    private String message;

    /**
     * 返回结果
     */
    @ApiModelProperty("返回结果")
    private T data;


    public ApiResponseT() {
        this.code = 200;
        this.message = "操作成功";
    }

    public ApiResponseT(T data) {
        new ApiResponseT();
        this.code = 200;
        this.data = data;
    }

    public void recordError(int code) {
        this.code = code;
        this.message = StatusCode.getStatusCode(code).getMessage();
    }

    public void recordError(String error) {
        this.code = 500;
        this.message = error;
    }

    public int getCode() {
        return code;
    }

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

    public String getMessage() {
        return message;
    }

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

    public T getData() {
        return data;
    }

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

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