package com.prolog.rdc.tms.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.*;
import com.prolog.rdc.tms.model.dto.basicConfigure.*;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehiclePopupDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleTypeCheckDTO;
import com.prolog.rdc.tms.model.entity.SysParamConfigModel;
import com.prolog.rdc.tms.model.entity.TmsCarrierMthStyCtModel;
import com.prolog.rdc.tms.model.entity.TmsCarrierMthStyModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseDriverModel;
import com.prolog.rdc.tms.model.entity.line.TmsBaseLineModel;
import com.prolog.rdc.tms.model.entity.line.TmsCustomerConsignee;
import com.prolog.rdc.tms.model.entity.line.TmsLineCarrierLinkModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.*;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehiclePopupVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeCheckVO;
import com.prolog.rdc.tms.service.CarrierConfigService;
import com.prolog.rdc.tms.service.CarrierMthStrategyService;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.basicConfigure.SysParamConfigService;
import com.prolog.rdc.tms.service.fleetManagement.VehicleService;
import com.prolog.rdc.tms.service.fleetManagement.VehicleTypeService;
import com.prolog.rdc.tms.util.PublicMethods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 承运方匹配策略业务实现层
 *
 * @author :lhb
 * @version :1.0.0
 * @className :CarrierMthStrategyServiceImpl
 * @creationTime :2022/05/07
 * @description :
 * @modifier :
 * @modifyTime :
 * @reviseRemarks:
 */
@Service
@Slf4j
public class CarrierMthStrategyServiceImpl implements CarrierMthStrategyService {

    @Autowired
    private TmsCarrierMthStrategyMapper tmsCarrierMthStrategyMapper;

    @Autowired
    private TmsCarrierMthStrategyCtMapper tmsCarrierMthStrategyCtMapper;

    @Autowired
    private SysCodeengineService sysCodeengineService;

    @Autowired
    private SysRefdatadService sysRefdatadService;

    @Autowired
    private TmsCustomerConsigneeMapper tmsCustomerConsigneeMapper;

    @Autowired
    private TmsLineCarrierLinkMapper tmsLineCarrierLinkMapper;

    @Autowired
    private TmsBaseTranslineMapper tmsBaseTranslineMapper;

    @Autowired
    private CarrierConfigService carrierConfigService;

    @Autowired
    private VehicleService vehicleService;

    @Autowired
    private TmsBaseDriverMapper tmsBaseDriverMapper;

    @Autowired
    private SysParamConfigService sysParamConfigService;

    @Autowired
    @Qualifier("carrierStrategyExecutor")
    private ThreadPoolTaskExecutor executor;

    @Autowired
    private VehicleTypeService vehicleTypeService;

    /**
     * 承运方匹配策略分页查询
     *
     * @param pageVO
     * @return
     */
    @SuppressWarnings("resource")
    @Override
    public Page<CarrierMthStrategyPageDTO> pageQuery(CarrierMthStrategyPageVO pageVO) {
        ToolKits.isParamNull(pageVO, "承运方匹配策略分页查询请求不为空");
        ToolKits.isParamNull(pageVO.getPageNum(), "分页页码不为空");
        ToolKits.isParamNull(pageVO.getPageSize(), "分页页码大小不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        Criteria carrierCriteria = Criteria.forClass(TmsCarrierMthStyModel.class);
        Restriction r1 = ToolKits.isEmpty(pageVO.getStrategyName()) ? null : Restrictions.like(TmsCarrierMthStyModel::getName, "%" + pageVO.getStrategyName() + "%");
        Restriction r2 = ToolKits.isEmpty(pageVO.getStatus()) ? null : Restrictions.eq(TmsCarrierMthStyModel::getStatus, pageVO.getStatus());
        Restriction r3 = Restrictions.eq(TmsCarrierMthStyModel::getEnterpriseId, user.getEnterpriseId());
        carrierCriteria.setRestriction(Restrictions.and(r1, r2, r3));
        carrierCriteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));

        PageUtils.startPage(pageVO.getPageNum(), pageVO.getPageSize());
        List<TmsCarrierMthStyModel> mthStyModels = tmsCarrierMthStrategyMapper.findByCriteria(carrierCriteria);

