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

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.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.enums.ApiResponseCodeEnum;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResult;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.InvoiceResultDto;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.VehicleRegisterDto;
import com.jkcredit.invoice.hub.model.dto.apiCarFree.WayBillNumFindInvoiceDto;
import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillEnd.CarFreeCarrierBillEndDto;
import com.jkcredit.invoice.hub.model.dto.carFreeCarrierBillStart.CarFreeCarrierBillStartDto;
import com.jkcredit.invoice.hub.model.dto.searchInvoice.SearchInvoiceDto;
import com.jkcredit.invoice.hub.model.po.carFreeCarrierBillStart.CarFreeCarrierBillStartPo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.service.carFreeCarrierBillEnd.CarFreeCarrierBillEndService;
import com.jkcredit.invoice.hub.service.carFreeCarrierBillStart.CarFreeCarrierBillStartService;
import com.jkcredit.invoice.hub.service.searchInvoice.SearchInvoiceService;
import com.jkcredit.invoice.hub.service.searchInvoiceResult.SearchInvoiceResultService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.rest.data.ApiResponseData;
import com.jkcredit.invoice.hub.util.BeanUtil;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

/**
 * @description: 无车api接口
 * @author: xusonglin
 * @create: 2020/1/16 21:52
 * @version: V1.0
 **/
@Service
@Slf4j
public class ApiCarFreeServiceImpl extends BaseService implements ApiCarFreeService {
    @Autowired
    CarFreeCarrierBillStartService wayBillStartService;
    @Autowired
    CarFreeCarrierBillEndService wayBillEndService;
    @Autowired
    SearchInvoiceResultService searchInvoiceResultService;
    @Autowired
    SearchInvoiceService searchInvoiceService;
    @Autowired
    ApiCarFreeChargeService apiCarFreeChargeService;

