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.eoms.company.*;
import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleQueryRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleQueryResponse;
import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleRegisterRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleRegisterResponse;
import cn.com.taiji.sdk.model.comm.protocol.tts.waybill.*;
import com.jkcredit.invoice.service.upService.NoCarInterface;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service("noCarInterface")
public class NoCarInterfaceImpl implements NoCarInterface {

    @Override
    public RespR<VehicleRegisterResponse> vehicleRegister(VehicleRegisterRequest registerRequest) {
        String fileName = registerRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            VehicleRegisterResponse response=ETCCommHelper.upload(fileName, registerRequest, VehicleRegisterResponse.class);
            log.info("NoCarInterfaceImpl.vehicleRegister上游接口返回：{},请求参数：{}", response.toString(), registerRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<VehicleQueryResponse> vehicleQuery(VehicleQueryRequest vehicleQueryRequest) {
        String fileName = vehicleQueryRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            VehicleQueryResponse response=ETCCommHelper.upload(fileName, vehicleQueryRequest, VehicleQueryResponse.class);
            log.info("NoCarInterfaceImpl.vehicleQuery上游接口返回：{},请求参数：{}", response.toString(), vehicleQueryRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillStartResponse> waybillStart(WaybillStartRequest waybillStartRequest) {
        String fileName = waybillStartRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillStartResponse response=ETCCommHelper.upload(fileName, waybillStartRequest, WaybillStartResponse.class);
            log.info("NoCarInterfaceImpl.waybillStart上游接口返回：{},请求参数：{}", response.toString(), waybillStartRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillEndResponse> waybillEnd(WaybillEndRequest waybillEndRequest) {
        String fileName = waybillEndRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillEndResponse response=ETCCommHelper.upload(fileName, waybillEndRequest, WaybillEndResponse.class);
            log.info("NoCarInterfaceImpl.waybillEnd上游接口返回：{},请求参数：{}", response.toString(), waybillEndRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillHistoryStartResponse> waybillHistoryStart(WaybillHistoryStartRequest waybillHistoryStartRequest) {
        String fileName = waybillHistoryStartRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillHistoryStartResponse response=ETCCommHelper.upload(fileName, waybillHistoryStartRequest, WaybillHistoryStartResponse.class);
            log.info("NoCarInterfaceImpl.waybillHistoryStart上游接口返回：{},请求参数：{}", response.toString(), waybillHistoryStartRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillHistoryEndResponse> waybillHistoryEnd(WaybillHistoryEndRequest waybillHistoryEndRequest) {
        String fileName = waybillHistoryEndRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillHistoryEndResponse response=ETCCommHelper.upload(fileName, waybillHistoryEndRequest, WaybillHistoryEndResponse.class);
            log.info("NoCarInterfaceImpl.waybillHistoryEnd上游接口返回：{},请求参数：{}", response.toString(), waybillHistoryEndRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillNumFindInvoiceResponse> waybillNumFindInvoice(WaybillNumFindInvoiceRequest waybillNumFindInvoiceRequest) {
        String fileName = waybillNumFindInvoiceRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillNumFindInvoiceResponse response=ETCCommHelper.upload(fileName, waybillNumFindInvoiceRequest, WaybillNumFindInvoiceResponse.class);
            log.info("NoCarInterfaceImpl.waybillNumFindInvoice上游接口返回：{},请求参数：{}", response.toString(), waybillNumFindInvoiceRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillInvoiceRedQueryResponse> waybillInvoiceRedQuery(WaybillInvoiceRedQueryRequest waybillInvoiceRedQueryRequest) {
        String fileName = waybillInvoiceRedQueryRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillInvoiceRedQueryResponse response=ETCCommHelper.upload(fileName, waybillInvoiceRedQueryRequest, WaybillInvoiceRedQueryResponse.class);
            log.info("NoCarInterfaceImpl.waybillInvoiceRedQuery上游接口返回：{},请求参数：{}", response.toString(), waybillInvoiceRedQueryRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillCountQueryResponse> waybillCountQuery(WaybillCountQueryRequest waybillCountQueryRequest) {
        String fileName = waybillCountQueryRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            WaybillCountQueryResponse response=ETCCommHelper.upload(fileName, waybillCountQueryRequest, WaybillCountQueryResponse.class);
            log.info("NoCarInterfaceImpl.waybillCountQuery上游接口返回：{},请求参数：{}", response.toString(), waybillCountQueryRequest.toString());

            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            return new RespR(false,"网络异常,请联系管理人员");
        }catch (ApiRequestException apie) {
            apie.printStackTrace();
            return new RespR(false,"错误码："+apie.getErrCode()+" 错误信息："+apie.getMessage());
        }
    }
}
