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.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;

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


    @Override
    public RespR<VehicleRegisterResponse> vehicleRegister(VehicleRegisterRequest registerRequest) {
        String fileName = registerRequest.getFilename();
        long startTime = System.currentTimeMillis();
        try {
            log.info("NoCarInterfaceImpl.vehicleRegister,请求参数：{}",  registerRequest.toString());
            VehicleRegisterResponse response=ETCCommHelper.upload(fileName, registerRequest, VehicleRegisterResponse.class);
            log.info("NoCarInterfaceImpl.vehicleRegister上游接口返回：{},请求参数：{},cost={}ms", response.toString(), registerRequest.toString(),System.currentTimeMillis()-startTime);

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

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

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

            return new RespR(false,apie.getMessage());
        }
    }

    @Override
    public RespR<WaybillStartResponse> waybillStart(WaybillStartRequest waybillStartRequest) {
        String fileName = waybillStartRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("NoCarInterfaceImpl.waybillStart上游请求参数：{}", waybillStartRequest.toString());
            WaybillStartResponse response=ETCCommHelper.upload(fileName, waybillStartRequest, WaybillStartResponse.class);
            log.info("NoCarInterfaceImpl.waybillStart上游接口返回：{},请求参数：{},cost={}ms", response.toString(), waybillStartRequest.toString(),System.currentTimeMillis()-startTime);

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

    @Override
    public RespR<WaybillEndResponse> waybillEnd(WaybillEndRequest waybillEndRequest) {
        long startTime = System.currentTimeMillis();

        String fileName = waybillEndRequest.getFilename();
        try {
            log.info("NoCarInterfaceImpl.waybillEnd请求参数：{}", waybillEndRequest.toString());
            WaybillEndResponse response=ETCCommHelper.upload(fileName, waybillEndRequest, WaybillEndResponse.class);
            log.info("NoCarInterfaceImpl.waybillEnd上游接口返回：{},请求参数：{},cost={}ms", response.toString(), waybillEndRequest.toString(),System.currentTimeMillis()-startTime);

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

    @Override
    public RespR<WaybillHistoryStartResponse> waybillHistoryStart(WaybillHistoryStartRequest waybillHistoryStartRequest) {
        String fileName = waybillHistoryStartRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("NoCarInterfaceImpl.waybillHistoryStart上游接口请求参数：{}", waybillHistoryStartRequest.toString());

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

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

    @Override
    public RespR<WaybillHistoryEndResponse> waybillHistoryEnd(WaybillHistoryEndRequest waybillHistoryEndRequest) {
        String fileName = waybillHistoryEndRequest.getFilename();
        long startTime = System.currentTimeMillis();

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

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

    @Override
    public RespR<WaybillNumFindInvoiceResponse> waybillNumFindInvoice(WaybillNumFindInvoiceRequest waybillNumFindInvoiceRequest) {
        String fileName = waybillNumFindInvoiceRequest.getFilename();
        long startTime = System.currentTimeMillis();

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

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

    @Override
    public RespR<WaybillInvoiceRedQueryResponse> waybillInvoiceRedQuery(WaybillInvoiceRedQueryRequest waybillInvoiceRedQueryRequest) {
        String fileName = waybillInvoiceRedQueryRequest.getFilename();
        long startTime = System.currentTimeMillis();

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

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

    @Override
    public RespR<WaybillCountQueryResponse> waybillCountQuery(WaybillCountQueryRequest waybillCountQueryRequest) {
        String fileName = waybillCountQueryRequest.getFilename();
        long startTime = System.currentTimeMillis();

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

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