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

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.DateUtils;
import com.prolog.common.util.ToolKits;
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.TmsStrategySplitConditionMapper;
import com.prolog.rdc.tms.dao.mapper.TmsStrategySplitMapper;
import com.prolog.rdc.tms.model.dto.basicConfigure.StrategySplitConditionViewDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StrategySplitMatchingDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StrategySplitPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.StrategySplitViewDTO;
import com.prolog.rdc.tms.model.entity.TmsStrategySplitCnModel;
import com.prolog.rdc.tms.model.entity.TmsStrategySplitModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitConditionSaveVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitConditionUpdateVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitMatchingVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitPageVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitSaveVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitUpdateVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.StrategySplitViewVO;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.SysRefdatadService;
import com.prolog.rdc.tms.service.basicConfigure.StrategySplitService;
import com.prolog.rdc.tms.util.PublicMethods;

/**
 * 策略中心-拆分策略实现层
 *
 * @author zhangsj
 * @since 2022/7/16 9:45
 */
@Service
public class StrategySplitServiceImpl implements StrategySplitService {

	@Autowired
	private TmsStrategySplitMapper tmsStrategySplitMapper;

	@Autowired
	private TmsStrategySplitConditionMapper tmsStrategySplitConditionMapper;

	@Autowired
	private SysRefdatadService sysRefdatadService;

	@Autowired
	private SysCodeengineService sysCodeengineService;

