package com.jkcredit.invoice.hub.service.apiCompany;

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.CompanyAddRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyAddResponse;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyQueryRequest;
import cn.com.taiji.sdk.model.comm.protocol.eoms.company.CompanyQueryResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.enums.ApiResponseCodeEnum;
import com.jkcredit.invoice.hub.model.dto.apiCompany.CompanyAddDto;
import com.jkcredit.invoice.hub.model.dto.apiCompany.CompanyQueryDto;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.rest.data.ApiResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/15 23:50
 * @version: V1.0
 **/
@Slf4j
@Service
public class ApiCompanyServiceImpl extends BaseService implements ApiCompanyService {

    @Override
    public ApiResponseData companyQuery(JSONObject param) {
        long costTimeStart = System.currentTimeMillis();
        String result;

        try {
            CompanyQueryDto dto = JSON.toJavaObject(param, CompanyQueryDto.class);
            validate(dto);

            CompanyQueryRequest request = new CompanyQueryRequest();
            request.setCompanyName(dto.getCompanyName());
            request.setTaxpaterCode(dto.getTaxplayerCode());
            String fileName = request.getFilename();

            // 调用upload 发送数据
            CompanyQueryResponse response = ETCCommHelper.upload(fileName, request, CompanyQueryResponse.class);

            result = response.toJson();
            long costTimeEnd = System.currentTimeMillis();
            log.info("[-companyQuery-] result is " + result.replaceAll("\r|\n", "") + " , request is "
                    + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
                    + ",endTime=" + costTimeEnd);
        } catch (IOException e) {
            log.error("[-companyQuery-] 网络异常 " + e);
            throw new ServiceException(CommonConstant.QUERY_FAILED);
        } catch (ApiRequestException apie) {
            log.error("[-companyQuery-] 错误信息：" + apie.getMessage());
            throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
        } catch (ServiceException se) {
            log.error("[-companyQuery-] 错误信息：" + se.getMessage());
            throw new ServiceException(se.getMessage());
        }
        return analyzeApiResultItems(result);
    }

    @Override
    public ApiResponseData companyAdd(JSONObject param) {
        long costTimeStart = System.currentTimeMillis();
        String result;

        try {
            CompanyAddDto dto = JSON.toJavaObject(param, CompanyAddDto.class);
            validate(dto);

            CompanyAddRequest request = new CompanyAddRequest();
            request.setName(dto.getName());
            request.setCompanyType(dto.getCompanyType());
            request.setTaxpayerCode(dto.getBuyerTaxpayerCode());
            request.setOperatingRangeType(dto.getOperatingRangeType());
            request.setContact(dto.getContact());
            request.setTel(dto.getTel());
            request.setEmergencyContact(dto.getEmergencyContact());
            request.setEmergencyTel(dto.getEmergencyTel());
            request.setBuyerName(dto.getBuyerName());
            request.setBuyerTaxpayerCode(dto.getBuyerTaxpayerCode());
            request.setBuyerAddr(dto.getBuyerAddr());
            request.setBuyerTel(dto.getBuyerTel());
            request.setBuyerBank(dto.getBuyerBank());
            request.setBuyerBankAccount(dto.getBuyerBankAccount());
            String fileName = request.getFilename();

            // 调用upload 发送数据
            CompanyAddResponse response = ETCCommHelper.upload(fileName, request, CompanyAddResponse.class);

            result = response.toJson();
            long costTimeEnd = System.currentTimeMillis();
            log.info("[-companyAdd-] result is " + result.replaceAll("\r|\n", "") + " , request is "
                    + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
                    + ",endTime=" + costTimeEnd);
        } catch (IOException e) {
            log.error("[-companyAdd-] 网络异常 " + e);
            throw new ServiceException(CommonConstant.QUERY_FAILED);
        } catch (ApiRequestException apie) {
            log.error("[-companyAdd-] 错误信息：" + apie.getMessage());
            throw new ApiRequestException(apie.getMessage(), ApiResponseCodeEnum.CODE_200.getValue());
        } catch (ServiceException se) {
            log.error("[-companyAdd-] 错误信息：" + se.getMessage());
            throw new ServiceException(se.getMessage());
        }
        return analyzeApiResultItems(result);
    }
}