    @Override
    public ApiResponseData vehicleRegisterQuery(JSONObject param) {
        long costTimeStart = System.currentTimeMillis();
        String result;
        String message;
        try {
            VehicleRegisterDto dto = JSON.toJavaObject(param, VehicleRegisterDto.class);
            validate(dto);

            VehicleQueryRequest request = new VehicleQueryRequest();
            request.setPlateNum(dto.getPlateNumber());
            request.setPlateColor(dto.getPlateColor());
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setWaybillSource(CommonConstant.WAYBILL_SOURCE_1);
            String fileName = request.getFilename();

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

            result = response.toJson();
            JSONObject resultObject = JSON.parseObject(result);
            JSONArray array = JSONArray.parseArray(JSON.toJSONString(resultObject.get("result")));
            if (array.size() == 0) {
                message = "车辆未备案";
            } else {
                message = "车辆已备案";
            }
            long costTimeEnd = System.currentTimeMillis();
            log.info("[-vehicleRegisterQuery-] result is " + result.replaceAll("\r|\n", "") + " , request is "
                    + param + ",costtime=" + (costTimeEnd - costTimeStart) + ",startTime=" + costTimeStart
                    + ",endTime=" + costTimeEnd);
        } catch (IOException e) {
            log.error("[-vehicleRegisterQuery-] 网络异常 " + e);
            throw new ServiceException(CommonConstant.QUERY_FAILED);
        } catch (ApiRequestException apie) {
            log.error("[-vehicleRegisterQuery-] 错误信息：" + apie.getMessage());
            throw new com.jkcredit.invoice.hub.spi.lang.exception.ApiRequestException(apie.getMessage());
        } catch (ServiceException se) {
            log.error("[-vehicleRegisterQuery-] 错误信息：" + se.getMessage());
            throw new ServiceException(se.getMessage());
        }
        return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), message);
    }

    @Override
    public ApiResponseData vehicleRegister(JSONObject param) {
        long costTimeStart = System.currentTimeMillis();
        String result;
        try {
            VehicleRegisterDto dto = JSON.toJavaObject(param, VehicleRegisterDto.class);
            validate(dto);

            VehicleRegisterRequest request = new VehicleRegisterRequest();
            request.setPlateNum(dto.getPlateNumber());
            request.setPlateColor(dto.getPlateColor());
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            String fileName = request.getFilename();

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

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

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

        try {
            CarFreeCarrierBillStartDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillStartDto.class);
            validate(dto);

            WaybillStartRequest request = new WaybillStartRequest();
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setNum(dto.getNum());
            request.setPlateNum(dto.getPlateNumber());
            request.setPlateColor(dto.getPlateColor());
            request.setStartTime(dto.getStartTime());
            request.setSourceAddr(dto.getSourceAddr());
            request.setDestAddr(dto.getDestAddr());
            request.setPredictEndTime(dto.getPredictEndTime());
            request.setFee(dto.getFee());
            request.setTitleType(2);

            if (!StringUtil.isNullOrEmpty(dto.getTaxPlayerCode())) {
                request.setTaxplayerCode(dto.getTaxPlayerCode());
            }
            String fileName = request.getFilename();

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

            dto.setIsHistory(CommonConstant.REALTIME_WAY_BILL);
            dto.setStatus(CommonConstant.STATUS_NOT_OVER);
            wayBillStartService.saveBillStart(dto);

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

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public ApiResponseData wayBillEnd(JSONObject param) {
        long costTimeStart = System.currentTimeMillis();
        String result;

        try {
            CarFreeCarrierBillEndDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillEndDto.class);
            validate(dto);

            WaybillEndRequest request = new WaybillEndRequest();
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setNum(dto.getNum());
            request.setRealDestAddr(dto.getRealDestAddr());
            request.setEndTime(dto.getEndTime());
            String fileName = request.getFilename();

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

            dto.setIsHistory(CommonConstant.REALTIME_WAY_BILL);
            wayBillEndService.saveBillEnd(dto);

            // 此处运单开始为空的情况仅限于老平台上传运单开始，新平台上传运单结束
            CarFreeCarrierBillStartPo po = wayBillStartService.getBillStartByNum(dto.getNum());
            if (po == null) {
                po = new CarFreeCarrierBillStartPo();
                po.setUserId(dto.getUserId());
                po.setNum(dto.getNum());
                po.setIsHistory(CommonConstant.REALTIME_WAY_BILL);
                wayBillStartService.saveBillStart(po);
            }
            wayBillStartService.updateBillStartStatus(dto.getNum(), CommonConstant.STATUS_OVER);

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

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

        try {
            CarFreeCarrierBillStartDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillStartDto.class);
            validate(dto);

            WaybillHistoryStartRequest request = new WaybillHistoryStartRequest();
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setNum(dto.getNum());
            request.setPlateNum(dto.getPlateNumber());
            request.setPlateColor(dto.getPlateColor());
            request.setStartTime(dto.getStartTime());
            request.setSourceAddr(dto.getSourceAddr());
            request.setDestAddr(dto.getDestAddr());
            request.setPredictEndTime(dto.getPredictEndTime());
            request.setFee(dto.getFee());
            request.setTitleType(2);

            if (!StringUtil.isNullOrEmpty(dto.getTaxPlayerCode())) {
                request.setTaxplayerCode(dto.getTaxPlayerCode());
            }
            String fileName = request.getFilename();

            // 调用upload 发送数据
            WaybillHistoryStartResponse response = ETCCommHelper.upload(fileName, request, WaybillHistoryStartResponse.class);
            dto.setIsHistory(CommonConstant.HISTORY_WAY_BILL);
            dto.setStatus(CommonConstant.STATUS_NOT_OVER);
            wayBillStartService.saveBillStart(dto);

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

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

        try {
            CarFreeCarrierBillEndDto dto = JSONObject.toJavaObject(param, CarFreeCarrierBillEndDto.class);
            validate(dto);

            WaybillHistoryEndRequest request = new WaybillHistoryEndRequest();
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setNum(dto.getNum());
            request.setRealDestAddr(dto.getRealDestAddr());
            request.setEndTime(dto.getEndTime());
            String fileName = request.getFilename();

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

            dto.setIsHistory(CommonConstant.HISTORY_WAY_BILL);
            wayBillEndService.saveBillEnd(dto);

            // 此处运单开始为空的情况仅限于老平台上传历史运单开始，新平台上传历史运单结束
            CarFreeCarrierBillStartPo po = wayBillStartService.getBillStartByNum(dto.getNum());
            if (po == null) {
                po = new CarFreeCarrierBillStartPo();
                po.setUserId(dto.getUserId());
                po.setNum(dto.getNum());
                po.setIsHistory(CommonConstant.HISTORY_WAY_BILL);
                wayBillStartService.saveBillStart(po);
            }

            wayBillStartService.updateBillStartStatus(dto.getNum(), CommonConstant.STATUS_OVER);

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

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

        try {
            WayBillNumFindInvoiceDto dto = JSONObject.toJavaObject(param, WayBillNumFindInvoiceDto.class);
            validate(dto);

            WaybillNumFindInvoiceRequest request = new WaybillNumFindInvoiceRequest();
            request.setCompanyNum(CommonConstant.COMPANY_NUMBER);
            request.setWaybillNum(dto.getNum());
            String fileName = request.getFilename();

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

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

    @Override
    public ApiResponseData findInvoice(JSONObject param) {
        try {
            if (param.get("num") != null && param.get("num").equals("YD123456")) {
                String result = "{\"info\":\"成功.\",\"receiveTime\":\"2019-04-29T10:30:34\",\"result\":[{\"invoiceNum\":\"02969150\",\"invoiceCode\":\"014001700110\",\"invoiceMakeTime\":\"2019-04-16T17:24:10\",\"invoiceUrl\":\"https://kp.txffp.com/ts/fp/d?d=599963096511912471553E0EECB05E79687E6449B484E7165A99A8A21A714813\",\"invoiceHtmlUrl\":\"https://kp.txffp.com/ts/openInvoice/invoice/previewInvoicetxf.html?param=ZnBkbT0wMTQwMDE3MDAxMTImZnBobT0wMjk2OTE1OQ==\",\"enStation\":\"大同站\",\"exStation\":\"朔州东站\",\"exTime\":\"2018-06-30T06:56:06\",\"fee\":10500,\"totalTaxAmount\":303,\"plateNum\":\"晋B00000\",\"vehicleType\":13,\"waybillNum\":\"20190416\",\"waybillStatus\":2,\"waybillStartTime\":\"2019-04-15T05:00:00\",\"waybillEndTime\":\"2019-04-16T08:00:00\",\"totalAmount\":10386,\"taxRate\":0.03,\"invoiceType\":\"消费发票\",\"amount\":10083,\"sellerName\":\"山西交通控股集团有限公司\",\"sellerTaxpayerCode\":\"91140000MA0JTP7A7K\",\"transactionId\":\"G005514009006020200502018063006560606\"},{\"invoiceNum\":\"02969152\",\"invoiceCode\":\"014001700112\",\"invoiceMakeTime\":\"2019-04-16T17:24:11\",\"invoiceUrl\":\"https://kp.txffp.com/ts/fp/d?d=599963096511912471553E0EECB05E79687E6449B484E7165A99A8A21A714813\",\"invoiceHtmlUrl\":\"https://kp.txffp.com/ts/openInvoice/invoice/previewInvoicetxf.html?param=ZnBkbT0wMTQwMDE3MDAxMTImZnBobT0wMjk2OTE1OQ==\",\"enStation\":\"大同站\",\"exStation\":\"朔州东站\",\"exTime\":\"2018-06-30T06:56:06\",\"fee\":10500,\"totalTaxAmount\":303,\"plateNum\":\"晋B00000\",\"vehicleType\":13,\"waybillNum\":\"20190416\",\"waybillStatus\":2,\"waybillStartTime\":\"2019-04-15T05:00:00\",\"waybillEndTime\":\"2019-04-16T08:00:00\",\"totalAmount\":10386,\"taxRate\":0.03,\"invoiceType\":\"消费发票\",\"amount\":10083,\"sellerName\":\"山西交通控股集团有限公司\",\"sellerTaxpayerCode\":\"91140000MA0JTP7A7K\",\"transactionId\":\"G005514009006020200502018063006560605\"},{\"invoiceNum\":\"02969151\",\"invoiceCode\":\"014001700111\",\"invoiceMakeTime\":\"2019-04-16T17:24:10\",\"invoiceUrl\":\"https://kp.txffp.com/ts/fp/d?d=599963096511912471553E0EECB05E79687E6449B484E7165A99A8A21A714813\",\"invoiceHtmlUrl\":\"https://kp.txffp.com/ts/openInvoice/invoice/previewInvoicetxf.html?param=ZnBkbT0wMTQwMDE3MDAxMTImZnBobT0wMjk2OTE1OQ==\",\"enStation\":\"大同站\",\"exStation\":\"朔州东站\",\"exTime\":\"2018-06-30T06:56:06\",\"fee\":10500,\"totalTaxAmount\":303,\"plateNum\":\"晋B00000\",\"vehicleType\":13,\"waybillNum\":\"20190416\",\"waybillStatus\":2,\"waybillStartTime\":\"2019-04-15T05:00:00\",\"waybillEndTime\":\"2019-04-16T08:00:00\",\"totalAmount\":10386,\"taxRate\":0.03,\"invoiceType\":\"消费发票\",\"amount\":10083,\"sellerName\":\"山西交通控股集团有限公司\",\"sellerTaxpayerCode\":\"91140000MA0JTP7A7K\",\"transactionId\":\"G005514009006020200502018063006560606\"}],\"plateNum\":\"京A11111\",\"vehicleType\":null,\"waybillNum\":\"20190416\",\"waybillStatus\":2,\"waybillStartTime\":\"2019-04-15T05:00:00\",\"waybillEndTime\":\"2019-04-16T08:00:00\"}";
                return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), result);
            }
            CarFreeCarrierBillStartPo po = wayBillStartService.getBillStartByNum(param.getString("num"));
            if (po != null && po.getStatus().equals(CommonConstant.STATUS_INVOICE_OVER)) {
                // 数据库中已经存在运单开始信息，并且运单状态为已结束，直接从数据库中查询运单发票数据，封装返回
                wayBillStartService.updateBillStartIsSearch(param.getString("num"));
                // 结果集外层数据
                SearchInvoiceDto searchInvoiceDto = searchInvoiceService.getSearchInvoiceByNum(param.getString("num"));
                // 结果集发票集合
                List<InvoiceResult> invoiceResultList = searchInvoiceResultService.getSearchInvoiceResult(param.getString("num"), param.getLong("userId"));

                // 返回结果集
                InvoiceResultDto invoiceResultDto = new InvoiceResultDto();
                BeanUtil.copyProperties(invoiceResultDto, searchInvoiceDto);
                invoiceResultDto.setResult(invoiceResultList);

                return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), JSON.toJSONString(invoiceResultDto, SerializerFeature.WriteMapNullValue));
            } else if (po != null && !po.getStatus().equals(CommonConstant.STATUS_INVOICE_OVER)) {
                // 数据库中已经存在运单开始信息，并且运单状态非已结束，需要重新查询接口
                return apiCarFreeChargeService.charge(po);
            } else {
                // 数据库中没有运单开始信息（这部分数据为遗留数据，老平台迁移用户，没有运单开始数据的存储），需要调接口
                po = new CarFreeCarrierBillStartPo();
                po.setUserId(param.getLong("userId"));
                po.setNum(param.getString("num"));
                return apiCarFreeChargeService.charge(po);
            }
        } catch (ServiceException se) {
            log.error("[-findInvoice-] 错误信息：" + se.getMessage());
            throw new ServiceException(se.getMessage());
        }
    }

    @Override
    public ApiResponseData findNoSearchNums(JSONObject param) {
        try {
            List<String> nums = wayBillStartService.getNoSearchNums(param.getString("userId"));
            return ApiResponseData.success(ApiResponseCodeEnum.CODE_200.getValue(), JSON.toJSONString(nums));
        } catch (ServiceException e) {
            log.error("[-findNoSearchNums-] 错误信息：" + e.getMessage());
            throw new ServiceException(e.getMessage());
        }
    }
}
