package com.jianshui.common.utils.jianshui;

import com.alibaba.fastjson.JSONObject;
import com.jianshui.common.domain.vo.AisinoRequestBodyVO;
import com.jianshui.common.domain.vo.AisinoRequestOrderVO;
import com.jianshui.common.exception.jianshui.JianshuiServiceException;
import com.jianshui.common.utils.http.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @Description 航信请求工具类
 * @Author 巩权林
 * @Date 2021-12-17 11:02
 **/
public class AisinoRequestUtils {

    private static final Logger log = LoggerFactory.getLogger(AisinoRequestUtils.class);

    public static AisinoRequestBuilder Builder() {
        return new AisinoRequestBuilder();
    }

    public static class AisinoRequestBuilder {

        // 请求体的order
        private AisinoRequestOrderVO order;

        // 请求体的identity
        private String identity;

        // 设置加密secret
        private String secret;

        // 要发送的数据
        private Object data;

        // 配置identity
        public AisinoRequestBuilder setIdentity(String myIdentity) {
            this.identity = myIdentity;
            return this;
        }

        // 配置secret
        public AisinoRequestBuilder setSecret(String mySecret) {
            this.secret = mySecret;
            return this;
        }

        // 配置data
        public AisinoRequestBuilder setData(Object data) {
            this.data = data;
            return this;
        }

        // 发送请求
        public JSONObject send(String apiUrl) throws JianshuiServiceException {

            // 判断identity
            if (this.identity == null || "".equals(this.identity)) {
                throw new JianshuiServiceException("系统identity未配置，请联系管理员！");
            }

            // 判断secret
            if (this.secret == null || "".equals(this.secret)) {
                throw new JianshuiServiceException("系统secret未配置，请联系管理员！");
            }

            AisinoRequestOrderVO requestOrderVO = new AisinoRequestOrderVO();

            // 构造请求order
            requestOrderVO.setOrder(this.data);
            requestOrderVO.setIdentity(this.identity);

            // 构造Body
            AisinoRequestBodyVO aisinoRequestBodyVO = buildBody(requestOrderVO, this.secret);

            String host = CommonUtils.getAisinoHost();
            String url = host + apiUrl;

            // 返回数据
            return sendRequestToAisino(url, aisinoRequestBodyVO);
        }
    }

    /**
     * 构造向简税平台发送API的请求体
     *
     * @return
     */
    public static AisinoRequestBodyVO buildBody(AisinoRequestOrderVO order, String secret) {

        String identity = order.getIdentity();
        if (identity == null || "".equals(identity)) {
            return null;
        }

        String myData = JSONObject.toJSONString(order.getOrder());
        log.info("【航信API请求】【构造body】即将发送请求，加密内容：{}", myData);
        String encrypted = EncryptUtils.encrypt(myData, secret);

        // 开始配置请求体
        AisinoRequestBodyVO aisinoRequestBodyVO = new AisinoRequestBodyVO();
        aisinoRequestBodyVO.setIdentity(identity);
        aisinoRequestBodyVO.setOrder(encrypted);

        return aisinoRequestBodyVO;
    }

    /**
     * 向简税平台发送请求
     *
     * @param apiUrl
     * @param bodyVO
     * @return
     */
    public static JSONObject sendRequestToAisino(String apiUrl, AisinoRequestBodyVO bodyVO) {

        // 判断一下是否是https
        String result = "";
        if (apiUrl.contains("https://")) {
            result = HttpUtils.sendSSLFormPost(apiUrl, bodyVO);
        } else {
            result = HttpUtils.sendFormPost(apiUrl, bodyVO);
        }

        return JSONObject.parseObject(result);

    }
}
