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

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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.ToolKits;
import com.prolog.framework.common.message.RestMessage;
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.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillPackageDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillPackageMapper;
import com.prolog.rdc.tms.model.dto.DeviceByOneDTO;
import com.prolog.rdc.tms.model.dto.DispatchAdsQueryDTO;
import com.prolog.rdc.tms.model.dto.DispatcheItemByPackagePageDTO;
import com.prolog.rdc.tms.model.dto.DispatcheItemPageDTO;
import com.prolog.rdc.tms.model.dto.DispatcheItemViewDTO;
import com.prolog.rdc.tms.model.dto.DispatchePackageByItemPageDTO;
import com.prolog.rdc.tms.model.dto.DispatchePackagePageDTO;
import com.prolog.rdc.tms.model.dto.DispatchePackageViewDTO;
import com.prolog.rdc.tms.model.dto.DispatchePageDTO;
import com.prolog.rdc.tms.model.dto.DispatchePrintHeadDTO;
import com.prolog.rdc.tms.model.dto.DispatchePrintTableDTO;
import com.prolog.rdc.tms.model.dto.DispatcheViewDTO;
import com.prolog.rdc.tms.model.dto.ExportDispatchDTO;
import com.prolog.rdc.tms.model.dto.VehicleCheckDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPopDTO;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillPackageModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillPkgDetailModel;
import com.prolog.rdc.tms.model.vo.DetailItemByPackagePageVO;
import com.prolog.rdc.tms.model.vo.DetailItemPageVO;
import com.prolog.rdc.tms.model.vo.DetailPackageByItemPageVO;
import com.prolog.rdc.tms.model.vo.DetailPackagePageVO;
import com.prolog.rdc.tms.model.vo.DispatchAdvanceQueryVO;
import com.prolog.rdc.tms.model.vo.DispatchExportAllVO;
import com.prolog.rdc.tms.model.vo.DispatchExportPageVO;
import com.prolog.rdc.tms.model.vo.DispatchPageVO;
import com.prolog.rdc.tms.model.vo.DispatcheViewVO;
import com.prolog.rdc.tms.model.vo.ShipbillOperateLogSaveVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPopVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleCheckVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.DispatchService;
import com.prolog.rdc.tms.service.ShipbillOperateLogService;
import com.prolog.rdc.tms.util.PublicMethods;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 调度管理-已调度实现层
 *
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/4/11 14:01
 */
@Service
public class DispatchServiceImpl implements DispatchService {

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillPackageMapper tmsShipbillPackageMapper;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private TmsShipbillPackageDetailMapper tmsShipbillPackageDetailMapper;

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Autowired
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Autowired
	private ShipbillOperateLogService shipbillOperateLogService;

