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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
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.TmsCarrierConfigCtMapper;
import com.prolog.rdc.tms.dao.mapper.TmsCarrierConfigMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.CarrierConfigCtViewDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.CarrierConfigPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.CarrierConfigViewDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.ShipbillMatchCarrierConfigDTO;
import com.prolog.rdc.tms.model.entity.TmsCarrierConfigCtModel;
import com.prolog.rdc.tms.model.entity.TmsCarrierConfigModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.*;
import com.prolog.rdc.tms.service.CarrierConfigService;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.util.PublicMethods;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 承运方配置业务实现层
 *
 * @author :lhb
 * @version :1.0.0
 * @className :CarrierConfigServiceImpl
 * @creationTime :2022/05/07
 * @description :
 * @modifier :
 * @modifyTime :
 * @reviseRemarks:
 */
@Service
public class CarrierConfigServiceImpl implements CarrierConfigService {

    @Autowired
    private TmsCarrierConfigMapper tmsCarrierConfigMapper;

    @Autowired
    private TmsCarrierConfigCtMapper tmsCarrierConfigCtMapper;

    @Autowired
    private SysCodeengineService sysCodeengineService;

    @Autowired
    private SysRefdatadService sysRefdatadService;

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

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

        Criteria carrierCriteria = Criteria.forClass(TmsCarrierConfigModel.class);
        Restriction r1 = ToolKits.isEmpty(pageVO.getConfigName()) ? null : Restrictions.like(TmsCarrierConfigModel::getName, "%" + pageVO.getConfigName() + "%");
        Restriction r2 = ToolKits.isEmpty(pageVO.getStatus()) ? null : Restrictions.eq(TmsCarrierConfigModel::getStatus, pageVO.getStatus());
        Restriction r3 = Restrictions.eq(TmsCarrierConfigModel::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<TmsCarrierConfigModel> configModels = tmsCarrierConfigMapper.findByCriteria(carrierCriteria);

        // 状态
        List<Map<String, Object>> status = sysRefdatadService.getDropDownById(DataDictionaryConstant.GENERAL_STATUS, null, null, null);
        List<Map<String, Object>> carrierType = sysRefdatadService.getDropDownById(DataDictionaryConstant.LINE_CARRIER_TYPE, 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<CarrierConfigPageDTO> pageDTOS = Lists.newArrayList();
        for (TmsCarrierConfigModel carrierConfigModel : configModels) {
            CarrierConfigPageDTO configPageDTO = new CarrierConfigPageDTO();
            BeanUtils.copyProperties(carrierConfigModel, configPageDTO);
            configPageDTO.setConfigName(carrierConfigModel.getName());
            if (carrierConfigModel.getCarrierType().equals(DataDictionaryConstant.LINE_CARRIER_TYPE_DRIVER)) {
                configPageDTO.setCarrierNameList(carrierConfigModel.getDriverName());
            } else {
                configPageDTO.setCarrierNameList(carrierConfigModel.getCarrierName());
            }
            configPageDTO.setCarrierTypeName(PublicMethods.change(carrierType, String.valueOf(configPageDTO.getCarrierType())));
            configPageDTO.setStatusName(PublicMethods.change(status, String.valueOf(configPageDTO.getStatus())));
            // 满足条件
            List<String> conditions = Lists.newArrayList();
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierConfigCtModel.class);
            Restriction r4 = Restrictions.eq(TmsCarrierConfigCtModel::getConfigId, carrierConfigModel.getId());
            Restriction r5 = Restrictions.eq(TmsCarrierConfigCtModel::getEnterpriseId, user.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r4, r5));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"configConditionType"}));
            List<TmsCarrierConfigCtModel> mthStyCtModels = tmsCarrierConfigCtMapper.findByCriteria(carrierCtCriteria);

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

        Page<CarrierConfigPageDTO> page = PageUtils.getPage(pageDTOS);
        if (configModels instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page<TmsCarrierConfigModel> p0 = (com.github.pagehelper.Page<TmsCarrierConfigModel>) configModels;
            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 carrierConfigSaveVO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CarrierConfigSaveVO carrierConfigSaveVO) {
        ToolKits.isParamNull(carrierConfigSaveVO, "承运方配置保存请求不为空");
        ToolKits.isParamNull(carrierConfigSaveVO.getConfigName(), "配置名称不为空");
        ToolKits.isParamNull(carrierConfigSaveVO.getCarrierType(), "承运方类型不为空");

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

        // 运单匹配条件集合
        Map<Integer, String> conditionMap = Maps.newHashMap();
        for (CarrierConfigCtSaveVO conditionVO : carrierConfigSaveVO.getConfigCtSaveVOList()) {
            // 配送方式
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 订单类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 订单业务类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 运输模式
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 温度要求
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 货主ID
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_OWNER, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_OWNER) + "_" + units);
                continue;
            }

            // 部门ID
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_DEPARTMENT, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DEPARTMENT) + "_" + units);
                continue;
            }
            // 收货方类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_CONSIGNEE, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_CONSIGNEE) + "_" + String.valueOf(conditionVO.getConfigConditionValue()) + "," + units);
                continue;
            }
            // 发货方类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_SHIPPER, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_SHIPPER) + "_" + String.valueOf(conditionVO.getConfigConditionValue()) + "," + units);
                continue;
            }
        }

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

        for (TmsCarrierConfigModel carrierConfigModel : configModels) {
            // 满足条件
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierConfigCtModel.class);
            Restriction r3 = Restrictions.eq(TmsCarrierConfigCtModel::getConfigId, carrierConfigModel.getId());
            Restriction r4 = Restrictions.eq(TmsCarrierConfigCtModel::getEnterpriseId, user.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r3, r4));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"configConditionType"}));
            List<TmsCarrierConfigCtModel> mthStyCtModels = tmsCarrierConfigCtMapper.findByCriteria(carrierCtCriteria);

            // 判定满足条件
            int fitCnt = 0;
            for (TmsCarrierConfigCtModel conditionModel : mthStyCtModels) {
                switch (conditionModel.getConfigConditionType().intValue()) {
                    case 2:
                    case 3:
                        String unit1s = Arrays.stream(String.valueOf(conditionModel.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                        if (conditionMap.containsValue(String.valueOf(conditionModel.getConfigConditionType()) + "_" + String.valueOf(conditionModel.getConfigConditionValue()) + "," + 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.getConfigConditionType()) + "_" + unit2s)) {
                            fitCnt++;
                        }
                        break;
                    default:
                        String unit3s = Arrays.stream(String.valueOf(conditionModel.getConfigConditionValue()).split(",")).sorted().collect(Collectors.joining(","));

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

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

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

        TmsCarrierConfigModel carrierConfigModel = new TmsCarrierConfigModel();
        BeanUtils.copyProperties(carrierConfigSaveVO, carrierConfigModel);

        // ID赋值
        carrierConfigModel.setId(Long.valueOf(id));
        carrierConfigModel.setName(carrierConfigSaveVO.getConfigName());
        carrierConfigModel.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
        carrierConfigModel.setEnterpriseId(user.getEnterpriseId());
        carrierConfigModel.setEnterpriseName(user.getEnterpriseName());
        carrierConfigModel.setCreateperson(user.getNickname());
        carrierConfigModel.setCreatetime(date);
        carrierConfigModel.setUpdateperson(user.getNickname());
        carrierConfigModel.setUpdatetime(date);
        // 保存
        tmsCarrierConfigMapper.save(carrierConfigModel);
        // 策略条件
        for (CarrierConfigCtSaveVO configCtSaveVO : carrierConfigSaveVO.getConfigCtSaveVOList()) {
            String cid = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_CARRIER_CONFIG_CT_ID, "L");
            ToolKits.isParamNull(cid, "承运方配置条件ID生成失败");
            TmsCarrierConfigCtModel carrierConfigCtModel = new TmsCarrierConfigCtModel();
            BeanUtils.copyProperties(configCtSaveVO, carrierConfigCtModel);
            carrierConfigCtModel.setId(Long.valueOf(cid));
            carrierConfigCtModel.setConfigId(Long.valueOf(id));

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

    /**
     * 承运方配置修改
     *
     * @param carrierConfigUpdateVO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CarrierConfigUpdateVO carrierConfigUpdateVO) {
        ToolKits.isParamNull(carrierConfigUpdateVO, "承运方配置修改请求不为空");
        ToolKits.isParamNull(carrierConfigUpdateVO.getId(), "配置ID不为空");
        ToolKits.isParamNull(carrierConfigUpdateVO.getConfigCtUpdateVOList(), "承运方配置条件不为空");

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

        // 运单匹配条件集合
        Map<Integer, String> conditionMap = Maps.newHashMap();
        for (CarrierConfigCtUpdateVO conditionVO : carrierConfigUpdateVO.getConfigCtUpdateVOList()) {
            // 配送方式
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 订单类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 订单业务类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 运输模式
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 温度要求
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE)) {
                conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE) + "_" + String.valueOf(conditionVO.getConfigConditionValue()));
                continue;
            }
            // 货主ID
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_OWNER, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_OWNER) + "_" + units);
                continue;
            }

            // 部门ID
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_DEPARTMENT, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DEPARTMENT) + "_" + units);
                continue;
            }
            // 收货方类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_CONSIGNEE, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_CONSIGNEE) + "_" + String.valueOf(conditionVO.getConfigConditionValue()) + "," + units);
                continue;
            }
            // 发货方类型
            if (conditionVO.getConfigConditionType().equals(DataDictionaryConstant.CONFIG_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.CONFIG_CONDITION_TYPE_SHIPPER, String.valueOf(DataDictionaryConstant.CONFIG_CONDITION_TYPE_SHIPPER) + "_" + String.valueOf(conditionVO.getConfigConditionValue()) + "," + units);
                continue;
            }
        }

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

        for (TmsCarrierConfigModel carrierConfigModel : carrierConfigModels) {
            // 满足条件
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierConfigCtModel.class);
            Restriction r3 = Restrictions.eq(TmsCarrierConfigCtModel::getConfigId, carrierConfigModel.getId());
            Restriction r4 = Restrictions.eq(TmsCarrierConfigCtModel::getEnterpriseId, user.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r3, r4));
            carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"configConditionType"}));
            List<TmsCarrierConfigCtModel> mthStyCtModels = tmsCarrierConfigCtMapper.findByCriteria(carrierCtCriteria);

            // 判定满足条件
            int fitCnt = 0;
            for (TmsCarrierConfigCtModel conditionModel : mthStyCtModels) {
                switch (conditionModel.getConfigConditionType().intValue()) {
                    case 2:
                    case 3:
                        String unit1s = Arrays.stream(String.valueOf(conditionModel.getUnitId()).split(",")).sorted().collect(Collectors.joining(","));
                        if (conditionMap.containsValue(String.valueOf(conditionModel.getConfigConditionType()) + "_" + String.valueOf(conditionModel.getConfigConditionValue()) + "," + 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.getConfigConditionType()) + "_" + unit2s)) {
                            fitCnt++;
                        }
                        break;
                    default:
                        String unit3s = Arrays.stream(String.valueOf(conditionModel.getConfigConditionValue()).split(",")).sorted().collect(Collectors.joining(","));

                        if (conditionMap.containsValue(String.valueOf(conditionModel.getConfigConditionType()) + "_" + 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("memo", carrierConfigUpdateVO.getMemo());

        mapUpdateMthStyModel.put("carrierType", carrierConfigUpdateVO.getCarrierType());
        mapUpdateMthStyModel.put("carrierId", carrierConfigUpdateVO.getCarrierId());
        mapUpdateMthStyModel.put("carrierCode", carrierConfigUpdateVO.getCarrierCode());
        mapUpdateMthStyModel.put("carrierName", carrierConfigUpdateVO.getCarrierName());

        mapUpdateMthStyModel.put("driverId", carrierConfigUpdateVO.getDriverId());
        mapUpdateMthStyModel.put("driverCode", carrierConfigUpdateVO.getDriverCode());
        mapUpdateMthStyModel.put("driverName", carrierConfigUpdateVO.getDriverName());

        mapUpdateMthStyModel.put("driverPhoneNumber", carrierConfigUpdateVO.getDriverPhoneNumber());

        mapUpdateMthStyModel.put("vehicleId", carrierConfigUpdateVO.getVehicleId());
        mapUpdateMthStyModel.put("vehicleCode", carrierConfigUpdateVO.getVehicleCode());
        mapUpdateMthStyModel.put("vehicleName", carrierConfigUpdateVO.getVehicleName());
        mapUpdateMthStyModel.put("vehiclePlateNumber", carrierConfigUpdateVO.getVehiclePlateNumber());

        mapUpdateMthStyModel.put("vehicleTypeId", carrierConfigUpdateVO.getVehicleTypeId());
        mapUpdateMthStyModel.put("vehicleTypeCode", carrierConfigUpdateVO.getVehicleTypeCode());
        mapUpdateMthStyModel.put("vehicleTypeName", carrierConfigUpdateVO.getVehicleTypeName());

        mapUpdateMthStyModel.put("deliverymanId", carrierConfigUpdateVO.getDeliverymanId());
        mapUpdateMthStyModel.put("deliverymanCode", carrierConfigUpdateVO.getDeliverymanCode());
        mapUpdateMthStyModel.put("deliverymanName", carrierConfigUpdateVO.getDeliverymanName());
        mapUpdateMthStyModel.put("deliverymanPhoneNumber", carrierConfigUpdateVO.getDeliverymanPhoneNumber());
        mapUpdateMthStyModel.put("weight", carrierConfigUpdateVO.getWeight());
        mapUpdateMthStyModel.put("volume", carrierConfigUpdateVO.getVolume());

        mapUpdateMthStyModel.put("updateperson", user.getNickname());
        mapUpdateMthStyModel.put("updatetime", date);

        // 修改
        tmsCarrierConfigMapper.updateMapById(carrierConfigUpdateVO.getId(), mapUpdateMthStyModel, TmsCarrierConfigModel.class);

        // 删除条件
        Criteria conditionCriteria = Criteria.forClass(TmsCarrierConfigCtModel.class);
        Restriction r7 = Restrictions.eq(TmsCarrierConfigCtModel::getConfigId, carrierConfigUpdateVO.getId());
        conditionCriteria.setRestriction(Restrictions.and(r7));
        tmsCarrierConfigCtMapper.deleteByCriteria(conditionCriteria);

        // 策略条件修改
        for (CarrierConfigCtUpdateVO carrierConfigCtUpdateVO : carrierConfigUpdateVO.getConfigCtUpdateVOList()) {
            String cid = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_CARRIER_CONFIG_CT_ID, "L");
            ToolKits.isParamNull(cid, "承运方配置条件ID生成失败");
            TmsCarrierConfigCtModel carrierConfigCtModel = new TmsCarrierConfigCtModel();
            BeanUtils.copyProperties(carrierConfigCtUpdateVO, carrierConfigCtModel);
            carrierConfigCtModel.setId(Long.valueOf(cid));
            carrierConfigCtModel.setConfigId(Long.valueOf(carrierConfigUpdateVO.getId()));

            carrierConfigCtModel.setEnterpriseId(user.getEnterpriseId());
            carrierConfigCtModel.setEnterpriseName(user.getEnterpriseName());
            carrierConfigCtModel.setCreateperson(user.getNickname());
            carrierConfigCtModel.setCreatetime(date);
            tmsCarrierConfigCtMapper.save(carrierConfigCtModel);
        }
    }

    /**
     * 承运方配置停用
     *
     * @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 -> {
            tmsCarrierConfigMapper.updateMapById(id, mapUpdate, TmsCarrierConfigModel.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 -> {
            tmsCarrierConfigMapper.updateMapById(id, mapUpdate, TmsCarrierConfigModel.class);
        });
    }

    /**
     * 承运方配置查看
     *
     * @param viewVO
     * @return
     */
    public CarrierConfigViewDTO view(CarrierConfigViewVO viewVO) {
        ToolKits.isParamNull(viewVO, "承运方配置查看请求不为空");
        ToolKits.isParamNull(viewVO.getId(), "承运方配置ID不为空");

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

        // 状态
        List<Map<String, Object>> status = sysRefdatadService.getDropDownById(DataDictionaryConstant.GENERAL_STATUS, null, null, null);
        List<Map<String, Object>> carrierType = sysRefdatadService.getDropDownById(DataDictionaryConstant.LINE_CARRIER_TYPE, null, null, null);


        TmsCarrierConfigModel carrierConfigModel = tmsCarrierConfigMapper.findById(viewVO.getId(), TmsCarrierConfigModel.class);

        if (ToolKits.isEmpty(carrierConfigModel)) {
            throw new PrologException("查看的承运方策略不存在");
        }
        Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierConfigCtModel.class);
        Restriction r1 = Restrictions.eq(TmsCarrierConfigCtModel::getConfigId, viewVO.getId());
        Restriction r2 = Restrictions.eq(TmsCarrierConfigCtModel::getEnterpriseId, user.getEnterpriseId());
        carrierCtCriteria.setRestriction(Restrictions.and(r1, r2));
        carrierCtCriteria.setOrder(Order.newInstance().asc(new String[]{"configConditionType"}));
        List<TmsCarrierConfigCtModel> carrierConfigCtModels = tmsCarrierConfigCtMapper.findByCriteria(carrierCtCriteria);

        CarrierConfigViewDTO viewDTO = new CarrierConfigViewDTO();
        BeanUtils.copyProperties(carrierConfigModel,viewDTO);
        viewDTO.setConfigName(carrierConfigModel.getName());
        viewDTO.setMemo(carrierConfigModel.getMemo());

        viewDTO.setCarrierTypeName(PublicMethods.change(carrierType, String.valueOf(viewDTO.getCarrierType())));
        viewDTO.setStatusName(PublicMethods.change(status, String.valueOf(viewDTO.getStatus())));

        // 策略条件类型
        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<CarrierConfigCtViewDTO> ctViewDTOS = Lists.newArrayList();
        for (TmsCarrierConfigCtModel ctModel : carrierConfigCtModels) {
            CarrierConfigCtViewDTO ctViewDTO = new CarrierConfigCtViewDTO();
            BeanUtils.copyProperties(ctModel, ctViewDTO);
            ctViewDTO.setConfigConditionTypeName(PublicMethods.change(restMsgStyConditionType, String.valueOf(ctModel.getConfigConditionType())));

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

    /**
     * 根据运单适配匹配承运方配置
     *
     * @param matchCarrierVO
     * @return
     */
    @Override
    public ShipbillMatchCarrierConfigDTO shipbillMatchCarrier(ShipbillMatchCarrierVO matchCarrierVO) {

        ToolKits.isParamNull(matchCarrierVO, "运单匹配承运方配置请求不为空");
        ToolKits.isParamNull(matchCarrierVO.getShipbillId(), "运单ID请求不为空");

        ShipbillMatchCarrierConfigDTO shipbillMatchCarrierDTO = new ShipbillMatchCarrierConfigDTO();


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

        if (ToolKits.isEmpty(mthStyModels)) {
            return shipbillMatchCarrierDTO;
        }
        // 运单匹配条件集合
        Map<Integer, Object> conditionMap = Maps.newHashMap();
        // 配送方式
        if (ToolKits.isNotEmpty(matchCarrierVO.getDeliveryMethodType())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY, DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVERY + "_" + String.valueOf(matchCarrierVO.getDeliveryMethodType()));
        }
        // 订单类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getOrderType())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER, DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER + "_" + String.valueOf(matchCarrierVO.getOrderType()));
        }
        // 订单业务类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getOrderBusinessType())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS, DataDictionaryConstant.CONFIG_CONDITION_TYPE_ORDER_BUSINESS + "_" + String.valueOf(matchCarrierVO.getOrderBusinessType()));
        }
        // 运输模式
        if (ToolKits.isNotEmpty(matchCarrierVO.getTransportType())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT, DataDictionaryConstant.CONFIG_CONDITION_TYPE_TRANSPORT + "_" + String.valueOf(matchCarrierVO.getTransportType()));
        }
        // 温度要求
        if (ToolKits.isNotEmpty(matchCarrierVO.getTemperatureRequirement())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE, DataDictionaryConstant.CONFIG_CONDITION_TYPE_TEMPERATURE + "_" + String.valueOf(matchCarrierVO.getTemperatureRequirement()));
        }
        // 货主ID
        if (ToolKits.isNotEmpty(matchCarrierVO.getCargoOwnerId())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_OWNER, String.valueOf(matchCarrierVO.getCargoOwnerId()));
        }

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

        // 发货方类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getShipperType())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_SHIPPER, String.valueOf(matchCarrierVO.getShipperType()) + "," + String.valueOf(matchCarrierVO.getShipperUnitId()));
        }
        // 收货方类型
        if (ToolKits.isNotEmpty(matchCarrierVO.getConsigneeType())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_CONSIGNEE, String.valueOf(matchCarrierVO.getConsigneeType()) + "," + String.valueOf(matchCarrierVO.getConsigneeUnitId()));
        }
        // 预计发货时间 10
        if (ToolKits.isNotEmpty(matchCarrierVO.getShipperAgingTime())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVER_TIME, matchCarrierVO.getShipperAgingTime());
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_PACK_TIME, matchCarrierVO.getShipperAgingTime());
        }
        // 预计提货时间 11
        if (ToolKits.isNotEmpty(matchCarrierVO.getPickupStartTime())) {
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_DELIVER_TIME, matchCarrierVO.getPickupStartTime());
            conditionMap.put(DataDictionaryConstant.CONFIG_CONDITION_TYPE_PACK_TIME, matchCarrierVO.getPickupStartTime());
        }
        for (TmsCarrierConfigModel carrierConfigModel : mthStyModels) {
            // 满足条件
            Criteria carrierCtCriteria = Criteria.forClass(TmsCarrierConfigCtModel.class);
            Restriction r3 = Restrictions.eq(TmsCarrierConfigCtModel::getConfigId, carrierConfigModel.getId());
            Restriction r4 = Restrictions.eq(TmsCarrierConfigCtModel::getEnterpriseId, matchCarrierVO.getEnterpriseId());
            carrierCtCriteria.setRestriction(Restrictions.and(r3, r4));
            List<TmsCarrierConfigCtModel> carrierConfigCtModels = tmsCarrierConfigCtMapper.findByCriteria(carrierCtCriteria);

            if (ToolKits.isEmpty(carrierConfigCtModels)) {
                continue;
            }
            // 判定满足条件
            int fitCnt = 0;
            for (TmsCarrierConfigCtModel conditionModel : carrierConfigCtModels) {
                switch (conditionModel.getConfigConditionType()) {
                    case 2:
                    case 3:
                        if (ObjectUtil.isNotEmpty(conditionMap.get(conditionModel.getConfigConditionType()))) {
                            String[] cmpStr1 = conditionMap.get(conditionModel.getConfigConditionType()).toString().split(",");
                            if (cmpStr1[0].equals(String.valueOf(conditionModel.getConfigConditionValue())) && conditionModel.getUnitId().contains(cmpStr1[1])) {
                                fitCnt++;
                            }
                        }
                        break;
                    case 8:
                    case 9:
                        if (ObjectUtil.isNotEmpty(conditionMap.get(conditionModel.getConfigConditionType()))) {
                            if (conditionModel.getUnitId().contains(conditionMap.get(conditionModel.getConfigConditionType()).toString())) {
                                fitCnt++;
                            }
                        }
                        break;
                    case 10:
                    case 11:
                        // 预计发货时间 周几
                        boolean isDeliver = this.checkDate(conditionModel,conditionMap);
                        if (isDeliver) {
                            fitCnt++;
                        }
                        break;
                    default:
                        if (conditionMap.containsValue(conditionModel.getConfigConditionType() + "_" + conditionModel.getConfigConditionValue())) {
                            fitCnt++;
                        }
                        break;
                }
            }
            // 匹配到了承运方配置
            if (fitCnt > 0 && fitCnt == carrierConfigCtModels.size()) {
                BeanUtils.copyProperties(carrierConfigModel,shipbillMatchCarrierDTO);
            }
        }
        return shipbillMatchCarrierDTO;
    }

    private boolean checkDate(TmsCarrierConfigCtModel conditionModel, Map<Integer, Object> conditionMap) {
        String unitName = conditionModel.getUnitName();
        String dayOfWeek = dayOfWeek((Date) conditionMap.get(conditionModel.getConfigConditionType()));
        if (unitName.contains(dayOfWeek)) {
            return true;
        }
        return false;
    }

    private String dayOfWeek(Date date) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] weeks = new String[]{"7","1","2","3","4","5","6"};
        Calendar calendar = Calendar.getInstance();
        String format = simpleDateFormat.format(date);
        calendar.setTime(DateUtil.parseDateTime(format));
        int index=calendar.get(Calendar.DAY_OF_WEEK)-1;
        if (index < 0 ) {
            index = 0;
        }
        return weeks[index];
    }
}
