package com.jkcredit.invoice.service.upservice.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.tts.card.server.*;
import cn.com.taiji.sdk.model.comm.protocol.tts.invoice.server.*;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.CardTradeResponse;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.TradeApplyQueryRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.trade.service.TradeApplyQueryResponse;
import com.jkcredit.invoice.service.upservice.SelfCarInterface;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 自有车上游请求接口实现类
 * @author mumuxigua
 */
@Service("selfCarInterface")
@Slf4j
public class SelfCarInterfaceImpl implements SelfCarInterface {


    @Override
    public RespR<CardBindQueryListToBResponse> cardBindQueryListToB(CardBindQueryListToBRequest request) {
        String fileName = request.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.cardBindQueryListToB请求参数：{}", request.toString());
            CardBindQueryListToBResponse response = ETCCommHelper.upload(fileName, request, CardBindQueryListToBResponse.class);
            log.info("SelfCarInterfaceImpl.cardBindQueryListToB上游接口返回：{},请求参数：{},cost={}ms", response.toString(), request.toString(), System.currentTimeMillis() - startTime);

            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + request.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + request.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardQueryCardToBResponse> cardQueryCardToB(CardQueryCardToBRequest bRequest) {
        String fileName = bRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("SelfCarInterfaceImpl.cardQueryCardToB请求参数：{}", bRequest.toString());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            CardQueryCardToBResponse response = ETCCommHelper.upload(fileName, bRequest, CardQueryCardToBResponse.class);
            log.info("SelfCarInterfaceImpl.cardQueryCardToB上游接口返回：{},请求参数：{},cost={}ms", response.toString(), bRequest.toString(), System.currentTimeMillis() - startTime);

            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + bRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + bRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardBindingToBResponse> cardBindingToB(CardBindingToBRequest bRequest) {
        String fileName = bRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.cardBindingToB上游请求参数：{}", bRequest.toString());
            CardBindingToBResponse response = ETCCommHelper.upload(fileName, bRequest, CardBindingToBResponse.class);
            log.info("SelfCarInterfaceImpl.cardBindingToB上游接口返回：{},请求参数：{},cost={}ms", response.toString(), bRequest.toString(), System.currentTimeMillis() - startTime);


            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + bRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + bRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardValidCodeToBResponse> cardValidCodeToB(CardValidCodeToBRequest request) {
        String fileName = request.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.cardValidCodeToB上游请求参数：{}", request.toString());
            CardValidCodeToBResponse response = ETCCommHelper.upload(fileName, request, CardValidCodeToBResponse.class);
            log.info("SelfCarInterfaceImpl.cardValidCodeToB上游接口返回：{},请求参数：{},cost={}ms", response.toString(), request.toString(), System.currentTimeMillis() - startTime);


            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + request.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + request.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardUnbindToBResponse> cardUnbindToB(CardUnbindToBRequest request) {
        String fileName = request.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.cardUnbindToB上游请求参数：{}", request.toString());
            CardUnbindToBResponse response = ETCCommHelper.upload(fileName, request, CardUnbindToBResponse.class);
            log.info("SelfCarInterfaceImpl.cardUnbindToB上游接口返回：{},请求参数：{},cost={}ms", response.toString(), request.toString(), System.currentTimeMillis() - startTime);


            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + request.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + request.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardTradeResponse> cardTrade(CardTradeRequest cardTradeRequest) {
        String fileName = cardTradeRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.cardTrade上游请求参数：{}", cardTradeRequest.toString());
            CardTradeResponse response = ETCCommHelper.upload(fileName, cardTradeRequest, CardTradeResponse.class);
            log.info("SelfCarInterfaceImpl.cardTrade上游接口返回：{},请求参数：{},cost={}ms", response.toString(), cardTradeRequest.toString(), System.currentTimeMillis() - startTime);


            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + cardTradeRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请稍后再试");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + cardTradeRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<B2BInvoiceApplyResponse> b2Binvoiceapply(B2BInvoiceApplyRequest bInvoiceApplyRequest) {
        String fileName = bInvoiceApplyRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.b2BInvoiceApply上游请求参数：{}", bInvoiceApplyRequest.toString());
            B2BInvoiceApplyResponse response = ETCCommHelper.upload(fileName, bInvoiceApplyRequest, B2BInvoiceApplyResponse.class);
            log.info("SelfCarInterfaceImpl.b2BInvoiceApply上游接口返回：{},请求参数：{},cost={}ms", response.toString(), bInvoiceApplyRequest.toString(), System.currentTimeMillis() - startTime);
            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + bInvoiceApplyRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + bInvoiceApplyRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<B2BInvoiceQueryResponse> b2Binvoicequery(B2BInvoiceQueryRequest bInvoiceQueryRequest) {
        String fileName = bInvoiceQueryRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("SelfCarInterfaceImpl.b2BInvoiceQuery上游请求参数：{}", bInvoiceQueryRequest.toString());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            B2BInvoiceQueryResponse response = ETCCommHelper.upload(fileName, bInvoiceQueryRequest, B2BInvoiceQueryResponse.class);
            log.info("SelfCarInterfaceImpl.b2BInvoiceQuery上游接口返回：{},请求参数：{},cost={}ms", response.toString(), bInvoiceQueryRequest.toString(), System.currentTimeMillis() - startTime);


            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + bInvoiceQueryRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + bInvoiceQueryRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<B2BInvoiceQueryByApplyResponse> b2bContractQuery(B2BInvoiceQueryByApplyRequest invoiceQueryByApplyRequest) {
        String fileName = invoiceQueryByApplyRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            log.info("SelfCarInterfaceImpl.b2bContractQuery上游请求参数：{}", invoiceQueryByApplyRequest.toString());
            B2BInvoiceQueryByApplyResponse response = ETCCommHelper.upload(fileName, invoiceQueryByApplyRequest, B2BInvoiceQueryByApplyResponse.class);
            log.info("SelfCarInterfaceImpl.b2bContractQuery上游接口返回：{},请求参数：{},cost={}ms", response.toString(), invoiceQueryByApplyRequest.toString(), System.currentTimeMillis() - startTime);

            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + invoiceQueryByApplyRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + invoiceQueryByApplyRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<B2bInvoicePackageResponse> b2bInvoicePackage(B2bInvoicePackageRequest b2bInvoicePackageRequest) {
        String fileName = b2bInvoicePackageRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("SelfCarInterfaceImpl.b2bInvoicePackage上游请求参数：{}", b2bInvoicePackageRequest.toString());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            B2bInvoicePackageResponse response = ETCCommHelper.upload(fileName, b2bInvoicePackageRequest, B2bInvoicePackageResponse.class);
            log.info("SelfCarInterfaceImpl.b2bInvoicePackage上游接口返回：{},请求参数：{},cost={}ms", response.toString(), b2bInvoicePackageRequest.toString(), System.currentTimeMillis() - startTime);

            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + b2bInvoicePackageRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + b2bInvoicePackageRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardChangeTitleResponse> changeCardTile(CardChangeTitleRequest cardChangeTitleRequest) {
        String fileName = cardChangeTitleRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("SelfCarInterfaceImpl.changeCardTile上游请求参数：{}", cardChangeTitleRequest.toString());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            CardChangeTitleResponse response = ETCCommHelper.upload(fileName, cardChangeTitleRequest, CardChangeTitleResponse.class);
            log.info("SelfCarInterfaceImpl.changeCardTile上游接口返回：{},请求参数：{},cost={}ms", response.toString(), cardChangeTitleRequest.toString(), System.currentTimeMillis() - startTime);

            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + cardChangeTitleRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + cardChangeTitleRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<CardChangeQueryResponse> queryChangeCard(CardChangeQueryRequest cardChangeQueryRequest) {
        String fileName = cardChangeQueryRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("SelfCarInterfaceImpl.queryChangeCard上游请求参数：{}", cardChangeQueryRequest.toString());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            CardChangeQueryResponse response = ETCCommHelper.upload(fileName, cardChangeQueryRequest, CardChangeQueryResponse.class);
            log.info("SelfCarInterfaceImpl.queryChangeCard上游接口返回：{},请求参数：{},cost={}ms", response.toString(), cardChangeQueryRequest.toString(), System.currentTimeMillis() - startTime);

            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + cardChangeQueryRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + cardChangeQueryRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<TradeApplyQueryResponse> queryApplyQueryByTradeId(TradeApplyQueryRequest tradeApplyQueryRequest) {


        String fileName = tradeApplyQueryRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("SelfCarInterfaceImpl.queryApplyQueryByTradeId上游请求参数：{}", tradeApplyQueryRequest.toString());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            TradeApplyQueryResponse response = ETCCommHelper.upload(fileName, tradeApplyQueryRequest, TradeApplyQueryResponse.class);
            log.info("SelfCarInterfaceImpl.queryApplyQueryByTradeId上游接口返回：{},请求参数：{},cost={}ms", response.toString(), tradeApplyQueryRequest.toString(), System.currentTimeMillis() - startTime);
            return new RespR<>(response);
        } catch (IOException e) {
            log.error(e.getMessage() + " ,请求参数：" + tradeApplyQueryRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            e.printStackTrace();
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error(apie.getMessage().replaceAll("[\r\n]", "") + " ,请求参数：" + tradeApplyQueryRequest.toString() + " ,cost=" + (System.currentTimeMillis() - startTime) + "ms");
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }

    }
}
