package cn.iocoder.yudao.module.order.service.merge;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.apollo.core.event.box.BoxCheckOrderSchedulingEvent;
import cn.iocoder.yudao.framework.apollo.core.event.Order.OrderApprovalTypeCheckEvent;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.dict.core.dto.DictDataRespDTO;
import cn.iocoder.yudao.framework.dict.core.util.DictFrameworkUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.service.AbstractService;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.mybatis.core.vo.PageVO;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.bpm.api.BpmCreateServiceFactory;
import cn.iocoder.yudao.module.bpm.enums.WorkFlowEmus;
import cn.iocoder.yudao.module.ecw.dal.dataobject.region.RegionDO;
import cn.iocoder.yudao.module.ecw.service.region.RegionService;
import cn.iocoder.yudao.module.order.convert.merge.MergeConvert;
import cn.iocoder.yudao.module.order.convert.order.OrderConvert;
import cn.iocoder.yudao.module.order.convert.orderConsignee.OrderConsigneeConvert;
import cn.iocoder.yudao.module.order.convert.orderConsignor.OrderConsignorConvert;
import cn.iocoder.yudao.module.order.convert.orderDeparture.OrderDepartureConvert;
import cn.iocoder.yudao.module.order.convert.orderObjective.OrderObjectiveConvert;
import cn.iocoder.yudao.module.order.dal.dataobject.approval.OrderApprovalDO;
import cn.iocoder.yudao.module.order.dal.dataobject.merge.MergeDO;
import cn.iocoder.yudao.module.order.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderFeeApplication.OrderFeeApplicationDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderItem.OrderItemDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderLocation.OrderLocationDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderSpecialNeed.OrderSpecialNeedDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderTime.OrderTimeDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderWarehouseIn.OrderWarehouseInDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderWarehousePicture.OrderWarehousePictureDO;
import cn.iocoder.yudao.module.order.dal.mysql.merge.MergeMapper;
import cn.iocoder.yudao.module.order.dto.OrderBackInfoDto;
import cn.iocoder.yudao.module.order.enums.ApprovalResultStatusEnum;
import cn.iocoder.yudao.module.order.enums.OrderApprovalTypeResultEnum;
import cn.iocoder.yudao.module.order.enums.OrderStatusEnum;
import cn.iocoder.yudao.module.order.enums.OrderWarehouseInStatusEnum;
import cn.iocoder.yudao.module.order.service.approval.OrderApprovalService;
import cn.iocoder.yudao.module.order.service.order.*;
import cn.iocoder.yudao.module.order.service.orderCargoControl.OrderCargoControlService;
import cn.iocoder.yudao.module.order.service.orderWarehouseIn.OrderWarehouseInService;
import cn.iocoder.yudao.module.order.service.orderWarehousePicture.OrderWarehousePictureService;
import cn.iocoder.yudao.module.order.service.targetLog.TargetLogService;
import cn.iocoder.yudao.module.order.vo.approval.OrderApprovalQueryVO;
import cn.iocoder.yudao.module.order.vo.merge.*;
import cn.iocoder.yudao.module.order.vo.order.OrderBackVO;
import cn.iocoder.yudao.module.order.vo.orderItem.OrderItemQueryVO;
import cn.iocoder.yudao.module.product.dto.FeeDto;
import cn.iocoder.yudao.module.wealth.dal.dataobject.receivable.ReceivableDO;
import cn.iocoder.yudao.module.wealth.service.receivable.ReceivableService;
import cn.iocoder.yudao.module.wealth.vo.receivable.ReceivableCreateReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.order.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.order.enums.OrderApprovalTypeEnum.*;

