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

import com.alibaba.fastjson.JSON;
import com.zm.order.tool.server.utils.HttpUtils;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Map;

/**
 * @author nianchang
 * @ClassName ThirdApiRequest
 * @date 2021/11/08 5:20 下午
 */
public class ThirdApiRequest implements Serializable {

    /**
     * 缺省的版本号
     */
    public static final String DEFAULT_VERSION = "1.0";

    /**
     * 缺省的内容类型
     */
    public static final HttpUtils.ContentTypeEnum DEFAULT_CONTENT_TYPE = HttpUtils.ContentTypeEnum.URL_ENCODED;

    private static final long serialVersionUID = -6772845827903345174L;

    /**
     * 内容类型
     */
    public HttpUtils.ContentTypeEnum contentType;

    /**
     * 请求方式，全大写
     */
    private HttpUtils.MethodEnum method;

    /**
     * 待访问App的项目名，如：thirdapi
     */
    private String appName;

    /**
     * 待访问App的域名，如：http://third-apiserver.zvcms.com
     */
    private String baseUri;

    /**
     * 请求路径，如：wechat/mini-app/auth/info
     * 不要加qapi/
     */
    private String path;

    /**
     * 版本号
     */
    private String version;

    /**
     * 完整的请求Url
     */
    private String url;

    /**
     * 请求头信息
     */
    private Map<String, String> headers;

    /**
     * 请求参数
     */
    private Map<String, Object> params;

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

    /**
     * GET请求方式？
     */
    public boolean isMethodGet() {
        return HttpUtils.MethodEnum.GET.getCode() == getMethod().getCode();
    }

    /**
     * POST请求方式？
     */
    public boolean isMethodPost() {
        return HttpUtils.MethodEnum.POST.getCode() == getMethod().getCode();
    }

    /**
     * 获取版本号，如果为空，返回默认版本号
     */
    public String getVersionOrDefault() {
        return StringUtils.isEmpty(version) ? DEFAULT_VERSION : version;
    }

    /**
     * 获取内容类型，如果为空，返回默认类型
     */
    public HttpUtils.ContentTypeEnum getContentTypeOrDefault() {
        return StringUtils.isEmpty(contentType) ? DEFAULT_CONTENT_TYPE : contentType;
    }

    public HttpUtils.ContentTypeEnum getContentType() {
        return contentType;
    }

    public void setContentType(HttpUtils.ContentTypeEnum contentType) {
        this.contentType = contentType;
    }

    public HttpUtils.MethodEnum getMethod() {
        return method;
    }

    public void setMethod(HttpUtils.MethodEnum method) {
        this.method = method;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getBaseUri() {
        return baseUri;
    }

    public void setBaseUri(String baseUri) {
        this.baseUri = baseUri;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }
}