	/**
	 * 已调度分页查询
	 * 
	 * @param dispatchPageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DispatchePageDTO> pageDispatchData(DispatchPageVO dispatchPageVO) {
		ToolKits.isParamNull(dispatchPageVO, "汇总查询请求不为空！");
		ToolKits.isParamNull(dispatchPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(dispatchPageVO.getPageSize(), "每页条数不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();
		List<Map<String, Object>> data4 = Lists.newArrayList();
		List<Map<String, Object>> data5 = Lists.newArrayList();
		List<Map<String, Object>> data6 = Lists.newArrayList();
		List<Map<String, Object>> data7 = Lists.newArrayList();
		List<SignconfigPopDTO> data8 = Lists.newArrayList();
		List<Map<String, Object>> data9 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new RuntimeException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new RuntimeException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new RuntimeException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}
		RestMessage<List<Map<String, Object>>> restMessage9 = maindataTmsResource.getDropDownById(DataDictionaryConstant.LINE_CARRIER_TYPE, null, null, null);
		if (!restMessage9.isSuccess()) {
			throw new RuntimeException("承运方类型查询失败!");
		} else {
			data9 = restMessage9.getData();
		}

		// 添加查询条件
		Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
		Restriction r1 = ToolKits.isEmpty(dispatchPageVO.getShipbillNo()) ? null : Restrictions.like("shipbillNo", "%" + dispatchPageVO.getShipbillNo() + "%");
		Restriction r2 = ToolKits.isEmpty(dispatchPageVO.getCargoOwnerName()) ? null : Restrictions.like("cargoOwnerName", "%" + dispatchPageVO.getCargoOwnerName() + "%");
		Restriction r3 = ToolKits.isEmpty(dispatchPageVO.getOrderNo()) ? null : Restrictions.like("orderNo", "%" + dispatchPageVO.getOrderNo() + "%");
		Restriction r4 = ToolKits.isEmpty(dispatchPageVO.getEstimatedPickupStartTime()) ? null : Restrictions.ge("estimatedPickupStartTime", dispatchPageVO.getEstimatedPickupStartTime());
		Restriction r5 = ToolKits.isEmpty(dispatchPageVO.getEstimatedPickupEndTime()) ? null : Restrictions.le("estimatedPickupEndTime", dispatchPageVO.getEstimatedPickupEndTime());
		Restriction r6 = ToolKits.isEmpty(dispatchPageVO.getOrderBusinessType()) ? null : Restrictions.eq("orderBusinessType", dispatchPageVO.getOrderBusinessType());
		Restriction r7 = ToolKits.isEmpty(dispatchPageVO.getEstimatedDeliveryStartTime()) ? null : Restrictions.ge("estimatedDeliveryStartTime", dispatchPageVO.getEstimatedDeliveryStartTime());
		Restriction r8 = ToolKits.isEmpty(dispatchPageVO.getEstimatedDeliveryEndTime()) ? null : Restrictions.le("estimatedDeliveryEndTime", dispatchPageVO.getEstimatedDeliveryEndTime());
		Restriction r9 = ToolKits.isEmpty(dispatchPageVO.getProvenance()) ? null : Restrictions.like("shipperProvinceName", "%" + dispatchPageVO.getProvenance() + "%");
		Restriction r10 = ToolKits.isEmpty(dispatchPageVO.getProvenance()) ? null : Restrictions.like("shipperCityName", "%" + dispatchPageVO.getProvenance() + "%");
		Restriction r11 = ToolKits.isEmpty(dispatchPageVO.getDestination()) ? null : Restrictions.like("consigneeProvinceName", "%" + dispatchPageVO.getDestination() + "%");
		Restriction r12 = ToolKits.isEmpty(dispatchPageVO.getDestination()) ? null : Restrictions.like("consigneeCityName", "%" + dispatchPageVO.getDestination() + "%");
		Restriction r13 = ToolKits.isEmpty(dispatchPageVO.getProvenance()) ? null : Restrictions.or(r9, r10);
		Restriction r14 = ToolKits.isEmpty(dispatchPageVO.getDestination()) ? null : Restrictions.or(r11, r12);
		Restriction r15 = ToolKits.isEmpty(dispatchPageVO.getLineName()) ? null : Restrictions.like("lineName", "%" + dispatchPageVO.getLineName() + "%");

		Restriction r17 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		// 权限控制
		Restriction r16 = null;
		Restriction r18 = null;
		Restriction r29 = null;
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {// 如果为自有账号
			// 拆单状态 0未拆单
			Restriction r30 = Restrictions.eq("splitOrderStatus", DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			// 承运商拆单类型 1未拆单
			Restriction r31 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN);
			Restriction r32 = Restrictions.and(r30, r31);
			// 承运商拆单类型 2拆分主单
			Restriction r33 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_SPLIT_MASTER);
			// 承运商拆单类型 2中转主单
			Restriction r34 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			r29 = Restrictions.or(r32, r33, r34);
			r16 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {// 为部门权限
				r18 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
			} else {// 为业务员权限
				Restriction r19 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
				Restriction r20 = Restrictions.in("cargoOwnerId", user.getConsignors().stream().toArray(Long[]::new));
				Restriction r21 = Restrictions.in("consigneeUnitId", user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r22 = Restrictions.in("consigneeUnitId", user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r23 = Restrictions.in("consigneeUnitId", user.getCustomers().stream().toArray(Long[]::new));

				Restriction r41 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r42 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r43 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getCustomers().stream().toArray(Long[]::new));

				Restriction r24 = Restrictions.or(r21, r22, r23, r41, r42, r43);
				r18 = Restrictions.or(r19, r20, r24);
			}
		} else {// 非自有账号(承运商)
			r16 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK);
			Restriction r19 = Restrictions.eq("carrierId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			Restriction r21 = Restrictions.eq("carrierShipbillStatus", DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED);
			r18 = Restrictions.and(r19, r21);
			r29 = Restrictions.eq("carrierSplitOrderStatus", DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
		}
		//
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r13, r14, r15, r16, r17, r18, r29));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		PageUtils.startPage(dispatchPageVO.getPageNum(), dispatchPageVO.getPageSize());
		List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteria);

		List<DispatchePageDTO> list = Lists.newArrayList();
		for (TmsShipbillHeaderModel model : tmsShipbillHeaderModels) {
			DispatchePageDTO dto = new DispatchePageDTO();
			BeanUtils.copyProperties(model, dto);

			// 判断是否有包裹 有包裹 修改件数体积总量
//			if (ToolKits.isNotEmpty(model.getPackageTotalWeight())) {
//				dto.setTotalWeight(model.getPackageTotalWeight());
//			}
//			if (ToolKits.isNotEmpty(model.getPackageTotalVolume())) {
//				dto.setTotalVolume(model.getPackageTotalVolume());
//			}
//			if (ToolKits.isNotEmpty(model.getPackageLclPiece())) {
//				if (ToolKits.isNotEmpty(model.getPackageWholePiece())) {
//					dto.setTotalPcs(model.getPackageLclPiece().add(model.getPackageWholePiece()));
//				} else {
//					dto.setTotalPcs(model.getPackageLclPiece());
//				}
//			} else {
//				dto.setTotalPcs(BigDecimal.ZERO);
//			}
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				dto.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

			// 类型翻译
			dto.setOrderTypeName(PublicMethods.change(data1, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(PublicMethods.change(data2, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(PublicMethods.change(data3, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(PublicMethods.change(data4, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(PublicMethods.change(data5, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(PublicMethods.change(data6, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(PublicMethods.change(data7, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierTypeName(PublicMethods.change(data9, String.valueOf(dto.getCarrierType())));

			// 获取背景颜色和字体颜色
			Signconfig: for (SignconfigPopDTO signconfigPopDTO : data8) {
				// 标记类型匹配
				switch (signconfigPopDTO.getType()) {
				case DataDictionaryConstant.SIGNCONFIG_TYPE_DDLX: // 订单类型
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						// 判断是否匹配
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getOrderTypeName()) ? dto.getOrderTypeName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							// 匹配上结束循环
							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_YWLX: // 业务类型
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getOrderBusinessTypeName()) ? dto.getOrderBusinessTypeName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_WDYQ: // 温度要求
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getTemperatureRequirementName()) ? dto.getTemperatureRequirementName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_PSFS: // 配送方式
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getDeliveryMethodTypeName()) ? dto.getDeliveryMethodTypeName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_DDBZ: // 订单备注
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(model.getMemo()) ? model.getMemo() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				}
			}

			Map<String, Object> queryMap = Maps.newHashMap();
			queryMap.put("shipbillId", model.getShipbillId());
			List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByMap(queryMap, TmsLoadingBillDetailModel.class);
			if (ToolKits.isNotEmpty(tmsLoadingBillDetails) && tmsLoadingBillDetails.size() > 0) {
				String loadingbillId = tmsLoadingBillDetails.get(0).getLoadingBillId();
				TmsLoadingBillHeaderModel tmsLoadingBillHeader = tmsLoadingBillHeaderMapper.findById(loadingbillId, TmsLoadingBillHeaderModel.class);
				if (ToolKits.isNotEmpty(tmsLoadingBillHeader)) {
					// 承运方类型为司机时展示司机姓名
					if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {
						dto.setCarrierName(tmsLoadingBillHeader.getDriverName());
					}
					dto.setLoadingBillNo(tmsLoadingBillHeader.getLoadingBillNo());
					dto.setDeliverymanName(tmsLoadingBillHeader.getDeliverymanName());
					dto.setVehiclePlateNumber(tmsLoadingBillHeader.getVehiclePlateNumber());
				}
			}

			list.add(dto);
		}

		// 分页重组
		Page<DispatchePageDTO> page = PageUtils.getPage(list);
		if (tmsShipbillHeaderModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillHeaderModel> p0 = (com.github.pagehelper.Page<TmsShipbillHeaderModel>) tmsShipbillHeaderModels;
			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 advancedQueryVO
	 * @return
	 */
	@SuppressWarnings("resource")
	public Page<DispatchAdsQueryDTO> pageAdvanced(DispatchAdvanceQueryVO advancedQueryVO) {
		ToolKits.isParamNull(advancedQueryVO, "高级查询请求不为空！");
		ToolKits.isParamNull(advancedQueryVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(advancedQueryVO.getPageSize(), "每页条数不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();
		List<Map<String, Object>> data4 = Lists.newArrayList();
		List<Map<String, Object>> data5 = Lists.newArrayList();
		List<Map<String, Object>> data6 = Lists.newArrayList();
		List<Map<String, Object>> data7 = Lists.newArrayList();
		List<SignconfigPopDTO> data8 = Lists.newArrayList();

		List<Map<String, Object>> data9 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new RuntimeException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new RuntimeException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new RuntimeException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}
		// 标记配置
		SignconfigPopVO signconfigPopVO = new SignconfigPopVO();
		RestMessage<List<SignconfigPopDTO>> restMessage8 = maindataTmsResource.getSignConfig(signconfigPopVO);
		if (!restMessage8.isSuccess()) {
			throw new RuntimeException("标记配置查询失败!");
		} else {
			data8 = restMessage8.getData();
		}

		// 承运方类型
		RestMessage<List<Map<String, Object>>> restMessage9 = maindataTmsResource.getDropDownById(DataDictionaryConstant.LINE_CARRIER_TYPE, null, null, null);
		if (!restMessage9.isSuccess()) {
			throw new RuntimeException("承运方类型翻译失败!");
		} else {
			data9 = restMessage9.getData();
		}
		// 添加查询条件
		// 权限控制
		advancedQueryVO.setEnterpriseId(user.getEnterpriseId());
		if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_ZY)) {
			// 自有账号
			advancedQueryVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
			advancedQueryVO.setDepts(user.getDepts().stream().toArray(String[]::new));
			if (BussinessConstant.USER_DEPARTMENT_AUTH_Y.equals(user.getOwnedAllDataAuthFlag())) {
				advancedQueryVO.setIsDept("YES");
			} else {
				advancedQueryVO.setIsDept("NO");
				// 其它权限
				// 货主
				advancedQueryVO.setConsignors(user.getConsignors().stream().toArray(Long[]::new));
				// 客户
				advancedQueryVO.setCustomers(user.getCustomers().stream().toArray(Long[]::new));
				// 供应商
				advancedQueryVO.setSuppliers(user.getSuppliers().stream().toArray(Long[]::new));
				// 仓库
				advancedQueryVO.setWsarehouses(user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {// 承运商账号
			// 承运商账号 单位ID
			advancedQueryVO.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			// 承运商运单状态
			Integer[] carrierStsList = new Integer[] { DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED };
			advancedQueryVO.setCarrierStatusList(carrierStsList);
		} else {
			throw new PrologException("当前人员没有操作权限！");
		}
		// 获取高级查询配置
		String conditions = null;
		RestMessage<String> restMessageAdvancedCfg = maindataTmsResource.getQueryConfiguration(DataDictionaryConstant.BIZ_TYPE_DISPATCHED);
		if (!restMessageAdvancedCfg.isSuccess()) {
			// throw new RuntimeException("订单类型翻译失败!");
		} else {
			conditions = restMessageAdvancedCfg.getData();
		}
		advancedQueryVO.setSql(conditions);

		PageUtils.startPage(advancedQueryVO.getPageNum(), advancedQueryVO.getPageSize());
		List<DispatchAdsQueryDTO> tmsShipbillHeaderModels = tmsLoadingBillDetailMapper.dispatchAdvancedQuery(advancedQueryVO);

		List<DispatchAdsQueryDTO> list = Lists.newArrayList();
		for (DispatchAdsQueryDTO model : tmsShipbillHeaderModels) {
			DispatchAdsQueryDTO dto = new DispatchAdsQueryDTO();
			BeanUtils.copyProperties(model, dto);

			// 判断是否有包裹 有包裹 修改件数体积总量
			if (ToolKits.isNotEmpty(model.getPackageTotalWeight())) {
				dto.setTotalWeight(model.getPackageTotalWeight());
			}
			if (ToolKits.isNotEmpty(model.getPackageTotalVolume())) {
				dto.setTotalVolume(model.getPackageTotalVolume());
			}
			if (ToolKits.isNotEmpty(model.getPackageLclPiece())) {
				if (ToolKits.isNotEmpty(model.getPackageWholePiece())) {
					dto.setTotalPcs(model.getPackageLclPiece().add(model.getPackageWholePiece()));
				} else {
					dto.setTotalPcs(model.getPackageLclPiece());
				}
			} else {
				dto.setTotalPcs(BigDecimal.ZERO);
			}
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				dto.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

			// 类型翻译
			dto.setOrderTypeName(PublicMethods.change(data1, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(PublicMethods.change(data2, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(PublicMethods.change(data3, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(PublicMethods.change(data4, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(PublicMethods.change(data5, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(PublicMethods.change(data6, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(PublicMethods.change(data7, String.valueOf(dto.getConsigneeType())));

			// 承运方类型
			dto.setCarrierTypeName(PublicMethods.change(data9, String.valueOf(dto.getCarrierType())));

			// 获取背景颜色和字体颜色
			Signconfig: for (SignconfigPopDTO signconfigPopDTO : data8) {
				// 标记类型匹配
				switch (signconfigPopDTO.getType()) {
				case DataDictionaryConstant.SIGNCONFIG_TYPE_DDLX: // 订单类型
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						// 判断是否匹配
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getOrderTypeName()) ? dto.getOrderTypeName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							// 匹配上结束循环
							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_YWLX: // 业务类型
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getOrderBusinessTypeName()) ? dto.getOrderBusinessTypeName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_WDYQ: // 温度要求
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getTemperatureRequirementName()) ? dto.getTemperatureRequirementName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_PSFS: // 配送方式
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(dto.getDeliveryMethodTypeName()) ? dto.getDeliveryMethodTypeName() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				case DataDictionaryConstant.SIGNCONFIG_TYPE_DDBZ: // 订单备注
					if (ToolKits.isNotEmpty(signconfigPopDTO.getMatching())) {
						if (PublicMethods.compare(signconfigPopDTO.getMatching(), ToolKits.isNotEmpty(signconfigPopDTO.getStoredValue()) ? signconfigPopDTO.getStoredValue() : "", ToolKits.isNotEmpty(model.getMemo()) ? model.getMemo() : "")) {
							dto.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							dto.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							dto.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				}
			}

			list.add(dto);
		}

		// 分页重组
		Page<DispatchAdsQueryDTO> page = PageUtils.getPage(list);
		if (tmsShipbillHeaderModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<DispatchAdsQueryDTO> p0 = (com.github.pagehelper.Page<DispatchAdsQueryDTO>) tmsShipbillHeaderModels;
			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 detailItemPageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DispatcheItemPageDTO> pageDetailItemData(DetailItemPageVO detailItemPageVO) {
		ToolKits.isParamNull(detailItemPageVO.getShipbillId(), "运单ID不为空");
		ToolKits.isParamNull(detailItemPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(detailItemPageVO.getPageSize(), "每页条数不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();

		// 包装类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.PACK_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 货物温层
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CARGO_THERMOSPHERE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("货物温层翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		// 根据运单ID查询明细数据
		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", detailItemPageVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().asc(new String[] { "seqno" }));

		PageUtils.startPage(detailItemPageVO.getPageNum(), detailItemPageVO.getPageSize());
		List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteria);

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

		for (TmsShipbillDetailModel tmsShipbillDetailModel : tmsShipbillDetailModels) {
			DispatcheItemPageDTO dto = new DispatcheItemPageDTO();
			BeanUtils.copyProperties(tmsShipbillDetailModel, dto);

			// 数据字典翻译
			dto.setPackTypeName(PublicMethods.change(data1, String.valueOf(dto.getPackType())));
			dto.setCargoThermosphereName(PublicMethods.change(data2, String.valueOf(dto.getCargoThermosphere())));

			list.add(dto);
		}

		// 分页重组
		Page<DispatcheItemPageDTO> page = PageUtils.getPage(list);
		if (tmsShipbillDetailModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillDetailModel> p0 = (com.github.pagehelper.Page<TmsShipbillDetailModel>) tmsShipbillDetailModels;
			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 detailPackagePageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DispatchePackagePageDTO> pageDetailPackageData(DetailPackagePageVO detailPackagePageVO) {
		ToolKits.isParamNull(detailPackagePageVO.getShipbillId(), "运单ID不为空");
		ToolKits.isParamNull(detailPackagePageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(detailPackagePageVO.getPageSize(), "每页条数不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();

		// 包裹类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.PACKAGE_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("包裹类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}

		// 根据运单ID查询明细数据
		Criteria criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", detailPackagePageVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().asc(new String[] { "seqno" }));

		PageUtils.startPage(detailPackagePageVO.getPageNum(), detailPackagePageVO.getPageSize());
		List<TmsShipbillPackageModel> tmsShipbillPackageModels = tmsShipbillPackageMapper.findByCriteria(criteria);

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

		for (TmsShipbillPackageModel tmsShipbillPackageModel : tmsShipbillPackageModels) {
			DispatchePackagePageDTO dto = new DispatchePackagePageDTO();
			BeanUtils.copyProperties(tmsShipbillPackageModel, dto);

			// 数据字典翻译
			dto.setPackageTypeName(PublicMethods.change(data1, String.valueOf(dto.getPackageType())));

			// 设备类型翻译
			if (ToolKits.isNotEmpty(tmsShipbillPackageModel.getDeviceId())) {
				RestMessage<DeviceByOneDTO> deviceByOneDTORestMessage = maindataTmsResource.deviceGetOne(tmsShipbillPackageModel.getDeviceId());
				if (!deviceByOneDTORestMessage.isSuccess()) {
					throw new RuntimeException("获取设备失败!");
				}
				DeviceByOneDTO data = deviceByOneDTORestMessage.getData();
				ToolKits.isParamNull(data, "获取设备为空");

				dto.setDeviceTypeName(data.getTypeName());
			}

			list.add(dto);
		}

		// 分页重组
		Page<DispatchePackagePageDTO> page = PageUtils.getPage(list);
		if (tmsShipbillPackageModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillPackageModel> p0 = (com.github.pagehelper.Page<TmsShipbillPackageModel>) tmsShipbillPackageModels;
			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 detailPackageByItemPageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DispatchePackageByItemPageDTO> pagePackageByItemData(DetailPackageByItemPageVO detailPackageByItemPageVO) {
		ToolKits.isParamNull(detailPackageByItemPageVO.getShipbillId(), "运单ID不为空");
		ToolKits.isParamNull(detailPackageByItemPageVO.getItemId(), "商品ID不为空");
		ToolKits.isParamNull(detailPackageByItemPageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(detailPackageByItemPageVO.getPageSize(), "每页条数不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();

		// 包裹类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.PACKAGE_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("包裹类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		List<DispatchePackageByItemPageDTO> list = Lists.newArrayList();
		Criteria criteria = Criteria.forClass(TmsShipbillPkgDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", detailPackageByItemPageVO.getShipbillId());
		Restriction r2 = Restrictions.eq("itemId", detailPackageByItemPageVO.getItemId());
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		criteria.setRestriction(Restrictions.and(r1, r2, r3));

		List<TmsShipbillPkgDetailModel> tmsShipbillPkgDetailModels = tmsShipbillPackageDetailMapper.findByCriteria(criteria);
		if (ToolKits.isEmpty(tmsShipbillPkgDetailModels)) {
			Page<DispatchePackageByItemPageDTO> page = PageUtils.getPage(list);
			return page;
		}
		List<String> packageIds = tmsShipbillPkgDetailModels.stream().filter(o -> ToolKits.isNotEmpty(o.getPackageId())).map(TmsShipbillPkgDetailModel::getPackageId).distinct().collect(Collectors.toList());

		List<TmsShipbillPackageModel> models = tmsShipbillPackageMapper.findByIds(TmsShipbillPackageModel.class, packageIds.toArray(), null);

		// 返回数据集
		for (TmsShipbillPackageModel tmsShipbillPackageModel : models) {
			DispatchePackageByItemPageDTO dto = new DispatchePackageByItemPageDTO();
			BeanUtils.copyProperties(tmsShipbillPackageModel, dto);

			// 数据字典翻译
			dto.setPackageTypeName(PublicMethods.change(data1, String.valueOf(dto.getPackageType())));

			list.add(dto);

			// 设备类型翻译
			if (ToolKits.isNotEmpty(tmsShipbillPackageModel.getDeviceId())) {
				RestMessage<DeviceByOneDTO> deviceByOneDTORestMessage = maindataTmsResource.deviceGetOne(tmsShipbillPackageModel.getDeviceId());
				if (!deviceByOneDTORestMessage.isSuccess()) {
					throw new RuntimeException("获取设备失败!");
				}
				DeviceByOneDTO data = deviceByOneDTORestMessage.getData();
				ToolKits.isParamNull(data, "获取设备为空");

				dto.setDeviceTypeName(data.getTypeName());
			}
		}

		// 分页重组
		Page<DispatchePackageByItemPageDTO> page = PageUtils.getPage(list);
		if (models instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillPackageModel> p0 = (com.github.pagehelper.Page<TmsShipbillPackageModel>) models;
			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 detailItemByPackagePageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<DispatcheItemByPackagePageDTO> pageItemByPackageData(DetailItemByPackagePageVO detailItemByPackagePageVO) {
		ToolKits.isParamNull(detailItemByPackagePageVO.getShipbillId(), "运单ID不为空");
		ToolKits.isParamNull(detailItemByPackagePageVO.getPackageId(), "包裹ID不为空");
		ToolKits.isParamNull(detailItemByPackagePageVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(detailItemByPackagePageVO.getPageSize(), "每页条数不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		List<DispatcheItemByPackagePageDTO> list = Lists.newArrayList();

		Criteria criteria = Criteria.forClass(TmsShipbillPkgDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", detailItemByPackagePageVO.getShipbillId());
		Restriction r2 = Restrictions.eq("packageId", detailItemByPackagePageVO.getPackageId());
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		criteria.setRestriction(Restrictions.and(r1, r2, r3));

		List<TmsShipbillPkgDetailModel> tmsShipbillPkgDetailModels = tmsShipbillPackageDetailMapper.findByCriteria(criteria);

		if (ToolKits.isEmpty(tmsShipbillPkgDetailModels)) {
			Page<DispatcheItemByPackagePageDTO> page = PageUtils.getPage(list);
			return page;
		}

		List<Long> itemIds = tmsShipbillPkgDetailModels.stream().filter(o -> ToolKits.isNotEmpty(o.getItemId())).map(TmsShipbillPkgDetailModel::getItemId).distinct().collect(Collectors.toList());

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();

		// 包装类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.PACK_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 货物温层
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CARGO_THERMOSPHERE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("货物温层翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		// 根据运单ID查询明细数据
		Criteria criteria2 = Criteria.forClass(TmsShipbillDetailModel.class);
		r1 = Restrictions.eq("shipbillId", detailItemByPackagePageVO.getShipbillId());
		r2 = Restrictions.in("itemId", itemIds.toArray());
		criteria2.setRestriction(Restrictions.and(r1, r2, r3));
		criteria2.setOrder(Order.newInstance().asc(new String[] { "seqno" }));

		PageUtils.startPage(detailItemByPackagePageVO.getPageNum(), detailItemByPackagePageVO.getPageSize());
		List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteria);

		if (ToolKits.isEmpty(tmsShipbillPkgDetailModels)) {
			Page<DispatcheItemByPackagePageDTO> page = PageUtils.getPage(list);
			return page;
		}
		// 返回数据集
		for (TmsShipbillDetailModel tmsShipbillDetailModel : tmsShipbillDetailModels) {
			DispatcheItemByPackagePageDTO dto = new DispatcheItemByPackagePageDTO();
			BeanUtils.copyProperties(tmsShipbillDetailModel, dto);

			// 数据字典翻译
			dto.setPackTypeName(PublicMethods.change(data1, String.valueOf(dto.getPackType())));
			dto.setCargoThermosphereName(PublicMethods.change(data2, String.valueOf(dto.getCargoThermosphere())));

			list.add(dto);
		}

		// 分页重组
		Page<DispatcheItemByPackagePageDTO> page = PageUtils.getPage(list);
		if (tmsShipbillDetailModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillDetailModel> p0 = (com.github.pagehelper.Page<TmsShipbillDetailModel>) tmsShipbillDetailModels;
			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;
	}

	/**
	 * 导出所有数据
	 * 
	 * @return
	 */
	@Override
	public List<ExportDispatchDTO> exportAllDispatchData(DispatchExportAllVO dispatchExportAllVO) {
		ToolKits.isParamNull(dispatchExportAllVO, "汇总查询请求不为空！");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();
		List<Map<String, Object>> data4 = Lists.newArrayList();
		List<Map<String, Object>> data5 = Lists.newArrayList();
		List<Map<String, Object>> data6 = Lists.newArrayList();
		List<Map<String, Object>> data7 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new RuntimeException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new RuntimeException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new RuntimeException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}

		// 添加查询条件
		Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
		Restriction r1 = ToolKits.isEmpty(dispatchExportAllVO.getShipbillNo()) ? null : Restrictions.like("shipbillNo", "%" + dispatchExportAllVO.getShipbillNo() + "%");
		Restriction r2 = ToolKits.isEmpty(dispatchExportAllVO.getCargoOwnerName()) ? null : Restrictions.like("cargoOwnerName", "%" + dispatchExportAllVO.getCargoOwnerName() + "%");
		Restriction r3 = ToolKits.isEmpty(dispatchExportAllVO.getOrderNo()) ? null : Restrictions.like("orderNo", "%" + dispatchExportAllVO.getOrderNo() + "%");
		Restriction r4 = ToolKits.isEmpty(dispatchExportAllVO.getEstimatedPickupStartTime()) ? null : Restrictions.ge("estimatedPickupStartTime", dispatchExportAllVO.getEstimatedPickupStartTime());
		Restriction r5 = ToolKits.isEmpty(dispatchExportAllVO.getEstimatedPickupEndTime()) ? null : Restrictions.le("estimatedPickupEndTime", dispatchExportAllVO.getEstimatedPickupEndTime());
		Restriction r6 = ToolKits.isEmpty(dispatchExportAllVO.getOrderBusinessType()) ? null : Restrictions.eq("orderBusinessType", dispatchExportAllVO.getOrderBusinessType());
		Restriction r7 = ToolKits.isEmpty(dispatchExportAllVO.getEstimatedDeliveryStartTime()) ? null : Restrictions.ge("estimatedDeliveryStartTime", dispatchExportAllVO.getEstimatedDeliveryStartTime());
		Restriction r8 = ToolKits.isEmpty(dispatchExportAllVO.getEstimatedDeliveryEndTime()) ? null : Restrictions.le("estimatedDeliveryEndTime", dispatchExportAllVO.getEstimatedDeliveryEndTime());
		Restriction r9 = ToolKits.isEmpty(dispatchExportAllVO.getProvenance()) ? null : Restrictions.like("shipperProvinceName", "%" + dispatchExportAllVO.getProvenance() + "%");
		Restriction r10 = ToolKits.isEmpty(dispatchExportAllVO.getProvenance()) ? null : Restrictions.like("shipperCityName", "%" + dispatchExportAllVO.getProvenance() + "%");
		Restriction r11 = ToolKits.isEmpty(dispatchExportAllVO.getDestination()) ? null : Restrictions.like("consigneeProvinceName", "%" + dispatchExportAllVO.getDestination() + "%");
		Restriction r12 = ToolKits.isEmpty(dispatchExportAllVO.getDestination()) ? null : Restrictions.like("consigneeCityName", "%" + dispatchExportAllVO.getDestination() + "%");
		Restriction r13 = ToolKits.isEmpty(dispatchExportAllVO.getProvenance()) ? null : Restrictions.or(r9, r10);
		Restriction r14 = ToolKits.isEmpty(dispatchExportAllVO.getDestination()) ? null : Restrictions.or(r11, r12);
		Restriction r15 = ToolKits.isEmpty(dispatchExportAllVO.getLineName()) ? null : Restrictions.like("lineName", "%" + dispatchExportAllVO.getLineName() + "%");
		Restriction r16 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
		Restriction r17 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}

		// 权限控制
		Restriction r18 = null;
		Restriction r29 = null;
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {// 如果为自有账号
			// 拆单状态 0未拆单
			Restriction r30 = Restrictions.eq("splitOrderStatus", DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			// 承运商拆单类型 1未拆单
			Restriction r31 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN);
			Restriction r32 = Restrictions.and(r30, r31);
			// 承运商拆单类型 2拆分主单
			Restriction r33 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_SPLIT_MASTER);
			// 承运商拆单类型 2中转主单
			Restriction r34 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			r29 = Restrictions.or(r32, r33, r34);

			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {// 为部门权限
				r18 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
			} else {// 为业务员权限
				Restriction r19 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
				Restriction r20 = Restrictions.in("cargoOwnerId", user.getConsignors().stream().toArray(Long[]::new));
				Restriction r21 = Restrictions.in("consigneeUnitId", user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r22 = Restrictions.in("consigneeUnitId", user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r23 = Restrictions.in("consigneeUnitId", user.getCustomers().stream().toArray(Long[]::new));

				Restriction r41 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r42 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r43 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getCustomers().stream().toArray(Long[]::new));

				Restriction r24 = Restrictions.or(r21, r22, r23, r41, r42, r43);
				r18 = Restrictions.or(r19, r20, r24);
			}
		} else {// 非自有账号(承运商)
			Restriction r19 = Restrictions.eq("carrierId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			Restriction r20 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
			Restriction r21 = Restrictions.eq("carrierShipbillStatus", DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED);
			r18 = Restrictions.and(r19, r20, r21);
			r29 = Restrictions.eq("carrierSplitOrderStatus", DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
		}
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r13, r14, r15, r16, r17, r18, r29));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteria);

		List<ExportDispatchDTO> list = Lists.newArrayList();
		for (TmsShipbillHeaderModel model : tmsShipbillHeaderModels) {
			ExportDispatchDTO dto = new ExportDispatchDTO();
			BeanUtils.copyProperties(model, dto);

			// 判断是否有包裹 有包裹 修改件数体积总量
			if (ToolKits.isNotEmpty(model.getPackageTotalWeight())) {
				dto.setTotalWeight(model.getPackageTotalWeight());
			}
			if (ToolKits.isNotEmpty(model.getPackageTotalVolume())) {
				dto.setTotalVolume(model.getPackageTotalVolume());
			}
			if (ToolKits.isNotEmpty(model.getPackageLclPiece())) {
				if (ToolKits.isNotEmpty(model.getPackageWholePiece())) {
					dto.setTotalPcs(model.getPackageLclPiece().add(model.getPackageWholePiece()));
				} else {
					dto.setTotalPcs(model.getPackageLclPiece());
				}
			} else {
				dto.setTotalPcs(BigDecimal.ZERO);
			}
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				dto.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

			// 类型翻译
			dto.setOrderTypeName(PublicMethods.change(data1, String.valueOf(model.getOrderType())));
			dto.setOrderBusinessTypeName(PublicMethods.change(data2, String.valueOf(model.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(PublicMethods.change(data3, String.valueOf(model.getDeliveryMethodType())));
			dto.setTransportTypeName(PublicMethods.change(data4, String.valueOf(model.getTransportType())));
			dto.setTemperatureRequirementName(PublicMethods.change(data5, String.valueOf(model.getTemperatureRequirement())));
			dto.setShipperTypeName(PublicMethods.change(data6, String.valueOf(model.getShipperType())));
			dto.setConsigneeTypeName(PublicMethods.change(data7, String.valueOf(model.getConsigneeType())));

			list.add(dto);
		}

		return list;
	}

	/**
	 * 导出当前页数据
	 * 
	 * @return
	 */
	@Override
	public List<ExportDispatchDTO> exportPageDispatchData(DispatchExportPageVO dispatchExportPageVO) {
		ToolKits.isParamNull(dispatchExportPageVO, "汇总查询请求不为空！");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();
		List<Map<String, Object>> data4 = Lists.newArrayList();
		List<Map<String, Object>> data5 = Lists.newArrayList();
		List<Map<String, Object>> data6 = Lists.newArrayList();
		List<Map<String, Object>> data7 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new RuntimeException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new RuntimeException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new RuntimeException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}

		// 添加查询条件
		Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
		Restriction r1 = ToolKits.isEmpty(dispatchExportPageVO.getShipbillNo()) ? null : Restrictions.like("shipbillNo", "%" + dispatchExportPageVO.getShipbillNo() + "%");
		Restriction r2 = ToolKits.isEmpty(dispatchExportPageVO.getCargoOwnerName()) ? null : Restrictions.like("cargoOwnerName", "%" + dispatchExportPageVO.getCargoOwnerName() + "%");
		Restriction r3 = ToolKits.isEmpty(dispatchExportPageVO.getOrderNo()) ? null : Restrictions.like("orderNo", "%" + dispatchExportPageVO.getOrderNo() + "%");
		Restriction r4 = ToolKits.isEmpty(dispatchExportPageVO.getEstimatedPickupStartTime()) ? null : Restrictions.ge("estimatedPickupStartTime", dispatchExportPageVO.getEstimatedPickupStartTime());
		Restriction r5 = ToolKits.isEmpty(dispatchExportPageVO.getEstimatedPickupEndTime()) ? null : Restrictions.le("estimatedPickupEndTime", dispatchExportPageVO.getEstimatedPickupEndTime());
		Restriction r6 = ToolKits.isEmpty(dispatchExportPageVO.getOrderBusinessType()) ? null : Restrictions.eq("orderBusinessType", dispatchExportPageVO.getOrderBusinessType());
		Restriction r7 = ToolKits.isEmpty(dispatchExportPageVO.getEstimatedDeliveryStartTime()) ? null : Restrictions.ge("estimatedDeliveryStartTime", dispatchExportPageVO.getEstimatedDeliveryStartTime());
		Restriction r8 = ToolKits.isEmpty(dispatchExportPageVO.getEstimatedDeliveryEndTime()) ? null : Restrictions.le("estimatedDeliveryEndTime", dispatchExportPageVO.getEstimatedDeliveryEndTime());
		Restriction r9 = ToolKits.isEmpty(dispatchExportPageVO.getProvenance()) ? null : Restrictions.like("shipperProvinceName", "%" + dispatchExportPageVO.getProvenance() + "%");
		Restriction r10 = ToolKits.isEmpty(dispatchExportPageVO.getProvenance()) ? null : Restrictions.like("shipperCityName", "%" + dispatchExportPageVO.getProvenance() + "%");
		Restriction r11 = ToolKits.isEmpty(dispatchExportPageVO.getDestination()) ? null : Restrictions.like("consigneeProvinceName", "%" + dispatchExportPageVO.getDestination() + "%");
		Restriction r12 = ToolKits.isEmpty(dispatchExportPageVO.getDestination()) ? null : Restrictions.like("consigneeCityName", "%" + dispatchExportPageVO.getDestination() + "%");
		Restriction r13 = ToolKits.isEmpty(dispatchExportPageVO.getProvenance()) ? null : Restrictions.or(r9, r10);
		Restriction r14 = ToolKits.isEmpty(dispatchExportPageVO.getDestination()) ? null : Restrictions.or(r11, r12);
		Restriction r15 = ToolKits.isEmpty(dispatchExportPageVO.getLineName()) ? null : Restrictions.like("lineName", "%" + dispatchExportPageVO.getLineName() + "%");
		Restriction r16 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
		Restriction r17 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}

		// 权限控制
		Restriction r18 = null;
		Restriction r29 = null;
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {// 如果为自有账号
			// 拆单状态 0未拆单
			Restriction r30 = Restrictions.eq("splitOrderStatus", DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			// 承运商拆单类型 1未拆单
			Restriction r31 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN);
			Restriction r32 = Restrictions.and(r30, r31);
			// 承运商拆单类型 2拆分主单
			Restriction r33 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_SPLIT_MASTER);
			// 承运商拆单类型 2中转主单
			Restriction r34 = Restrictions.eq("carrierSplitOrderType", DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			r29 = Restrictions.or(r32, r33, r34);

			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {// 为部门权限
				r18 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
			} else {// 为业务员权限
				Restriction r19 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
				Restriction r20 = Restrictions.in("cargoOwnerId", user.getConsignors().stream().toArray(Long[]::new));
				Restriction r21 = Restrictions.in("consigneeUnitId", user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r22 = Restrictions.in("consigneeUnitId", user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r23 = Restrictions.in("consigneeUnitId", user.getCustomers().stream().toArray(Long[]::new));

				Restriction r41 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r42 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r43 = Restrictions.in(TmsShipbillHeaderModel::getShipperUnitId, user.getCustomers().stream().toArray(Long[]::new));

				Restriction r24 = Restrictions.or(r21, r22, r23, r41, r42, r43);
				r18 = Restrictions.or(r19, r20, r24);
			}
		} else {// 非自有账号(承运商)
			r29 = Restrictions.eq("carrierSplitOrderStatus", DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
			Restriction r19 = Restrictions.eq("carrierId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			Restriction r20 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
			Restriction r21 = Restrictions.eq("carrierShipbillStatus", DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED);
			r18 = Restrictions.and(r19, r20, r21);
		}
		//

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r13, r14, r15, r16, r17, r18, r29));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		PageUtils.startPage(dispatchExportPageVO.getPageNum(), dispatchExportPageVO.getPageSize());
		List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteria);

		List<ExportDispatchDTO> list = Lists.newArrayList();
		for (TmsShipbillHeaderModel model : tmsShipbillHeaderModels) {
			ExportDispatchDTO dto = new ExportDispatchDTO();
			BeanUtils.copyProperties(model, dto);

			// 判断是否有包裹 有包裹 修改件数体积总量
			if (ToolKits.isNotEmpty(model.getPackageTotalWeight())) {
				dto.setTotalWeight(model.getPackageTotalWeight());
			}
			if (ToolKits.isNotEmpty(model.getPackageTotalVolume())) {
				dto.setTotalVolume(model.getPackageTotalVolume());
			}
			if (ToolKits.isNotEmpty(model.getPackageLclPiece())) {
				if (ToolKits.isNotEmpty(model.getPackageWholePiece())) {
					dto.setTotalPcs(model.getPackageLclPiece().add(model.getPackageWholePiece()));
				} else {
					dto.setTotalPcs(model.getPackageLclPiece());
				}
			} else {
				dto.setTotalPcs(BigDecimal.ZERO);
			}
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				dto.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

			// 类型翻译
			dto.setOrderTypeName(PublicMethods.change(data1, String.valueOf(model.getOrderType())));
			dto.setOrderBusinessTypeName(PublicMethods.change(data2, String.valueOf(model.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(PublicMethods.change(data3, String.valueOf(model.getDeliveryMethodType())));
			dto.setTransportTypeName(PublicMethods.change(data4, String.valueOf(model.getTransportType())));
			dto.setTemperatureRequirementName(PublicMethods.change(data5, String.valueOf(model.getTemperatureRequirement())));
			dto.setShipperTypeName(PublicMethods.change(data6, String.valueOf(model.getShipperType())));
			dto.setConsigneeTypeName(PublicMethods.change(data7, String.valueOf(model.getConsigneeType())));

			list.add(dto);
		}

		return list;
	}

	/**
	 * 导出选中数据
	 * 
	 * @param ids
	 * @return
	 */
	@Override
	public List<ExportDispatchDTO> exportChooseDispatchData(List<String> ids) {
		ToolKits.isParamNull(ids, "运单ID集合不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();
		List<Map<String, Object>> data4 = Lists.newArrayList();
		List<Map<String, Object>> data5 = Lists.newArrayList();
		List<Map<String, Object>> data6 = Lists.newArrayList();
		List<Map<String, Object>> data7 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new RuntimeException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new RuntimeException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new RuntimeException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}

		// 添加查询条件
		Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
		Restriction r1 = Restrictions.in("shipbillId", ids.toArray());
		Restriction r2 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
		Restriction r3 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}

		// 权限控制
		Restriction r4 = null;
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {// 如果为自有账号
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {// 为部门权限
				r4 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
			} else {// 为业务员权限
				Restriction r19 = Restrictions.in("departmentId", user.getDepts().stream().toArray(String[]::new));
				Restriction r20 = Restrictions.in("cargoOwnerId", user.getConsignors().stream().toArray(Long[]::new));
				Restriction r21 = Restrictions.in("consigneeUnitId", user.getWarehouses().stream().toArray(Long[]::new));
				Restriction r22 = Restrictions.in("consigneeUnitId", user.getSuppliers().stream().toArray(Long[]::new));
				Restriction r23 = Restrictions.in("consigneeUnitId", user.getCustomers().stream().toArray(Long[]::new));
				Restriction r24 = Restrictions.or(r21, r22, r23);
				r4 = Restrictions.or(r19, r20, r24);
			}
		} else {// 非自有账号(承运商)
			Restriction r19 = Restrictions.eq("carrierId", Long.valueOf(user.getLoginUserCompanyInfo().getId()));
			Restriction r20 = Restrictions.eq("status", DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
			Restriction r21 = Restrictions.eq("carrierShipbillStatus", DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED);
			r4 = Restrictions.and(r19, r20, r21);
		}
		//

		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteria);

		List<ExportDispatchDTO> list = Lists.newArrayList();
		for (TmsShipbillHeaderModel model : tmsShipbillHeaderModels) {
			ExportDispatchDTO dto = new ExportDispatchDTO();
			BeanUtils.copyProperties(model, dto);

			// 判断是否有包裹 有包裹 修改件数体积总量
			if (ToolKits.isNotEmpty(model.getPackageTotalWeight())) {
				dto.setTotalWeight(model.getPackageTotalWeight());
			}
			if (ToolKits.isNotEmpty(model.getPackageTotalVolume())) {
				dto.setTotalVolume(model.getPackageTotalVolume());
			}
			if (ToolKits.isNotEmpty(model.getPackageLclPiece())) {
				if (ToolKits.isNotEmpty(model.getPackageWholePiece())) {
					dto.setTotalPcs(model.getPackageLclPiece().add(model.getPackageWholePiece()));
				} else {
					dto.setTotalPcs(model.getPackageLclPiece());
				}
			} else {
				dto.setTotalPcs(BigDecimal.ZERO);
			}
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				dto.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				dto.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

			// 类型翻译
			dto.setOrderTypeName(PublicMethods.change(data1, String.valueOf(model.getOrderType())));
			dto.setOrderBusinessTypeName(PublicMethods.change(data2, String.valueOf(model.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(PublicMethods.change(data3, String.valueOf(model.getDeliveryMethodType())));
			dto.setTransportTypeName(PublicMethods.change(data4, String.valueOf(model.getTransportType())));
			dto.setTemperatureRequirementName(PublicMethods.change(data5, String.valueOf(model.getTemperatureRequirement())));
			dto.setShipperTypeName(PublicMethods.change(data6, String.valueOf(model.getShipperType())));
			dto.setConsigneeTypeName(PublicMethods.change(data7, String.valueOf(model.getConsigneeType())));

			list.add(dto);
		}

		return list;
	}

	/**
	 * 打印已调度运单
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public Map<String, Object> printDispatchData(String id) {
		ToolKits.isParamNull(id, "运单ID不为空");

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

		// 查询运单信息
		TmsShipbillHeaderModel tmsShipbillHeaderModel = tmsShipbillHeaderMapper.findById(id, TmsShipbillHeaderModel.class);

		ToolKits.isParamNull(tmsShipbillHeaderModel, "运单ID[" + id + "]信息不存在");

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}

		// 打印头部数据
		DispatchePrintHeadDTO dispatchePrintHeadDTO = new DispatchePrintHeadDTO();
		dispatchePrintHeadDTO.setShipbillNo(tmsShipbillHeaderModel.getShipbillNo());
		dispatchePrintHeadDTO.setCreatetime(tmsShipbillHeaderModel.getCreatetime());
		dispatchePrintHeadDTO.setOrderType(PublicMethods.change(data1, String.valueOf(tmsShipbillHeaderModel.getOrderType())));
		dispatchePrintHeadDTO.setShipperName(tmsShipbillHeaderModel.getShipperName());
		dispatchePrintHeadDTO.setShipperContact(tmsShipbillHeaderModel.getShipperContact());
		dispatchePrintHeadDTO.setShipperPhoneNumber(tmsShipbillHeaderModel.getShipperPhoneNumber());
		dispatchePrintHeadDTO.setShipperLandlineNumber(tmsShipbillHeaderModel.getShipperLandlineNumber());
		dispatchePrintHeadDTO.setShipperProvinceName(tmsShipbillHeaderModel.getShipperProvinceName());
		dispatchePrintHeadDTO.setShipperCityName(tmsShipbillHeaderModel.getShipperCityName());
		dispatchePrintHeadDTO.setShipperDistrictName(tmsShipbillHeaderModel.getShipperDistrictName());
		dispatchePrintHeadDTO.setShipperAddress(tmsShipbillHeaderModel.getShipperAddress());
		dispatchePrintHeadDTO.setConsigneeName(tmsShipbillHeaderModel.getConsigneeName());
		dispatchePrintHeadDTO.setConsigneeContact(tmsShipbillHeaderModel.getConsigneeContact());
		dispatchePrintHeadDTO.setConsigneePhoneNumber(tmsShipbillHeaderModel.getConsigneePhoneNumber());
		dispatchePrintHeadDTO.setConsigneeLandlineNumber(tmsShipbillHeaderModel.getConsigneeLandlineNumber());
		dispatchePrintHeadDTO.setConsigneeProvinceName(tmsShipbillHeaderModel.getConsigneeProvinceName());
		dispatchePrintHeadDTO.setConsigneeCityName(tmsShipbillHeaderModel.getConsigneeCityName());
		dispatchePrintHeadDTO.setConsigneeDistrictName(tmsShipbillHeaderModel.getConsigneeDistrictName());
		dispatchePrintHeadDTO.setConsigneeAddress(tmsShipbillHeaderModel.getConsigneeAddress());

		// 获取商品信息
		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", id);
		criteria.setRestriction(r1);
		List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteria);

		List<DispatchePrintTableDTO> dispatchePrintTableDTOS = Lists.newArrayList();

		if (ToolKits.isNotEmpty(tmsShipbillDetailModels)) {
			for (TmsShipbillDetailModel item : tmsShipbillDetailModels) {
				DispatchePrintTableDTO dto = new DispatchePrintTableDTO();
				dto.setItemCode(item.getItemCode());
				dto.setItemName(item.getItemName());
				dto.setPcs(item.getPcs());
				dto.setWeight(item.getWeight());
				dto.setVolume(item.getVolume());
				dispatchePrintTableDTOS.add(dto);
			}
		}

		// TODO print
		return null;
	}

	/**
	 * 已调度运单查看
	 * 
	 * @param dispatcheViewVO
	 * @return
	 */
	@Override
	public DispatcheViewDTO viewDispatchData(DispatcheViewVO dispatcheViewVO) {
		ToolKits.isParamNull(dispatcheViewVO.getShipbillId(), "运单ID不为空");

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

		// 数据字典翻译数据集合
		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();
		List<Map<String, Object>> data4 = Lists.newArrayList();
		List<Map<String, Object>> data5 = Lists.newArrayList();
		List<Map<String, Object>> data6 = Lists.newArrayList();
		List<Map<String, Object>> data7 = Lists.newArrayList();
		List<Map<String, Object>> data8 = Lists.newArrayList();
		List<Map<String, Object>> data9 = Lists.newArrayList();
		List<Map<String, Object>> data10 = Lists.newArrayList();

		// 订单类型
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new RuntimeException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new RuntimeException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new RuntimeException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}
		// 包装类型
		RestMessage<List<Map<String, Object>>> restMessage8 = maindataTmsResource.getDropDownById(DataDictionaryConstant.PACK_TYPE, null, null, null);
		if (!restMessage8.isSuccess()) {
			throw new RuntimeException("订单类型翻译失败!");
		} else {
			data8 = restMessage8.getData();
		}
		// 货物温层
		RestMessage<List<Map<String, Object>>> restMessage9 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CARGO_THERMOSPHERE, null, null, null);
		if (!restMessage9.isSuccess()) {
			throw new RuntimeException("货物温层翻译失败!");
		} else {
			data9 = restMessage9.getData();
		}
		// 包裹类型
		RestMessage<List<Map<String, Object>>> restMessage10 = maindataTmsResource.getDropDownById(DataDictionaryConstant.PACKAGE_TYPE, null, null, null);
		if (!restMessage10.isSuccess()) {
			throw new RuntimeException("包裹类型翻译失败!");
		} else {
			data10 = restMessage10.getData();
		}

		// 查询运单汇总信息
		TmsShipbillHeaderModel model = tmsShipbillHeaderMapper.findById(dispatcheViewVO.getShipbillId(), TmsShipbillHeaderModel.class);

		DispatcheViewDTO dto = new DispatcheViewDTO();
		BeanUtils.copyProperties(model, dto);

		// 类型翻译
		dto.setOrderTypeName(PublicMethods.change(data1, String.valueOf(dto.getOrderType())));
		dto.setOrderBusinessTypeName(PublicMethods.change(data2, String.valueOf(dto.getOrderBusinessType())));
		dto.setDeliveryMethodTypeName(PublicMethods.change(data3, String.valueOf(dto.getDeliveryMethodType())));
		dto.setTransportTypeName(PublicMethods.change(data4, String.valueOf(dto.getTransportType())));
		dto.setTemperatureRequirementName(PublicMethods.change(data5, String.valueOf(dto.getTemperatureRequirement())));
		dto.setShipperTypeName(PublicMethods.change(data6, String.valueOf(dto.getShipperType())));
		dto.setConsigneeTypeName(PublicMethods.change(data7, String.valueOf(dto.getConsigneeType())));

		StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
		// 目的地
		consigneeProvinceCityDistrict.append(model.getConsigneeProvinceName());
		consigneeProvinceCityDistrict.append(model.getConsigneeCityName());
		consigneeProvinceCityDistrict.append(model.getConsigneeDistrictName());
		dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());
		// 始发地
		StringBuffer shipperProvinceCityDistrict = new StringBuffer();
		shipperProvinceCityDistrict.append(model.getShipperProvinceName());
		shipperProvinceCityDistrict.append(model.getShipperCityName());
		shipperProvinceCityDistrict.append(model.getShipperDistrictName());
		dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

		// 运单明细数据集
		List<DispatcheItemViewDTO> dispatcheItemViewDTOS = Lists.newArrayList();

		// 查询商品
		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", dispatcheViewVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().asc(new String[] { "seqno" }));
		List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(tmsShipbillDetailModels)) {
			for (TmsShipbillDetailModel tmsShipbillDetailModel : tmsShipbillDetailModels) {
				DispatcheItemViewDTO detailDTO = new DispatcheItemViewDTO();
				BeanUtils.copyProperties(tmsShipbillDetailModel, detailDTO);

				// 数据字典翻译
				detailDTO.setPackTypeName(PublicMethods.change(data8, String.valueOf(detailDTO.getPackType())));
				detailDTO.setCargoThermosphereName(PublicMethods.change(data9, String.valueOf(detailDTO.getCargoThermosphere())));

				dispatcheItemViewDTOS.add(detailDTO);
			}
			dto.setDispatcheItemViewDTOS(dispatcheItemViewDTOS);
		}

		// 查询包裹
		criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		r1 = Restrictions.eq("shipbillId", dispatcheViewVO.getShipbillId());
		r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));

		List<TmsShipbillPackageModel> tmsShipbillPackageModels = tmsShipbillPackageMapper.findByCriteria(criteria);

		List<DispatchePackageViewDTO> dispatchePackageViewDTOS = Lists.newArrayList();

		for (TmsShipbillPackageModel tmsShipbillPackageModel : tmsShipbillPackageModels) {
			DispatchePackageViewDTO dispatchePackageViewDTO = new DispatchePackageViewDTO();
			BeanUtils.copyProperties(tmsShipbillPackageModel, dispatchePackageViewDTO);

			// 数据字典翻译
			dispatchePackageViewDTO.setPackageTypeName(PublicMethods.change(data10, String.valueOf(dispatchePackageViewDTO.getPackageType())));

			// 设备类型翻译
			if (ToolKits.isNotEmpty(tmsShipbillPackageModel.getDeviceId())) {
				RestMessage<DeviceByOneDTO> deviceByOneDTORestMessage = maindataTmsResource.deviceGetOne(tmsShipbillPackageModel.getDeviceId());
				if (!deviceByOneDTORestMessage.isSuccess()) {
					throw new RuntimeException("获取设备失败!");
				}
				DeviceByOneDTO data = deviceByOneDTORestMessage.getData();
				ToolKits.isParamNull(data, "获取设备为空");

				dispatchePackageViewDTO.setDeviceTypeName(data.getTypeName());
			}

			dispatchePackageViewDTOS.add(dispatchePackageViewDTO);
		}
		dto.setDispatchePackageViewDTOS(dispatchePackageViewDTOS);

		// 设置包裹总件数
		if (ToolKits.isNotEmpty(model.getPackageLclPiece())) {
			if (ToolKits.isNotEmpty(model.getPackageWholePiece())) {
				dto.setPackageTotalPcs(model.getPackageLclPiece().add(model.getPackageWholePiece()));
			} else {
				dto.setPackageTotalPcs(model.getPackageLclPiece());
			}
		} else {
			dto.setPackageTotalPcs(BigDecimal.ZERO);
		}

		return dto;
	}

	/**
	 * 已调度运单撤销
	 * 
	 * @param ids
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void undoDispatchData(List<String> ids,String processType) {
		ToolKits.isParamNull(ids, "运单ID不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}

		Criteria criteria = Criteria.forClass(TmsLoadingBillDetailModel.class);
		Restriction r1 = Restrictions.in("shipbillId", ids.toArray());
		Restriction r3 = null;
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS) && ToolKits.isEmpty(processType)) {
			r3 = Restrictions.eq(TmsLoadingBillDetailModel::getCarrierShipbillStatus, DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED);
		} if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS) && ToolKits.isNotEmpty(processType)) {
			r3 = Restrictions.eq(TmsLoadingBillDetailModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK);
		} else {
			r3 = Restrictions.eq(TmsLoadingBillDetailModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
		}

		criteria.setRestriction(Restrictions.and(r1, r3));
		List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteria);

		// 修改配送顺序以及总重量体积件数等数据
		for (TmsLoadingBillDetailModel tmsLoadingBillDetailModel : tmsLoadingBillDetailModels) {

			// 修改配送顺序
			r1 = Restrictions.eq("loadingBillId", tmsLoadingBillDetailModel.getLoadingBillId());
			Restriction r2 = Restrictions.ne("shipbillId", tmsLoadingBillDetailModel.getShipbillId());
			criteria.setRestriction(Restrictions.and(r1, r2));
			List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModelList = tmsLoadingBillDetailMapper.findByCriteria(criteria);
			for (TmsLoadingBillDetailModel loadingBillDetailModel : tmsLoadingBillDetailModelList) {
				if (loadingBillDetailModel.getShippingOrder() > tmsLoadingBillDetailModel.getShippingOrder()) {
					loadingBillDetailModel.setShippingOrder(loadingBillDetailModel.getShippingOrder() - 1);
				}
				tmsLoadingBillDetailMapper.update(loadingBillDetailModel);
			}
			// 修改汇总的数据
			TmsLoadingBillHeaderModel tmsLoadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(tmsLoadingBillDetailModel.getLoadingBillId(), TmsLoadingBillHeaderModel.class);

			// 客户数
			Integer customerNumber = tmsLoadingBillDetailModelList.stream().filter(o -> ToolKits.isNotEmpty(o.getConsigneeName())).map(TmsLoadingBillDetailModel::getConsigneeName).distinct().collect(Collectors.toList()).size();

			// 站点数
			Integer siteNumber = tmsLoadingBillDetailModelList.stream().filter(o -> ToolKits.isNotEmpty(o.getConsigneeAddress())).map(TmsLoadingBillDetailModel::getConsigneeAddress).distinct().collect(Collectors.toList()).size();

			// 重量体积件数
			tmsLoadingBillHeaderModel.setTotalPcs(tmsLoadingBillHeaderModel.getTotalPcs().subtract(tmsLoadingBillDetailModel.getTotalPcs()));
			tmsLoadingBillHeaderModel.setTotalVolume(tmsLoadingBillHeaderModel.getTotalVolume().subtract(tmsLoadingBillDetailModel.getTotalVolume()));
			tmsLoadingBillHeaderModel.setTotalWeight(tmsLoadingBillHeaderModel.getTotalWeight().subtract(tmsLoadingBillDetailModel.getTotalWeight()));

			// 实际重量体积件数
			tmsLoadingBillHeaderModel.setActualTotalPcs(tmsLoadingBillHeaderModel.getActualTotalPcs().subtract(tmsLoadingBillDetailModel.getActualTotalPcs()));
			tmsLoadingBillHeaderModel.setActualTotalVolume(tmsLoadingBillHeaderModel.getActualTotalVolume().subtract(tmsLoadingBillDetailModel.getActualTotalVolume()));
			tmsLoadingBillHeaderModel.setActualTotalWeight(tmsLoadingBillHeaderModel.getActualTotalWeight().subtract(tmsLoadingBillDetailModel.getActualTotalWeight()));

			// 整件数拼箱数
			tmsLoadingBillHeaderModel.setTotalWholePiece(tmsLoadingBillDetailModel.getPackageWholePiece() == null ? null : tmsLoadingBillHeaderModel.getTotalWholePiece().subtract(tmsLoadingBillDetailModel.getPackageWholePiece()));
			tmsLoadingBillHeaderModel.setTotalLclPiece(tmsLoadingBillDetailModel.getPackageLclPiece() == null ? null : tmsLoadingBillHeaderModel.getTotalLclPiece().subtract(tmsLoadingBillDetailModel.getPackageLclPiece()));

			// 运单数
			tmsLoadingBillHeaderModel.setShipbillNumber(tmsLoadingBillHeaderModel.getShipbillNumber() - 1);

			// 客户数
			tmsLoadingBillHeaderModel.setCustomerNumber(customerNumber);
			// 站点数
			tmsLoadingBillHeaderModel.setSiteNumber(siteNumber);

			// 计算装载率
			if (ToolKits.isNotEmpty(tmsLoadingBillHeaderModel.getVehicleId())) {
				Map<String, String> map = this.calculateLoadingRate(tmsLoadingBillHeaderModel.getVehicleId(), tmsLoadingBillHeaderModel.getTotalWeight(), tmsLoadingBillHeaderModel.getTotalVolume());
				tmsLoadingBillHeaderModel.setVolumeLoadingRate(map.get("volume"));
				tmsLoadingBillHeaderModel.setWeightLoadingRate(map.get("weight"));
			}

			// 删除明细内容
			tmsLoadingBillDetailMapper.deleteById(tmsLoadingBillDetailModel.getId(), TmsLoadingBillDetailModel.class);

			// 修改汇总表
			tmsLoadingBillHeaderMapper.update(tmsLoadingBillHeaderModel);

			// 运单操作日志保存
			ShipbillOperateLogSaveVO shipbillOperateLogSaveVO = new ShipbillOperateLogSaveVO();
			shipbillOperateLogSaveVO.setShipbillId(tmsLoadingBillDetailModel.getShipbillId());
			shipbillOperateLogSaveVO.setShipbillNo(tmsLoadingBillDetailModel.getShipbillNo());
			shipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_DIS_REVOKE);
			shipbillOperateLogSaveVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH);
			shipbillOperateLogSaveVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_REVIEW);
			shipbillOperateLogService.addShipbillOperateLogData(shipbillOperateLogSaveVO);

		}

		// 运单状态回退
		criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
		r1 = Restrictions.in("shipbillId", ids.toArray());
		criteria.setRestriction(r1);

		Map<String, Object> map = Maps.newHashMap();
		map.put("updateperson", user.getNickname());
		map.put("updatetime", DateUtil.date());
		// 如果是承运商操作撤销,还需更新运单【承运商运单状态】为【已撤销】
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS) && ToolKits.isEmpty(processType)) {
			map.put("carrierShipbillStatus", DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED);
		} else {
			map.put("status", DataDictionaryConstant.SHIPBILL_STATUS_REVIEW);
		}
		tmsShipbillHeaderMapper.updateMapByCriteria(map, criteria);
	}

	/**
	 * 计算装载率
	 *
	 * @param id
	 * @param weight
	 * @param volume
	 * @return
	 */
	private Map<String, String> calculateLoadingRate(Long id, BigDecimal weight, BigDecimal volume) {

		VehicleCheckVO vehicleCheckVO = new VehicleCheckVO();
		vehicleCheckVO.setVehicleId(id);

		RestMessage<VehicleCheckDTO> data = maindataTmsResource.getVehicle(vehicleCheckVO);
		if (!data.isSuccess()) {
			throw new RuntimeException("获取车辆失败");
		}
		VehicleCheckDTO vehicleCheckDTO = data.getData();

		StringBuffer weightStr = new StringBuffer();
		StringBuffer volumeStr = new StringBuffer();

		if (ToolKits.isNotEmpty(weight)) {
			BigDecimal divide = ToolKits.isEmpty(vehicleCheckDTO.getWeight()) ? BigDecimal.ZERO : weight.divide(vehicleCheckDTO.getWeight(), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal multiply = divide.multiply(new BigDecimal("100"));
			weightStr.append(multiply.intValue());
			weightStr.append("%");
		}
		if (ToolKits.isNotEmpty(volume)) {
			BigDecimal divide = ToolKits.isEmpty(vehicleCheckDTO.getVolume()) ? BigDecimal.ZERO : volume.divide(vehicleCheckDTO.getVolume(), 2, BigDecimal.ROUND_HALF_UP);
			BigDecimal multiply = divide.multiply(new BigDecimal("100"));
			volumeStr.append(multiply.intValue());
			volumeStr.append("%");
		}

		Map<String, String> map = Maps.newHashMap();
		map.put("weight", weightStr.toString());
		map.put("volume", volumeStr.toString());

		return map;
	}

}
