package com.jkcredit.invoice.credit.custInterface;

import cn.com.taiji.sdk.model.comm.protocol.tts.vehicle.VehicleQueryResponse;
import com.alibaba.fastjson.JSONObject;
import com.jkcredit.invoice.common.DataResult;
import com.jkcredit.invoice.mapper.Binvoce.BillInvoiceMapper;
import com.jkcredit.invoice.mapper.customer.CustomerCarRecMapper;
import com.jkcredit.invoice.mapper.customer.CustomerMapper;
import com.jkcredit.invoice.mapper.waybill.NoCarWaybillMapper;
import com.jkcredit.invoice.model.entity.customer.Customer;
import com.jkcredit.invoice.model.entity.customer.CustomerCarRec;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.entity.invoice.BillInvoice;
import com.jkcredit.invoice.model.entity.manager.Param;
import com.jkcredit.invoice.model.entity.waybill.NoCarWayBill;
import com.jkcredit.invoice.service.customer.CustomerRecService;
import com.jkcredit.invoice.service.lowerService.NoCarService;
import com.jkcredit.invoice.service.manager.ParamService;
import com.jkcredit.invoice.service.nocar.NoCarBillWayImportService;
import com.jkcredit.invoice.util.DateUtil;
import com.jkcredit.invoice.util.RespR;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static java.util.stream.Collectors.toList;


@Slf4j
@Service("noCarInterService")
public class NoCarInterServiceImpl implements NoCarInterService {

    @Autowired
    private NoCarService noCarService;
    @Autowired
    NoCarBillWayImportService noCarWaybillMapperImprt;

    @Autowired
    CustomerCarRecMapper customerCarRecMapper;

    @Autowired
    NoCarWaybillMapper noCarWaybillMapper;
    @Autowired
    CustomerRecService customerRecMapper;


    @Autowired
    BillInvoiceMapper billInvoiceMapper;


    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    ParamService paramService;