        // 状态
        List<Map<String, Object>> status = sysRefdatadService.getDropDownById(DataDictionaryConstant.GENERAL_STATUS, null, null, null);
        // 策略条件类型
        List<Map<String, Object>> restMsgStyConditionType = sysRefdatadService.getDropDownById(DataDictionaryConstant.STRATEGY_CONDITION_TYPE, null, null, null);
        // 配送方式
        List<Map<String, Object>> restMsgDeliveryMethodType = sysRefdatadService.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
        // 订单类型
        List<Map<String, Object>> restMsgOrderType = sysRefdatadService.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
        // 订单业务类型
        List<Map<String, Object>> restMsgOrderBusinessType = sysRefdatadService.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
        // 运输类型(运输模式)
        List<Map<String, Object>> restMsgTransportType = sysRefdatadService.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
        // 收货方类型
        List<Map<String, Object>> restMsgConsigneeType = sysRefdatadService.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
        // 发货方类型
        List<Map<String, Object>> restMsgShipperType = sysRefdatadService.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
        // 温度要求
        List<Map<String, Object>> restMsgTemperature = sysRefdatadService.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);

        List<CarrierMthStrategyPageDTO> pageDTOS = Lists.newArrayList();
        for (TmsCarrierMthStyModel mthStyModel : mthStyModels) {
            CarrierMthStrategyPageDTO strategyPageDTO = new CarrierMthStrategyPageDTO();
            BeanUtils.copyProperties(mthStyModel, strategyPageDTO);
            strategyPageDTO.setStrategyName(mthStyModel.getName());

            List<CarrierMthStrategyPrioDTO> prioDTOS = Lists.newArrayList();
            CarrierMthStrategyPrioDTO prio1DTO = new CarrierMthStrategyPrioDTO();
            prio1DTO.setMatchTypeItem("按照客户匹配承运方");
            prio1DTO.setNum(mthStyModel.getMatchTypeCustomer());
            prioDTOS.add(prio1DTO);

            CarrierMthStrategyPrioDTO prio2DTO = new CarrierMthStrategyPrioDTO();
            prio2DTO.setMatchTypeItem("按照线路匹配承运方");
            prio2DTO.setNum(mthStyModel.getMatchTypeLineConsignor());
            prioDTOS.add(prio2DTO);

            CarrierMthStrategyPrioDTO prio3DTO = new CarrierMthStrategyPrioDTO();
            prio3DTO.setMatchTypeItem("按照承运方匹配策略");
            prio3DTO.setNum(mthStyModel.getMatchTypeCarrier());
            prioDTOS.add(prio3DTO);

            if (ObjectUtil.isNotEmpty(mthStyModel.getMatchTypeWeight())) {
                CarrierMthStrategyPrioDTO prio4DTO = new CarrierMthStrategyPrioDTO();
                prio4DTO.setMatchTypeItem("按照重量匹配策略");
                prio4DTO.setNum(mthStyModel.getMatchTypeWeight());
                prioDTOS.add(prio4DTO);
            }
            if (ObjectUtil.isNotEmpty(mthStyModel.getMatchTypeVolume())) {
                CarrierMthStrategyPrioDTO prio5DTO = new CarrierMthStrategyPrioDTO();
                prio5DTO.setMatchTypeItem("按照体积匹配策略");
                prio5DTO.setNum(mthStyModel.getMatchTypeVolume());
                prioDTOS.add(prio5DTO);
            }
            if (ObjectUtil.isNotEmpty(mthStyModel.getMatchTypeWeightVolume())) {
                CarrierMthStrategyPrioDTO prio6DTO = new CarrierMthStrategyPrioDTO();
                prio6DTO.setMatchTypeItem("按照重量体积匹配策略");
                prio6DTO.setNum(mthStyModel.getMatchTypeWeightVolume());
                prioDTOS.add(prio6DTO);
            }

            List<CarrierMthStrategyPrioDTO> list = prioDTOS.stream().sorted(Comparator.comparing(CarrierMthStrategyPrioDTO::getNum)).collect(Collectors.toList());

            // 匹配优先级
            strategyPageDTO.setMatchPrioItems(list.stream().map(item -> item.getMatchTypeItem()).collect(Collectors.joining(",")));
            strategyPageDTO.setStatusName(PublicMethods.change(status, String.valueOf(mthStyModel.getStatus())));

            // 满足条件
            List<String> conditions = Lists.newArrayList();
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierMthStyCtModel.class);
            Restriction r4 = Restrictions.eq(TmsCarrierMthStyCtModel::getStrategyId, mthStyModel.getId());
            Restriction r5 = Restrictions.eq(TmsCarrierMthStyCtModel::getEnterpriseId, user.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r4, r5));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"strategyConditionType"}));
            List<TmsCarrierMthStyCtModel> mthStyCtModels = tmsCarrierMthStrategyCtMapper.findByCriteria(carrierCtCriteria);

            for (TmsCarrierMthStyCtModel mthStyCtModel : mthStyCtModels) {
                conditions.add(PublicMethods.change(restMsgStyConditionType, String.valueOf(mthStyCtModel.getStrategyConditionType())));
                // 满足条件
                switch (mthStyCtModel.getStrategyConditionType().intValue()) {
                    case 1:
                        // 1配送方式
                        conditions.add(PublicMethods.change(restMsgDeliveryMethodType, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                    case 2:
                        // 2发货方类型
                        conditions.add(PublicMethods.change(restMsgShipperType, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                    case 3:
                        // 3收货方类型
                        conditions.add(PublicMethods.change(restMsgConsigneeType, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                    case 4:
                        // 4订单类型
                        conditions.add(PublicMethods.change(restMsgOrderType, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                    case 5:
                        // 5订单业务类型
                        conditions.add(PublicMethods.change(restMsgOrderBusinessType, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                    case 6:
                        // 6运输模式
                        conditions.add(PublicMethods.change(restMsgTransportType, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                    case 7:
                        // 7温度要求
                        conditions.add(PublicMethods.change(restMsgTemperature, String.valueOf(mthStyCtModel.getStrategyConditionValue())));
                        break;
                }
            }
            strategyPageDTO.setStrategyConditions(conditions.stream().collect(Collectors.joining(",")));
            pageDTOS.add(strategyPageDTO);
        }

        Page<CarrierMthStrategyPageDTO> page = PageUtils.getPage(pageDTOS);
        if (mthStyModels instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<TmsCarrierMthStyModel> p0 = (com.github.pagehelper.Page<TmsCarrierMthStyModel>) mthStyModels;
            page.setPageCount(p0.getPages());
            page.setPageNum(p0.getPageNum());
            page.setPageSize(p0.getPageSize());
            page.setTotalCount(p0.getTotal());
            page.setStartRow(p0.getStartRow());
            page.setEndRow(p0.getEndRow());
        }
        return page;
    }

    /**
     * 承运方匹配策略保存
     *
     * @param carrierMthStrategySaveVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CarrierMthStrategySaveVO carrierMthStrategySaveVO) {
        ToolKits.isParamNull(carrierMthStrategySaveVO, "承运方匹配策略保存请求不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getStrategyName(), "策略名称不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getMatchTypeCustomer(), "客户匹配承运方顺序不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getMatchTypeLineConsignor(), "线路匹配承运方顺序不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getMatchTypeCarrier(), "承运方匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getMatchTypeWeight(), "重量匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getMatchTypeVolume(), "体积匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getMatchTypeWeightVolume(), "重量体积匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategySaveVO.getStrategyCtSaveVOList(), "承运方匹配策略条件不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 运单匹配条件集合
        Map<Integer, String> conditionMap = Maps.newHashMap();
        for (CarrierMthStrategyCtSaveVO conditionVO : carrierMthStrategySaveVO.getStrategyCtSaveVOList()) {
            // 配送方式
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 订单类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 订单业务类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 运输模式
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 温度要求
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 货主ID
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("货主不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER) + "_" + units);
                continue;
            }

            // 部门ID
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("部门不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT) + "_" + units);
                continue;
            }
            // 收货方类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("收货方不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()) + "," + units);
                continue;
            }
            // 发货方类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("发货方不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()) + "," + units);
                continue;
            }
        }

        Criteria carrierCriteria = Criteria.forClass(TmsCarrierMthStyModel.class);
        Restriction r1 = Restrictions.eq(TmsCarrierMthStyModel::getEnterpriseId, user.getEnterpriseId());
        Restriction r2 = Restrictions.eq(TmsCarrierMthStyModel::getStatus, DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        carrierCriteria.setRestriction(Restrictions.and(r1, r2));
        carrierCriteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        List<TmsCarrierMthStyModel> mthStyModels = tmsCarrierMthStrategyMapper.findByCriteria(carrierCriteria);

        for (TmsCarrierMthStyModel carrierMthStyModel : mthStyModels) {
            // 满足条件
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierMthStyCtModel.class);
            Restriction r3 = Restrictions.eq(TmsCarrierMthStyCtModel::getStrategyId, carrierMthStyModel.getId());
            Restriction r4 = Restrictions.eq(TmsCarrierMthStyCtModel::getEnterpriseId, user.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r3, r4));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"strategyConditionType"}));
            List<TmsCarrierMthStyCtModel> mthStyCtModels = tmsCarrierMthStrategyCtMapper.findByCriteria(carrierCtCriteria);

            // 判定满足条件
            int fitCnt = 0;
            for (TmsCarrierMthStyCtModel conditionModel : mthStyCtModels) {
                switch (conditionModel.getStrategyConditionType().intValue()) {
                    case 2:
                    case 3:
                        String unit1s = Arrays.stream(String.valueOf(conditionModel.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                        if (conditionMap.containsValue(String.valueOf(conditionModel.getStrategyConditionType()) + "_" + String.valueOf(conditionModel.getStrategyConditionValue()) + "," + unit1s)) {
                            fitCnt++;
                        }
                        break;
                    case 8:
                    case 9:
                        String unit2s = Arrays.stream(String.valueOf(conditionModel.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));

                        if (conditionMap.containsValue(String.valueOf(conditionModel.getStrategyConditionType()) + "_" + unit2s)) {
                            fitCnt++;
                        }
                        break;
                    default:
                        String unit3s = Arrays.stream(String.valueOf(conditionModel.getStrategyConditionValue()).split(",")).sorted().collect(Collectors.joining(","));

                        if (conditionMap.containsValue(String.valueOf(conditionModel.getStrategyConditionType()) + "_" + unit3s)) {
                            fitCnt++;
                        }
                        break;
                }
            }
            // 已经存在相同条件的承运方匹配策略
            Assert.isTrue(fitCnt != mthStyCtModels.size(), "承运方匹配策略的条件已经存在，请重新配置承运方匹配策略条件!");
        }

        // 当前时间
        Date date = DateUtil.date();

        String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_CARRIER_MATCH_STRATEGY_ID, "L");
        ToolKits.isParamNull(id, "承运方匹配策略ID生成失败");

        TmsCarrierMthStyModel carrierMthStyModel = new TmsCarrierMthStyModel();
        BeanUtils.copyProperties(carrierMthStrategySaveVO, carrierMthStyModel);

        // ID赋值
        carrierMthStyModel.setId(Long.valueOf(id));
        carrierMthStyModel.setName(carrierMthStrategySaveVO.getStrategyName());
        carrierMthStyModel.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        carrierMthStyModel.setEnterpriseId(user.getEnterpriseId());
        carrierMthStyModel.setEnterpriseName(user.getEnterpriseName());
        carrierMthStyModel.setCreateperson(user.getNickname());
        carrierMthStyModel.setCreatetime(date);
        carrierMthStyModel.setUpdateperson(user.getNickname());
        carrierMthStyModel.setUpdatetime(date);
        // 保存
        tmsCarrierMthStrategyMapper.save(carrierMthStyModel);
        // 策略条件
        for (CarrierMthStrategyCtSaveVO strategyCtSaveVO : carrierMthStrategySaveVO.getStrategyCtSaveVOList()) {
            String cid = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_CARRIER_MATCH_STRATEGY_CT_ID, "L");
            ToolKits.isParamNull(cid, "承运方匹配策略条件ID生成失败");
            TmsCarrierMthStyCtModel carrierMthStyCtModel = new TmsCarrierMthStyCtModel();
            BeanUtils.copyProperties(strategyCtSaveVO, carrierMthStyCtModel);
            carrierMthStyCtModel.setId(Long.valueOf(cid));
            carrierMthStyCtModel.setStrategyId(Long.valueOf(id));

            carrierMthStyCtModel.setEnterpriseId(user.getEnterpriseId());
            carrierMthStyCtModel.setEnterpriseName(user.getEnterpriseName());
            carrierMthStyCtModel.setCreateperson(user.getNickname());
            carrierMthStyCtModel.setCreatetime(date);
            carrierMthStyCtModel.setUpdateperson(user.getNickname());
            carrierMthStyCtModel.setUpdatetime(date);
            tmsCarrierMthStrategyCtMapper.save(carrierMthStyCtModel);
        }
    }

    /**
     * 承运方匹配策略修改
     *
     * @param carrierMthStrategyUpdateVO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CarrierMthStrategyUpdateVO carrierMthStrategyUpdateVO) {
        ToolKits.isParamNull(carrierMthStrategyUpdateVO, "承运方匹配策略修改请求不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getId(), "策略ID不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getStrategyName(), "策略名称不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getMatchTypeCustomer(), "客户匹配承运方顺序不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getMatchTypeLineConsignor(), "线路匹配承运方顺序不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getMatchTypeCarrier(), "承运方匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getMatchTypeWeight(), "重量匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getMatchTypeVolume(), "体积匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getMatchTypeWeightVolume(), "重量体积匹配策略顺序不为空");
        ToolKits.isParamNull(carrierMthStrategyUpdateVO.getStrategyCtUpdateVOList(), "承运方匹配策略条件不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 运单匹配条件集合
        Map<Integer, String> conditionMap = Maps.newHashMap();
        for (CarrierMthStrategyCtUpdateVO conditionVO : carrierMthStrategyUpdateVO.getStrategyCtUpdateVOList()) {
            // 配送方式
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 订单类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 订单业务类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 运输模式
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 温度要求
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE)) {
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()));
                continue;
            }
            // 货主ID
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("货主不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER) + "_" + units);
                continue;
            }

            // 部门ID
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("部门不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT) + "_" + units);
                continue;
            }
            // 收货方类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("收货方不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()) + "," + units);
                continue;
            }
            // 发货方类型
            if (conditionVO.getStrategyConditionType().equals(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER)) {
                if (ToolKits.isEmpty(conditionVO.getUnitId())) {
                    throw new PrologException("发货方不为空");
                }
                String units = Arrays.stream(String.valueOf(conditionVO.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER, String.valueOf(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER) + "_" + String.valueOf(conditionVO.getStrategyConditionValue()) + "," + units);
                continue;
            }
        }

        Criteria carrierCriteria = Criteria.forClass(TmsCarrierMthStyModel.class);
        Restriction r1 = Restrictions.eq(TmsCarrierMthStyModel::getEnterpriseId, user.getEnterpriseId());
        Restriction r2 = Restrictions.eq(TmsCarrierMthStyModel::getStatus, DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        Restriction r5 = Restrictions.ne(TmsCarrierMthStyModel::getId, carrierMthStrategyUpdateVO.getId());
        carrierCriteria.setRestriction(Restrictions.and(r1, r2, r5));
        carrierCriteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        List<TmsCarrierMthStyModel> mthStyModels = tmsCarrierMthStrategyMapper.findByCriteria(carrierCriteria);

        for (TmsCarrierMthStyModel carrierMthStyModel : mthStyModels) {
            // 满足条件
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierMthStyCtModel.class);
            Restriction r3 = Restrictions.eq(TmsCarrierMthStyCtModel::getStrategyId, carrierMthStyModel.getId());
            Restriction r4 = Restrictions.eq(TmsCarrierMthStyCtModel::getEnterpriseId, user.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r3, r4));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"strategyConditionType"}));
            List<TmsCarrierMthStyCtModel> mthStyCtModels = tmsCarrierMthStrategyCtMapper.findByCriteria(carrierCtCriteria);

            // 判定满足条件
            int fitCnt = 0;
            for (TmsCarrierMthStyCtModel conditionModel : mthStyCtModels) {
                switch (conditionModel.getStrategyConditionType().intValue()) {
                    case 2:
                    case 3:
                        String unit1s = Arrays.stream(String.valueOf(conditionModel.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                        if (conditionMap.containsValue(String.valueOf(conditionModel.getStrategyConditionType()) + "_" + String.valueOf(conditionModel.getStrategyConditionValue()) + "," + unit1s)) {
                            fitCnt++;
                        }
                        break;
                    case 8:
                    case 9:
                        String unit2s = Arrays.stream(String.valueOf(conditionModel.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));

                        if (conditionMap.containsValue(String.valueOf(conditionModel.getStrategyConditionType()) + "_" + unit2s)) {
                            fitCnt++;
                        }
                        break;
                    default:
                        String unit3s = Arrays.stream(String.valueOf(conditionModel.getStrategyConditionValue()).split(",")).sorted().collect(Collectors.joining(","));

                        if (conditionMap.containsValue(String.valueOf(conditionModel.getStrategyConditionType()) + "_" + unit3s)) {
                            fitCnt++;
                        }
                        break;
                }
            }
            // 已经存在相同条件的承运方匹配策略
            Assert.isTrue(fitCnt != mthStyCtModels.size(), "承运方匹配策略的条件已经存在，请重新配置承运方匹配策略条件!");
        }
        // 当前时间
        Date date = DateUtil.date();

        Map<String, Object> mapUpdateMthStyModel = Maps.newHashMap();
        mapUpdateMthStyModel.put("status", DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        mapUpdateMthStyModel.put("matchTypeCustomer", carrierMthStrategyUpdateVO.getMatchTypeCustomer());
        mapUpdateMthStyModel.put("matchTypeLineConsignor", carrierMthStrategyUpdateVO.getMatchTypeLineConsignor());
        mapUpdateMthStyModel.put("matchTypeCarrier", carrierMthStrategyUpdateVO.getMatchTypeCarrier());
        mapUpdateMthStyModel.put("matchTypeWeight", carrierMthStrategyUpdateVO.getMatchTypeWeight());
        mapUpdateMthStyModel.put("matchTypeVolume", carrierMthStrategyUpdateVO.getMatchTypeVolume());
        mapUpdateMthStyModel.put("matchTypeWeightVolume", carrierMthStrategyUpdateVO.getMatchTypeWeightVolume());
        mapUpdateMthStyModel.put("memo", carrierMthStrategyUpdateVO.getMemo());
        mapUpdateMthStyModel.put("updateperson", user.getNickname());
        mapUpdateMthStyModel.put("updatetime", date);

        // 修改
        tmsCarrierMthStrategyMapper.updateMapById(carrierMthStrategyUpdateVO.getId(), mapUpdateMthStyModel, TmsCarrierMthStyModel.class);

        // 删除条件
        Criteria flowConditionCriteria = Criteria.forClass(TmsCarrierMthStyCtModel.class);
        Restriction r7 = Restrictions.eq(TmsCarrierMthStyCtModel::getStrategyId, carrierMthStrategyUpdateVO.getId());
        flowConditionCriteria.setRestriction(Restrictions.and(r7));
        tmsCarrierMthStrategyCtMapper.deleteByCriteria(flowConditionCriteria);

        // 策略条件修改
        for (CarrierMthStrategyCtUpdateVO strategyCtUpdateVO : carrierMthStrategyUpdateVO.getStrategyCtUpdateVOList()) {
            String cid = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_CARRIER_MATCH_STRATEGY_CT_ID, "L");
            ToolKits.isParamNull(cid, "承运方匹配策略条件ID生成失败");
            TmsCarrierMthStyCtModel carrierMthStyCtModel = new TmsCarrierMthStyCtModel();
            BeanUtils.copyProperties(strategyCtUpdateVO, carrierMthStyCtModel);
            carrierMthStyCtModel.setId(Long.valueOf(cid));
            carrierMthStyCtModel.setStrategyId(Long.valueOf(carrierMthStrategyUpdateVO.getId()));

            carrierMthStyCtModel.setEnterpriseId(user.getEnterpriseId());
            carrierMthStyCtModel.setEnterpriseName(user.getEnterpriseName());
            carrierMthStyCtModel.setCreateperson(user.getNickname());
            carrierMthStyCtModel.setCreatetime(date);
            tmsCarrierMthStrategyCtMapper.save(carrierMthStyCtModel);
        }
    }

    /**
     * 承运方匹配策略停用
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void stop(List<Long> ids) {
        ToolKits.isParamNull(ids, "承运方匹配策略停用请求不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 当前时间
        Date date = DateUtil.date();
        Map<String, Object> mapUpdate = Maps.newHashMap();
        mapUpdate.put("status", DataDictionaryConstant.GENERAL_STATUS_STOP);
        mapUpdate.put("updateperson", user.getNickname());
        mapUpdate.put("updatetime", date);

        ids.forEach(id -> {
            tmsCarrierMthStrategyMapper.updateMapById(id, mapUpdate, TmsCarrierMthStyModel.class);
        });
    }

    /**
     * 承运方匹配策略启用
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void enable(List<Long> ids) {
        ToolKits.isParamNull(ids, "承运方匹配策略启用请求不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        // 当前时间
        Date date = DateUtil.date();

        Map<String, Object> mapUpdate = Maps.newHashMap();
        mapUpdate.put("status", DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        mapUpdate.put("updateperson", user.getNickname());
        mapUpdate.put("updatetime", date);

        ids.forEach(id -> {
            tmsCarrierMthStrategyMapper.updateMapById(id, mapUpdate, TmsCarrierMthStyModel.class);
        });
    }

    /**
     * 承运方匹配策略查看
     *
     * @param viewVO
     * @return
     */
    public CarrierMthStrategyViewDTO view(CarrierMthStrategyViewVO viewVO) {
        ToolKits.isParamNull(viewVO, "承运方匹配策略查看请求不为空");
        ToolKits.isParamNull(viewVO.getId(), "承运方匹配策略ID不为空");

        UserEntity user = PlgFxUsrUtils.getUserEntity();
        ToolKits.isParamNull(user, "当前人员没有操作权限");

        TmsCarrierMthStyModel mthStyModel = tmsCarrierMthStrategyMapper.findById(viewVO.getId(), TmsCarrierMthStyModel.class);

        if (ToolKits.isEmpty(mthStyModel)) {
            throw new PrologException("查看的运营方匹配策略不存在");
        }
        Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierMthStyCtModel.class);
        Restriction r1 = Restrictions.eq(TmsCarrierMthStyCtModel::getStrategyId, viewVO.getId());
        Restriction r2 = Restrictions.eq(TmsCarrierMthStyCtModel::getEnterpriseId, user.getEnterpriseId());
        carrierCtCriteria.setRestriction(Restrictions.and(r1, r2));
        carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"strategyConditionType"}));
        List<TmsCarrierMthStyCtModel> mthStyCtModels = tmsCarrierMthStrategyCtMapper.findByCriteria(carrierCtCriteria);

        CarrierMthStrategyViewDTO viewDTO = new CarrierMthStrategyViewDTO();
        viewDTO.setStrategyName(mthStyModel.getName());
        viewDTO.setMemo(mthStyModel.getMemo());

        List<CarrierMthStrategyPrioDTO> prioDTOS = Lists.newArrayList();
        CarrierMthStrategyPrioDTO prio1DTO = new CarrierMthStrategyPrioDTO();
        prio1DTO.setMatchTypeItem("按照客户匹配承运方");
        prio1DTO.setNum(mthStyModel.getMatchTypeCustomer());
        prioDTOS.add(prio1DTO);

        CarrierMthStrategyPrioDTO prio2DTO = new CarrierMthStrategyPrioDTO();
        prio2DTO.setMatchTypeItem("按照线路匹配承运方");
        prio2DTO.setNum(mthStyModel.getMatchTypeLineConsignor());
        prioDTOS.add(prio2DTO);

        CarrierMthStrategyPrioDTO prio3DTO = new CarrierMthStrategyPrioDTO();
        prio3DTO.setMatchTypeItem("按照承运方匹配策略");
        prio3DTO.setNum(mthStyModel.getMatchTypeCarrier());
        prioDTOS.add(prio3DTO);

        if (ObjectUtil.isNotEmpty(mthStyModel.getMatchTypeWeight())) {
            CarrierMthStrategyPrioDTO prio4DTO = new CarrierMthStrategyPrioDTO();
            prio4DTO.setMatchTypeItem("按照重量匹配策略");
            prio4DTO.setNum(mthStyModel.getMatchTypeWeight());
            prioDTOS.add(prio4DTO);
        }
        if (ObjectUtil.isNotEmpty(mthStyModel.getMatchTypeVolume())) {
            CarrierMthStrategyPrioDTO prio5DTO = new CarrierMthStrategyPrioDTO();
            prio5DTO.setMatchTypeItem("按照体积匹配策略");
            prio5DTO.setNum(mthStyModel.getMatchTypeVolume());
            prioDTOS.add(prio5DTO);
        }
        if (ObjectUtil.isNotEmpty(mthStyModel.getMatchTypeWeightVolume())) {
            CarrierMthStrategyPrioDTO prio6DTO = new CarrierMthStrategyPrioDTO();
            prio6DTO.setMatchTypeItem("按照重量体积匹配策略");
            prio6DTO.setNum(mthStyModel.getMatchTypeWeightVolume());
            prioDTOS.add(prio6DTO);
        }

        List<CarrierMthStrategyPrioDTO> list = prioDTOS.stream().sorted(Comparator.comparing(CarrierMthStrategyPrioDTO::getNum)).collect(Collectors.toList());

        viewDTO.setCarrierMthStrategyPrioDTOS(list);

        // 策略条件类型
        List<Map<String, Object>> restMsgStyConditionType = sysRefdatadService.getDropDownById(DataDictionaryConstant.STRATEGY_CONDITION_TYPE, null, null, null);
        // 配送方式
        List<Map<String, Object>> restMsgDeliveryMethodType = sysRefdatadService.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
        // 订单类型
        List<Map<String, Object>> restMsgOrderType = sysRefdatadService.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
        // 订单业务类型
        List<Map<String, Object>> restMsgOrderBusinessType = sysRefdatadService.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
        // 运输类型(运输模式)
        List<Map<String, Object>> restMsgTransportType = sysRefdatadService.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
        // 收货方类型
        List<Map<String, Object>> restMsgConsigneeType = sysRefdatadService.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
        // 发货方类型
        List<Map<String, Object>> restMsgShipperType = sysRefdatadService.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
        // 温度要求
        List<Map<String, Object>> restMsgTemperature = sysRefdatadService.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);

        List<CarrierMthStrategyCtViewDTO> ctViewDTOS = Lists.newArrayList();
        for (TmsCarrierMthStyCtModel ctModel : mthStyCtModels) {
            CarrierMthStrategyCtViewDTO ctViewDTO = new CarrierMthStrategyCtViewDTO();
            BeanUtils.copyProperties(ctModel, ctViewDTO);
            ctViewDTO.setStrategyConditionTypeName(PublicMethods.change(restMsgStyConditionType, String.valueOf(ctModel.getStrategyConditionType())));

            // 满足条件
            switch (ctModel.getStrategyConditionType().intValue()) {
                case 1:
                    // 1配送方式
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgDeliveryMethodType, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
                case 2:
                    // 2发货方类型
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgShipperType, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
                case 3:
                    // 3收货方类型
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgConsigneeType, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
                case 4:
                    // 4订单类型
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgOrderType, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
                case 5:
                    // 5订单业务类型
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgOrderBusinessType, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
                case 6:
                    // 6运输模式
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgTransportType, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
                case 7:
                    // 7温度要求
                    ctViewDTO.setStrategyConditionValueName(PublicMethods.change(restMsgTemperature, String.valueOf(ctModel.getStrategyConditionValue())));
                    break;
            }
            ctViewDTOS.add(ctViewDTO);
        }
        viewDTO.setCarrierMthStrategyCtViewDTOS(ctViewDTOS);
        return viewDTO;
    }

    /**
     * 根据运单适配匹配承运方策略
     *
     * @param matchCarrierVOs
     * @return
     */
    public Map<String, ShipbillMatchCarrierDTO> shipbillMatchCarrier(List<ShipbillMatchCarrierVO> matchCarrierVOs) {
        log.info("运单匹配承运方处理开始:" + JSON.toJSONString(matchCarrierVOs));
        ToolKits.isParamNull(matchCarrierVOs, "运单适配匹配承运方策略请求不为空");
        SysParamConfigModel configModel = null;
        try {
            configModel = sysParamConfigService.getValue("RELATION_THIRD_SYSTEM");
        } catch (Exception e) {
            return null;
        }
        SysParamConfigModel finalConfigModel = configModel;

        Criteria carrierCriteria = Criteria.forClass(TmsCarrierMthStyModel.class);
        Restriction r1 = Restrictions.eq(TmsCarrierMthStyModel::getEnterpriseId, matchCarrierVOs.get(0).getEnterpriseId());
        Restriction r2 = Restrictions.eq(TmsCarrierMthStyModel::getStatus, DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        carrierCriteria.setRestriction(Restrictions.and(r1, r2));
        carrierCriteria.setOrder(Order.newInstance().desc(new String[]{"updatetime"}));
        List<TmsCarrierMthStyModel> mthStyModels = tmsCarrierMthStrategyMapper.findByCriteria(carrierCriteria);
        List<CompletableFuture<ShipbillMatchCarrierDTO>> lists = Lists.newArrayList();
        for (ShipbillMatchCarrierVO matchCarrierVO : matchCarrierVOs) {
            CompletableFuture<ShipbillMatchCarrierDTO> future = CompletableFuture.supplyAsync(() -> {
                return matchCarrier(matchCarrierVO, finalConfigModel.getValue(), mthStyModels);
            }, executor);
            lists.add(future);
        }
        CompletableFuture allFuture = CompletableFuture.allOf(lists.toArray(new CompletableFuture[lists.size()]));

        CompletableFuture<Map<String, ShipbillMatchCarrierDTO>> mapFuture = allFuture.thenApplyAsync(a -> {
            Map<String, ShipbillMatchCarrierDTO> rsMaps = Maps.newHashMap();
            lists.stream().forEach(entry -> {
                ShipbillMatchCarrierDTO dto = entry.join();
                log.info("线程的结果{}",JSON.toJSONString(dto));
                if (ToolKits.isNotEmpty(dto) && ToolKits.isNotEmpty(dto.getShipbillId())) {
                    rsMaps.put(dto.getShipbillId(), dto);
                }
            });
            return rsMaps;
        });
        return mapFuture.join();
    }

    public ShipbillMatchCarrierDTO matchCarrier(ShipbillMatchCarrierVO matchCarrierVO, String configModel, List<TmsCarrierMthStyModel> mthStyModels) {
        ShipbillMatchCarrierDTO shipbillMatchCarrierDTO = new ShipbillMatchCarrierDTO();
        shipbillMatchCarrierDTO.setShipbillId(matchCarrierVO.getShipbillId());
        if (ToolKits.isEmpty(mthStyModels)) {
            return shipbillMatchCarrierDTO;
        }
        // 运单匹配条件集合
        Map<Integer, String> conditionMap = Maps.newHashMap();
        // 配送方式
        if (ToolKits.isNotEmpty(matchCarrierVO.getDeliveryMethodType())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY, DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DELIVERY + "_" + String.valueOf(matchCarrierVO.getDeliveryMethodType()));
        }
        // 订单类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getOrderType())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER, DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER + "_" + String.valueOf(matchCarrierVO.getOrderType()));
        }
        // 订单业务类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getOrderBusinessType())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS, DataDictionaryConstant.STRATEGY_CONDITION_TYPE_ORDER_BUSINESS + "_" + String.valueOf(matchCarrierVO.getOrderBusinessType()));
        }
        // 运输模式
        if (ToolKits.isNotEmpty(matchCarrierVO.getTransportType())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT, DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TRANSPORT + "_" + String.valueOf(matchCarrierVO.getTransportType()));
        }
        // 温度要求
        if (ToolKits.isNotEmpty(matchCarrierVO.getTemperatureRequirement())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE, DataDictionaryConstant.STRATEGY_CONDITION_TYPE_TEMPERATURE + "_" + String.valueOf(matchCarrierVO.getTemperatureRequirement()));
        }
        // 货主ID
        if (ToolKits.isNotEmpty(matchCarrierVO.getCargoOwnerId())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER, String.valueOf(matchCarrierVO.getCargoOwnerId()));
        }

        // 部门ID
        if (ToolKits.isNotEmpty(matchCarrierVO.getDepartmentId())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT, String.valueOf(matchCarrierVO.getDepartmentId()));
        }

        // 发货方类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getShipperType())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER, String.valueOf(matchCarrierVO.getShipperType()) + "," + String.valueOf(matchCarrierVO.getShipperUnitId()));
        }
        // 收货方类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getConsigneeType())) {
            conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE, String.valueOf(matchCarrierVO.getConsigneeType()) + "," + String.valueOf(matchCarrierVO.getConsigneeUnitId()));
        }
        for (TmsCarrierMthStyModel carrierMthStyModel : mthStyModels) {
            // 满足条件
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierMthStyCtModel.class);
            Restriction r3 = Restrictions.eq(TmsCarrierMthStyCtModel::getStrategyId, carrierMthStyModel.getId());
            Restriction r4 = Restrictions.eq(TmsCarrierMthStyCtModel::getEnterpriseId, matchCarrierVO.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r3, r4));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"strategyConditionType"}));
            List<TmsCarrierMthStyCtModel> mthStyCtModels = tmsCarrierMthStrategyCtMapper.findByCriteria(carrierCtCriteria);

            if (ToolKits.isEmpty(mthStyCtModels)) {
                continue;
            }
            // 判定满足条件
            int fitCnt = 0;
            for (TmsCarrierMthStyCtModel conditionModel : mthStyCtModels) {
                switch (conditionModel.getStrategyConditionType().intValue()) {
                    case 2:
                    case 3:
                        String[] cmpStr1 = conditionMap.get(conditionModel.getStrategyConditionType()).split(",");
                        if (cmpStr1[0].equals(String.valueOf(conditionModel.getStrategyConditionValue())) && conditionModel.getUnitId().contains(cmpStr1[1])) {
                            fitCnt++;
                        }
                        break;
                    case 8:
                    case 9:
                        if (conditionModel.getUnitId().contains(conditionMap.get(conditionModel.getStrategyConditionType()))) {
                            fitCnt++;
                        }
                        break;
                    default:
                        if (conditionMap.containsValue(conditionModel.getStrategyConditionType() + "_" + String.valueOf(conditionModel.getStrategyConditionValue()))) {
                            fitCnt++;
                        }
                        break;
                }
            }
            // 匹配到了承运方匹配策略
            if (fitCnt > 0 && fitCnt == mthStyCtModels.size()) {
                log.info("运单【" + matchCarrierVO.getShipbillId() + "】匹配到的承运方策略:" + JSON.toJSONString(carrierMthStyModel.getName()));

                Map<Integer, String> levelMap = Maps.newHashMap();
                levelMap.put(carrierMthStyModel.getMatchTypeCustomer(), "按照客户匹配承运方");
                levelMap.put(carrierMthStyModel.getMatchTypeLineConsignor(), "按照线路匹配承运方");
                levelMap.put(carrierMthStyModel.getMatchTypeCarrier(), "按照承运方匹配策略");
                if (ObjectUtil.isNotEmpty(carrierMthStyModel.getMatchTypeWeight())) {
                    levelMap.put(carrierMthStyModel.getMatchTypeWeight(), "按照重量匹配承运方");
                }
                if (ObjectUtil.isNotEmpty(carrierMthStyModel.getMatchTypeVolume())) {
                    levelMap.put(carrierMthStyModel.getMatchTypeVolume(), "按照体积匹配承运方");
                }
                if (ObjectUtil.isNotEmpty(carrierMthStyModel.getMatchTypeWeightVolume())) {
                    levelMap.put(carrierMthStyModel.getMatchTypeWeightVolume(), "按照重量体积匹配承运方");
                }

                List<Map.Entry<Integer, String>> infoIds = new ArrayList<Map.Entry<Integer, String>>(levelMap.entrySet());

                Collections.sort(infoIds, new Comparator<Map.Entry<Integer, String>>() {
                    public int compare(Map.Entry<Integer, String> o1, Map.Entry<Integer, String> o2) {
                        return (o1.getKey()).toString().compareTo(o2.getKey().toString());
                    }
                });
                for (Map.Entry<Integer, String> entry : infoIds) {
                    if ("按照客户匹配承运方".equals(entry.getValue())) {
                        // 按照客户匹配承运方
                        if (matchCarrierVO.getConsigneeType().equals(DataDictionaryConstant.CONSIGNEE_TYPE_KH)) {
                            TmsCustomerConsignee customerConsignee = tmsCustomerConsigneeMapper.findById(matchCarrierVO.getConsigneeId(), TmsCustomerConsignee.class);
                            if (ToolKits.isNotEmpty(customerConsignee) && ToolKits.isNotEmpty(customerConsignee.getCarrierId())) {
                                shipbillMatchCarrierDTO.setCarrierType(customerConsignee.getCarrierType());
                                shipbillMatchCarrierDTO.setCarrierId(String.valueOf(customerConsignee.getCarrierId()));
                                shipbillMatchCarrierDTO.setCarrierCode(customerConsignee.getCarrierCode());
                                shipbillMatchCarrierDTO.setCarrierName(customerConsignee.getCarrierName());

                                shipbillMatchCarrierDTO.setDriverPhoneNumber(customerConsignee.getCarrierPhoneNumber());
                                if (ToolKits.isNotEmpty(customerConsignee.getVehicleId())) {
                                    shipbillMatchCarrierDTO.setVehicleId(Long.valueOf(customerConsignee.getVehicleId()));
                                }
                                shipbillMatchCarrierDTO.setVehicleCode(customerConsignee.getVehicleCode());
                                shipbillMatchCarrierDTO.setVehicleName(customerConsignee.getVehicleName());
                                shipbillMatchCarrierDTO.setVehiclePlateNumber(customerConsignee.getVehiclePlateNumber());

                                if (customerConsignee.getCarrierType().equals(DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER)) {
                                    // 获取车辆信息
                                    getVehicleInfo(shipbillMatchCarrierDTO, customerConsignee.getCarrierId(), customerConsignee.getVehicleCode(), customerConsignee.getVehiclePlateNumber(), matchCarrierVO.getEnterpriseId(), configModel);
                                }
                                log.info("按照客户匹配承运方:" + JSON.toJSONString(shipbillMatchCarrierDTO));
                                return shipbillMatchCarrierDTO;
                            }
                        }
                    } else if ("按照线路匹配承运方".equals(entry.getValue())) {
                        // 线路
                        if (ToolKits.isEmpty(matchCarrierVO.getLineId())) {
                            continue;
                        }
                        TmsBaseLineModel lineModel = tmsBaseTranslineMapper.findById(matchCarrierVO.getLineId(), TmsBaseLineModel.class);
                        if (ToolKits.isNotEmpty(lineModel)) {
                            // 线路承运方
                            Criteria lineCarrierCriteria = Criteria.forClass(TmsLineCarrierLinkModel.class);
                            Restriction r7 = Restrictions.eq(TmsLineCarrierLinkModel::getLineId, lineModel.getLineId());
                            lineCarrierCriteria.setRestriction(Restrictions.and(r7));
                            lineCarrierCriteria.setOrder(Order.newInstance().desc(new String[]{"shares"}));
                            List<TmsLineCarrierLinkModel> lineCarrierLinkModels = tmsLineCarrierLinkMapper.findByCriteria(lineCarrierCriteria);

                            if (ToolKits.isNotEmpty(lineCarrierLinkModels) && lineCarrierLinkModels.size() == 1) {
                                TmsLineCarrierLinkModel carrierLinkModel = lineCarrierLinkModels.get(0);
                                if (ToolKits.isEmpty(carrierLinkModel.getCarrierId())) {
                                    continue;
                                }
                                shipbillMatchCarrierDTO.setCarrierType(carrierLinkModel.getType());
                                shipbillMatchCarrierDTO.setCarrierId(String.valueOf(carrierLinkModel.getCarrierId()));
                                shipbillMatchCarrierDTO.setCarrierCode(carrierLinkModel.getCarrierCode());
                                shipbillMatchCarrierDTO.setCarrierName(carrierLinkModel.getCarrierName());

                                shipbillMatchCarrierDTO.setDriverPhoneNumber(carrierLinkModel.getPhoneNumber());
                                shipbillMatchCarrierDTO.setVehicleId(carrierLinkModel.getVehicleId());
                                shipbillMatchCarrierDTO.setVehicleCode(carrierLinkModel.getVehicleCode());
                                shipbillMatchCarrierDTO.setVehicleName(carrierLinkModel.getVehicleName());
                                shipbillMatchCarrierDTO.setVehiclePlateNumber(carrierLinkModel.getVehiclePlateNumber());

                                if (carrierLinkModel.getType().equals(DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER)) {
                                    // 获取车辆信息
                                    getVehicleInfo(shipbillMatchCarrierDTO, carrierLinkModel.getCarrierId(), carrierLinkModel.getVehicleCode(), carrierLinkModel.getVehiclePlateNumber(), matchCarrierVO.getEnterpriseId(), configModel);
                                }
                                log.info("按照线路匹配承运方:" + JSON.toJSONString(shipbillMatchCarrierDTO));
                                return shipbillMatchCarrierDTO;
                            }
                        }
                    } else if ("按照承运方匹配策略".equals(entry.getValue())) {
                        // 根据运单匹配承运方策略
                        ShipbillMatchCarrierConfigDTO rsDTO = carrierConfigService.shipbillMatchCarrier(matchCarrierVO);
                        if (ToolKits.isNotEmpty(rsDTO) && ToolKits.isNotEmpty(rsDTO.getCarrierType())) {
                            // 承运商信息
                            if (rsDTO.getCarrierType().equals(DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER)) {
                                shipbillMatchCarrierDTO.setCarrierId(rsDTO.getDriverId());
                                shipbillMatchCarrierDTO.setCarrierCode(rsDTO.getDriverCode());
                                shipbillMatchCarrierDTO.setCarrierName(rsDTO.getDriverName());
                                Long driverId = Long.valueOf(rsDTO.getDriverId().split(",")[0]);
                                // 获取车辆信息
                                getVehicleInfo(shipbillMatchCarrierDTO, driverId, rsDTO.getVehicleCode(), rsDTO.getVehiclePlateNumber(), matchCarrierVO.getEnterpriseId(), configModel);
                            } else {
                                shipbillMatchCarrierDTO.setCarrierId(String.valueOf(rsDTO.getCarrierId()));
                                shipbillMatchCarrierDTO.setCarrierCode(rsDTO.getCarrierCode());
                                shipbillMatchCarrierDTO.setCarrierName(rsDTO.getCarrierName());
                            }
                            shipbillMatchCarrierDTO.setCarrierType(rsDTO.getCarrierType());

                            // 司机信息
                            shipbillMatchCarrierDTO.setDriverId(rsDTO.getDriverId());
                            shipbillMatchCarrierDTO.setDriverCode(rsDTO.getDriverCode());
                            shipbillMatchCarrierDTO.setDriverName(rsDTO.getDriverName());
                            shipbillMatchCarrierDTO.setDriverPhoneNumber(rsDTO.getDriverPhoneNumber());

                            // 送货员信息
                            shipbillMatchCarrierDTO.setDeliverymanId(rsDTO.getDeliverymanId());
                            shipbillMatchCarrierDTO.setDeliverymanCode(rsDTO.getDeliverymanCode());
                            shipbillMatchCarrierDTO.setDeliverymanName(rsDTO.getDeliverymanName());
                            shipbillMatchCarrierDTO.setDeliverymanPhoneNumber(rsDTO.getDeliverymanPhoneNumber());

                            // 车辆
                            if (ToolKits.isNotEmpty(rsDTO.getVehicleId())) {
                                shipbillMatchCarrierDTO.setVehicleId(Long.valueOf(rsDTO.getVehicleId()));
                            }

                            shipbillMatchCarrierDTO.setVehicleCode(rsDTO.getVehicleCode());
                            shipbillMatchCarrierDTO.setVehicleName(rsDTO.getVehicleName());
                            shipbillMatchCarrierDTO.setVehiclePlateNumber(rsDTO.getVehiclePlateNumber());

                            // 车型
                            if (ToolKits.isNotEmpty(rsDTO.getVehicleTypeId())) {
                                shipbillMatchCarrierDTO.setVehicleTypeId(Long.valueOf(rsDTO.getVehicleTypeId()));
                                shipbillMatchCarrierDTO.setVehicleTypeCode(rsDTO.getVehicleTypeCode());
                                shipbillMatchCarrierDTO.setVehicleTypeName(rsDTO.getVehicleTypeName());
                            }

                            shipbillMatchCarrierDTO.setWeight(rsDTO.getWeight());
                            shipbillMatchCarrierDTO.setVolume(rsDTO.getVolume());

                            log.info("按照承运方匹配策略:" + JSON.toJSONString(shipbillMatchCarrierDTO));
                            return shipbillMatchCarrierDTO;
                        }
                    } else if ("按照重量匹配承运方".equals(entry.getValue())) {
                        if (ToolKits.isEmpty(matchCarrierVO.getTotalWeight()) || NumberUtil.equals(BigDecimal.ZERO,matchCarrierVO.getTotalWeight())) {
                            continue;
                        }
                        getVehicleTypeInfo(shipbillMatchCarrierDTO,matchCarrierVO.getTotalWeight(),BigDecimal.ZERO);
                        log.info("按照重量匹配策略:" + JSON.toJSONString(shipbillMatchCarrierDTO));
                        if (ObjectUtil.isNotEmpty(shipbillMatchCarrierDTO.getVehicleTypeCode())) {
                            return shipbillMatchCarrierDTO;
                        }
                    } else if ("按照体积匹配承运方".equals(entry.getValue())) {
                        if (ToolKits.isEmpty(matchCarrierVO.getTotalVolume()) || NumberUtil.equals(BigDecimal.ZERO,matchCarrierVO.getTotalVolume())) {
                            continue;
                        }
                        getVehicleTypeInfo(shipbillMatchCarrierDTO,BigDecimal.ZERO,matchCarrierVO.getTotalVolume());
                        log.info("按照体积匹配策略:" + JSON.toJSONString(shipbillMatchCarrierDTO));
                        if (ObjectUtil.isNotEmpty(shipbillMatchCarrierDTO.getVehicleTypeCode())) {
                            return shipbillMatchCarrierDTO;
                        }
                    } else if ("按照重量体积匹配承运方".equals(entry.getValue())) {
                        if (ToolKits.isEmpty(matchCarrierVO.getTotalWeight()) || NumberUtil.equals(BigDecimal.ZERO,matchCarrierVO.getTotalWeight())
                        || ToolKits.isEmpty(matchCarrierVO.getTotalVolume()) || NumberUtil.equals(BigDecimal.ZERO,matchCarrierVO.getTotalVolume())) {
                            continue;
                        }
                        getVehicleTypeInfo(shipbillMatchCarrierDTO,matchCarrierVO.getTotalWeight(),matchCarrierVO.getTotalVolume());
                        log.info("按照重量体积匹配策略:" + JSON.toJSONString(shipbillMatchCarrierDTO));
                        if (ObjectUtil.isNotEmpty(shipbillMatchCarrierDTO.getVehicleTypeCode())) {
                            return shipbillMatchCarrierDTO;
                        }
                    }
                }
            }
        }
        return shipbillMatchCarrierDTO;
    }

    public ShipbillMatchCarrierDTO getVehicleInfo(ShipbillMatchCarrierDTO matchCarrierDTO, Long driverId, String vehicleCode, String VehiclePlateNumber, Long enterpriseId, String configModel) {
        VehiclePopupVO vehiclePopupVO = new VehiclePopupVO();
        vehiclePopupVO.setDriverId(driverId);
        TmsBaseDriverModel tmsBaseDriverModel = tmsBaseDriverMapper.findById(vehiclePopupVO.getDriverId(), TmsBaseDriverModel.class);
        if (ObjectUtil.isNotNull(tmsBaseDriverModel)) {
            if (ObjectUtil.isNotNull(tmsBaseDriverModel.getAffiliationId())) {
                matchCarrierDTO.setDriverAffiliationId(tmsBaseDriverModel.getAffiliationId().toString());
            }
            matchCarrierDTO.setDriverAffiliationCode(tmsBaseDriverModel.getAffiliationCode());
            matchCarrierDTO.setDriverAffiliationName(tmsBaseDriverModel.getAffiliationName());
        }
        if (ToolKits.isNotEmpty(vehicleCode)) {
            vehiclePopupVO.setCode(vehicleCode);
        }
        if (ToolKits.isNotEmpty(VehiclePlateNumber)) {
            vehiclePopupVO.setPlateNumber(VehiclePlateNumber);
        }
        vehiclePopupVO.setEnterpriseId(enterpriseId);
        vehiclePopupVO.setConfigParam(configModel);
        List<VehiclePopupDTO> popupDTOPage = vehicleService.vehicleData(vehiclePopupVO);
        if (ToolKits.isNotEmpty(popupDTOPage)) {
            VehiclePopupDTO vehiclePopupDTO = popupDTOPage.get(0);
            if (ToolKits.isNotEmpty(vehiclePopupDTO.getTypeCategory())) {
                matchCarrierDTO.setVehicleTypeCategory(vehiclePopupDTO.getTypeCategory());
                matchCarrierDTO.setVehicleTypeCategoryName(vehiclePopupDTO.getTypeCategoryName());

                matchCarrierDTO.setTypeCategory(vehiclePopupDTO.getTypeCategory());
                matchCarrierDTO.setTypeCategoryName(vehiclePopupDTO.getTypeCategoryName());
            }
            if (ToolKits.isNotEmpty(vehiclePopupDTO.getAffiliationId())) {
                matchCarrierDTO.setVehicleAffiliationId(String.valueOf(vehiclePopupDTO.getAffiliationId()));
                matchCarrierDTO.setVehicleAffiliationCode(String.valueOf(vehiclePopupDTO.getAffiliationCode()));
                matchCarrierDTO.setVehicleAffiliationName(String.valueOf(vehiclePopupDTO.getAffiliationName()));
            }
            if (ObjectUtil.isNotNull(vehiclePopupDTO.getVehicleTypeId())) {
                matchCarrierDTO.setVehicleTypeId(vehiclePopupDTO.getVehicleTypeId());
                matchCarrierDTO.setVehicleTypeCode(vehiclePopupDTO.getVehicleTypeCode());
                matchCarrierDTO.setVehicleTypeName(vehiclePopupDTO.getVehicleTypeName());
            }
            matchCarrierDTO.setWeight(vehiclePopupDTO.getWeight());
            matchCarrierDTO.setVolume(vehiclePopupDTO.getVolume());

            matchCarrierDTO.setVehicleAscription(vehiclePopupDTO.getVehicleAscription());
            matchCarrierDTO.setVehicleAscriptionName(vehiclePopupDTO.getVehicleAscriptionName());
            if (ToolKits.isNotEmpty(configModel)) {
                // 设备
                if (configModel.contains("1") && ToolKits.isNotEmpty(vehiclePopupDTO.getDeviceId())) {
                    matchCarrierDTO.setVehicleDeviceId(vehiclePopupDTO.getDeviceId());
                    matchCarrierDTO.setVehicleDeviceCode(vehiclePopupDTO.getDeviceCode());
                    matchCarrierDTO.setVehicleDeviceName(vehiclePopupDTO.getDeviceName());

                    matchCarrierDTO.setDeviceId(vehiclePopupDTO.getDeviceId());
                    matchCarrierDTO.setDeviceCode(vehiclePopupDTO.getDeviceCode());
                    matchCarrierDTO.setDeviceName(vehiclePopupDTO.getDeviceName());
                }
                // 容器
                if ((configModel.contains("2") || configModel.contains("3")) && ToolKits.isNotEmpty(vehiclePopupDTO.getContainerId())) {
                    matchCarrierDTO.setContainerId(vehiclePopupDTO.getContainerId());
                    matchCarrierDTO.setContainerCode(vehiclePopupDTO.getContainerCode());
                    matchCarrierDTO.setContainerName(vehiclePopupDTO.getContainerName());
                }
            }
        }
        return matchCarrierDTO;
    }

    public ShipbillMatchCarrierDTO getVehicleTypeInfo(ShipbillMatchCarrierDTO matchCarrierDTO, BigDecimal weight,BigDecimal volume) {
        VehicleTypeCheckVO checkVO = new VehicleTypeCheckVO();
        checkVO.setWeight(weight);
        checkVO.setVolume(volume);
        VehicleTypeCheckDTO checkDTO = vehicleTypeService.queryVehicleTypeByWeightVolume(checkVO);
        if (ObjectUtil.isNotEmpty(checkDTO) && ObjectUtil.isNotEmpty(checkDTO.getName())) {
            matchCarrierDTO.setVehicleTypeId(checkDTO.getId());
            matchCarrierDTO.setVehicleTypeCode(checkDTO.getCode());
            matchCarrierDTO.setVehicleTypeName(checkDTO.getName());
            matchCarrierDTO.setCarrierType(2);
            matchCarrierDTO.setWeight(checkDTO.getWeight());
            matchCarrierDTO.setVolume(checkDTO.getVolume());
        }
        return matchCarrierDTO;
    }
}