	/**
	 * 分页查询
	 *
	 * @param strategySplitPageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<StrategySplitPageDTO> queryPageData(StrategySplitPageVO strategySplitPageVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 拆分策略状态
		List<Map<String, Object>> restMessage = sysRefdatadService.getDropDownById(DataDictionaryConstant.GENERAL_STATUS, null, null, null);

		// 审批流程类型名称
		List<Map<String, Object>> restMessageFlowType = sysRefdatadService.getDropDownById(DataDictionaryConstant.FLOW_TYPE, null, null, null);
		// 配送方式
		List<Map<String, Object>> restMessageDeliveryMethodType = sysRefdatadService.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		// 收货方类型
		List<Map<String, Object>> restMessageConsigneeType = sysRefdatadService.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		// 发货方类型
		List<Map<String, Object>> restMessageShipperType = sysRefdatadService.getDropDownById(DataDictionaryConstant.SHIPPER_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>> restMsgTemperature = sysRefdatadService.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);

		Criteria criteria = Criteria.forClass(TmsStrategySplitModel.class);
		Restriction r1 = Restrictions.eq(TmsStrategySplitModel::getEnterpriseId, user.getEnterpriseId());
		Restriction r2 = ToolKits.isEmpty(strategySplitPageVO.getStatus()) ? null : Restrictions.eq(TmsStrategySplitModel::getStatus, strategySplitPageVO.getStatus());
		Restriction r3 = ToolKits.isEmpty(strategySplitPageVO.getName()) ? null : Restrictions.like(TmsStrategySplitModel::getName, "%" + strategySplitPageVO.getName() + "%");
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		criteria.setOrder(Order.newInstance().desc("updatetime"));

		PageUtils.startPage(strategySplitPageVO.getPageNum(), strategySplitPageVO.getPageSize());
		List<TmsStrategySplitModel> tmsStrategySplitModels = tmsStrategySplitMapper.findByCriteria(criteria);

		// 返回数据集
		List<StrategySplitPageDTO> strategySplitPageDTOS = Lists.newArrayList();

		for (TmsStrategySplitModel tmsStrategySplitModel : tmsStrategySplitModels) {
			StrategySplitPageDTO strategySplitPageDTO = new StrategySplitPageDTO();
			BeanUtils.copyProperties(tmsStrategySplitModel, strategySplitPageDTO);

			// 状态翻译
			strategySplitPageDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(strategySplitPageDTO.getStatus())));

			// 查询对应条件
			Criteria criteriaCondition = Criteria.forClass(TmsStrategySplitCnModel.class);
			r1 = Restrictions.eq(TmsStrategySplitCnModel::getEnterpriseId, user.getEnterpriseId());
			r2 = Restrictions.eq(TmsStrategySplitCnModel::getStrategyId, tmsStrategySplitModel.getId());
			criteriaCondition.setRestriction(Restrictions.and(r1, r2));
			List<TmsStrategySplitCnModel> tmsStrategySplitConditionModels = tmsStrategySplitConditionMapper.findByCriteria(criteriaCondition);
			// 不为空拼接条件
			if (ToolKits.isNotEmpty(tmsStrategySplitConditionModels)) {
				StringBuffer conditionsBuff = new StringBuffer();
				List<String> conditionIds = Lists.newArrayList();
				for (TmsStrategySplitCnModel conditionModel : tmsStrategySplitConditionModels) {
					switch (conditionModel.getStrategyConditionType().intValue()) {
					case 1:
						// 1配送方式
						conditionsBuff.append("配送方式 ");
						conditionsBuff.append(PublicMethods.change(restMessageDeliveryMethodType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
						break;
					case 2:
						// 2发货方类型
						conditionsBuff.append("发货方类型 ");
						conditionsBuff.append(PublicMethods.change(restMessageShipperType, String.valueOf(conditionModel.getStrategyConditionValue())) + " " + conditionModel.getUnitName() + " ");
						break;
					case 3:
						// 3收货方类型
						conditionsBuff.append("收货方类型 ");
						conditionsBuff.append(PublicMethods.change(restMessageConsigneeType, String.valueOf(conditionModel.getStrategyConditionValue())) + " " + conditionModel.getUnitName() + " ");
						break;
					case 4:
						// 订单类型
						conditionsBuff.append("订单类型 ");
						conditionsBuff.append(PublicMethods.change(restMsgOrderType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
						break;
					case 5:
						// 订单业务类型
						conditionsBuff.append("订单业务类型 ");
						conditionsBuff.append(PublicMethods.change(restMsgOrderBusinessType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
						break;
					case 6:
						// 运输模式
						conditionsBuff.append("运输模式 ");
						conditionsBuff.append(PublicMethods.change(restMsgTransportType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
						break;
					case 7:
						// 温度要求
						conditionsBuff.append("温度要求 ");
						conditionsBuff.append(PublicMethods.change(restMsgTemperature, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
						break;
					case 8:
						// 货主
						conditionsBuff.append("货主 ");
						conditionsBuff.append(conditionModel.getUnitName() + " ");
						break;
					case 9:
						// 部门
						conditionsBuff.append("部门 ");
						conditionsBuff.append(conditionModel.getUnitName() + " ");
						break;
					}
					conditionIds.add(String.valueOf(conditionModel.getId()));
				}
				strategySplitPageDTO.setConditionName(conditionsBuff.toString());
			}
			strategySplitPageDTOS.add(strategySplitPageDTO);
		}
		// 分页重组
		Page<StrategySplitPageDTO> page = PageUtils.getPage(strategySplitPageDTOS);
		if (tmsStrategySplitModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsStrategySplitModel> p0 = (com.github.pagehelper.Page<TmsStrategySplitModel>) tmsStrategySplitModels;
			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 strategySplitViewVO
	 * @return
	 */
	@Override
	public StrategySplitViewDTO viewData(StrategySplitViewVO strategySplitViewVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 承运方策略条件类型
		List<Map<String, Object>> restMessage5 = sysRefdatadService.getDropDownById(DataDictionaryConstant.STRATEGY_CONDITION_TYPE, null, null, null);

		TmsStrategySplitModel tmsStrategySplitModel = tmsStrategySplitMapper.findById(strategySplitViewVO.getId(), TmsStrategySplitModel.class);
		ToolKits.isParamNull(tmsStrategySplitModel, "策略ID[" + strategySplitViewVO.getId() + "]有误,查询为空!");

		// 返回数据
		StrategySplitViewDTO strategySplitViewDTO = new StrategySplitViewDTO();
		BeanUtils.copyProperties(tmsStrategySplitModel, strategySplitViewDTO);

		// 查询对应条件
		Criteria criteria = Criteria.forClass(TmsStrategySplitCnModel.class);
		Restriction r1 = Restrictions.eq(TmsStrategySplitCnModel::getStrategyId, tmsStrategySplitModel.getId());
		criteria.setRestriction(r1);
		List<TmsStrategySplitCnModel> tmsStrategySplitConditionModels = tmsStrategySplitConditionMapper.findByCriteria(criteria);

		// 条件集合
		List<StrategySplitConditionViewDTO> strategySplitConditionViewDTOS = Lists.newArrayList();
		if (ToolKits.isNotEmpty(tmsStrategySplitConditionModels)) {
			for (TmsStrategySplitCnModel conditionModel : tmsStrategySplitConditionModels) {
				StrategySplitConditionViewDTO strategySplitConditionViewDTO = new StrategySplitConditionViewDTO();
				BeanUtils.copyProperties(conditionModel, strategySplitConditionViewDTO);

				strategySplitConditionViewDTO.setStrategyConditionTypeName(PublicMethods.change(restMessage5, String.valueOf(conditionModel.getStrategyConditionType())));
				strategySplitConditionViewDTOS.add(strategySplitConditionViewDTO);
			}

		}

		strategySplitViewDTO.setStrategySplitConditionViewDTOS(strategySplitConditionViewDTOS);

		return strategySplitViewDTO;
	}

