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

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.waybill.WaybillCountQueryRequest;
import cn.com.taiji.sdk.model.comm.protocol.tts.waybill.WaybillCountQueryResponse;
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.apiWayBillCountQuery.WayBillCountQueryDto;
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/17 22:58
 * @version: V1.0
 **/
@Service
@Slf4j
public class WayBillCountQueryServiceImpl extends BaseService implements WayBillCountQueryService {
    @Override
    public ApiResponseData wayBillCountQuery(JSONObject param) {
        long costTimeStart = System.currentTimeMillis();
        String result;

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

            WaybillCountQueryRequest request = new WaybillCountQueryRequest();
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setTaxCode(dto.getTaxCode());
            request.setWaybillSource(1);
            request.setTradeMonth(dto.getTradeMonth());
            request.setPageNo(dto.getPageNo());
            String fileName = request.getFilename();

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

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