package com.zm.order.tool.server.inner;

import com.alibaba.fastjson.JSON;
import com.zm.order.tool.server.exception.InnerApiException;
import com.zm.util.dto.Result;
import org.apache.http.HttpStatus;

import javax.validation.constraints.NotNull;

/**
 * 内部Http请求返回值
 *
 * @author 宋欢
 */
public class InnerApiResult {
    /**
     * Http状态码
     */
    private int statusCode;

    /**
     * Http状态描述
     */
    private String message;

    /**
     * 返回结果
     */
    private Result<Object> response;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public boolean isStatusOk() {
        return getStatusCode() == HttpStatus.SC_OK;
    }

    public void setThrowable(@NotNull Throwable tr) {
        if (tr instanceof InnerApiException) {
            setStatusCode(((InnerApiException) tr).getStatusCode());
        }

        setMessage(tr.getMessage());
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getMessage() {
        return message;
    }

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

    public Result<Object> getResponse() {
        return response;
    }

    public void setResponse(Result<Object> response) {
        this.response = response;
    }
}
