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 com.jkcredit.invoice.service.upService.CommInterFace;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Slf4j
@Service("commInterFace")
public class CommInterFaceImpl implements CommInterFace{
    @Override
    public RespR<CompanyQueryResponse> companyQuery(CompanyQueryRequest companyQueryRequest) {
        String fileName = companyQueryRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            CompanyQueryResponse response= ETCCommHelper.upload(fileName, companyQueryRequest, CompanyQueryResponse.class);
            log.info("CommInterFaceImpl.companyQuery上游接口返回：{},请求参数：{}", response.toString(), companyQueryRequest.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<CompanyAddResponse> addCompany(CompanyAddRequest companyAddRequest) {
        String fileName = companyAddRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            CompanyAddResponse response=ETCCommHelper.upload(fileName, companyAddRequest, CompanyAddResponse.class);
            log.info("CommInterFaceImpl.addCompany上游接口返回：{},请求参数：{}", response.toString(), companyAddRequest.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<B2bContractAddResponse> b2bContractAdd(B2bContractAddRequest contractAddRequest) {
        String fileName = contractAddRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            B2bContractAddResponse response=ETCCommHelper.upload(fileName, contractAddRequest, B2bContractAddResponse.class);
            log.info("CommInterFaceImpl.b2bContractAdd上游接口返回：{},请求参数：{}", response.toString(), contractAddRequest.toString());
            log.info("conName"+response.getContrantNum());
            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<B2bContractQueryResponse> b2bContractQuery(B2bContractQueryRequest b2bContractQueryRequest) {
        String fileName = b2bContractQueryRequest.getFilename();
        try {
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            B2bContractQueryResponse response=ETCCommHelper.upload(fileName, b2bContractQueryRequest, B2bContractQueryResponse.class);
            log.info("CommInterFaceImpl.b2bContractQuery上游接口返回：{},请求参数：{}", response.toString(), b2bContractQueryRequest.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());
        }
    }

}