/**
 * 订单合单申请 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class MergeServiceImpl extends AbstractService<MergeMapper, MergeDO> implements MergeService {

    @Resource
    private MergeMapper mergeMapper;

    @Resource
    private OrderService orderService;

    @Resource
    private OrderBusinessService orderBusinessService;
    @Resource
    private OrderQueryService orderQueryService;

    @Resource
    private RegionService regionService;

    @Resource
    private OrderItemService orderItemService;

    @Resource
    private BpmCreateServiceFactory bpmCreateServiceFactory;

    @Resource
    private OrderTimeService orderTimeService;

    @Resource
    private TargetLogService targetLogService;

    @Resource
    private OrderCargoControlService orderCargoControlService;

    @Resource
    private ReceivableService receivableService;

    @Resource
    private OrderWarehouseInService orderWarehouseInService;

    @Resource
    private OrderApprovalService orderApprovalService;

    @Resource
    private OrderSpecialNeedService orderSpecialNeedService;

    @Resource
    private OrderFeeApplicationService orderFeeApplicationService;

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private OrderLocationService orderLocationService;

    @Resource
    private OrderGuanlianService orderGuanlianService;

    @Resource
    private OrderWarehousePictureService orderWarehousePictureService;

    @Override
    @Transactional
    public void createMerge(List<MergeCreateReqVO> createReqVOS) {
        List<OrderDO> approvalOrderList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(createReqVOS)) {
            List<String> collect = createReqVOS.stream().map(MergeBaseVO::getOrderNos).collect(Collectors.toList());
            collect.add(createReqVOS.get(0).getApplyOrderNo());

            //判断这些订单是否已经已出
            checkOrderCanOut(collect);
            for (String item : collect) {
                OrderDO one = orderService.getOne(new LambdaQueryWrapperX<OrderDO>().eq(OrderDO::getOrderNo, item));
                applicationContext.publishEvent(new BoxCheckOrderSchedulingEvent(one.getOrderId()));
                if (one.getAbnormalState() != 0) {
                    throw exception(ORDER_HAS_PENDING_EXCEPTION, one.getOrderNo());
                }
                OrderApprovalTypeCheckEvent approvalTypeCheckEvent = new OrderApprovalTypeCheckEvent(one.getOrderId(), null, merge_order.getValue(), null, false);
                applicationContext.publishEvent(approvalTypeCheckEvent);
                if (approvalTypeCheckEvent.getResult()) {
                    throw exception(ORDER_HAS_PROCESSING_APPROVAL, one.getOrderNo());
                }
                approvalOrderList.add(one);
            }
        }
        OrderDO one = orderService.getOne(new LambdaQueryWrapperX<OrderDO>().eq(OrderDO::getOrderNo, createReqVOS.get(0).getApplyOrderNo()));
        applicationContext.publishEvent(new BoxCheckOrderSchedulingEvent(one.getOrderId()));
        List<MergeCreateReqVO> collect = createReqVOS.stream().filter(item -> mergeMapper.selectCount(new LambdaQueryWrapperX<MergeDO>()
                .eq(MergeDO::getApplyOrderNo, item.getApplyOrderNo())
                .ne(MergeDO::getStatus, 3)
                .eq(MergeDO::getOrderNos, item.getOrderNos())) > 0L).collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            throw exception(MERGE_EXISTS);
        }
        Long id = 0L;
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        Date now = new Date();
        List<MergeDO> mergeDOList = new ArrayList<>();
        for (MergeCreateReqVO createReqVO : createReqVOS) {
            MergeDO merge = MergeConvert.INSTANCE.convert(createReqVO);
            merge.setAddTime(now);
            mergeMapper.insert(merge);
            id = merge.getId();
            mergeDOList.add(merge);
        }
        //创建审批流，这里只保存随便一个合并订单记录的主键id
        String bpmProcessId = bpmCreateServiceFactory.createBmp(userId, id, WorkFlowEmus.ORDER_MERGE_ORDER.getKey(), createReqVOS.get(0).getApplyOrderNo(), createReqVOS.get(0).getCopyUserId());
        for (MergeDO merge : mergeDOList) {
            mergeMapper.update(null, new LambdaUpdateWrapper<MergeDO>()
                    .eq(MergeDO::getId, merge.getId())
                    .set(MergeDO::getBpmProcessId, bpmProcessId));
        }
        for (OrderDO orderDO : approvalOrderList) {
            //创建审批单，发起流程
            OrderApprovalDO orderApprovalDO = new OrderApprovalDO();
            orderApprovalDO.setFollowUpSalesmanId(String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
            orderApprovalDO.setOrderId(orderDO.getOrderId());
            //批量费用申请
            orderApprovalDO.setType(merge_order.getValue());
            orderApprovalDO.setStatus(ApprovalResultStatusEnum.processing.getValue());
            orderApprovalDO.setFormId(bpmProcessId);
            orderApprovalService.save(orderApprovalDO);
            orderService.updateStatus(orderDO.getOrderId(), null, OrderStatusEnum.IN_WAREHOUSE.getValue(), null, OrderWarehouseInStatusEnum.order_merge_applying.getValue(), null, merge_order.getValue(), OrderApprovalTypeResultEnum.merge_order_processing.getDesc());
        }
    }

    private void checkOrderCanOut(List<String> orderNoList) {
        List<OrderDO> orderList = orderQueryService.getOrderListByOrderNoList(orderNoList);
        long count = orderList.stream()
                .filter(t -> t.getAirShipment() > 1)
                .count();
        if (count > 0) {
            throw new ServiceException(500, "订单已出，不能合单");
        }
    }

    @Override
    public void updateMerge(MergeUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateMergeExists(updateReqVO.getId());
        // 更新
        MergeDO updateObj = MergeConvert.INSTANCE.convert(updateReqVO);
        mergeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMerge(Long id) {
        // 校验存在
        this.validateMergeExists(id);
        // 删除
        mergeMapper.deleteById(id);
    }

    private void validateMergeExists(Long id) {
        if (mergeMapper.selectById(id) == null) {
            throw exception(MERGE_NOT_EXISTS);
        }
    }

    @Override
    public MergeDO getMerge(Long id) {
        return mergeMapper.selectById(id);
    }

    @Override
    public List<MergeDO> getMergeList(Collection<Long> ids) {
        return mergeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MergeDO> getMergePage(MergeQueryVO query, PageVO page) {
        return mergeMapper.selectPage(page, query);
    }

    @Override
    public List<MergeDO> getMergeList(MergeQueryVO query) {
        return mergeMapper.selectList(query);
    }

    @Override
    public Map<String, Object> getMergeListByOrderNo(String mergedOrderNo, PageVO pageVO) {
        Map<String, Object> map = new HashMap<>(4);
        List<MergeBackVO> backVOList = new ArrayList<>();
        List<MergeDO> mergeDOS = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>().eq(MergeDO::getMergedOrderNo, mergedOrderNo)
                .eq(MergeDO::getStatus, 2));
        map.put("orderInfo", orderQueryService.info(orderService.selectOne(OrderDO::getOrderNo, mergedOrderNo).getOrderId()));
        if (mergeDOS != null && mergeDOS.size() > 0) {
            List<MergeDO> mergeDOS1 = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>().and(i -> i.eq(MergeDO::getApplyOrderNo, mergedOrderNo)
                            .or().eq(MergeDO::getOrderNos, mergedOrderNo))
                    .eq(MergeDO::getStatus, 1));
            if (!CollectionUtil.isEmpty(mergeDOS1)) {
                map.put("bpmProcessId", mergeDOS1.get(0).getBpmProcessId());
            }
            OrderBackVO applyOrder = orderQueryService.getOrderByNo(mergeDOS.get(0).getApplyOrderNo());
            MergeBackVO applyConvertor = MergeConvert.INSTANCE.convert(mergeDOS.get(0));
            applyConvertor.setMergedOrder(applyOrder);
            backVOList.add(applyConvertor);
            for (MergeDO mergeDO : mergeDOS) {
                MergeBackVO convert = MergeConvert.INSTANCE.convert(mergeDO);
                OrderBackVO order = orderQueryService.getOrderByNo(mergeDO.getOrderNos());
                convert.setMergedOrder(order);
                backVOList.add(convert);
            }
            map.put("mergedList", backVOList);
            map.put("unMergeList", getUnMergedOrder(mergedOrderNo, pageVO));
        } else {
            List<MergeDO> mergeDOS1 = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>().and(i -> i.eq(MergeDO::getApplyOrderNo, mergedOrderNo)
                            .or().eq(MergeDO::getOrderNos, mergedOrderNo))
                    .eq(MergeDO::getStatus, 1));
            if (!CollectionUtil.isEmpty(mergeDOS1)) {
                map.put("bpmProcessId", mergeDOS1.get(0).getBpmProcessId());
            }
            map.put("unMergeList", getUnMergedOrder(mergedOrderNo, pageVO));
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrderMergeResult(String id, int result) {
        MergeDO mergeDO = mergeMapper.selectById(id);
        // 需要先更新订单合单审批信息
        OrderApprovalQueryVO query = new OrderApprovalQueryVO();
        query.setFormId(mergeDO.getBpmProcessId());
        List<OrderApprovalDO> approvalDOList = orderApprovalService.getApprovalList(query);
        List<Long> mergeOrderIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(approvalDOList)) {
            for (OrderApprovalDO it : approvalDOList) {
                it.setStatus(result);
                mergeOrderIdList.add(it.getOrderId());
            }
            orderApprovalService.updateBatchById(approvalDOList);
        }
        //审批拒绝则修改所有流程编号一致的合单申请状态
        if (result == 3) {
            List<MergeDO> mergeDOS = mergeMapper.selectList(MergeDO::getBpmProcessId, mergeDO.getBpmProcessId());
            Integer type = OrderApprovalTypeResultEnum.merge_order_reject.getType();
            for (MergeDO mergeDO1 : mergeDOS) {
                mergeDO1.setStatus(result);
                mergeMapper.updateById(mergeDO1);
            }
            for (Long orderId : mergeOrderIdList) {
                orderService.updateStatus(orderId, null, OrderStatusEnum.IN_WAREHOUSE.getValue(), null, OrderWarehouseInStatusEnum.warehouse_finished.getValue(), null, 0, OrderApprovalTypeResultEnum.typeAndResultOf(type, 3).getDesc());
            }
        } else if (result == 2) {
            //审批通过，需考虑分多次提交的合单申请
            List<MergeDO> mergeDOS = new ArrayList<>();
            if (mergeDO.getOrderNos().endsWith("S") || mergeDO.getApplyOrderNo().endsWith("S")) {
                mergeDOS = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>()
                        .eq(MergeDO::getStatus, 2)
                        .and(i ->
                                i.eq(MergeDO::getMergedOrderNo, mergeDO.getApplyOrderNo())
                                        .or()
                                        .eq(MergeDO::getMergedOrderNo, mergeDO.getOrderNos()))
                );
            }
            if (mergeDOS != null && mergeDOS.size() > 0) {//审批通过且已有合并订单
                //已合并的S订单
                String mergedOrderNo = mergeDOS.get(0).getMergedOrderNo();
                OrderBackVO mergedOrderDO = orderQueryService.getOrder(orderService.selectOne(OrderDO::getOrderNo, mergedOrderNo).getOrderId());//已合并订单S

                List<ReceivableDO> receivableList = receivableService.selectList(ReceivableDO::getOrderId, mergedOrderDO.getOrderId());
                receivableList.stream().forEach(item -> {
                    if (item.getState() != 0) {
                        throw exception(ORDER_MERGE_ERROR);
                    }
                });
                OrderDO orderDO = OrderConvert.INSTANCE.convert(mergedOrderDO);
                orderDO.setOrderConsigneeDO(OrderConsigneeConvert.INSTANCE.convert(mergedOrderDO.getConsigneeVO()));
                orderDO.setOrderConsignorDO(OrderConsignorConvert.INSTANCE.convert(mergedOrderDO.getConsignorVO()));
                orderDO.setOrderDepartureDO(OrderDepartureConvert.INSTANCE.convert(mergedOrderDO.getOrderDepartureVO()));
                orderDO.setOrderObjectiveDO(OrderObjectiveConvert.INSTANCE.convert(mergedOrderDO.getOrderObjectiveVO()));
                orderDO.setOrderCargoControlDO(orderCargoControlService.getActualOrderCargoControlByOrderId(mergedOrderDO.getOrderId()));
                List<MergeDO> mergeDOList = mergeMapper.selectList(MergeDO::getBpmProcessId, mergeDO.getBpmProcessId());
                int sumNum = orderDO.getSumNum();
                BigDecimal sumVolume = orderDO.getSumVolume();
                BigDecimal sumWeight = orderDO.getSumWeight();
                OrderItemQueryVO orderItemQueryVO = new OrderItemQueryVO();
                orderItemQueryVO.setOrderId(orderDO.getOrderId());
                List<OrderItemDO> orderItemDOS = orderItemService.getOrderItemList(orderItemQueryVO);
                StringBuilder sb = new StringBuilder(orderDO.getOldNumbers());
                Integer type = OrderApprovalTypeResultEnum.merge_order_pass.getType();
                List<OrderSpecialNeedDO> specialNeedList = new ArrayList<>();
                List<OrderWarehouseInDO> orderWarehouseInList = new ArrayList<>();
                List<OrderFeeApplicationDO> feeApplicationList = new ArrayList<>();
                List<OrderLocationDO> orderLocationList = new ArrayList<>();
                List<OrderWarehousePictureDO> warehousePictureDOList = new ArrayList<>();
                Set<String> packageTypeSet = new HashSet<>();
                if (StringUtils.isNotBlank(mergedOrderDO.getPackageType())) {
                    packageTypeSet.addAll(Arrays.asList(mergedOrderDO.getPackageType().split(StrUtil.COMMA)));
                }
                for (MergeDO mergeDO1 : mergeDOList) {
                    //合并 已合并订单S和这个流程中的所有订单的入仓箱数  体积  重量等
                    String orderNo = "";
                    if (!mergeDO1.getApplyOrderNo().endsWith("S")) {
                        orderNo = mergeDO1.getApplyOrderNo();
                    } else {
                        orderNo = mergeDO1.getOrderNos();
                    }
                    OrderDO orderDO1 = orderService.selectOne(OrderDO::getOrderNo, orderNo);
                    BigDecimal oldWeightUnit = orderDO1.getWeightUnit() == null ? new BigDecimal("0") : orderDO1.getWeightUnit();
                    BigDecimal newWeightUnit = orderDO.getWeightUnit() == null ? new BigDecimal("0") : orderDO.getWeightUnit();
                    BigDecimal oldVolumeUnit = orderDO1.getVolumeUnit() == null ? new BigDecimal("0") : orderDO1.getVolumeUnit();
                    BigDecimal newVolumeUnit = orderDO.getVolumeUnit() == null ? new BigDecimal("0") : orderDO.getVolumeUnit();
                    if (oldWeightUnit.compareTo(newWeightUnit) > 0) {
                        orderDO.setWeightUnit(oldWeightUnit);
                    }
                    if (oldVolumeUnit.compareTo(newVolumeUnit) > 0) {
                        orderDO.setVolumeUnit(oldVolumeUnit);
                    }
                    List<ReceivableDO> receivableListByOrderId = receivableService.selectList(ReceivableDO::getOrderId, orderDO1.getOrderId());
                    receivableListByOrderId.stream().forEach(item -> {
                        if (item.getState() != 0) {
                            throw exception(ORDER_MERGE_ERROR);
                        }
                    });
                    sb.append("," + orderNo);
                    sumNum = sumNum + orderDO1.getSumNum();
                    sumVolume.add(orderDO1.getSumVolume());
                    sumWeight.add(orderDO1.getSumWeight());
                    if (StringUtils.isNotBlank(orderDO1.getPackageType())) {
                        packageTypeSet.addAll(Arrays.asList(orderDO1.getPackageType().split(StrUtil.COMMA)));
                    }
                    List<OrderItemDO> orderItemDOS1 = orderItemService.selectList(OrderItemDO::getOrderId, orderDO1.getOrderId());
                    List<Long> orderItemIds = orderItemDOS1.stream().map(OrderItemDO::getOrderItemId).collect(Collectors.toList());
                    orderItemDOS1.stream().forEach(ii -> {
                        ii.setOrgItemId(ii.getOrderItemId());
                        ii.setOrderId(null);
                        ii.setOrderItemId(null);
                    });
                    orderItemDOS.addAll(orderItemDOS1);
                    mergeDO1.setStatus(result);
                    mergeDO1.setMergedOrderNo(mergedOrderNo);
                    orderGuanlianService.deleteRelationByOrderId(orderDO1.getOrderId());
                    mergeMapper.updateById(mergeDO1);
                    receivableService.remove(new LambdaQueryWrapperX<ReceivableDO>().eq(ReceivableDO::getOrderId, orderDO1.getOrderId()).eq(ReceivableDO::getFeeSource, 1));
                    //修改入仓数据
                    orderWarehouseInList.addAll(orderWarehouseInService.selectList(OrderWarehouseInDO::getOrderId, orderDO1.getOrderId()));
                    specialNeedList.addAll(orderSpecialNeedService.getListByOrderId(orderDO1.getOrderId()));
                    feeApplicationList.addAll(orderFeeApplicationService.getFeeApplicationListByOrderId(orderDO1.getOrderId()));
                    orderLocationList.addAll(orderLocationService.getOrderLocationListByOrderId(orderDO1.getOrderId()));
                    List<OrderWarehousePictureDO> orderWarehousePictureDOS = orderWarehousePictureService.selectList(new LambdaQueryWrapperX<OrderWarehousePictureDO>()
                            .eq(OrderWarehousePictureDO::getType, 5)
                            .in(OrderWarehousePictureDO::getBizId, orderItemIds)
                    );
                    warehousePictureDOList.addAll(orderWarehousePictureDOS);
                }
                for (Long orderId : mergeOrderIdList) {
                    orderService.updateStatus(orderId, null, OrderStatusEnum.MERGED_ORDER.getValue(), null, OrderWarehouseInStatusEnum.order_merge_apply_finish.getValue(), null, 0, OrderApprovalTypeResultEnum.typeAndResultOf(type, 2).getDesc());
                }
                orderDO.setOldNumbers(sb.toString());
                orderDO.setSumNum(sumNum);
                orderDO.setSumVolume(sumVolume);
                orderDO.setSumWeight(sumWeight);
                orderDO.setOrderItemDOList(orderItemDOS);
                StringBuffer stringBuffer = null;
                List<String> packageTypeList = new ArrayList<>(packageTypeSet);
                for (String s : packageTypeList) {
                    if (Objects.isNull(stringBuffer)) {
                        stringBuffer = new StringBuffer();
                        stringBuffer.append(s);
                    } else {
                        stringBuffer.append(StrUtil.COMMA).append(s);
                    }
                }
                if (Objects.nonNull(stringBuffer)) {
                    orderDO.setPackageType(stringBuffer.toString());
                }
                receivableService.remove(new LambdaQueryWrapperX<ReceivableDO>().eq(ReceivableDO::getOrderId, orderDO.getOrderId()).eq(ReceivableDO::getFeeSource, 1));
                //修改合并订单
                orderService.mergeOrder(orderDO);
                orderService.updateStatus(null, orderDO.getOrderNo(), OrderStatusEnum.IN_WAREHOUSE.getValue(), null, OrderWarehouseInStatusEnum.warehouse_finished.getValue(), null, 0, OrderApprovalTypeResultEnum.typeAndResultOf(type, 2).getDesc());
                // 删除原单的特需费用应收记录
                specialNeedList.stream().forEach(item -> {
                    if (item.getReceivableId() != null && item.getReceivableId() > 0L) {
                        receivableService.removeById(item.getReceivableId());
                    }
                });
                //订单项入仓和货物信息处理
                Map<String, Long> itemIdMap = orderDO.getOrderItemDOList().stream().collect(Collectors.toMap(ii -> String.valueOf(ii.getOrgItemId()), OrderItemDO::getOrderItemId, (key1, key2) -> key1));
                orderWarehouseInList.stream().forEach(item -> {
                    item.setOrderId(orderDO.getOrderId());
                    item.setOrderNo(orderDO.getOrderNo());
                    item.setOrderItemId(itemIdMap.get(String.valueOf(item.getOrderItemId())));
                });
                orderWarehouseInService.dealMergeOrderWarehouseInAndLocation(orderWarehouseInList);
                //处理入仓影像后批量插入
                warehousePictureDOList.stream().forEach(item -> {
                    item.setId(null);
                    item.setBizId(itemIdMap.get(String.valueOf(item.getBizId())));
                });
                orderWarehousePictureService.insertBatch(warehousePictureDOList);

                //处理费用申请，新增新订单的费用申请
                feeApplicationList.stream().forEach(item -> {
                    item.setOrderId(orderDO.getOrderId());
                    item.setId(null);
                    if (item.getReceivableId() != null && item.getReceivableId() > 0) {
                        receivableService.deleteReceivable(item.getReceivableId().longValue());
                    }
                    item.setReceivableId(null);
                });
                orderFeeApplicationService.insertBatch(feeApplicationList);
                feeApplicationCreateReceivable(feeApplicationList);

                //处理新订单的储位
//                orderLocationList.stream().forEach(item -> {
//                    item.setOrderId(orderDO.getOrderId());
//                    item.setId(null);
////                    orderLocationService.updateById(item);
//                });
//                orderLocationService.insertBatch(orderLocationList);
            } else {//审批通过且没有合并订单
                //申请的原订单
                OrderDO orderDO2 = orderService.selectOne(OrderDO::getOrderNo, mergeDO.getApplyOrderNo());
                //查询原订单应收状态，如果有已核销的应收则不允许合单，不然没办法退钱
                List<ReceivableDO> receivableListByOrderId = receivableService.selectList(ReceivableDO::getOrderId, orderDO2.getOrderId());
                receivableListByOrderId.stream().forEach(item -> {
                    if (item.getState() != 0) {
                        throw exception(ORDER_MERGE_ERROR);
                    }
                });
                //设置合单后的新订单的字段为原申请单的字段
                OrderBackVO orderBackVO = orderQueryService.getOrder(orderDO2.getOrderId());
                OrderDO orderDO = OrderConvert.INSTANCE.convert(orderBackVO);
                orderDO.setOrderConsigneeDO(OrderConsigneeConvert.INSTANCE.convert(orderBackVO.getConsigneeVO()));
                orderDO.setOrderConsignorDO(OrderConsignorConvert.INSTANCE.convert(orderBackVO.getConsignorVO()));
                orderDO.setOrderDepartureDO(OrderDepartureConvert.INSTANCE.convert(orderBackVO.getOrderDepartureVO()));
                orderDO.setOrderObjectiveDO(OrderObjectiveConvert.INSTANCE.convert(orderBackVO.getOrderObjectiveVO()));
                orderDO.setOrderCargoControlDO(orderCargoControlService.getActualOrderCargoControlByOrderId(orderDO2.getOrderId()));
                orderDO.setNumber(orderDO2.getNumber());
                RegionDO region = regionService.getRegion(orderBackVO.getOrderObjectiveVO().getObjectiveId());
                String mergedOrderNo = orderBusinessService.saveOrUpdateOrderNo(region, orderDO.getTransportId(), orderDO.getIsExternalWarehouse(),
                        StringUtils.isNotBlank(orderDO.getType()) && orderDO.getType().contains("1"));
                String newOrderNo = mergedOrderNo.substring(0, mergedOrderNo.length() - 1) + "S";//新订单号
                orderDO.setOrderNo(newOrderNo);
                //将几个id设置成空  生成新的收货 发货人和始发 目的信息
                orderDO.getOrderDepartureDO().setId(null);
                orderDO.getOrderConsigneeDO().setId(null);
                orderDO.getOrderConsignorDO().setId(null);
                orderDO.getOrderObjectiveDO().setId(null);
                orderDO.setGuanLianOrderStatus(null);
                if (orderDO.getOrderCargoControlDO() != null) {
                    orderDO.getOrderCargoControlDO().setId(null);
                }
                orderDO.setOrderId(null);
                orderDO.setStatus(OrderStatusEnum.MERGED_ORDER.getValue());//已合单
                orderDO.setInWarehouseState(OrderWarehouseInStatusEnum.warehouse_finished.getValue());
                Integer type1 = OrderApprovalTypeResultEnum.merge_order_pass.getType();
                orderDO.setAuditType(type1);
                orderDO.setAuditResult(OrderApprovalTypeResultEnum.typeAndResultOf(type1, 2).getDesc());
                Set<String> packageTypeSet = new HashSet<>();
                if (StringUtils.isNotBlank(orderDO2.getPackageType())) {
                    packageTypeSet.addAll(Arrays.asList(orderDO2.getPackageType().split(StrUtil.COMMA)));
                }
                //计算箱数  体积  重量等信息
                int sumNum = orderDO.getSumNum();
                BigDecimal sumVolume = orderDO.getSumVolume();
                BigDecimal sumWeight = orderDO.getSumWeight();
                List<MergeDO> mergeDOList = mergeMapper.selectList(MergeDO::getBpmProcessId, mergeDO.getBpmProcessId());
                OrderItemQueryVO orderItemQueryVO = new OrderItemQueryVO();
                orderItemQueryVO.setOrderId(orderDO2.getOrderId());
                List<OrderItemDO> orderItemDOS = orderItemService.getOrderItemList(orderItemQueryVO);
                List<Long> orderItemIds = orderItemDOS.stream().map(OrderItemDO::getOrderItemId).collect(Collectors.toList());
                orderItemDOS.stream().forEach(item -> {
                    item.setOrgItemId(item.getOrderItemId());
                    item.setOrderId(null);
                    item.setOrderItemId(null);
                });
                StringBuilder sb = new StringBuilder();
                sb.append(mergeDO.getApplyOrderNo()).append(",");
                List<Long> orderIds = new ArrayList<>();
                Integer type = OrderApprovalTypeResultEnum.merge_order_pass.getType();
                List<OrderWarehouseInDO> orderWarehouseInList = orderWarehouseInService.selectList(OrderWarehouseInDO::getOrderId, orderDO2.getOrderId());
                List<OrderSpecialNeedDO> specialNeedList = orderSpecialNeedService.getListByOrderId(orderDO2.getOrderId());
                List<OrderFeeApplicationDO> feeApplicationList = orderFeeApplicationService.getFeeApplicationListByOrderId(orderDO2.getOrderId());
                List<OrderLocationDO> orderLocationList = orderLocationService.getOrderLocationListByOrderId(orderDO2.getOrderId());
                List<OrderWarehousePictureDO> warehousePictureList = orderWarehousePictureService.selectList(new LambdaQueryWrapper<OrderWarehousePictureDO>()
                        .eq(OrderWarehousePictureDO::getType, 5)
                        .in(OrderWarehousePictureDO::getBizId, orderItemIds)
                );
                //处理和原订单合并的其他订单的信息
                for (MergeDO mergeDO1 : mergeDOList) {
                    //合并 已合并订单S和这个流程中的所有订单的入仓箱数  体积  重量等
                    OrderDO orderDO1 = orderService.selectOne(OrderDO::getOrderNo, mergeDO1.getOrderNos());
                    BigDecimal oldWeightUnit = orderDO1.getWeightUnit() == null ? new BigDecimal("0") : orderDO1.getWeightUnit();
                    BigDecimal newWeightUnit = orderDO.getWeightUnit() == null ? new BigDecimal("0") : orderDO.getWeightUnit();
                    BigDecimal oldVolumeUnit = orderDO1.getVolumeUnit() == null ? new BigDecimal("0") : orderDO1.getVolumeUnit();
                    BigDecimal newVolumeUnit = orderDO.getVolumeUnit() == null ? new BigDecimal("0") : orderDO.getVolumeUnit();
                    if (oldWeightUnit.compareTo(newWeightUnit) > 0) {
                        orderDO.setWeightUnit(oldWeightUnit);
                    }
                    if (oldVolumeUnit.compareTo(newVolumeUnit) > 0) {
                        orderDO.setVolumeUnit(oldVolumeUnit);
                    }
                    //判断子订单的应收是否有已核销  有的话不让合单
                    List<ReceivableDO> receivableBackVOS = receivableService.selectList(ReceivableDO::getOrderId, orderDO2.getOrderId());
                    receivableBackVOS.stream().forEach(item -> {
                        if (item.getState() != 0) {
                            throw exception(ORDER_MERGE_ERROR);
                        }
                    });
                    sb.append(mergeDO1.getOrderNos() + ",");
                    if (StringUtils.isNotBlank(orderDO1.getPackageType())) {
                        packageTypeSet.addAll(Arrays.asList(orderDO1.getPackageType().split(StrUtil.COMMA)));
                    }
                    sumNum = sumNum + orderDO1.getSumNum();
                    sumVolume = sumVolume.add(orderDO1.getSumVolume());
                    sumWeight = sumWeight.add(orderDO1.getSumWeight());
                    //设置订单项的id为空  用于新增合单的子订单
                    List<OrderItemDO> orderItemDOS1 = orderItemService.selectList(OrderItemDO::getOrderId, orderDO1.getOrderId());
                    List<Long> orderItemIds1 = orderItemDOS1.stream().map(OrderItemDO::getOrderItemId).collect(Collectors.toList());
                    orderItemDOS1.stream().forEach(item -> {
                        item.setOrgItemId(item.getOrderItemId());
                        item.setOrderId(null);
                        item.setOrderItemId(null);
                    });
                    orderItemDOS.addAll(orderItemDOS1);
                    mergeDO1.setStatus(result);
                    mergeDO1.setMergedOrderNo(newOrderNo);
                    orderGuanlianService.deleteRelationByOrderId(orderDO1.getOrderId());
                    mergeMapper.updateById(mergeDO1);
                    orderIds.add(orderDO1.getOrderId());
                    receivableService.remove(new LambdaQueryWrapperX<ReceivableDO>().eq(ReceivableDO::getOrderId, orderDO1.getOrderId()).eq(ReceivableDO::getFeeSource, 1));
                    //合并除订单外的其他数据  TODO 入仓数据怎么处理？  复制原来的加上订单id来新增？
                    orderWarehouseInList.addAll(orderWarehouseInService.selectList(OrderWarehouseInDO::getOrderId, orderDO1.getOrderId()));
                    specialNeedList.addAll(orderSpecialNeedService.getListByOrderId(orderDO1.getOrderId()));
                    feeApplicationList.addAll(orderFeeApplicationService.getFeeApplicationListByOrderId(orderDO1.getOrderId()));
                    orderLocationList.addAll(orderLocationService.getOrderLocationListByOrderId(orderDO1.getOrderId()));
                    List<OrderWarehousePictureDO> orderWarehousePictureDOS = orderWarehousePictureService.selectList(new LambdaQueryWrapperX<OrderWarehousePictureDO>()
                            .eq(OrderWarehousePictureDO::getType, 5)
                            .in(OrderWarehousePictureDO::getBizId, orderItemIds1)
                    );
                    warehousePictureList.addAll(orderWarehousePictureDOS);
                }

                receivableService.remove(new LambdaQueryWrapperX<ReceivableDO>().eq(ReceivableDO::getOrderId, orderDO2.getOrderId()).eq(ReceivableDO::getFeeSource, 1));
                orderGuanlianService.deleteRelationByOrderId(orderDO2.getOrderId());
                for (Long orderId : mergeOrderIdList) {
                    orderService.updateStatus(orderId, null, OrderStatusEnum.MERGED_ORDER.getValue(), null, OrderWarehouseInStatusEnum.order_merge_apply_finish.getValue(), null, 0, OrderApprovalTypeResultEnum.typeAndResultOf(type, 2).getDesc());
                }
                orderDO.setOldNumbers(sb.deleteCharAt(sb.length() - 1).toString());
                orderDO.setSumNum(sumNum);
                orderDO.setSumVolume(sumVolume);
                orderDO.setSumWeight(sumWeight);
                StringBuffer stringBuffer = null;
                List<String> packageTypeList = new ArrayList<>(packageTypeSet);
                for (String s : packageTypeList) {
                    if (Objects.isNull(stringBuffer)) {
                        stringBuffer = new StringBuffer();
                        stringBuffer.append(s);
                    } else {
                        stringBuffer.append(StrUtil.COMMA).append(s);
                    }
                }
                if (Objects.nonNull(stringBuffer)) {
                    orderDO.setPackageType(stringBuffer.toString());
                }
                orderDO.setOrderItemDOList(orderItemDOS);
                //新增合并订单
                orderService.mergeOrder(orderDO);
                orderService.updateStatus(null, orderDO.getOrderNo(), OrderStatusEnum.IN_WAREHOUSE.getValue(), null, OrderWarehouseInStatusEnum.warehouse_finished.getValue(), null, 0, OrderApprovalTypeResultEnum.typeAndResultOf(type, 2).getDesc());

                //订单项入仓和货物信息和储位处理
                Map<String, Long> itemIdMap = orderDO.getOrderItemDOList().stream().collect(Collectors.toMap(ii -> String.valueOf(ii.getOrgItemId()), OrderItemDO::getOrderItemId, (key1, key2) -> key1));
                orderWarehouseInList.stream().forEach(item -> {
                    item.setOrderId(orderDO.getOrderId());
                    item.setOrderNo(orderDO.getOrderNo());
                    item.setOrderItemId(itemIdMap.get(String.valueOf(item.getOrderItemId())));
                });
                orderWarehouseInService.dealMergeOrderWarehouseInAndLocation(orderWarehouseInList);
                //处理入仓影像后批量插入
                warehousePictureList.stream().forEach(item -> {
                    item.setId(null);
                    item.setBizId(itemIdMap.get(String.valueOf(item.getBizId())));
                });
                orderWarehousePictureService.insertBatch(warehousePictureList);

                specialNeedList.stream().forEach(item -> {
                    if (item.getReceivableId() != null && item.getReceivableId() > 0L) {
                        receivableService.deleteReceivable(item.getReceivableId());
                    }
                });
                //处理费用申请，新增新订单的费用申请
                feeApplicationList.stream().forEach(item -> {
                    item.setOrderId(orderDO.getOrderId());
                    item.setId(null);
                    if (item.getReceivableId() != null && item.getReceivableId() > 0) {
                        receivableService.deleteReceivable(item.getReceivableId().longValue());
                    }
                    item.setReceivableId(null);
                });
                orderFeeApplicationService.insertBatch(feeApplicationList);
                feeApplicationCreateReceivable(feeApplicationList);
            }
        } else if (result == 4) {
            Integer type = OrderApprovalTypeResultEnum.merge_order_cancel.getType();
            for (Long orderId : mergeOrderIdList) {
                orderService.updateStatus(orderId, null, OrderStatusEnum.IN_WAREHOUSE.getValue(), null, OrderWarehouseInStatusEnum.warehouse_finished.getValue(), null, 0, OrderApprovalTypeResultEnum.typeAndResultOf(type, 4).getDesc());
            }
            mergeMapper.delete(new LambdaQueryWrapperX<MergeDO>().eq(MergeDO::getBpmProcessId, mergeDO.getBpmProcessId()));
        }
    }

    @Override
    public Map<String, List<OrderTimeDO>> getMergeLogByMergedOrderNo(String mergedOrderNo) {
        Map<String, List<OrderTimeDO>> map = new HashMap<>();
        List<MergeDO> mergeDOList = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>()
                .eq(MergeDO::getMergedOrderNo, mergedOrderNo)
                .eq(MergeDO::getStatus, 2));
        if (mergeDOList != null && mergeDOList.size() > 0) {
            OrderDO one = orderService.getOne(new LambdaQueryWrapperX<OrderDO>().eq(OrderDO::getOrderNo, mergeDOList.get(0).getApplyOrderNo()));
            List<OrderTimeDO> orderTimeList = orderTimeService.getOrderTimeList(one.getOrderId());
            map.put(mergeDOList.get(0).getApplyOrderNo(), orderTimeList);
            for (MergeDO mergeDO : mergeDOList) {
                OrderDO orderDO = orderService.getOne(new LambdaQueryWrapperX<OrderDO>().eq(OrderDO::getOrderNo, mergeDO.getOrderNos()));
                map.put(mergeDO.getOrderNos(), orderTimeService.getOrderTimeList(orderDO.getOrderId()));
            }
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelMerge(MergeQueryVO mergeQueryVO) {
        List<MergeDO> mergeDOList = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>()
                .eq(MergeDO::getApplyOrderNo, mergeQueryVO.getApplyOrderNo())
                .or().eq(MergeDO::getOrderNos, mergeQueryVO.getApplyOrderNo())
                .eq(MergeDO::getStatus, 1));

//        Integer type = OrderApprovalTypeResultEnum.merge_order_cancel.getType();
//        List<MergeDO> cancelList = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>().eq(MergeDO::getBpmProcessId, mergeDOList.get(0).getBpmProcessId()).eq(MergeDO::getStatus, 1));
//        if (CollectionUtil.isNotEmpty(cancelList)) {
//            orderService.updateStatus(null,cancelList.get(0).getApplyOrderNo(),OrderStatusEnum.IN_WAREHOUSE.getValue(),null,OrderWarehouseInStatusEnum.warehouse_finished.getValue(),null,type,OrderApprovalTypeResultEnum.typeAndResultOf(type,4).getDesc());
//            cancelList.stream().forEach(item -> orderService.updateStatus(null,item.getOrderNos(),OrderStatusEnum.IN_WAREHOUSE.getValue(),null,OrderWarehouseInStatusEnum.warehouse_finished.getValue(),null,type,OrderApprovalTypeResultEnum.typeAndResultOf(type,4).getDesc()));
//        }
//
//        mergeMapper.delete(new LambdaQueryWrapperX<MergeDO>().eq(MergeDO::getBpmProcessId,mergeDOList.get(0).getBpmProcessId()));
        bpmCreateServiceFactory.cancelBmpById(SecurityFrameworkUtils.getLoginUserId(), mergeDOList.get(0).getBpmProcessId(), mergeQueryVO.getReason());
//        OrderDO orderDO = orderService.selectOne(OrderDO::getOrderNo, mergeQueryVO.getApplyOrderNo());
//        orderService.addOrderOperateLog(orderDO.getOrderId(),"","取消提交合单申请","");
    }

    @Override
    public List<OrderBackInfoDto> getMergeListByMergeId(Long id) {
        MergeDO mergeDO = mergeMapper.selectById(id);
        List<MergeDO> mergeDOS = mergeMapper.selectList(new LambdaQueryWrapperX<MergeDO>()
                .eq(MergeDO::getBpmProcessId, mergeDO.getBpmProcessId())
                .eq(MergeDO::getStatus, 1));
        List<OrderBackInfoDto> collect = mergeDOS.stream().map(item -> orderQueryService.info(orderService.selectOne(OrderDO::getOrderNo, item.getOrderNos()).getOrderId())).collect(Collectors.toList());
        collect.add(orderQueryService.info(orderService.selectOne(OrderDO::getOrderNo, mergeDO.getApplyOrderNo()).getOrderId()));
        return collect;
    }

    private PageResult<OrderDO> getUnMergedOrder(String orderNo, PageVO pageVO) {
        OrderDO orderDO = orderService.selectOne(OrderDO::getOrderNo, orderNo);
        pageVO.setField("t.order_id");
        IPage<OrderDO> mpPage = MyBatisUtils.buildPage(pageVO);
        pageVO.setField("t.order_id");
        if (orderDO.getParentOrderId() == null || orderDO.getParentOrderId().longValue() == 0L) {
            mergeMapper.getUnMergedOrder(mpPage, orderDO);
        }
        return PageResult.of(mpPage);
    }

    private void feeApplicationCreateReceivable(List<OrderFeeApplicationDO> batchFeeList) {
        List<FeeDto> feeDtos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(batchFeeList)) {
            OrderDO order = orderService.selectOne(OrderDO::getOrderId, batchFeeList.get(0).getOrderId());
            for (OrderFeeApplicationDO feeApplicationDO : batchFeeList) {
                //生成应收
                DictDataRespDTO dictDto = DictFrameworkUtils.getDictDataFromCache("receivable_fee_type", String.valueOf(feeApplicationDO.getFeeType()));
                if (feeApplicationDO.getReceivableId() != null && feeApplicationDO.getReceivableId() > 0) {
                    ReceivableDO receivableDO = new ReceivableDO();
                    receivableDO.setId(Long.valueOf(feeApplicationDO.getReceivableId()));
                    receivableDO.setTitleZh(dictDto.getLabel());
                    receivableDO.setTitleEn(dictDto.getLabelEn());
                    receivableDO.setOrderId(feeApplicationDO.getOrderId());
                    receivableDO.setOrderNo(order.getOrderNo());
                    receivableDO.setTotalAmount(feeApplicationDO.getApplicationFee());
                    receivableDO.setCollectionType(feeApplicationDO.getPayType());
                    receivableDO.setUnitPrice(feeApplicationDO.getApplicationFee());
                    receivableDO.setCurrencyId(Long.valueOf(feeApplicationDO.getApplicationFeeCurrency()));
                    receivableDO.setFeeType(feeApplicationDO.getFeeType());
                    receivableDO.setIsPayAdvance(feeApplicationDO.getPayType() == 4 ? 0 : 1);
                    receivableDO.setState(feeApplicationDO.getReceiveFlag());
                    receivableDO.setPaymentUser(String.valueOf(order.getDrawee() != 3 ? order.getDrawee() : orderQueryService.customDrawee(order, feeApplicationDO.getFeeType())));//6待修改
                    receivableDO.setFeeSource(2);
                    receivableDO.setAuthor(feeApplicationDO.getApplicationAuthor());
                    receivableDO.setRemark(feeApplicationDO.getRemarks());
                    receivableService.updateReceivableById(receivableDO);
                } else {
                    ReceivableCreateReqVO receivableCreateReqVO = new ReceivableCreateReqVO();
                    receivableCreateReqVO.setTitleZh(dictDto.getLabel());
                    receivableCreateReqVO.setTitleEn(dictDto.getLabelEn());
                    receivableCreateReqVO.setOrderId(feeApplicationDO.getOrderId());
                    receivableCreateReqVO.setOrderNo(order.getOrderNo());
                    receivableCreateReqVO.setTotalAmount(feeApplicationDO.getApplicationFee());
                    receivableCreateReqVO.setUnitPrice(feeApplicationDO.getApplicationFee());
                    receivableCreateReqVO.setCollectionType(feeApplicationDO.getPayType());
                    receivableCreateReqVO.setCurrencyId(Long.valueOf(feeApplicationDO.getApplicationFeeCurrency()));
                    receivableCreateReqVO.setFeeType(feeApplicationDO.getFeeType());
                    receivableCreateReqVO.setIsPayAdvance(feeApplicationDO.getPayType() == 4 ? 0 : 1);
                    receivableCreateReqVO.setState(feeApplicationDO.getReceiveFlag());
                    receivableCreateReqVO.setPaymentUser(String.valueOf(order.getDrawee() != 3 ? order.getDrawee() : orderQueryService.customDrawee(order, feeApplicationDO.getFeeType())));//6待修改
                    receivableCreateReqVO.setFeeSource(2);
                    receivableCreateReqVO.setAuthor(feeApplicationDO.getApplicationAuthor());
                    receivableCreateReqVO.setRemark(feeApplicationDO.getRemarks());
                    Long receivableId = receivableService.createReceivable(receivableCreateReqVO);
                    feeApplicationDO.setReceivableId(receivableId.intValue());
                }
                feeApplicationDO.setStatus(ApprovalResultStatusEnum.pass.getValue());
                orderFeeApplicationService.updateById(feeApplicationDO);
                FeeDto feeDto = new FeeDto();
                feeDto.setFeeType(feeApplicationDO.getFeeType());
                feeDto.setAmount(feeApplicationDO.getApplicationFee());
                feeDto.setCurrencyId(feeApplicationDO.getApplicationFeeCurrency());
                feeDtos.add(feeDto);

            }
            orderService.addOrderOtherFee(order.getOrderId(), feeDtos);
        }
    }
}