    /**
     * 无车 车辆备案接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customerCarRec(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.customerCarRec-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String companyName =  jsonObject.getString("companyName");//企业名称 选输
            String plateNumber =  jsonObject.getString("plateNumber");//plateNumber 必输
            String plateColor =  jsonObject.getString("plateColor");//plateColor 必输
            if(StringUtils.isEmpty(data)|| null == jsonObject || StringUtils.isEmpty(plateNumber)
                    || StringUtils.isEmpty(plateColor)

            ){
                return  result;
            }


            List<CustomerCarRec> customerCarRecList = new ArrayList<CustomerCarRec>();
            CustomerCarRec customerCarRec = new CustomerCarRec();
                customerCarRec.setCustomerName(appKey);
            //    String companyNum = paramService.getParamsByParamName("REQUEST_COMPANY_NUM").getParamValue();
            //    String appKeyDemo = paramService.getParamsByParamName("REQUEST_COMPANY_NAME").getParamValue();
                Customer cust = customerMapper.selectByCustomerName(appKey);

                CustomerRec customerRec = new CustomerRec();
                customerRec.setCustomerName(appKey);
                customerRec.setCompanyName(cust.getCompany());
                CustomerRec customerRec1 =  customerRecMapper.selectByCustomerNameAndCompanyConcat(customerRec);
                customerCarRec.setCompanyName(customerRec1.getCompanyName());
                customerCarRec.setCustomerName(customerRec1.getCustomerName());
            customerCarRec.setBusinessType("2");//无车
            customerCarRec.setCarColor(plateColor);
            customerCarRec.setCarNum(plateNumber);
            customerCarRec.setInterType(0);
            if("0".equals(customerCarRec.getCarColor())){
                customerCarRec.setServiceOperation(1);//默认运营车辆
            }

            customerCarRecList.add(customerCarRec);

            RespR rs = noCarService.customerCarRec(customerCarRecList);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.customerCarRec-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.customerCarRec-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }



    /**
     * 无车 车辆备案查询接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult customeRecUpperQuery(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.customeRecUpperQuery-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String waybillSource =  jsonObject.getString("waybillSource");//备案来源 选输
            String plateNumber =  jsonObject.getString("plateNumber");//plateNumber 必输
            String plateColor =  jsonObject.getString("plateColor");//plateColor 必输
            if(StringUtils.isEmpty(data)|| null == jsonObject || StringUtils.isEmpty(plateNumber)
                    || StringUtils.isEmpty(plateColor)

            ){
                return  result;
            }


            CustomerCarRec customerCarRec = new CustomerCarRec();
            Param param = paramService.getParamsByParamName("REQUEST_COMPANY_NUM");
            customerCarRec.setCompanyNum(param.getParamValue());//无车，车辆备案，运单上传，运单查询都是统一用交科的企业编号;自有车用客户的企业编号
            customerCarRec.setCarColor(plateColor);
            customerCarRec.setCarNum(plateNumber);
            customerCarRec.setCustomerName(appKey);

            RespR rs = noCarService.customerCarRecQueryUpper(customerCarRec);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.customeRecUpperQuery-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                JSONObject jb = JSONObject.parseObject(rs.getData().toString());
                if(null == jb.getJSONArray("result") || jb.getJSONArray("result").size() == 0){
                    result.setData(2);
                    result.setMsg("车辆未备案");
                }
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.customeRecUpperQuery-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }





    /**
     * 无车 实时运单开始指令接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult noCarBillStart(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.noCarBillStart-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String num =  jsonObject.getString("num");//运单编号 必选
            String plateNumber =  jsonObject.getString("plateNumber");//车牌号 必选
            String plateColor =  jsonObject.getString("plateColor");//车牌颜色 必选
            String startTime =  jsonObject.getString("startTime");//运单开始时间 必选
            String sourceAddr =  jsonObject.getString("sourceAddr");//运单开始地址 必选
            String destAddr =  jsonObject.getString("destAddr");//运单目的地址 必选
            String predictEndTime =  jsonObject.getString("predictEndTime");//运单预计完成时间 必选
            Integer fee =  jsonObject.getInteger("fee");//运单费用 必选
            Integer titleType =  jsonObject.getInteger("titleType");//发票抬头类型 必选
            String taxplayerCode =  jsonObject.getString("taxplayerCode");//税号 必选
            if(StringUtils.isEmpty(data)|| null == jsonObject
                    || StringUtils.isEmpty(num)
                    || StringUtils.isEmpty(plateNumber)
                    || StringUtils.isEmpty(plateColor)
                    || StringUtils.isEmpty(startTime)
                    || StringUtils.isEmpty(sourceAddr)
                    || StringUtils.isEmpty(destAddr)
                    || StringUtils.isEmpty(predictEndTime)
                    || null == fee
                    || null == titleType
                    || StringUtils.isEmpty(taxplayerCode)

            ){
                return  result;
            }
            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyReferencenum(taxplayerCode);
            customerRec = customerRecMapper.selectByCustomerNameAndCompanyReference(customerRec);
            if(customerRec == null || customerRec.getRecStatus()!=1){
                result.setMsg("客户："+appKey+";税号"+taxplayerCode+"未在平台备案成功，请先做备案");
                return result;
            }
           /* CustomerCarRec customerCarRec1 = customerCarRecMapper.selectByCarNum(plateNumber);
            if (null == customerCarRec1){
                return result;
            }*/

            NoCarWayBill noCarWayBill = new NoCarWayBill();
            noCarWayBill.setCustomerName(appKey);
            noCarWayBill.setCompanyName(customerRec.getCompanyName());
            noCarWayBill.setPlateNum(plateNumber);
            noCarWayBill.setBillNum(num);
            noCarWayBill.setPlateColor(plateColor);
            noCarWayBill.setStartTime(DateUtil.getDistanceHoursFormat(startTime));
            noCarWayBill.setSourceAddr(sourceAddr);
            noCarWayBill.setDestAddr(destAddr);
            noCarWayBill.setPredictEndTime(DateUtil.getDistanceHoursFormat(predictEndTime));
            noCarWayBill.setFee(fee.longValue());
            noCarWayBill.setTitleType(titleType);
            noCarWayBill.setTaxplayerCode(taxplayerCode);
            noCarWayBill.setHisFlag(0);
            noCarWayBill.setIntfaceStartTime(DateUtil.getCurrentDateStr());
            NoCarWayBill noCarWayBillDb = noCarWaybillMapper.selectByBillNum(noCarWayBill.getBillNum());

