package com.sckw.transport.service;

import com.sckw.core.model.constant.Global;
import com.sckw.core.model.enums.CarWaybillEnum;
import com.sckw.core.model.enums.LogisticsOrderEnum;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.response.HttpResult;
import com.sckw.mongo.enums.BusinessTypeEnum;
import com.sckw.mongo.model.SckwLogisticsOrder;
import com.sckw.mongo.model.SckwWaybillOrder;
import com.sckw.stream.model.SckwBusSum;
import com.sckw.transport.dao.*;
import com.sckw.transport.model.*;
import com.sckw.transport.model.dto.OperateWaybillDto;
import com.sckw.transport.model.dto.OperateSendCarDto;
import com.sckw.transport.model.dto.SendCarDto1;
import com.sckw.transport.model.dto.WaybillOrderTicketDto;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zk
 * @desc TODO
 * @date 2023/7/19 0019
 */
@Service
public class KwtWaybillOrderService {

    @Autowired
    KwtWaybillOrderMapper waybillOrderDao;
    @Autowired
    KwtLogisticsOrderMapper logisticsOrderDao;
    @Autowired
    KwtLogisticsOrderTrackMapper logisticsOrderTrackDao;
    @Autowired
    KwtLogisticsOrderAddressMapper logisticsOrderAddressDao;
    @Autowired
    KwtWaybillOrderAddressMapper waybillOrderAddressDao;
    @Autowired
    KwtWaybillOrderTrackMapper waybillOrderTrackDao;
    @Autowired
    KwtWaybillOrderTicketMapper waybillOrderTicketDao;
    @Autowired
    KwtLogisticsOrderUnitMapper logisticsOrderUnitDao;
    @Autowired
    KwtLogisticsOrderGoodsMapper logisticsOrderGoodsDao;
    @Autowired
    KwtLogisticsOrderCirculateMapper logisticsOrderCirculateDao;
    @Resource
    private StreamBridge streamBridge;

    /**
     * @param params
     * @desc 派车-趟次
     * @author zk
     * @date 2023/7/19
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult sendCar(List<SendCarDto1> params) {
        /**校验**/
        //承运单信息
        KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(params.get(0).getLOrderId());
        HttpResult result = checkWaybillOrder(params, logisticsOrder);
        if (result.getCode() != HttpStatus.SUCCESS_CODE) {
            return result;
        }
        //下游司机委派量累加（趟次）
        BigDecimal ranksAmount = (BigDecimal)result.getData();

        /**数据处理**/
        result = setWaybillOrder(params, logisticsOrder);
        if (result.getCode() != HttpStatus.SUCCESS_CODE) {
            return result;
        }
        //Mongodb数据存储model
        List<SckwWaybillOrder> sckwWaybillOrders = (List<SckwWaybillOrder>) result.getData();

        /**变更承运单状态（累计派车量等于剩余可运量）/已委托量entrust_amount**/
        setLogisticsBySendCar(logisticsOrder.getId(), ranksAmount);

        /**Mongodb数据存储**/
        for (SckwWaybillOrder waybillOrder:sckwWaybillOrders) {
            //rabbitMq业务汇总数据发送/消费对象
            SckwBusSum busSum = new SckwBusSum();
            //业务汇总类型
            busSum.setBusSumType(BusinessTypeEnum.WAYBILL_ORDER_TYPE.getName());
            //操作对象（1新增/2修改/3替换数据）
            busSum.setMethod(1);
            //业务汇总数据对象
            busSum.setObject(waybillOrder);
            streamBridge.send("sckw-busSum", busSum);
        }