	/**
	 * 删除
	 *
	 * @param ids
	 */
	@Override
	public void removeData(List<Long> ids) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

		// 删除策略表
		Criteria criteria = Criteria.forClass(TmsStrategySplitModel.class);
		Restriction r1 = Restrictions.in("id", ids.stream().toArray());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		tmsStrategySplitMapper.deleteByCriteria(criteria);

		// 删除策略条件表
		Criteria conditionCriteria = Criteria.forClass(TmsStrategySplitCnModel.class);
		r1 = Restrictions.in("strategyId", ids.stream().toArray());
		r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		conditionCriteria.setRestriction(Restrictions.and(r1, r2));
		tmsStrategySplitConditionMapper.deleteByCriteria(conditionCriteria);
	}

	/**
	 * 新增
	 *
	 * @param strategySplitSaveVO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveData(StrategySplitSaveVO strategySplitSaveVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

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

		// 判定是否有重复条件流程
		Criteria criteria = Criteria.forClass(TmsStrategySplitModel.class);
		Restriction r1 = Restrictions.eq(TmsStrategySplitModel::getName, strategySplitSaveVO.getName());
		Restriction r2 = Restrictions.eq(TmsStrategySplitModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsStrategySplitModel> tmsStrategySplitModels = tmsStrategySplitMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(tmsStrategySplitModels)) {
			throw new RuntimeException("策略名称[" + strategySplitSaveVO.getName() + "]已存在");
		}

		// 保存策略表
		String id = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_STRATEGY_SPLIT_ID, "L");
		ToolKits.isParamNull(id, "获取运单拆分策略表ID失败！");

		TmsStrategySplitModel tmsStrategySplitModel = new TmsStrategySplitModel();
		BeanUtils.copyProperties(strategySplitSaveVO, tmsStrategySplitModel);

		tmsStrategySplitModel.setId(Long.valueOf(id));
		tmsStrategySplitModel.setStatus(DataDictionaryConstant.GENERAL_STATUS_ENABLE);
		tmsStrategySplitModel.setRematchLine(ToolKits.isEmpty(strategySplitSaveVO.getRematchLine()) ? DataDictionaryConstant.REMATCH_LINE_NO : strategySplitSaveVO.getRematchLine());

		tmsStrategySplitModel.setEnterpriseId(user.getEnterpriseId());
		tmsStrategySplitModel.setEnterpriseName(user.getEnterpriseName());

		tmsStrategySplitModel.setCreatetime(date);
		tmsStrategySplitModel.setCreateperson(user.getNickname());
		tmsStrategySplitModel.setUpdatetime(date);
		tmsStrategySplitModel.setUpdateperson(user.getNickname());

		tmsStrategySplitMapper.save(tmsStrategySplitModel);

		// 保存策略条件表
		for (StrategySplitConditionSaveVO strategySplitConditionSaveVO : strategySplitSaveVO.getStrategySplitConditionSaveVOS()) {
			String conditionId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_STRATEGY_SPLIT_CONDITION_ID, "L");
			ToolKits.isParamNull(conditionId, "获取运单拆分策略表ID失败！");

			TmsStrategySplitCnModel tmsStrategySplitConditionModel = new TmsStrategySplitCnModel();
			BeanUtils.copyProperties(strategySplitConditionSaveVO, tmsStrategySplitConditionModel);

			tmsStrategySplitConditionModel.setId(Long.valueOf(conditionId));
			tmsStrategySplitConditionModel.setStrategyId(Long.valueOf(id));

			tmsStrategySplitConditionModel.setEnterpriseId(user.getEnterpriseId());
			tmsStrategySplitConditionModel.setEnterpriseName(user.getEnterpriseName());

			tmsStrategySplitConditionModel.setCreatetime(date);
			tmsStrategySplitConditionModel.setCreateperson(user.getNickname());
			tmsStrategySplitConditionModel.setUpdatetime(date);
			tmsStrategySplitConditionModel.setUpdateperson(user.getNickname());

			tmsStrategySplitConditionMapper.save(tmsStrategySplitConditionModel);
		}
	}

	/**
	 * 修改
	 *
	 * @param strategySplitUpdateVO
	 */
	@Override

	public void updateData(StrategySplitUpdateVO strategySplitUpdateVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

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

		TmsStrategySplitModel tmsStrategySplitModel = tmsStrategySplitMapper.findById(strategySplitUpdateVO.getId(), TmsStrategySplitModel.class);
		ToolKits.isParamNull(tmsStrategySplitModel, "策略ID[" + strategySplitUpdateVO.getId() + "]有误,查询为空!");

		BeanUtils.copyProperties(strategySplitUpdateVO, tmsStrategySplitModel);

		tmsStrategySplitModel.setUpdatetime(date);
		tmsStrategySplitModel.setUpdateperson(user.getNickname());

		// 修改策略表
		tmsStrategySplitMapper.update(tmsStrategySplitModel);

		// 删除条件
		Criteria flowConditionCriteria = Criteria.forClass(TmsStrategySplitCnModel.class);
		Restriction r2 = Restrictions.eq(TmsStrategySplitCnModel::getStrategyId, strategySplitUpdateVO.getId());
		flowConditionCriteria.setRestriction(Restrictions.and(r2));
		tmsStrategySplitConditionMapper.deleteByCriteria(flowConditionCriteria);
		// 修改策略条件表
		for (StrategySplitConditionUpdateVO strategySplitConditionUpdateVO : strategySplitUpdateVO.getStrategySplitConditionUpdateVOS()) {

			String conditionId = sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_STRATEGY_SPLIT_CONDITION_ID, "L");
			ToolKits.isParamNull(conditionId, "获取运单拆分策略表ID失败！");

			TmsStrategySplitCnModel tmsStrategySplitConditionModel = new TmsStrategySplitCnModel();
			BeanUtils.copyProperties(strategySplitConditionUpdateVO, tmsStrategySplitConditionModel);

			tmsStrategySplitConditionModel.setId(Long.valueOf(conditionId));
			tmsStrategySplitConditionModel.setStrategyId(tmsStrategySplitModel.getId());

			tmsStrategySplitConditionModel.setEnterpriseId(user.getEnterpriseId());
			tmsStrategySplitConditionModel.setEnterpriseName(user.getEnterpriseName());

			tmsStrategySplitConditionModel.setCreatetime(date);
			tmsStrategySplitConditionModel.setCreateperson(user.getNickname());
			tmsStrategySplitConditionModel.setUpdatetime(date);
			tmsStrategySplitConditionModel.setUpdateperson(user.getNickname());

			tmsStrategySplitConditionMapper.save(tmsStrategySplitConditionModel);
		}

	}

	/**
	 * 停用
	 *
	 * @param ids
	 */
	@Override
	public void disableData(List<Long> ids) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

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

		// 封装修改参数
		Map<String, Object> mapUpdate = Maps.newHashMap();
		mapUpdate.put("status", DataDictionaryConstant.GENERAL_STATUS_STOP);
		mapUpdate.put("updateperson", user.getNickname());
		mapUpdate.put("updatetime", date);

		Criteria criteria = Criteria.forClass(TmsStrategySplitModel.class);
		Restriction r1 = Restrictions.in("id", ids.stream().toArray());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));

		tmsStrategySplitMapper.updateMapByCriteria(mapUpdate, criteria);
	}

	/**
	 * 启用
	 *
	 * @param ids
	 */
	@Override
	public void activeData(List<Long> ids) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "没有操作权限！");

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

		// 封装修改参数
		Map<String, Object> mapUpdate = Maps.newHashMap();
		mapUpdate.put("status", DataDictionaryConstant.GENERAL_STATUS_ENABLE);
		mapUpdate.put("updateperson", user.getNickname());
		mapUpdate.put("updatetime", date);

		Criteria criteria = Criteria.forClass(TmsStrategySplitModel.class);
		Restriction r1 = Restrictions.in("id", ids.stream().toArray());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));

		tmsStrategySplitMapper.updateMapByCriteria(mapUpdate, criteria);
	}

	/**
	 * 匹配
	 *
	 * @param strategySplitMatchingVO
	 * @return
	 */
	@Override
	public List<StrategySplitMatchingDTO> matchingData(StrategySplitMatchingVO strategySplitMatchingVO) {
		ToolKits.isParamNull(strategySplitMatchingVO, "运单匹配拆分策略请求不为空");

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

		List<StrategySplitMatchingDTO> list = Lists.newArrayList();

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

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

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

		// 发货方类型
		if (ToolKits.isNotEmpty(strategySplitMatchingVO.getShipperType())) {
			conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER, String.valueOf(strategySplitMatchingVO.getShipperType()) + "," + String.valueOf(strategySplitMatchingVO.getShipperUnitId()));
		}
		// 收货方类型
		if (ToolKits.isNotEmpty(strategySplitMatchingVO.getConsigneeType())) {
			conditionMap.put(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE, String.valueOf(strategySplitMatchingVO.getConsigneeType()) + "," + String.valueOf(strategySplitMatchingVO.getConsigneeUnitId()));
		}
		for (TmsStrategySplitModel tmsStrategySplitModel : tmsStrategySplitModels) {
			StrategySplitMatchingDTO strategySplitMatchingDTO = new StrategySplitMatchingDTO();
			BeanUtils.copyProperties(tmsStrategySplitModel, strategySplitMatchingDTO);

			// 满足条件
			Criteria carrierCtCriteria = Criteria.forClass(TmsStrategySplitCnModel.class);
			Restriction r2 = Restrictions.eq(TmsStrategySplitCnModel::getStrategyId, tmsStrategySplitModel.getId());
			Restriction r3 = Restrictions.eq(TmsStrategySplitCnModel::getEnterpriseId, user.getEnterpriseId());
			carrierCtCriteria.setRestriction(Restrictions.and(r2, r3));
			carrierCtCriteria.setOrder(Order.newInstance().asc(new String[] { "strategyConditionType" }));
			List<TmsStrategySplitCnModel> tmsStrategySplitConditionModels = tmsStrategySplitConditionMapper.findByCriteria(carrierCtCriteria);

			if (ToolKits.isEmpty(tmsStrategySplitConditionModels)) {
				list.add(strategySplitMatchingDTO);

				continue;
			}
			// 判定满足条件
			int fitCnt = 0;
			for (TmsStrategySplitCnModel conditionModel : tmsStrategySplitConditionModels) {
				switch (conditionModel.getStrategyConditionType().intValue()) {
				case 2: // 发货方类型
					String shipperStr = conditionMap.get(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_SHIPPER);
					if (ToolKits.isNotEmpty(shipperStr)) {
						String[] shipper = shipperStr.split(",");
						if (conditionModel.getUnitId().contains(shipper[1])) {
							fitCnt++;
						}
					}
					break;
				case 3: // 收货方类型
					String consigneeStr = conditionMap.get(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_CONSIGNEE);
					if (ToolKits.isNotEmpty(consigneeStr)) {
						String[] consignee = consigneeStr.split(",");
						if (conditionModel.getUnitId().contains(consignee[1])) {
							fitCnt++;
						}
					}
					break;
				case 8: // 货主
					String owner = conditionMap.get(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_OWNER);
					if (ToolKits.isNotEmpty(owner)) {
						if (conditionModel.getUnitId().contains(owner)) {
							fitCnt++;
						}
					}
					break;
				case 9: // 部门
					String department = conditionMap.get(DataDictionaryConstant.STRATEGY_CONDITION_TYPE_DEPARTMENT);
					if (ToolKits.isNotEmpty(department)) {
						if (conditionModel.getUnitId().contains(department)) {
							fitCnt++;
						}
					}
					break;
				default:
					if (conditionMap.containsValue(conditionModel.getStrategyConditionType() + "_" + String.valueOf(conditionModel.getStrategyConditionValue()))) {
						fitCnt++;
					}
					break;
				}
			}

			// 匹配到了拆分策略
			if (fitCnt == tmsStrategySplitConditionModels.size()) {
				// 拆分策略状态
				List<Map<String, Object>> restMessage = sysRefdatadService.getDropDownById(DataDictionaryConstant.GENERAL_STATUS, null, null, null);

				// 配送方式
				List<Map<String, Object>> restMessageDeliveryMethodType = sysRefdatadService.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
				// 收货方类型
				List<Map<String, Object>> restMessageConsigneeType = sysRefdatadService.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
				// 发货方类型
				List<Map<String, Object>> restMessageShipperType = sysRefdatadService.getDropDownById(DataDictionaryConstant.SHIPPER_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>> restMsgTemperature = sysRefdatadService.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);

				// 状态翻译
				strategySplitMatchingDTO.setStatusName(PublicMethods.change(restMessage, String.valueOf(strategySplitMatchingDTO.getStatus())));

				// 不为空拼接条件
				if (ToolKits.isNotEmpty(tmsStrategySplitConditionModels)) {
					StringBuffer conditionsBuff = new StringBuffer();
					List<String> conditionIds = Lists.newArrayList();
					for (TmsStrategySplitCnModel conditionModel : tmsStrategySplitConditionModels) {
						switch (conditionModel.getStrategyConditionType().intValue()) {
						case 1:
							// 1配送方式
							conditionsBuff.append("配送方式 ");
							conditionsBuff.append(PublicMethods.change(restMessageDeliveryMethodType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
							break;
						case 2:
							// 2发货方类型
							conditionsBuff.append("发货方类型 ");
							conditionsBuff.append(PublicMethods.change(restMessageShipperType, String.valueOf(conditionModel.getStrategyConditionValue())) + " " + conditionModel.getUnitName() + " ");
							break;
						case 3:
							// 3收货方类型
							conditionsBuff.append("收货方类型 ");
							conditionsBuff.append(PublicMethods.change(restMessageConsigneeType, String.valueOf(conditionModel.getStrategyConditionValue())) + " " + conditionModel.getUnitName() + " ");
							break;
						case 4:
							// 订单类型
							conditionsBuff.append("订单类型 ");
							conditionsBuff.append(PublicMethods.change(restMsgOrderType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
							break;
						case 5:
							// 订单业务类型
							conditionsBuff.append("订单业务类型 ");
							conditionsBuff.append(PublicMethods.change(restMsgOrderBusinessType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
							break;
						case 6:
							// 运输模式
							conditionsBuff.append("运输模式 ");
							conditionsBuff.append(PublicMethods.change(restMsgTransportType, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
							break;
						case 7:
							// 温度要求
							conditionsBuff.append("温度要求 ");
							conditionsBuff.append(PublicMethods.change(restMsgTemperature, String.valueOf(conditionModel.getStrategyConditionValue())) + " ");
							break;
						case 8:
							// 货主
							conditionsBuff.append("货主 ");
							conditionsBuff.append(conditionModel.getUnitName() + " ");
							break;
						case 9:
							// 部门
							conditionsBuff.append("部门 ");
							conditionsBuff.append(conditionModel.getUnitName() + " ");
							break;
						}
						conditionIds.add(String.valueOf(conditionModel.getId()));
					}
					strategySplitMatchingDTO.setConditionName(conditionsBuff.toString());
				}
				list.add(strategySplitMatchingDTO);
			}
		}

		return list;
	}
}
