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.B2bContractAddRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.B2bContractAddResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.B2bContractQueryRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.B2bContractQueryResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyAddRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyAddResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyEditRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyEditResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyQueryRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyQueryResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.ConfirmContractAddRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.ConfirmContractAddResponse;
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;

/**
 * @author mumuxigua
 */
@Service("commInterFace")
@Slf4j
public class CommInterFaceImpl implements CommInterFace {


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

    @Override
    public RespR<CompanyAddResponse> addCompany(CompanyAddRequest companyAddRequest) {
        String fileName = companyAddRequest.getFilename();
        long startTime = System.currentTimeMillis();

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

    @Override
    public RespR<B2bContractAddResponse> b2bContractAdd(B2bContractAddRequest contractAddRequest) {
        String fileName = contractAddRequest.getFilename();
        long startTime = System.currentTimeMillis();

        try {
            log.info("CommInterFaceImpl.b2bContractAdd上游请求参数,Filename：{},CompanyNum：{},ContractFileName:{},ServiceStartTime:{},ServiceEndTime:{},ServiceType:{}", contractAddRequest.getFilename(), contractAddRequest.getCompanyNum(), contractAddRequest.getContractFileName(), contractAddRequest.getServiceStartTime(), contractAddRequest.getServiceEndTime(), contractAddRequest.getServiceType());
            //(4)指定协议的响应模型（IssuerUploadResponse），调用upload 发送数据
            B2bContractAddResponse response = ETCCommHelper.upload(fileName, contractAddRequest, B2bContractAddResponse.class);
            log.info("CommInterFaceImpl.b2bContractAdd上游接口返回：{}   ,请求参数-Filename：{},CompanyNum：{},ContractFileName:{},ServiceStartTime:{},ServiceEndTime:{},ServiceType:{},cost={}ms", response.toString(), contractAddRequest.getFilename(), contractAddRequest.getCompanyNum(), contractAddRequest.getContractFileName(), contractAddRequest.getServiceStartTime(), contractAddRequest.getServiceEndTime(), contractAddRequest.getServiceType(), System.currentTimeMillis() - startTime);
            log.info("conName" + response.getContrantNum());
            return new RespR<>(response);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("CommInterFaceImpl.b2bContractAdd：{} ,请求参数-Filename：{},CompanyNum：{},ContractFileName:{},ServiceStartTime:{},ServiceEndTime:{},ServiceType:{},cost={}ms", e.getMessage(), contractAddRequest.getFilename(), contractAddRequest.getCompanyNum(), contractAddRequest.getContractFileName(), contractAddRequest.getServiceStartTime(), contractAddRequest.getServiceEndTime(), contractAddRequest.getServiceType(), System.currentTimeMillis() - startTime);
            return new RespR(false, "网络异常,请联系管理人员");
        } catch (ApiRequestException apie) {
            log.error("CommInterFaceImpl.b2bContractAdd：{} ,请求参数-Filename：{},CompanyNum：{},ContractFileName:{},ServiceStartTime:{},ServiceEndTime:{},ServiceType:{},cost={}ms", apie.getMessage().replaceAll("[\r\n]", ""), contractAddRequest.getFilename(), contractAddRequest.getCompanyNum(), contractAddRequest.getContractFileName(), contractAddRequest.getServiceStartTime(), contractAddRequest.getServiceEndTime(), contractAddRequest.getServiceType(), System.currentTimeMillis() - startTime);
            apie.printStackTrace();
            return new RespR(false, apie.getMessage());
        }
    }

    @Override
    public RespR<ConfirmContractAddResponse> promiseAdd(ConfirmContractAddRequest confirmContractAddRequest) {
        String fileName = confirmContractAddRequest.getFilename();
        long startTime = System.currentTimeMillis();

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

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

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

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

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

}