            if(noCarWayBillDb!=null && (noCarWayBillDb.getBillwayStatus() == 1||noCarWayBillDb.getBillwayStatus() == 2 || noCarWayBillDb.getBillwayStatus() == 3 || noCarWayBillDb.getBillwayStatus() == 4)) {
                log.info("chongfuyundanhao:"+noCarWayBillDb.getBillNum());
                result.setMsg("运单号重复");
                return result;
            }
            List<CustomerCarRec> customerCarRecs = new ArrayList<>();
            CustomerCarRec customerCarRec = new CustomerCarRec();
            customerCarRec.setCompanyName(noCarWayBill.getCompanyName());
            customerCarRec.setCarNum(noCarWayBill.getPlateNum());
            customerCarRec.setCarColor(noCarWayBill.getPlateColor());
            customerCarRec.setCustomerName(appKey);
            customerCarRec.setInterType(0);//接口
            customerCarRec.setCompanyNum(customerRec.getCompanyNum());
            customerCarRec.setBusinessType("2");
            customerCarRec.setServiceOperation(1);//默认运营车辆
            noCarWayBill.setTitleType(2);
            noCarWayBill.setUpdateTime(DateUtil.getCurrentDateStr());
            //先查询上游是否已经备案
            RespR<VehicleQueryResponse> respRquery = noCarService.customerCarRecQueryUpper(customerCarRec);
            if(respRquery.getCode() == 1 || respRquery.getData() == null || respRquery.getData().getResult() == null || respRquery.getData().getResult().size()<=0){
                customerCarRecs.add(customerCarRec);
                RespR respR = noCarService.customerCarRec(customerCarRecs);
                if(respR.getCode() ==1){
                    noCarWayBill.setFailReason(respR.getMsg());
                    noCarWayBill.setBillwayStatus(-2);
                    insertOrUpdateBill(noCarWayBill,noCarWayBillDb);
                    result.setMsg("车牌号备案失败，失败原因："+respR.getMsg());
                    return result;
                }
            }
            RespR rs = noCarService.noCarWaybillStart(noCarWayBill,noCarWayBillDb);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.noCarBillStart-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                noCarWayBill.setFailReason(rs.getMsg());
                noCarWayBill.setBillwayStatus(-2);
                insertOrUpdateBill(noCarWayBill,noCarWayBillDb);
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.noCarBillStart-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }



    /**
     * 无车 实时运单结束指令接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult noCarBillEnd(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.noCarBillEnd-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String num =  jsonObject.getString("num");//运单号 必输
            String realDestAddr =  jsonObject.getString("realDestAddr");//运单实际目的地址 必输
            String endTime =  jsonObject.getString("endTime");//运单实际结束时间 必输
            if(StringUtils.isEmpty(data)|| null == jsonObject || StringUtils.isEmpty(num)
                    || StringUtils.isEmpty(realDestAddr)
                    || StringUtils.isEmpty(endTime)

            ){
                return  result;
            }


            NoCarWayBill noCarWayBill1 = noCarWaybillMapper.selectByBillNum(num);
            if (null == noCarWayBill1){
                log.info("chongfuyundanhao1:"+num);
                result.setMsg("运单开始指令还未上传!");
                return result;
            }


            NoCarWayBill noCarWayBill = new NoCarWayBill();
            noCarWayBill.setBillNum(num);
            noCarWayBill.setDestAddr(realDestAddr);
            noCarWayBill.setPredictEndTime(DateUtil.getDistanceHoursFormat(endTime));
            noCarWayBill.setStartTime(noCarWayBill1.getStartTime());
            noCarWayBill.setHisFlag(noCarWayBill1.getHisFlag());
            noCarWayBill.setInterType(0);//接口
            noCarWayBill.setUpdateTime(DateUtil.getCurrentDateStr());
            noCarWayBill.setInterfaceEndTime(DateUtil.getCurrentDateStr());
            RespR rs = noCarService.noCarWaybillEnd(noCarWayBill,noCarWayBill1);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.noCarBillEnd-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.noCarBillEnd-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }




    /**
     * 无车 历史运单开始指令接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult noCarHisWaybillStart(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.noCarHisWaybillStart-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data + " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String num =  jsonObject.getString("num");//运单编号 必选
            String plateNumber =  jsonObject.getString("plateNumber");//车牌号 必选
            String plateColor =  jsonObject.getString("plateColor");//车牌颜色 必选
            String startTime =  jsonObject.getString("startTime");//运单开始时间 必选
            String sourceAddr =  jsonObject.getString("sourceAddr");//运单开始地址 必选
            String destAddr =  jsonObject.getString("destAddr");//运单目的地址 必选
            String predictEndTime =  jsonObject.getString("predictEndTime");//运单预计完成时间 必选
            Integer fee =  jsonObject.getInteger("fee");//运单费用 必选
            Integer titleType =  jsonObject.getInteger("titleType");//发票抬头类型 必选
            String taxplayerCode =  jsonObject.getString("taxplayerCode");//税号 必选
            if(StringUtils.isEmpty(data)|| null == jsonObject
                    || StringUtils.isEmpty(num)
                    || StringUtils.isEmpty(plateNumber)
                    || StringUtils.isEmpty(plateColor)
                    || StringUtils.isEmpty(startTime)
                    || StringUtils.isEmpty(sourceAddr)
                    || StringUtils.isEmpty(destAddr)
                    || StringUtils.isEmpty(predictEndTime)
                    || null == fee
                    || null == titleType
                    || StringUtils.isEmpty(taxplayerCode)

            ){
                return  result;
            }

            CustomerRec customerRec = new CustomerRec();
            customerRec.setCustomerName(appKey);
            customerRec.setCompanyReferencenum(taxplayerCode);
            customerRec = customerRecMapper.selectByCustomerNameAndCompanyReference(customerRec);
            if(customerRec == null){
                result.setMsg("客户："+appKey+";税号"+taxplayerCode+"未在平台备案成功，请先做备案");
                return result;
            }

            NoCarWayBill noCarWayBill = new NoCarWayBill();
            noCarWayBill.setCustomerName(appKey);
            noCarWayBill.setCompanyName(customerRec.getCompanyName());
            noCarWayBill.setPlateNum(plateNumber);
            noCarWayBill.setBillNum(num);
            noCarWayBill.setPlateColor(plateColor);
            noCarWayBill.setStartTime(DateUtil.getDistanceHoursFormat(startTime));
            noCarWayBill.setSourceAddr(sourceAddr);
            noCarWayBill.setDestAddr(destAddr);
            noCarWayBill.setPredictEndTime(DateUtil.getDistanceHoursFormat(predictEndTime));
            noCarWayBill.setFee(fee.longValue());
            noCarWayBill.setTitleType(titleType);
            noCarWayBill.setTaxplayerCode(taxplayerCode);
            noCarWayBill.setHisFlag(1);
            noCarWayBill.setUpdateTime(DateUtil.getCurrentDateStr());
            noCarWayBill.setIntfaceStartTime(DateUtil.getCurrentDateStr());
            noCarWayBill.setInterType(0);//接口
            NoCarWayBill noCarWayBillDb = noCarWaybillMapper.selectByBillNum(noCarWayBill.getBillNum());

            if(noCarWayBillDb!=null && (noCarWayBillDb.getBillwayStatus() == 1||noCarWayBillDb.getBillwayStatus() == 2 || noCarWayBillDb.getBillwayStatus() == 3 || noCarWayBillDb.getBillwayStatus() == 4)) {
                log.info("chongfuyundanhao1:"+noCarWayBillDb.getBillNum());
                result.setMsg("运单号重复");
                return result;
            }
            List<CustomerCarRec> customerCarRecs = new ArrayList<>();
            CustomerCarRec customerCarRec = new CustomerCarRec();
            customerCarRec.setCompanyName(noCarWayBill.getCompanyName());
            customerCarRec.setCarNum(noCarWayBill.getPlateNum());
            customerCarRec.setCarColor(noCarWayBill.getPlateColor());
            customerCarRec.setCustomerName(appKey);
            customerCarRec.setInterType(0);
            customerCarRec.setCompanyNum(customerRec.getCompanyNum());
            customerCarRec.setBusinessType("2");
            customerCarRec.setServiceOperation(1);//默认运营车辆
            noCarWayBill.setTitleType(2);
            //先查询上游是否已经备案
            RespR<VehicleQueryResponse> respRquery = noCarService.customerCarRecQueryUpper(customerCarRec);
            if(respRquery.getCode() == 1 || respRquery.getData() == null || respRquery.getData().getResult() == null || respRquery.getData().getResult().size()<=0){
                customerCarRecs.add(customerCarRec);
                RespR respR = noCarService.customerCarRec(customerCarRecs);
                if(respR.getCode() ==1){
                    result.setMsg("车牌号备案失败，失败原因："+respR.getMsg());
                    noCarWayBill.setFailReason(respR.getMsg());
                    noCarWayBill.setBillwayStatus(-2);
                    insertOrUpdateBill(noCarWayBill,noCarWayBillDb);
                    return result;
                }
            }
            RespR rs = noCarService.noCarHisWaybillStart(noCarWayBill,noCarWayBillDb);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.noCarHisWaybillStart-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {

                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                noCarWayBill.setFailReason(rs.getMsg());
                noCarWayBill.setBillwayStatus(-2);
                insertOrUpdateBill(noCarWayBill,noCarWayBillDb);
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.noCarHisWaybillStart-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }

    //插入或者更新运单
    public void insertOrUpdateBill(NoCarWayBill noCarWayBill,NoCarWayBill noCarWayBillDb){

        if(noCarWayBillDb == null){
            noCarWaybillMapper.insert(noCarWayBill);
        }else{
            noCarWaybillMapper.updateByBillNum(noCarWayBill);
        }
    }



    /**
     * 无车 历史运单结束指令接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult noCarHisWaybillEnd(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.noCarHisWaybillEnd-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String num =  jsonObject.getString("num");//运单号 必输
            String realDestAddr =  jsonObject.getString("realDestAddr");//运单实际目的地址 必输
            String endTime =  jsonObject.getString("endTime");//运单实际结束时间 必输
            if(StringUtils.isEmpty(data)|| null == jsonObject || StringUtils.isEmpty(num)
                    || StringUtils.isEmpty(realDestAddr)
                    || StringUtils.isEmpty(endTime)

            ){
                return  result;
            }


            NoCarWayBill noCarWayBill1 = noCarWaybillMapper.selectByBillNum(num);
            if (null == noCarWayBill1){
                result.setMsg("未上传开始运单");
                return result;
            }


            NoCarWayBill noCarWayBill = new NoCarWayBill();
            noCarWayBill.setBillNum(num);
            noCarWayBill.setDestAddr(realDestAddr);
            noCarWayBill.setPredictEndTime(DateUtil.getDistanceHoursFormat(endTime));
            noCarWayBill.setStartTime(noCarWayBill1.getStartTime());
            noCarWayBill.setHisFlag(noCarWayBill1.getHisFlag());
            noCarWayBill.setInterType(0);//接口
            noCarWayBill.setUpdateTime(DateUtil.getCurrentDateStr());
            noCarWayBill.setInterfaceEndTime(DateUtil.getCurrentDateStr());
            RespR rs = noCarService.noCarHisWaybillEnd(noCarWayBill,noCarWayBill1);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.noCarHisWaybillEnd-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.noCarHisWaybillEnd-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }




    /**
     * 无车 运单号查询发票数据
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult noCarVoiceQuery(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.noCarVoiceQuery-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String num =  jsonObject.getString("num");//运单号 必输
            if(StringUtils.isEmpty(data)|| null == jsonObject || StringUtils.isEmpty(num)

            ){
                return  result;
            }


            NoCarWayBill noCarWayBill1 = noCarWaybillMapper.selectByBillNum(num);
            if (null == noCarWayBill1){
                result.setMsg("运单号异常,无法开票");
                return result;
            }
            noCarWayBill1.setCustomerName(appKey);
            noCarWayBill1.setCompanyName(noCarWayBill1.getCompanyName());
            RespR rs = noCarService.getInvoiceByWayBillNumReal(noCarWayBill1,true);
            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.noCarVoiceQuery-] result is "
                    + rs.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != rs && rs.getCode() == 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(rs.getData().toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg(rs.getMsg());
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.noCarVoiceQuery-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }







    /**
     * 无车  获取未查询过发票的运单编号
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult noCarNoVoiceQuery(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.noCarNoVoiceQuery-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);




            List<NoCarWayBill> noCarWayBills = noCarWaybillMapper.getNoCarNoVoiceQuery(appKey);
            List<String> list1 = new ArrayList<String>();

            for (NoCarWayBill noCarWayBill:noCarWayBills
                 ) {
                list1.add(noCarWayBill.getBillNum());

            }
            List<BillInvoice>  billInvoices =  billInvoiceMapper.selectNoCarNoVoiceQuery(appKey);
            List<String> list2 = new ArrayList<String>();
            for (BillInvoice billInvoice:billInvoices
            ) {
                list2.add(billInvoice.getWaybillNum());

            }

            List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());

            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.noCarNoVoiceQuery-] result is "
                    + reduce1.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != reduce1 && reduce1.size() > 0){
                result.setData(1);
                result.setCode(200);
                result.setMsg(reduce1.toString());
                return result;
            } else {
                result.setData(2);
                result.setCode(200);
                result.setMsg("未查得");
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.noCarNoVoiceQuery-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }





    /**
     * 无车  账户余额查询接口
     * @param appKey
     * @param api
     * @param data
     * @return
     */
    @Override
    public DataResult balanceQuery(String appKey, String api, String data,String requestid) {

        long costtimestart = System.currentTimeMillis();

        DataResult result = new DataResult();

        result.setData(3);
        result.setCode(200);
        result.setRequestid(requestid);
        result.setMsg("无法认证");

        try {
            log.info("[-NoCarInterServiceImpl.balanceQuery-] request appKey=" + appKey + " ,api=" + api + " ,data=" +data+ " ,requestid=" +requestid);


            Customer customer = customerMapper.selectByCustomerName(appKey);



            long costtimeend = System.currentTimeMillis();
            log.info("[-NoCarInterServiceImpl.balanceQuery-] result is "
                    + customer.toString() + ", request is " + data + " ,costtime="
                    + (costtimeend - costtimestart));
            if(null != customer){
                result.setData(1);
                result.setCode(200);
                JSONObject jb = new JSONObject();
                jb.put("balance",customer.getAccountBalance());
                jb.put("lastDeductionTime",customer.getInvoiceTime());
                result.setMsg(jb.toString());
                return result;
            } else {
                result.setData(3);
                result.setCode(200);
                result.setMsg("无法认证");
                return result;
            }
        } catch (Exception e) {
            log.error("[-NoCarInterServiceImpl.balanceQuery-] get httpclient exception is "
                    + e + ", request is " + data);
        }
        return result;
    }


    public static void main(String[] args) {
            List<String> list1 = new ArrayList<String>();
            list1.add("1");
            list1.add("2");
            list1.add("3");
            list1.add("5");
            list1.add("6");

            List<String> list2 = new ArrayList<String>();
            list2.add("2");
            list2.add("3");
            list2.add("7");
            list2.add("8");

            // 交集
            List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
            System.out.println("---交集 intersection---");
            intersection.parallelStream().forEach(System.out :: println);

            // 差集 (list1 - list2)
            List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
            System.out.println("---差集 reduce1 (list1 - list2)---");
            reduce1.parallelStream().forEach(System.out :: println);    // 并集
            List<String> listAll = list1.parallelStream().collect(toList());
            List<String> listAll2 = list2.parallelStream().collect(toList());
            listAll.addAll(listAll2);
            System.out.println("---并集 listAll---");
            listAll.parallelStream().forEachOrdered(System.out :: println);

            // 去重并集
            List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
            System.out.println("---得到去重并集 listAllDistinct---");
            listAllDistinct.parallelStream().forEachOrdered(System.out :: println);

            System.out.println("---原来的List1---");
            list1.parallelStream().forEachOrdered(System.out :: println);
            System.out.println("---原来的List2---");
            list2.parallelStream().forEachOrdered(System.out :: println);

    }


}