        /**发送消息**/
        return HttpResult.ok();
    }

    /**
     * @param params 派车请求参数
     * @param logisticsOrder 物流订单
     * @desc 派车校验
     * @author zk
     * @date 2023/7/19
     **/
    public HttpResult checkWaybillOrder(List<SendCarDto1> params, KwtLogisticsOrder logisticsOrder) {
        /**校验物流运单是否可以派车**/
        if (logisticsOrder == null) {
            return HttpResult.error("订单已不存在！" );
        }

        //状态校验
        if (logisticsOrder.getStatus() != LogisticsOrderEnum.WAIT_DELIVERY.getCode()
                && logisticsOrder.getStatus() != LogisticsOrderEnum.IN_TRANSIT.getCode()) {
            return HttpResult.error("订单"+LogisticsOrderEnum.getName(logisticsOrder.getStatus())+"不能派车！" );
        }

        //剩余可运量校验
        BigDecimal entrustAmount = logisticsOrder.getEntrustAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getEntrustAmount();
        BigDecimal subcontractAmount = logisticsOrder.getSubcontractAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getSubcontractAmount();
        //历史下游司机委派量 + 分包量
        BigDecimal entrustAmountTotal = entrustAmount.add(subcontractAmount);
        if (entrustAmountTotal.compareTo(logisticsOrder.getAmount()) > 0) {
            return HttpResult.error("剩余可运量已不能派车！" );
        }

        /**校验是车辆委托量否超过物流订单剩余量**/
        //下游司机委派量累加（趟次）
        BigDecimal ranksAmount = new BigDecimal(Global.AMOUNT);
        for (SendCarDto1 sendCarDto:params) {
            if (sendCarDto.getType() == Global.NUMERICAL_ONE) {
                //趟次派车趟次数量必填
                if (sendCarDto.getCount() == null || sendCarDto.getCount() <= 0) {
                    return HttpResult.error(sendCarDto.getTruckNo() + "趟次必须等于或大于0！" );
                }
                //承运订单该车辆计划运输总量：核定载量 * 趟次
                Double truckEntrustAmount = sendCarDto.getEntrustAmount() * sendCarDto.getCount();
                ranksAmount = ranksAmount.add(new BigDecimal(truckEntrustAmount));

                //当前下游司机委派量 + 历史下游司机委派量 + 分包量
                BigDecimal actualAmount = ranksAmount.add(entrustAmountTotal);
                if (actualAmount.compareTo(logisticsOrder.getAmount()) > 0) {
                    return HttpResult.error("累计派车量已大于剩余可运量！" );
                }
            }
        }
        return HttpResult.ok(ranksAmount);
    }

    /**
     * @param params 派车请求参数
     * @param logisticsOrder 物流订单
     * @desc 派车存储
     * @author zk
     * @date 2023/7/19
     **/
    public HttpResult setWaybillOrder(List<SendCarDto1> params, KwtLogisticsOrder logisticsOrder) {
        /**获取基础信息**/
        //装货地址
        KwtWaybillOrderAddress loadAddress = this.getAddress(logisticsOrder.getId(), Global.NUMERICAL_ONE);
        //卸货地址
        KwtWaybillOrderAddress unloadAddress = this.getAddress(logisticsOrder.getId(), Global.NUMERICAL_TWO);
        //托运企业
        KwtLogisticsOrderUnit checkEnt = logisticsOrderUnitDao.findByUnit(logisticsOrder.getId(), Global.NUMERICAL_ONE);
        //承运企业
        KwtLogisticsOrderUnit carrierEnt = logisticsOrderUnitDao.findByUnit(logisticsOrder.getId(), Global.NUMERICAL_TWO);
        //商品信息
        KwtLogisticsOrderGoods goods = logisticsOrderGoodsDao.findByGoods(logisticsOrder.getId());

        /**MySql车辆运单数据处理/存储**/
        List<SckwWaybillOrder> sckwWaybillOrders = new ArrayList();
        for (SendCarDto1 sendCarDto:params) {
            /**KwtWaybillOrder数据组装**/
            KwtWaybillOrder waybillOrder = this.setKwtWaybillOrder(sendCarDto);

            /**数据存储**/
            if (sendCarDto.getType() == Global.NUMERICAL_ONE) {
                for (int i=0; i<sendCarDto.getCount(); i++) {
                    //车辆运单存储
                    ranksSave(waybillOrder, loadAddress, unloadAddress);

                    /**SckwWaybillOrder数据组装**/
                    SckwWaybillOrder mongoOrder = setSckwWaybillOrder(sendCarDto, waybillOrder, logisticsOrder, loadAddress,
                            unloadAddress, checkEnt, carrierEnt, goods);
                    sckwWaybillOrders.add(mongoOrder);
                }
            } else {
                //循环派车存储
                KwtLogisticsOrderCirculate circulate = circulateSave(waybillOrder);
                if (circulate != null) {
                    /**SckwWaybillOrder数据组装**/
                    SckwWaybillOrder mongoOrder = setSckwWaybillOrder(sendCarDto, waybillOrder, logisticsOrder, loadAddress,
                            unloadAddress, checkEnt, carrierEnt, goods);
                    mongoOrder.set_id(circulate.getId());
                    mongoOrder.setWOrderNo(null);
                    sckwWaybillOrders.add(mongoOrder);
                }
            }
        }

        /**Mongodb数据存储
         for (SckwWaybillOrder waybillOrder:sckwWaybillOrders) {
         //rabbitMq业务汇总数据发送/消费对象
         SckwBusSum busSum = new SckwBusSum();
         //业务汇总类型
         busSum.setBusSumType(BusinessTypeEnum.WAYBILL_ORDER_TYPE.getName());
         //操作对象（1新增/2修改/3替换数据）
         busSum.setMethod(1);
         //业务汇总数据对象
         busSum.setObject(waybillOrder);
         streamBridge.send("sckw-busSum", busSum);
         }**/

        return HttpResult.ok(sckwWaybillOrders);
    }

    /**
     * @param waybillOrder 车辆订单信息
     * @param loadAddress 装货地址信息
     * @param unloadAddress 卸货地址信息
     * @desc 趟次派车数据存储
     * @author zk
     * @date 2023/7/21
     **/
    public void ranksSave(KwtWaybillOrder waybillOrder, KwtWaybillOrderAddress loadAddress, KwtWaybillOrderAddress unloadAddress) {
        /**车辆运单信息**/
        waybillOrder.setId(null);
        waybillOrderDao.insert(waybillOrder);

        /**车辆运单地址信息**/
        loadAddress.setId(null);
        loadAddress.setWOrderId(waybillOrder.getId());
        loadAddress.setCreateTime(waybillOrder.getCreateTime());
        loadAddress.setUpdateTime(waybillOrder.getUpdateTime());
        waybillOrderAddressDao.insert(loadAddress);
        unloadAddress.setId(null);
        unloadAddress.setWOrderId(waybillOrder.getId());
        unloadAddress.setCreateTime(waybillOrder.getCreateTime());
        unloadAddress.setUpdateTime(waybillOrder.getUpdateTime());
        waybillOrderAddressDao.insert(unloadAddress);

        /**车辆运单状态记录**/
        this.setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), null);
    }

    /**
     * @param waybillOrder 车辆订单信息
     * @desc 循环派车数据存储
     * @author zk
     * @date 2023/7/21
     **/
    public KwtLogisticsOrderCirculate circulateSave(KwtWaybillOrder waybillOrder) {
        //循环派车信息
        KwtLogisticsOrderCirculate circulate = logisticsOrderCirculateDao.findCirculate(waybillOrder.getLOrderId(),
                waybillOrder.getTruckId(), waybillOrder.getDriverId(), 0);
        if (circulate == null) {
            circulate = new KwtLogisticsOrderCirculate();
            BeanUtils.copyProperties(waybillOrder, circulate);
            circulate.setWOrderId(null);
            circulate.setStatus(0);
            int count = logisticsOrderCirculateDao.insert(circulate);
            return count > 0 ? circulate : null;
        }
        return circulate;
    }

    /**
     * @param lOrderId 物流订单ID
     * @param addressType 地址类型（1装货地址、2卸货地址）
     * @desc 获取物流订单地址信息
     * @author zk
     * @date 2023/7/20
     **/
    public KwtWaybillOrderAddress getAddress(Long lOrderId, int addressType) {
        /**查询数据**/
        KwtLogisticsOrderAddress orderAddress = logisticsOrderAddressDao.findByAddress(lOrderId, addressType);

        /**组装数据**/
        KwtWaybillOrderAddress address = new KwtWaybillOrderAddress();
        BeanUtils.copyProperties(orderAddress, address);
        address.setId(null);
        address.setCreateBy(null);
        address.setCreateTime(null);
        address.setUpdateBy(null);
        address.setUpdateBy(null);
        return address;
    }

    /**
     * @param wOrderId 车辆订单ID
     * @param status 车辆运单状态
     * @param remark 备注
     * @desc 新增运单状态记录
     * @author zk
     * @date 2023/7/20
     **/
    public KwtWaybillOrderTrack setWaybillOrderTrack(Long wOrderId, int status, String remark) {
        KwtWaybillOrderTrack orderTrack = new KwtWaybillOrderTrack();
        orderTrack.setWOrderId(wOrderId);
        orderTrack.setStatus(status);
        orderTrack.setRemark(remark);
        int count = waybillOrderTrackDao.insert(orderTrack);
        return count > 0 ? orderTrack : null;
    }

    /**
     * @param sendCarDto 派车请求参数
     * @desc 车辆运单对象赋值
     * @author zk
     * @date 2023/7/20
     **/
    public KwtWaybillOrder setKwtWaybillOrder(SendCarDto1 sendCarDto) {
        KwtWaybillOrder waybillOrder = new KwtWaybillOrder();
        waybillOrder.setEntId(LoginUserHolder.getEntId());
        waybillOrder.setLOrderId(sendCarDto.getLOrderId());
        waybillOrder.setWOrderNo("xxxxx");
        waybillOrder.setType(sendCarDto.getType());
        waybillOrder.setStartTime(sendCarDto.getLoadDateTime());
        waybillOrder.setEndTime(sendCarDto.getUnloadDateTime());
        waybillOrder.setTruckId(sendCarDto.getTruckId());
        waybillOrder.setTruckNo(sendCarDto.getTruckNo());
        waybillOrder.setDriverId(sendCarDto.getDriverId());
        waybillOrder.setDriverName(sendCarDto.getDriverName());
        waybillOrder.setDriverPhone(sendCarDto.getDriverPhone());
        waybillOrder.setDriverIdcard(sendCarDto.getDriverIdcard());
        waybillOrder.setEntrustAmount(new BigDecimal(sendCarDto.getEntrustAmount()));
        waybillOrder.setUnloadAmount(new BigDecimal(Global.AMOUNT));
        waybillOrder.setLoadAmount(new BigDecimal(Global.AMOUNT));
        waybillOrder.setDeficitAmount(new BigDecimal(Global.AMOUNT));
        waybillOrder.setStatus(CarWaybillEnum.PENDING_ORDER.getCode());
        return waybillOrder;
    }

    /**
     * @param sendCarDto 派车请求参数
     * @param logisticsOrder 承运订单
     * @param loadAddress 装货地址
     * @param unloadAddress 卸货地址
     * @param checkEnt 托运企业
     * @param carrierEnt 承运企业
     * @param goods 商品信息
     * @desc mongodb车辆运单对象赋值
     * @author zk
     * @date 2023/7/20
     **/
    public SckwWaybillOrder setSckwWaybillOrder(SendCarDto1 sendCarDto, KwtWaybillOrder waybillOrder,
                                                KwtLogisticsOrder logisticsOrder, KwtWaybillOrderAddress loadAddress,
                                                KwtWaybillOrderAddress unloadAddress, KwtLogisticsOrderUnit checkEnt,
                                                KwtLogisticsOrderUnit carrierEnt, KwtLogisticsOrderGoods goods) {
        //基础信息
        SckwWaybillOrder order = new SckwWaybillOrder();
        order.set_id(new IdWorker(1).nextId());
        order.setWOrderId(waybillOrder.getId());
        order.setWOrderNo(waybillOrder.getWOrderNo());
        order.setLOrderId(logisticsOrder.getId());
        order.setLOrderNo(logisticsOrder.getLOrderNo());
        order.setTOrderId(logisticsOrder.getTOrderId());
        order.setTOrderNo(logisticsOrder.getTOrderNo());
        order.setEntId(LoginUserHolder.getEntId());
        order.setFirmName(LoginUserHolder.getEntName());

        //车辆信息
        order.setTruckId(sendCarDto.getTruckId());
        order.setTruckNo(sendCarDto.getTruckNo());
        order.setTruckType(sendCarDto.getTruckType());
        order.setTruckEnergyType(sendCarDto.getTruckEnergyType());
        order.setTruckUseType(sendCarDto.getTruckUseType());
        order.setTruckColor(sendCarDto.getTruckColor());
        order.setTruckTrailerNo(sendCarDto.getTruckTrailerNo());

        //司机信息
        order.setDriverId(sendCarDto.getDriverId());
        order.setDriverName(sendCarDto.getDriverName());
        order.setDriverPhone(sendCarDto.getDriverPhone());
        order.setDriverIdcard(sendCarDto.getDriverIdcard());

        //运输信息
        order.setLoadTime(sendCarDto.getLoadDateTime());
        order.setUnloadTime(sendCarDto.getUnloadDateTime());
        order.setType(sendCarDto.getType());
        order.setStartTime(sendCarDto.getLoadDateTime());
        order.setEndTime(sendCarDto.getUnloadDateTime());
        order.setEntrustAmount(new BigDecimal(sendCarDto.getEntrustAmount()));
        order.setLoadAmount(new BigDecimal(Global.AMOUNT));
        order.setLoadTareAmount(new BigDecimal(Global.AMOUNT));
        order.setLoadGrossAmount(new BigDecimal(Global.AMOUNT));
        order.setLoadUrls(Global.EMPTY_STRING);
        order.setUnloadAmount(new BigDecimal(Global.AMOUNT));
        order.setUnloadTareAmount(new BigDecimal(Global.AMOUNT));
        order.setUnloadGrossAmount(new BigDecimal(Global.AMOUNT));
        order.setUnloadUrls(Global.EMPTY_STRING);
        order.setDeficitAmount(new BigDecimal(Global.AMOUNT));
        order.setLoss(logisticsOrder.getLoss());
        order.setLossUnit(logisticsOrder.getLossUnit());
        order.setGoodsPrice(logisticsOrder.getGoodsPrice());
        order.setGoodsPriceUnit(logisticsOrder.getGoodsPriceUnit());

        //商品信息
        order.setGoodsId(goods.getGoodsId());
        order.setGoodsCode(null);
        order.setGoodsName(goods.getGoodsName());
        order.setGoodsType(null);
        order.setGoodsType(null);
        order.setGoodsIndustry(null);
        order.setGoodsSpec(null);

        //托运/承运企业信息
        order.setCheckEntId(checkEnt.getEntId());
        order.setCheckFirmName(checkEnt.getFirmName());
        order.setCheckContacts(checkEnt.getContacts());
        order.setCheckPhone(checkEnt.getPhone());
        order.setCarrierEntId(carrierEnt.getEntId());
        order.setCarrierFirmName(carrierEnt.getFirmName());
        order.setCarrierContacts(carrierEnt.getContacts());
        order.setCarrierPhone(carrierEnt.getPhone());

        //装卸货地址相关信息
        order.setLoadType(loadAddress.getType());
        order.setLoadContacts(loadAddress.getContacts());
        order.setLoadPhone(loadAddress.getPhone());
        order.setLoadCityCode(loadAddress.getCityCode());
        order.setLoadCityName(loadAddress.getCityName());
        order.setLoadDetailAddress(loadAddress.getDetailAddress());
        order.setLoadLat(loadAddress.getLat());
        order.setLoadLng(loadAddress.getLng());
        order.setUnloadType(unloadAddress.getType());
        order.setUnloadContacts(unloadAddress.getContacts());
        order.setUnloadPhone(unloadAddress.getPhone());
        order.setUnloadCityCode(unloadAddress.getCityCode());
        order.setUnloadCityName(unloadAddress.getCityName());
        order.setUnloadDetailAddress(unloadAddress.getDetailAddress());
        order.setUnloadLat(unloadAddress.getLat());
        order.setUnloadLng(unloadAddress.getLng());

        //操作人信息
        order.setStatus(waybillOrder.getStatus());
        order.setCreateBy(LoginUserHolder.getUserId());
        order.setCreateByName(LoginUserHolder.getUserName());
        order.setCreateTime(waybillOrder.getCreateTime());
        order.setUpdateBy(LoginUserHolder.getUserId());
        order.setUpdateByName(LoginUserHolder.getUserName());
        order.setUpdateTime(waybillOrder.getUpdateTime());
        order.setDelFlag(waybillOrder.getDelFlag());
        return order;
    }

    /**
     * @param lOrderId 承运订单ID
     * @param ranksAmount 当前派车总量
     * @description 派车更新承运订单信息
     * @author zk
     * @date 2023/7/24
     **/
    public void setLogisticsBySendCar(Long lOrderId, BigDecimal ranksAmount) {
        /**
         状态判断是否是分包承运单（不考虑该承运单下级分包）
         存在：更新当前承运订单状态及委派量entrustAmount，并且校验承运订单所有父级状态是否需要修改（是修改状态，否则不修改）
         不存在：更新当前承运订单状态及委派量entrustAmount
         **/
        //承运单信息
        KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(lOrderId);

        //当前承运订单所属分包层级
        //int level = checkLogisticsLevel(logisticsOrder);
        String pids = logisticsOrder.getPids();
        if (StringUtils.isNotBlank(pids)) {
            String [] idArray = pids.split(",");
            for (String id : idArray) {
                KwtLogisticsOrder order = logisticsOrderDao.selectById(id);
                //承运订单-待派车
                if (logisticsOrder.getStatus() == LogisticsOrderEnum.WAIT_DELIVERY.getCode()) {
                    //承运订单不存在分包：更新当前承运订单状态及委派量entrustAmount
                    if (order.getId() == logisticsOrder.getId()) {
                        //承运订单已委派量
                        BigDecimal entrustAmount = logisticsOrder.getEntrustAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getEntrustAmount();
                        logisticsOrder.setEntrustAmount(entrustAmount.add(ranksAmount));
                    }
                    //承运订单:待派车->运输中
                    logisticsOrder.setStatus(LogisticsOrderEnum.IN_TRANSIT.getCode());
                    logisticsOrderDao.updateById(order);

                    //承运订单状态记录
                    KwtLogisticsOrderTrack orderTrack = new KwtLogisticsOrderTrack();
                    orderTrack.setLOrderId(order.getId());
                    orderTrack.setStatus(order.getStatus());
                    orderTrack.setRemark(LogisticsOrderEnum.getName(order.getStatus()));
                    logisticsOrderTrackDao.insert(orderTrack);
                }
            }
        }
    }

    /**
     * @param logisticsOrder 承运订单
     * @description 当前承运订单所属分包层级
     * @author zk
     * @date 2023/7/24
     **/
    public int checkLogisticsLevel(KwtLogisticsOrder logisticsOrder) {
        String pids = logisticsOrder.getPids();
        String [] idArrays = StringUtils.isBlank(pids) ? new String[0] : pids.split(Global.COMMA);
        return idArrays.length <= 1 ? Global.NUMERICAL_ONE : (idArrays.length - Global.NUMERICAL_ONE);
    }

    /**
     * @param ids 车辆运单主键ID，多个已逗号隔开
     * @description 取消派车(趟次派车)
     * @author zk
     * @date 2023/7/24
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult cancelSendCar(List<Long> ids){
        /**数据校验**/
        if (CollectionUtils.isEmpty(ids)) {
            return HttpResult.error("请选择需要撤回的车辆运单！");
        }
        //1校验运单ID是否有效/2循环派车才能撤回/3运单当前状态是否可以撤回
        for (Long id : ids) {
            KwtWaybillOrder order = waybillOrderDao.selectById(id);
            //1校验运单ID是否有效
            if (order != null) {
                //3待接单的车辆运单才能撤回
                if (order.getStatus() != CarWaybillEnum.PENDING_ORDER.getCode()) {
                    return HttpResult.error("运单"+order.getWOrderNo()+"当前状态已不能撤回！");
                }
            } else {
                //循环派车信息
                KwtLogisticsOrderCirculate circulate = logisticsOrderCirculateDao.selectById(id);
                //2循环派车才能撤回
                if (circulate != null) {
                    return HttpResult.error("循环派车单不能撤回！");
                }
                return HttpResult.error("选择撤回车辆运单的数据不存在！");
            }
        }

        /**业务处理**/
        for (Long id : ids) {
            KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(id);
            if (waybillOrder != null) {
                //待接单的车辆运单才能撤回
                if (waybillOrder.getStatus() == CarWaybillEnum.PENDING_ORDER.getCode()) {
                    /**1更新车辆运单**/
                    waybillOrder.setStatus(CarWaybillEnum.REVOKED.getCode());
                    waybillOrderDao.updateById(waybillOrder);

                    /**2新增运单状态记录**/
                    setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), null);

                    /**3回退承运订单entrustAmount**/
                    KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(waybillOrder.getLOrderId());
                    //承运订单已委派量=承运订单原委派量+当前车辆运单委派量
                    BigDecimal entrustAmount = logisticsOrder.getEntrustAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getEntrustAmount();
                    logisticsOrder.setEntrustAmount(entrustAmount.subtract(waybillOrder.getEntrustAmount()));
                    logisticsOrderDao.updateById(logisticsOrder);

                    /**4Mongodb数据更新**/
                    //1车辆订单
                    SckwWaybillOrder wOrder = new SckwWaybillOrder();
                    wOrder.set_id(waybillOrder.getId());
                    editSckwWaybillOrder(wOrder, waybillOrder, 2);

                    //2承运订单
                    SckwLogisticsOrder lOrder = new SckwLogisticsOrder();
                    editSckwLogisticsOrder(lOrder, logisticsOrder);

                    /**5发送消息**/

                }
            } else {
                return HttpResult.error("选择撤回的数据已不存在！");
            }
        }

        return HttpResult.ok("车辆运单撤回成功!");
    }

    /**
     * @param wOrder mongodb中车辆运单信息
     * @param order 车辆运单信息
     * @param method 操作对象（1新增/2修改/3替换数据）
     * @description 更新mongodb中车辆运单信息
     * @author zk
     * @date 2023/7/26
     **/
    public void editSckwWaybillOrder(SckwWaybillOrder wOrder, KwtWaybillOrder order, int method){
        wOrder.setWOrderId(order.getId());
        wOrder.setStatus(order.getStatus());
        wOrder.setUpdateTime(order.getUpdateTime());
        wOrder.setUpdateBy(order.getUpdateBy());
        //rabbitMq业务汇总数据发送/消费对象
        SckwBusSum busSum = new SckwBusSum();
        //业务汇总类型
        busSum.setBusSumType(BusinessTypeEnum.WAYBILL_ORDER_TYPE.getName());
        //操作对象（1新增/2修改/3替换数据）
        busSum.setMethod(method);
        //业务汇总数据对象
        busSum.setObject(wOrder);
        streamBridge.send("sckw-busSum", busSum);
    }

    /**
     * @param lOrder mongodb中承运订单信息
     * @param order 承运订单
     * @description 更新mongodb中承运订单信息
     * @author zk
     * @date 2023/7/26
     **/
    public void editSckwLogisticsOrder(SckwLogisticsOrder lOrder, KwtLogisticsOrder order){
        lOrder.set_id(order.getId());
        lOrder.setLOrderId(order.getId());
        lOrder.setEntrustAmount(order.getEntrustAmount());
        lOrder.setUpdateTime(order.getUpdateTime());
        lOrder.setUpdateBy(order.getUpdateBy());
        //rabbitMq业务汇总数据发送/消费对象
        SckwBusSum busSum = new SckwBusSum();
        //业务汇总类型
        busSum.setBusSumType(BusinessTypeEnum.LOGISTICS_ORDER_TYPE.getName());
        //操作对象（1新增/2修改/3替换数据）
        busSum.setMethod(2);
        //业务汇总数据对象
        busSum.setObject(lOrder);
        streamBridge.send("sckw-busSum", busSum);
    }

    /**
     * @param params {}
     * @description 确认接单
     * @author zk
     * @date 2023/7/25
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult confirmSendCar(OperateSendCarDto params) {
        /**趟次派车**/
        if (params.getType() == Global.NUMERICAL_ONE) {
            /**数据校验**/
            KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
            if (waybillOrder == null) {
                return HttpResult.error("车辆趟次运单不存在！");
            }
            //待接单车辆运单才能拒绝接单
            if (waybillOrder.getStatus() != CarWaybillEnum.PENDING_ORDER.getCode()) {
                return HttpResult.error("车辆运单当前状态已不能拒绝接单！");
            }

            /**业务处理**/
            /**1更新车辆运单**/
            waybillOrder.setStatus(CarWaybillEnum.PENDING_VEHICLE.getCode());
            waybillOrderDao.updateById(waybillOrder);

            /**2新增运单状态记录**/
            setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

            /**3Mongodb数据更新**/
            //1车辆运单
            SckwWaybillOrder wOrder = new SckwWaybillOrder();
            wOrder.set_id(waybillOrder.getId());
            editSckwWaybillOrder(wOrder, waybillOrder, 2);

            /**4发送消息**/

        }

        /**循环派车**/
        if (params.getType() == Global.NUMERICAL_TWO) {
            /**数据校验**/
            KwtLogisticsOrderCirculate circulate = logisticsOrderCirculateDao.findCirculate(params.getLOrderId(),
                    params.getTruckId(), params.getDriverId(), 0);
            if (circulate == null) {
                return HttpResult.error("车辆循环运单不存在！");
            }

            /**获取基础信息**/
            //装货地址
            KwtWaybillOrderAddress loadAddress = this.getAddress(params.getLOrderId(), Global.NUMERICAL_ONE);
            //卸货地址
            KwtWaybillOrderAddress unloadAddress = this.getAddress(params.getLOrderId(), Global.NUMERICAL_TWO);
            //KwtWaybillOrder数据组装
            KwtWaybillOrder waybillOrder = new KwtWaybillOrder();
            waybillOrder.setEntId(LoginUserHolder.getEntId());
            waybillOrder.setLOrderId(params.getLOrderId());
            waybillOrder.setWOrderNo("xxxxxx");
            waybillOrder.setType(params.getType());
            waybillOrder.setStartTime(circulate.getStartTime());
            waybillOrder.setEndTime(circulate.getEndTime());
            waybillOrder.setTruckId(circulate.getTruckId());
            waybillOrder.setTruckNo(circulate.getTruckNo());
            waybillOrder.setDriverId(circulate.getDriverId());
            waybillOrder.setDriverName(circulate.getDriverName());
            waybillOrder.setDriverPhone(circulate.getDriverPhone());
            waybillOrder.setDriverIdcard(circulate.getDriverIdcard());
            waybillOrder.setEntrustAmount(circulate.getEntrustAmount());
            waybillOrder.setStatus(CarWaybillEnum.PENDING_VEHICLE.getCode());

            /**业务处理**/
            /**1车辆运单存储**/
            ranksSave(waybillOrder, loadAddress, unloadAddress);
            //补上车辆运单待接单状态
            setWaybillOrderTrack(circulate, waybillOrder.getId());

            /**2更新承运订单信息**/
            //承运单信息
            KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(waybillOrder.getLOrderId());
            //承运订单已委派量:承运订单历史委派量+当前运单委派量
            BigDecimal entrustAmount = logisticsOrder.getEntrustAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getEntrustAmount();
            logisticsOrder.setEntrustAmount(entrustAmount.add(waybillOrder.getEntrustAmount()));
            logisticsOrderDao.updateById(logisticsOrder);

            /**3更新循环派车数据**/
            circulate.setWOrderId(waybillOrder.getId());
            logisticsOrderCirculateDao.updateById(circulate);

            /**4Mongodb数据更新**/
            //1车辆订单
            SckwWaybillOrder wOrder = new SckwWaybillOrder();
            wOrder.set_id(circulate.getId());
            editSckwWaybillOrder(wOrder, waybillOrder, 3);

            //2承运订单
            SckwLogisticsOrder lOrder = new SckwLogisticsOrder();
            editSckwLogisticsOrder(lOrder, logisticsOrder);

            /**5发送消息**/
        }
        return HttpResult.ok("确认接单成功！");
    }

    /**
     * @param circulate 循环派车记录
     * @param wOrderId 车辆订单ID
     * @desc 新增循环派车接单后车辆运单待接单记录
     * @author zk
     * @date 2023/7/26
     **/
    public KwtWaybillOrderTrack setWaybillOrderTrack(KwtLogisticsOrderCirculate circulate, Long wOrderId) {
        KwtWaybillOrderTrack orderTrack = new KwtWaybillOrderTrack();
        orderTrack.setWOrderId(wOrderId);
        orderTrack.setStatus(CarWaybillEnum.PENDING_ORDER.getCode());
        orderTrack.setCreateBy(circulate.getCreateBy());
        orderTrack.setCreateTime(circulate.getCreateTime());
        orderTrack.setUpdateBy(circulate.getCreateBy());
        orderTrack.setUpdateTime(circulate.getUpdateTime());
        int count = waybillOrderTrackDao.insert(orderTrack);
        return count > 0 ? orderTrack : null;
    }

    /**
     * @param params {}
     * @description 拒绝接单
     * @author zk
     * @date 2023/7/25
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult refuseSendCar(OperateSendCarDto params) {
        /**趟次派车**/
        if (params.getType() == Global.NUMERICAL_ONE) {
            /**数据校验**/
            KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
            if (waybillOrder == null) {
                return HttpResult.error("车辆趟次运单不存在！");
            }
            //待接单车辆运单才能拒绝接单
            if (waybillOrder.getStatus() != CarWaybillEnum.PENDING_ORDER.getCode()) {
                return HttpResult.error("车辆运单当前状态已不能拒绝接单！");
            }

            /**业务处理**/
            /**1更新车辆运单**/
            waybillOrder.setStatus(CarWaybillEnum.REVOKED.getCode());
            waybillOrderDao.updateById(waybillOrder);

            /**2新增运单状态记录**/
            setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

            /**3回退承运订单entrustAmount**/
            KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(waybillOrder.getLOrderId());
            //承运订单已委派量=承运订单原委派量+当前车辆运单委派量
            BigDecimal entrustAmount = logisticsOrder.getEntrustAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getEntrustAmount();
            logisticsOrder.setEntrustAmount(entrustAmount.subtract(waybillOrder.getEntrustAmount()));
            logisticsOrderDao.updateById(logisticsOrder);

            /**4Mongodb数据更新**/
            //1车辆运单
            SckwWaybillOrder wOrder = new SckwWaybillOrder();
            wOrder.set_id(waybillOrder.getId());
            editSckwWaybillOrder(wOrder, waybillOrder, 2);

            //2承运订单
            SckwLogisticsOrder lOrder = new SckwLogisticsOrder();
            editSckwLogisticsOrder(lOrder, logisticsOrder);

            /**5发送消息**/

        }

        /**循环派车**/
        if (params.getType() == Global.NUMERICAL_TWO) {
            /**数据校验**/
            KwtLogisticsOrderCirculate circulate = logisticsOrderCirculateDao.findCirculate(params.getLOrderId(),
                    params.getTruckId(), params.getDriverId(), 0);
            if (circulate == null) {
                return HttpResult.error("车辆循环运单不存在！");
            }

            /**1更新循环单状态为失效**/
            circulate.setStatus(Global.YES);
            circulate.setRemark(params.getRemark());
            logisticsOrderCirculateDao.updateById(circulate);

            /**2Mongodb数据更新**/
            SckwWaybillOrder wOrder = new SckwWaybillOrder();
            wOrder.set_id(circulate.getId());
            KwtWaybillOrder waybillOrder = new KwtWaybillOrder();
            waybillOrder.setStatus(CarWaybillEnum.REJECT_ORDER.getCode());
            waybillOrder.setUpdateTime(circulate.getUpdateTime());
            waybillOrder.setUpdateBy(circulate.getUpdateBy());
            editSckwWaybillOrder(wOrder, waybillOrder, 2);

            /**3发送消息**/
        }

        return HttpResult.ok("拒绝接单成功!");
    }

    /**
     * @param params 确认/拒绝出车参数
     * @description 确认出车
     * @author zk
     * @date 2023/7/26
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult confirmDeparture(OperateWaybillDto params){
        /**数据校验**/
        KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
        if (waybillOrder == null) {
            return HttpResult.error("车辆运单不存在！");
        }
        //待出车车辆运单才能出车
        if (waybillOrder.getStatus() != CarWaybillEnum.PENDING_VEHICLE.getCode()) {
            return HttpResult.error("车辆运单当前状态已不能出车！");
        }

        /**业务处理**/
        /**1更新车辆运单**/
        waybillOrder.setStatus(CarWaybillEnum.EXIT_COMPLETED.getCode());
        waybillOrderDao.updateById(waybillOrder);

        /**2新增运单状态记录**/
        setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

        /**3Mongodb数据更新**/
        //1车辆运单
        SckwWaybillOrder wOrder = new SckwWaybillOrder();
        wOrder.set_id(waybillOrder.getId());
        editSckwWaybillOrder(wOrder, waybillOrder, 2);

        /**4发送消息**/

        return HttpResult.ok("确认出车成功！");
    }

    /**
     * @param params 确认/拒绝出车参数
     * @description 拒绝出车
     * @author zk
     * @date 2023/7/26
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult refuseDeparture(OperateWaybillDto params){
        /**数据校验**/
        KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
        if (waybillOrder == null) {
            return HttpResult.error("车辆运单不存在！");
        }
        //待出车车辆运单才能拒绝出车
        if (waybillOrder.getStatus() != CarWaybillEnum.PENDING_VEHICLE.getCode()) {
            return HttpResult.error("车辆运单当前状态已不能拒绝出车！");
        }

        /**业务处理**/
        /**1更新车辆运单**/
        waybillOrder.setStatus(CarWaybillEnum.REFUSE_TRAFFIC.getCode());
        waybillOrderDao.updateById(waybillOrder);

        /**2新增运单状态记录**/
        setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

        /**3回退承运订单entrustAmount**/
        KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(waybillOrder.getLOrderId());
        //承运订单已委派量=承运订单原委派量+当前车辆运单委派量
        BigDecimal entrustAmount = logisticsOrder.getEntrustAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getEntrustAmount();
        logisticsOrder.setEntrustAmount(entrustAmount.subtract(waybillOrder.getEntrustAmount()));
        logisticsOrderDao.updateById(logisticsOrder);

        /**4Mongodb数据更新**/
        //1车辆运单
        SckwWaybillOrder wOrder = new SckwWaybillOrder();
        wOrder.set_id(waybillOrder.getId());
        editSckwWaybillOrder(wOrder, waybillOrder, 2);

        //2承运订单
        SckwLogisticsOrder lOrder = new SckwLogisticsOrder();
        editSckwLogisticsOrder(lOrder, logisticsOrder);

        /**5发送消息**/

        return HttpResult.ok("拒绝出车成功！");
    }

    /**
     * @param params {}
     * @desc 到达装货地点
     * @author zk
     * @date 2023/7/26
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult arriveLoadLocation(OperateWaybillDto params){
        /**数据校验**/
        KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
        if (waybillOrder == null) {
            return HttpResult.error("车辆运单不存在！");
        }
        //已出车车辆运单才能到达装货地点
        if (waybillOrder.getStatus() != CarWaybillEnum.EXIT_COMPLETED.getCode()) {
            return HttpResult.error("车辆运单当前状态已不能到达装货地点！");
        }

        /**业务处理**/
        /**1更新车辆运单**/
        waybillOrder.setStatus(CarWaybillEnum.WAIT_LOADING.getCode());
        waybillOrderDao.updateById(waybillOrder);

        /**2新增运单状态记录**/
        setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

        /**3Mongodb数据更新**/
        //1车辆运单
        SckwWaybillOrder wOrder = new SckwWaybillOrder();
        wOrder.set_id(waybillOrder.getId());
        editSckwWaybillOrder(wOrder, waybillOrder, 2);

        /**4发送消息**/

        return HttpResult.ok("到达装货地点成功！");
    }

    /**
     * @param params {}
     * @desc 装货
     * @author zk
     * @date 2023/7/26
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult loading(WaybillOrderTicketDto params){
        /**数据校验**/
        KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
        if (waybillOrder == null) {
            return HttpResult.error("车辆运单不存在！");
        }
        //到达装货地车辆运单才能装货
        if (waybillOrder.getStatus() != CarWaybillEnum.WAIT_LOADING.getCode()) {
            return HttpResult.error("车辆运单当前状态已不能装货！");
        }

        /**业务处理**/
        /**1更新车辆运单**/
        waybillOrder.setLoadAmount(params.getAmount());
        waybillOrder.setStatus(CarWaybillEnum.COMPLETION_LOADING.getCode());
        waybillOrderDao.updateById(waybillOrder);

        /**2新增运单状态记录**/
        setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

        /**3新增装卸货榜单信息**/
        KwtWaybillOrderTicket ticket = new KwtWaybillOrderTicket();
        ticket.setWOrderId(waybillOrder.getId());
        ticket.setType(Global.NUMERICAL_ONE);
        ticket.setAmount(waybillOrder.getLoadAmount());
        ticket.setUrls(params.getUrls());
        waybillOrderTicketDao.insert(ticket);

        /**4更新承运订单entrustAmount**/
        KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(waybillOrder.getLOrderId());
        //承运订单已装货量=承运订单原装货量+当前车辆运单装货量
        BigDecimal loadAmount = logisticsOrder.getLoadAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getLoadAmount();
        logisticsOrder.setLoadAmount(loadAmount.add(waybillOrder.getLoadAmount()));
        logisticsOrder.setLoadTime(waybillOrder.getUpdateTime());
        logisticsOrderDao.updateById(logisticsOrder);

        /**5Mongodb数据更新**/
        //1车辆运单
        SckwWaybillOrder wOrder = new SckwWaybillOrder();
        wOrder.set_id(waybillOrder.getId());
        wOrder.setLoadAmount(waybillOrder.getLoadAmount());
        wOrder.setLoadUrls(params.getUrls());
        wOrder.setLoadTime(waybillOrder.getUpdateTime());
        editSckwWaybillOrder(wOrder, waybillOrder, 2);

        //2承运订单
        SckwLogisticsOrder lOrder = new SckwLogisticsOrder();
        lOrder.setLoadTime(logisticsOrder.getLoadTime());
        lOrder.setLoadAmount(logisticsOrder.getLoadAmount());
        editSckwLogisticsOrder(lOrder, logisticsOrder);

        /**6发送消息**/

        return HttpResult.ok("装货成功！");
    }

    /**
     * @param params {}
     * @desc 到达卸货地点
     * @author zk
     * @date 2023/7/26
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult arriveUnloadLocation(OperateWaybillDto params){
        /**数据校验**/
        KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
        if (waybillOrder == null) {
            return HttpResult.error("车辆运单不存在！");
        }
        //已装货车辆运单才能到达卸货地点
        if (waybillOrder.getStatus() != CarWaybillEnum.COMPLETION_LOADING.getCode()) {
            return HttpResult.error("车辆运单当前状态已不能到达卸货地点！");
        }

        /**业务处理**/
        /**1更新车辆运单**/
        waybillOrder.setStatus(CarWaybillEnum.WAIT_UNLOADING.getCode());
        waybillOrderDao.updateById(waybillOrder);

        /**2新增运单状态记录**/
        setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

        /**3Mongodb数据更新**/
        //1车辆运单
        SckwWaybillOrder wOrder = new SckwWaybillOrder();
        wOrder.set_id(waybillOrder.getId());
        editSckwWaybillOrder(wOrder, waybillOrder, 2);

        /**4发送消息**/

        return HttpResult.ok("到达卸货地点成功！");
    }

    /**
     * @param params {}
     * @desc 卸货
     * @author zk
     * @date 2023/7/26
     **/
    @Transactional(rollbackFor = Exception.class)
    public HttpResult unloading(@RequestBody @Valid WaybillOrderTicketDto params){
        /**数据校验**/
        KwtWaybillOrder waybillOrder = waybillOrderDao.selectById(params.getWOrderId());
        if (waybillOrder == null) {
            return HttpResult.error("车辆运单不存在！");
        }
        //到达卸货地车辆运单才能卸货
        if (waybillOrder.getStatus() != CarWaybillEnum.WAIT_UNLOADING.getCode()) {
            return HttpResult.error("车辆运单当前状态已不能装货！");
        }

        /**业务处理**/
        /**1更新车辆运单**/
        waybillOrder.setUnloadAmount(params.getAmount());
        waybillOrder.setDeficitAmount(waybillOrder.getLoadAmount().subtract(waybillOrder.getUnloadAmount()));
        waybillOrder.setStatus(CarWaybillEnum.COMPLETION_UNLOADING.getCode());
        waybillOrderDao.updateById(waybillOrder);

        /**2新增运单状态记录**/
        setWaybillOrderTrack(waybillOrder.getId(), waybillOrder.getStatus(), params.getRemark());

        /**3新增装卸货榜单信息**/
        KwtWaybillOrderTicket ticket = new KwtWaybillOrderTicket();
        ticket.setWOrderId(waybillOrder.getId());
        ticket.setType(Global.NUMERICAL_TWO);
        ticket.setAmount(waybillOrder.getUnloadAmount());
        ticket.setUrls(params.getUrls());
        waybillOrderTicketDao.insert(ticket);

        /**4更新承运订单entrustAmount**/
        //更新卸货量/卸货时间/亏吨/亏吨扣款
        KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(waybillOrder.getLOrderId());
        //承运订单已卸货量=承运订单原卸货量+当前车辆运单卸货量
        BigDecimal unloadAmount = logisticsOrder.getUnloadAmount() == null ? new BigDecimal(Global.AMOUNT) : logisticsOrder.getUnloadAmount();
        logisticsOrder.setUnloadAmount(unloadAmount.add(waybillOrder.getUnloadAmount()));
        logisticsOrder.setUnloadTime(waybillOrder.getUpdateTime());
        logisticsOrder.setDeficitAmount(logisticsOrder.getLoadAmount().subtract(logisticsOrder.getUnloadAmount()));
        logisticsOrder.setDeficitPrice(null);
        logisticsOrderDao.updateById(logisticsOrder);

        /**5Mongodb数据更新**/
        //1车辆运单
        SckwWaybillOrder wOrder = new SckwWaybillOrder();
        wOrder.set_id(waybillOrder.getId());
        wOrder.setUnloadAmount(waybillOrder.getUnloadAmount());
        wOrder.setUnloadUrls(params.getUrls());
        wOrder.setUnloadTime(waybillOrder.getUpdateTime());
        editSckwWaybillOrder(wOrder, waybillOrder, 2);

        //2承运订单
        SckwLogisticsOrder lOrder = new SckwLogisticsOrder();
        lOrder.setUnloadTime(logisticsOrder.getUnloadTime());
        lOrder.setUnloadAmount(logisticsOrder.getUnloadAmount());
        editSckwLogisticsOrder(lOrder, logisticsOrder);

        /**6发送消息**/

        return HttpResult.ok("装货成功！");
    }

}
