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

import java.util.Date;
import java.util.GregorianCalendar;
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.TmsShipbillPackageMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillTransportLogMapper;
import com.prolog.rdc.tms.model.dto.DeviceByOneDTO;
import com.prolog.rdc.tms.model.dto.OutStockOnWayPageDTO;
import com.prolog.rdc.tms.model.dto.WholeCarDeliveryItemDTO;
import com.prolog.rdc.tms.model.dto.WholeCarDeliveryPackDTO;
import com.prolog.rdc.tms.model.dto.WholeCarDeliveryShibillDTO;
import com.prolog.rdc.tms.model.dto.message.SendMessageDTO;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
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.TmsShipbillTransLogModel;
import com.prolog.rdc.tms.model.enums.MessageOrderTypeEnum;
import com.prolog.rdc.tms.model.enums.MessageTypeEnum;
import com.prolog.rdc.tms.model.vo.DeliveryConfirmVO;
import com.prolog.rdc.tms.model.vo.OutStockOnWayAbnormalRegisterVO;
import com.prolog.rdc.tms.model.vo.OutStockOnWayAdvancedQueryVO;
import com.prolog.rdc.tms.model.vo.OutStockOnWayDeliveryConfirmVO;
import com.prolog.rdc.tms.model.vo.OutStockOnWayFeedbackVO;
import com.prolog.rdc.tms.model.vo.OutStockOnWayQueryVO;
import com.prolog.rdc.tms.model.vo.ShipbillNodeTrackingSaveNewVO;
import com.prolog.rdc.tms.model.vo.ShipbillOperateLogSaveNewVO;
import com.prolog.rdc.tms.model.vo.WholeCarAbnormalRegisterVO;
import com.prolog.rdc.tms.model.vo.WholeCarDeliveryItemQueryVO;
import com.prolog.rdc.tms.model.vo.WholeCarDeliveryPackQueryVO;
import com.prolog.rdc.tms.model.vo.WholeCarFeedbackVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.OutStockOnWayService;
import com.prolog.rdc.tms.service.ShipbillNodeTrackingService;
import com.prolog.rdc.tms.service.ShipbillOperateLogService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import com.prolog.rdc.tms.util.MessageUtil;
import com.prolog.rdc.tms.util.PublicMethods;

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

/**
 * 运输管理——出库在途实现层
 *
 * @author xyf
 * @since 2022/6/27 12:43
 */
@Service
public class OutStockOnWayServiceImpl implements OutStockOnWayService {

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Autowired
	private ConvertorUtil convertorUtil;

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

	@Autowired
	private ShipbillOperateLogService shipbillOperateLogService;

	@Autowired
	private ShipbillNodeTrackingService shipbillNodeTrackingService;

	@Autowired
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private TmsShipbillPackageMapper tmsShipbillPackageMapper;

	/**
	 * 主界面分页查询
	 *
	 * @param
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<OutStockOnWayPageDTO> queryPage(OutStockOnWayQueryVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.LOADING_BILL_STATUS// 派车单状态
		});

		Criteria criteria = Criteria.forClass(TmsLoadingBillHeaderModel.class);
		Restriction r1 = ToolKits.isEmpty(vo.getLoadingBillNo()) ? null : Restrictions.like("loadingBillNo", "%" + vo.getLoadingBillNo() + "%");
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		Restriction r3 = Restrictions.eq("status", DataDictionaryConstant.LOADING_BILL_STATUS_QY);
		Restriction r4 = null;
		if (ToolKits.isNotEmpty(vo.getStartDate()) && ToolKits.isEmpty(vo.getEndDate())) {
			r4 = Restrictions.between("createtime", vo.getStartDate(), new GregorianCalendar(3000, 1, 1).getTime());
		}
		if (ToolKits.isEmpty(vo.getStartDate()) && ToolKits.isNotEmpty(vo.getEndDate())) {
			r4 = Restrictions.between("createtime", new GregorianCalendar(1700, 1, 1).getTime(), vo.getEndDate());
		}
		if (ToolKits.isNotEmpty(vo.getStartDate()) && ToolKits.isNotEmpty(vo.getEndDate())) {
			r4 = Restrictions.between("createtime", vo.getStartDate(), vo.getEndDate());
		}
		Restriction r5 = Restrictions.eq("loadingBillType", DataDictionaryConstant.LOADING_BILL_TYPE_CK);
		// 查询业务标识 开单时当运单发货方都为仓库时 对装车单标记一个出库在途查询标识
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsLoadingBillHeaderModel> tmsLoadingBillHeaderModels = tmsLoadingBillHeaderMapper.findByCriteria(criteria);

		List<OutStockOnWayPageDTO> dtos = Lists.newArrayList();
		for (TmsLoadingBillHeaderModel model : tmsLoadingBillHeaderModels) {
			OutStockOnWayPageDTO dto = new OutStockOnWayPageDTO();
			BeanUtils.copyProperties(model, dto);
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LOADING_BILL_STATUS, String.valueOf(dto.getStatus())));
			dtos.add(dto);
		}
		// 分页重构
		Page<OutStockOnWayPageDTO> page = PageUtils.getPage(dtos);
		if (tmsLoadingBillHeaderModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsLoadingBillHeaderModel> p0 = (com.github.pagehelper.Page<TmsLoadingBillHeaderModel>) tmsLoadingBillHeaderModels;
			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 vo
	 * @return
	 */
	@Override
	public Page<OutStockOnWayPageDTO> advancedQueryPage(OutStockOnWayAdvancedQueryVO vo) {
		ToolKits.isParamNull(vo, "汇总查询请求不为空");
		ToolKits.isParamNull(vo.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(vo.getPageSize(), "每页条数不为空");

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

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.LOADING_BILL_STATUS });// 派车单状态

		// 权限控制
		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setDepts(user.getDepts().stream().toArray(String[]::new));
		vo.setCarrierType(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ);
		vo.setLoadingBillType(DataDictionaryConstant.LOADING_BILL_TYPE_CK);

		// 判断调度类型
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			vo.setScheduleType(DataDictionaryConstant.SCHEDULE_TYPE_ZY);
		} else {
			vo.setScheduleType(DataDictionaryConstant.SCHEDULE_TYPE_CYS);
		}

		// 获取高级查询配置
		String conditions = null;
		RestMessage<String> restMessageAdvancedCfg = maindataTmsResource.getQueryConfiguration(DataDictionaryConstant.BIZ_TYPE_OUTSTOCKONWAY);
		if (!restMessageAdvancedCfg.isSuccess()) {
			throw new RuntimeException("获取高级查询配置失败!");
		} else {
			conditions = restMessageAdvancedCfg.getData();
		}
		vo.setSql(conditions);

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<OutStockOnWayPageDTO> outStockOnWayPageDTOS = tmsLoadingBillHeaderMapper.outStockOnWayAdvancedQuery(vo);

		for (OutStockOnWayPageDTO dto : outStockOnWayPageDTOS) {
			// 状态翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LOADING_BILL_STATUS, String.valueOf(dto.getStatus())));

		}
		return PageUtils.getPage(outStockOnWayPageDTOS);
	}

	/**
	 * 运单送达确认
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void shipbillDeliveryConfirm(OutStockOnWayDeliveryConfirmVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (ToolKits.isEmpty(vo.getShipbillIds()) || vo.getShipbillIds().size() == 0) {
			throw new PrologException("请选择要操作的数据");
		}

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

		// 签到时间是否为空
		boolean deliverySignTime = false;
		// 货物图片
		String goodsPhoto = "";
		// 回单图片
		String receiptPhoto = "";

		// 节点内容
		StringBuffer nodeEvent = new StringBuffer();
		nodeEvent.append("送达确认 送达签收人:");
		nodeEvent.append(ToolKits.isEmpty(vo.getSignPerson()) ? "" : vo.getSignPerson());
		nodeEvent.append(" 送达备注:");
		nodeEvent.append(ToolKits.isEmpty(vo.getMemo()) ? "" : vo.getMemo());

		List<TmsShipbillHeaderModel> tmsShipbillHeaders = tmsShipbillHeaderMapper.findByIds(TmsShipbillHeaderModel.class, vo.getShipbillIds().toArray(), null);
		// 如果运单中存在需要送达签到的运单 则签到时间不能为空
		List<TmsShipbillHeaderModel> tmsShipbillHeaders2 = tmsShipbillHeaders.stream().filter(o -> ObjectUtil.equal(StatusConstant.WHETHER_YES, o.getConsigneeDeliverySign())).collect(Collectors.toList());
		if (ToolKits.isNotEmpty(tmsShipbillHeaders2) && ToolKits.isEmpty(vo.getSignDate())) {
			throw new PrologException("签到时间不能为空");
		}
		// 如果运单中存在需要送货图片的运单 则签到时间不能为空
		List<TmsShipbillHeaderModel> tmsShipbillHeaders3 = tmsShipbillHeaders.stream().filter(o -> ObjectUtil.equal(StatusConstant.WHETHER_YES, o.getConsigneeDeliveryPicture())).collect(Collectors.toList());
		if (ToolKits.isNotEmpty(tmsShipbillHeaders3) && ToolKits.isEmpty(vo.getGoodsPicUrl())) {
			throw new PrologException("货物图片不能为空");
		}

		int status = DataDictionaryConstant.SHIPBILL_STATUS_END;
		for (TmsShipbillHeaderModel tmsShipbillHeader : tmsShipbillHeaders) {
			// 更新运单
			tmsShipbillHeader.setTransportStatus(DataDictionaryConstant.TRANSPORT_STATUS_ARRIVED);
			tmsShipbillHeader.setUpdateperson(user.getNickname());
			tmsShipbillHeader.setUpdatetime(DateUtil.date());

			// 更新运单运输记录
			Map<String, Object> queryMap = Maps.newHashMap();
			queryMap.put("shipbillId", tmsShipbillHeader.getShipbillId());
			List<TmsShipbillTransLogModel> tmsShipbillTransportLogs = tmsShipbillTransportLogMapper.findByMap(queryMap, TmsShipbillTransLogModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillTransportLogs) && tmsShipbillTransportLogs.size() > 0) {
				TmsShipbillTransLogModel tmsShipbillTransportLog = tmsShipbillTransportLogs.get(0);
				tmsShipbillTransportLog.setUpdateperson(user.getNickname());
				tmsShipbillTransportLog.setUpdatetime(DateUtil.date());
				tmsShipbillTransportLog.setDeliveryTime(vo.getDeliveryDate());
				tmsShipbillTransportLog.setDeliveryStatus(vo.getDeliveryStatus());
				if (ToolKits.isNotEmpty(vo.getSignDate())) {
					tmsShipbillTransportLog.setDeliverySignTime(vo.getSignDate());
				}
				if (ToolKits.isNotEmpty(vo.getMemo())) {
					tmsShipbillTransportLog.setDeliveryMemo(vo.getMemo());
				}
				tmsShipbillTransportLogMapper.update(tmsShipbillTransportLog);
			}

			// 运单节点信息
			ShipbillNodeTrackingSaveNewVO shipbillNodeTrackingSaveNewVO = new ShipbillNodeTrackingSaveNewVO();
			shipbillNodeTrackingSaveNewVO.setNodeDate(DateUtil.date());
			shipbillNodeTrackingSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
			shipbillNodeTrackingSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
			shipbillNodeTrackingSaveNewVO.setUserId(user.getUserid());
			shipbillNodeTrackingSaveNewVO.setUserName(user.getNickname());
			shipbillNodeTrackingSaveNewVO.setEnterpriseId(user.getEnterpriseId());
			shipbillNodeTrackingSaveNewVO.setEnterpriseName(user.getEnterpriseName());

			// 运单日志信息
			ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
			shipbillOperateLogSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
			shipbillOperateLogSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
			shipbillOperateLogSaveNewVO.setOperateDate(DateUtil.date());
			shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
			shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
			shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
			shipbillOperateLogSaveNewVO.setUserName(user.getNickname());

			if (ToolKits.isNotEmpty(vo.getSignDate())) {
				// 运单节点保存
				shipbillNodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
				shipbillNodeTrackingSaveNewVO.setNodeEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_SDQD);
				shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveNewVO);

				// 运单日志保存
				shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_SDQD);
				shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
				shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
				shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);

				deliverySignTime = true;
			}
			// 运单节点保存
			shipbillNodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
			shipbillNodeTrackingSaveNewVO.setNodeEvent(nodeEvent.toString());
			shipbillNodeTrackingSaveNewVO.setPicUrl(vo.getGoodsPicUrl());
			shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveNewVO);
			// 运单日志保存
			shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_SD);
			shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
			shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
			shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);

			if (ToolKits.isNotEmpty(vo.getReturnOrderPicUrl())) {// 如果回单图片不为空
				if (ObjectUtil.equal(tmsShipbillHeader.getReturnOrderFlag(), DataDictionaryConstant.WHETHER_YES)) {
					status = DataDictionaryConstant.SHIPBILL_STATUS_HD_ZB;
				}
				// 运单节点保存
				shipbillNodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_HD_PIC);
				shipbillNodeTrackingSaveNewVO.setNodeEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_HD_PIC);
				shipbillNodeTrackingSaveNewVO.setPicUrl(vo.getReturnOrderPicUrl());
				shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveNewVO);

				// 运单日志保存
				shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_HD_PIC);
				shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
				shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_HD_PIC);
				shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);
				receiptPhoto = vo.getReturnOrderPicUrl();
			} else {
				if (ObjectUtil.equal(tmsShipbillHeader.getConsigneeReturnOrderPicture(), DataDictionaryConstant.WHETHER_YES)) {
					status = DataDictionaryConstant.SHIPBILL_STATUS_HD_PIC;
				} else if (ObjectUtil.equal(tmsShipbillHeader.getReturnOrderFlag(), DataDictionaryConstant.WHETHER_YES)) {
					status = DataDictionaryConstant.SHIPBILL_STATUS_HD_ZB;
				}
			}
			// 更新运单
			tmsShipbillHeader.setStatus(status);
			tmsShipbillHeaderMapper.update(tmsShipbillHeader);
			// 同步更新运单对应的派车单明细
			Map<String, Object> queryMapLoad = Maps.newHashMap();
			queryMapLoad.put("shipbillId", tmsShipbillHeader.getShipbillId());
			List<TmsLoadingBillDetailModel> list = tmsLoadingBillDetailMapper.findByMap(queryMapLoad, TmsLoadingBillDetailModel.class);
			if (ToolKits.isNotEmpty(list) && list.size() > 0) {
				TmsLoadingBillDetailModel tmsLoadingBillDetail = list.get(0);
				tmsLoadingBillDetail.setTransportStatus(DataDictionaryConstant.TRANSPORT_STATUS_ARRIVED);
				tmsLoadingBillDetail.setUpdateperson(user.getNickname());
				tmsLoadingBillDetail.setUpdatetime(DateUtil.date());
				tmsLoadingBillDetail.setStatus(status);
				tmsLoadingBillDetailMapper.update(tmsLoadingBillDetail);
			} else {
				throw new PrologException("运单【" + tmsShipbillHeader.getShipbillNo() + "】对应的装车单明细查询失败");
			}

			// 同步递归更新运单的主单
			String fatherShipbillId = tmsShipbillHeader.getShipbillId();
			while (ToolKits.isNotEmpty(fatherShipbillId)) {
				fatherShipbillId = fatherDeliveredConfirm(fatherShipbillId, DateUtil.date(), user, deliverySignTime, goodsPhoto, receiptPhoto, nodeEvent.toString(), vo);
				deliverySignTime = false;
			}
		}
	}

	// 送达确认递归更新运单
	private String fatherDeliveredConfirm(String shipbillId, Date date, UserEntity user, boolean deliverySignTime, String goodsPhoto, String receiptPhoto, String nodeEvent, OutStockOnWayDeliveryConfirmVO vo) {
		// 查询运单
		TmsShipbillHeaderModel tmsShipbillHeaderModel = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);

		// 查询派车单明细
		Criteria criteriaDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
		Restriction rDetail = Restrictions.eq("shipbillId", shipbillId);
		criteriaDetail.setRestriction(rDetail);
		List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteriaDetail);
		ToolKits.isParamNull(tmsLoadingBillDetailModels, "查询派车单明细[运单:" + shipbillId + "]为空");

		TmsLoadingBillDetailModel tmsLoadingBillDetailModel = tmsLoadingBillDetailModels.get(0);

		// 获取同一个派车单下的明细
		rDetail = Restrictions.eq("loadingBillId", tmsLoadingBillDetailModel.getLoadingBillId());
		criteriaDetail.setRestriction(rDetail);
		List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModelList = tmsLoadingBillDetailMapper.findByCriteria(criteriaDetail);

		List<TmsLoadingBillDetailModel> DetailModelList = tmsLoadingBillDetailModelList.stream().filter(o -> o.getStatus() == DataDictionaryConstant.SHIPBILL_STATUS_SD).collect(Collectors.toList());

		// 判断是否还有未完成的派车单明细
		if (ToolKits.isEmpty(DetailModelList)) {
			Map<String, Object> mapHeadUpdate = Maps.newHashMap();
			mapHeadUpdate.put("status", DataDictionaryConstant.LOADING_BILL_STATUS_HD_PIC);
			mapHeadUpdate.put("updateperson", user.getNickname());
			mapHeadUpdate.put("updatetime", date);
			// 修改派车单
			tmsLoadingBillHeaderMapper.updateMapById(tmsLoadingBillDetailModel.getLoadingBillId(), mapHeadUpdate, TmsLoadingBillHeaderModel.class);
		}

		// 判断是否为原始单号
		if (ToolKits.isNotEmpty(tmsShipbillHeaderModel.getFatherShipbillId()) && !tmsShipbillHeaderModel.getFatherShipbillId().equals(shipbillId)) {
			// 查找父级单号
			TmsShipbillHeaderModel fatherShipbillHeaderModel = tmsShipbillHeaderMapper.findById(tmsShipbillHeaderModel.getFatherShipbillId(), TmsShipbillHeaderModel.class);

			// 查找同级的所有子单
			Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
			Restriction r1 = Restrictions.eq("fatherShipbillId", tmsShipbillHeaderModel.getFatherShipbillId());
			criteria.setRestriction(r1);
			List<TmsShipbillHeaderModel> tmsShipbillHeaderModelList = tmsShipbillHeaderMapper.findByCriteria(criteria);

			// 查找是否还有未完成的运单
			List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderModelList.stream().filter(o -> o.getStatus() == DataDictionaryConstant.SHIPBILL_STATUS_SD).collect(Collectors.toList());

			// 判断子单是否全部完成
			if (ToolKits.isEmpty(tmsShipbillHeaderModels)) {
				// 修改父级运单状态
				fatherShipbillHeaderModel.setStatus(DataDictionaryConstant.LOADING_BILL_STATUS_HD_PIC);
				fatherShipbillHeaderModel.setTransportStatus(DataDictionaryConstant.TRANSPORT_STATUS_ARRIVED);
				fatherShipbillHeaderModel.setUpdatetime(date);
				fatherShipbillHeaderModel.setUpdateperson(user.getNickname());

				// 更新父级运单运输记录表
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("shipbillId", fatherShipbillHeaderModel.getShipbillId());
				List<TmsShipbillTransLogModel> tmsShipbillTransportLogs = tmsShipbillTransportLogMapper.findByMap(queryMap, TmsShipbillTransLogModel.class);
				if (ToolKits.isNotEmpty(tmsShipbillTransportLogs) && tmsShipbillTransportLogs.size() > 0) {
					TmsShipbillTransLogModel tmsShipbillTransportLog = tmsShipbillTransportLogs.get(0);
					tmsShipbillTransportLog.setUpdateperson(user.getNickname());
					tmsShipbillTransportLog.setUpdatetime(DateUtil.date());
					tmsShipbillTransportLog.setDeliveryTime(vo.getDeliveryDate());
					tmsShipbillTransportLog.setDeliveryStatus(vo.getDeliveryStatus());
					if (ToolKits.isNotEmpty(vo.getSignDate())) {
						tmsShipbillTransportLog.setDeliverySignTime(vo.getSignDate());
					}
					if (ToolKits.isNotEmpty(vo.getMemo())) {
						tmsShipbillTransportLog.setDeliveryMemo(vo.getMemo());
					}
					tmsShipbillTransportLogMapper.update(tmsShipbillTransportLog);
				}

				// 运单节点信息
				ShipbillNodeTrackingSaveNewVO shipbillNodeTrackingSaveNewVO = new ShipbillNodeTrackingSaveNewVO();
				shipbillNodeTrackingSaveNewVO.setNodeDate(date);
				shipbillNodeTrackingSaveNewVO.setShipbillId(fatherShipbillHeaderModel.getShipbillId());
				shipbillNodeTrackingSaveNewVO.setShipbillNo(fatherShipbillHeaderModel.getShipbillId());
				shipbillNodeTrackingSaveNewVO.setUserId(user.getUserid());
				shipbillNodeTrackingSaveNewVO.setUserName(user.getNickname());
				shipbillNodeTrackingSaveNewVO.setEnterpriseId(user.getEnterpriseId());
				shipbillNodeTrackingSaveNewVO.setEnterpriseName(user.getEnterpriseName());

				// 运单日志信息
				ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
				shipbillOperateLogSaveNewVO.setShipbillId(fatherShipbillHeaderModel.getShipbillId());
				shipbillOperateLogSaveNewVO.setShipbillNo(fatherShipbillHeaderModel.getShipbillId());
				shipbillOperateLogSaveNewVO.setOperateDate(date);
				shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
				shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
				shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
				shipbillOperateLogSaveNewVO.setUserName(user.getNickname());

				// 判断第一个子单是否有签到时间
				if (deliverySignTime) {
					// 运单节点保存
					shipbillNodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
					shipbillNodeTrackingSaveNewVO.setNodeEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_SDQD);
					shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveNewVO);

					// 运单日志保存
					shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_SDQD);
					shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
					shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
					shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);
				}

				// 运单节点保存
				shipbillNodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
				shipbillNodeTrackingSaveNewVO.setNodeEvent(nodeEvent);
				shipbillNodeTrackingSaveNewVO.setPicUrl(goodsPhoto);
				shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveNewVO);

				// 运单日志保存
				shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_SD);
				shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_QY);
				shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
				shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);

				// 回单日志
				if (ToolKits.isNotEmpty(receiptPhoto)) {
					// 运单节点保存
					shipbillNodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_HD_PIC);
					shipbillNodeTrackingSaveNewVO.setNodeEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_HD_PIC);
					shipbillNodeTrackingSaveNewVO.setPicUrl(receiptPhoto);
					shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveNewVO);

					// 运单日志保存
					shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_HD_PIC);
					shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
					shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_HD_PIC);
					shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);

					fatherShipbillHeaderModel.setStatus(DataDictionaryConstant.LOADING_BILL_STATUS_HD_ZB);
				}

				tmsShipbillHeaderMapper.update(fatherShipbillHeaderModel);
				return fatherShipbillHeaderModel.getShipbillId();
			}
		}
		return null;
	}

	/**
	 * 运单客服反馈
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void shipbillFeedback(OutStockOnWayFeedbackVO vo) throws Exception {

	}

	/**
	 * 运单异常登记
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void shipbillAbnormalRegister(OutStockOnWayAbnormalRegisterVO vo) throws Exception {

	}

	/**
	 * 派车单送达确认
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void loadingDeliveryConfirm(DeliveryConfirmVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 节点内容
		StringBuffer nodeEvent = new StringBuffer();
		nodeEvent.append("送达确认 送达签收人:");
		nodeEvent.append(ToolKits.isEmpty(vo.getSignPerson()) ? "" : vo.getSignPerson());
		nodeEvent.append(" 送达备注:");
		nodeEvent.append(ToolKits.isEmpty(vo.getMemo()) ? "" : vo.getMemo());

		for (String loadingBillId : vo.getLoadingBillids()) {
			TmsLoadingBillHeaderModel tmsLoadingBillHeader = tmsLoadingBillHeaderMapper.findById(loadingBillId, TmsLoadingBillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsLoadingBillHeader)) {
				// 更新派车单状态为【送达】
				tmsLoadingBillHeader.setStatus(DataDictionaryConstant.LOADING_BILL_STATUS_SD);
				tmsLoadingBillHeader.setUpdateperson(user.getNickname());
				tmsLoadingBillHeader.setUpdatetime(DateUtil.date());
				tmsLoadingBillHeaderMapper.update(tmsLoadingBillHeader);

				// 查询派车单明细
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("loadingBillId", tmsLoadingBillHeader.getLoadingBillId());
				List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByMap(queryMap, TmsLoadingBillDetailModel.class);
				for (TmsLoadingBillDetailModel tmsLoadingBillDetail : tmsLoadingBillDetails) {
					// 更新派车单运单运输状态为【已送达】
					TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(tmsLoadingBillDetail.getShipbillId(), TmsShipbillHeaderModel.class);
					if (ToolKits.isNotEmpty(tmsShipbillHeader)) {
						tmsShipbillHeader.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_SD);
						tmsShipbillHeader.setUpdateperson(user.getNickname());
						tmsShipbillHeader.setUpdatetime(DateUtil.date());
						tmsShipbillHeaderMapper.update(tmsShipbillHeader);

						// 运单日志信息
						ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
						shipbillOperateLogSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
						shipbillOperateLogSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
						shipbillOperateLogSaveNewVO.setOperateDate(DateUtil.date());
						shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
						shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
						shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
						shipbillOperateLogSaveNewVO.setUserName(user.getNickname());

						ShipbillNodeTrackingSaveNewVO nodeTrackingSaveNewVO = new ShipbillNodeTrackingSaveNewVO();
						nodeTrackingSaveNewVO.setNodeDate(DateUtil.date());
						nodeTrackingSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
						nodeTrackingSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
						nodeTrackingSaveNewVO.setUserId(user.getUserid());
						nodeTrackingSaveNewVO.setUserName(user.getNickname());
						nodeTrackingSaveNewVO.setEnterpriseId(user.getEnterpriseId());
						nodeTrackingSaveNewVO.setEnterpriseName(user.getEnterpriseName());
						nodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.TRANSPORT_STATUS_ARRIVED);
						nodeTrackingSaveNewVO.setNodeEvent(nodeEvent.toString());
						shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingSaveNewVO);
					}
				}
			}
			MessageUtil.addMessage(new SendMessageDTO(MessageOrderTypeEnum.LOADING_NO.getCode(), MessageTypeEnum.DELIVERY.getCode(), Lists.newArrayList(loadingBillId)));
		}
	}

	/**
	 * 派车单客服反馈
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void loadingFeedback(WholeCarFeedbackVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 节点内容
		StringBuffer nodeEvent = new StringBuffer(DataDictionaryConstant.SHIPBILL_NODE_FEEDBACK);
		nodeEvent.append("客服反馈,反馈内容:");
		nodeEvent.append(vo.getFeedbackContent());
		nodeEvent.append(",实时地点:");
		nodeEvent.append(vo.getAddress());

		for (String loadingBillId : vo.getLoadingBillids()) {
			TmsLoadingBillHeaderModel tmsLoadingBillHeader = tmsLoadingBillHeaderMapper.findById(loadingBillId, TmsLoadingBillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsLoadingBillHeader)) {
				// 查询派车单明细
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("loadingBillId", tmsLoadingBillHeader.getLoadingBillId());
				List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByMap(queryMap, TmsLoadingBillDetailModel.class);
				for (TmsLoadingBillDetailModel tmsLoadingBillDetail : tmsLoadingBillDetails) {
					TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(tmsLoadingBillDetail.getShipbillId(), TmsShipbillHeaderModel.class);
					if (ToolKits.isNotEmpty(tmsShipbillHeader)) {
						// 运单日志信息
						ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
						shipbillOperateLogSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
						shipbillOperateLogSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
						shipbillOperateLogSaveNewVO.setOperateDate(DateUtil.date());
						shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
						shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
						shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
						shipbillOperateLogSaveNewVO.setUserName(user.getNickname());

						ShipbillNodeTrackingSaveNewVO nodeTrackingSaveNewVO = new ShipbillNodeTrackingSaveNewVO();
						nodeTrackingSaveNewVO.setNodeDate(DateUtil.date());
						nodeTrackingSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
						nodeTrackingSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
						nodeTrackingSaveNewVO.setUserId(user.getUserid());
						nodeTrackingSaveNewVO.setUserName(user.getNickname());
						nodeTrackingSaveNewVO.setEnterpriseId(user.getEnterpriseId());
						nodeTrackingSaveNewVO.setEnterpriseName(user.getEnterpriseName());
						nodeTrackingSaveNewVO.setStatus(DataDictionaryConstant.TRANSPORT_STATUS_ARRIVED);
						nodeTrackingSaveNewVO.setNodeEvent(nodeEvent.toString());
						shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingSaveNewVO);
					}
				}
			}
		}
	}

	/**
	 * 派车单异常登记
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void loadingAbnormalRegister(WholeCarAbnormalRegisterVO vo) throws Exception {
		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.ABNORMAL_TYPE, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("已派车状态翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}

		// 异常事件
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ABNORMAL_EVENT, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("已派车状态翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		// 节点内容
		StringBuffer nodeEvent = new StringBuffer(DataDictionaryConstant.SHIPBILL_NODE_ABNORMAL_REGISTER);
		nodeEvent.append("异常类型为:");
		nodeEvent.append(PublicMethods.change(data1, String.valueOf(vo.getAbnormalType())));
		nodeEvent.append(",异常事件:");
		nodeEvent.append(PublicMethods.change(data2, String.valueOf(vo.getAbnormalCase())));

		for (String loadingBillId : vo.getLoadingBillids()) {
			TmsLoadingBillHeaderModel tmsLoadingBillHeader = tmsLoadingBillHeaderMapper.findById(loadingBillId, TmsLoadingBillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsLoadingBillHeader)) {
				// 查询派车单明细
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("loadingBillId", tmsLoadingBillHeader.getLoadingBillId());
				List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByMap(queryMap, TmsLoadingBillDetailModel.class);
				for (TmsLoadingBillDetailModel tmsLoadingBillDetail : tmsLoadingBillDetails) {
					// 更新派车单运单异常状态为【异常】
					TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(tmsLoadingBillDetail.getShipbillId(), TmsShipbillHeaderModel.class);
					if (ToolKits.isNotEmpty(tmsShipbillHeader)) {
						tmsShipbillHeader.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS);
						tmsShipbillHeader.setUpdateperson(user.getNickname());
						tmsShipbillHeader.setUpdatetime(DateUtil.date());
						tmsShipbillHeaderMapper.update(tmsShipbillHeader);

						// 运单日志信息
						ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
						shipbillOperateLogSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
						shipbillOperateLogSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
						shipbillOperateLogSaveNewVO.setOperateDate(DateUtil.date());
						shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
						shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
						shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
						shipbillOperateLogSaveNewVO.setUserName(user.getNickname());

						ShipbillNodeTrackingSaveNewVO nodeTrackingSaveNewVO = new ShipbillNodeTrackingSaveNewVO();
						nodeTrackingSaveNewVO.setNodeDate(DateUtil.date());
						nodeTrackingSaveNewVO.setShipbillId(tmsShipbillHeader.getShipbillId());
						nodeTrackingSaveNewVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
						nodeTrackingSaveNewVO.setUserId(user.getUserid());
						nodeTrackingSaveNewVO.setUserName(user.getNickname());
						nodeTrackingSaveNewVO.setEnterpriseId(user.getEnterpriseId());
						nodeTrackingSaveNewVO.setEnterpriseName(user.getEnterpriseName());
						nodeTrackingSaveNewVO.setNodeEvent(DataDictionaryConstant.SHIPBILL_NODE_ABNORMAL_REGISTER);
						nodeTrackingSaveNewVO.setNodeEvent(nodeEvent.toString());
						shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingSaveNewVO);
					}
				}
			}
		}
	}

	/**
	 * 配载运单查询
	 *
	 * @param
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<WholeCarDeliveryShibillDTO> queryShipbill(Integer pageNum, Integer pageSize, String loadingBillId) throws Exception {
		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 PrologException("订单类型翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 订单业务类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ORDER_BUSINESS_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new PrologException("订单业务类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 配送方式
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DELIVERY_METHOD_TYPE, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new PrologException("配送方式翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}
		// 运输类型(运输模式)
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TRANSPORT_TYPE, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new PrologException("运输类型(运输模式)翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}
		// 温度要求
		RestMessage<List<Map<String, Object>>> restMessage5 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TEMPERATURE_REQUIREMENT, null, null, null);
		if (!restMessage5.isSuccess()) {
			throw new PrologException("温度要求翻译失败!");
		} else {
			data5 = restMessage5.getData();
		}
		// 发货方类型
		RestMessage<List<Map<String, Object>>> restMessage6 = maindataTmsResource.getDropDownById(DataDictionaryConstant.SHIPPER_TYPE, null, null, null);
		if (!restMessage6.isSuccess()) {
			throw new PrologException("发货方类型翻译失败!");
		} else {
			data6 = restMessage6.getData();
		}
		// 收货方类型
		RestMessage<List<Map<String, Object>>> restMessage7 = maindataTmsResource.getDropDownById(DataDictionaryConstant.CONSIGNEE_TYPE, null, null, null);
		if (!restMessage7.isSuccess()) {
			throw new PrologException("收货方类型翻译失败!");
		} else {
			data7 = restMessage7.getData();
		}

		// 封装运单明细查询
		Criteria criteria = Criteria.forClass(TmsLoadingBillDetailModel.class);
		Restriction r1 = Restrictions.eq("loadingBillId", loadingBillId);
		criteria.setRestriction(r1);
		criteria.setOrder(Order.newInstance().asc(new String[] { "shippingOrder" }));
		PageUtils.startPage(pageNum, pageSize);
		List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteria);

		// 返回数据集合
		List<WholeCarDeliveryShibillDTO> dtos = Lists.newArrayList();

		for (TmsLoadingBillDetailModel model : tmsLoadingBillDetailModels) {
			WholeCarDeliveryShibillDTO dto = new WholeCarDeliveryShibillDTO();
			BeanUtils.copyProperties(model, dto);
			// 收货方位置(经纬度判断)
			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())));
			dtos.add(dto);
		}

		Page<WholeCarDeliveryShibillDTO> page = PageUtils.getPage(dtos);
		if (tmsLoadingBillDetailModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsLoadingBillDetailModel> p0 = (com.github.pagehelper.Page<TmsLoadingBillDetailModel>) tmsLoadingBillDetailModels;
			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
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<WholeCarDeliveryItemDTO> queryItem(WholeCarDeliveryItemQueryVO vo) throws Exception {
		Map<String, Object> queryMap = Maps.newHashMap();
		queryMap.put("loadingBillId", vo.getLoadingBillId());
		List<TmsLoadingBillDetailModel> list = tmsLoadingBillDetailMapper.findByMap(queryMap, TmsLoadingBillDetailModel.class);
		List<String> shipbillIds = Lists.newArrayList();
		for (TmsLoadingBillDetailModel tmsLoadingBillDetail : list) {
			shipbillIds.add(tmsLoadingBillDetail.getShipbillId());
		}

		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.in("loadingBillNo", shipbillIds.toArray());
		criteria.setRestriction(Restrictions.and(r1));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsShipbillDetailModel> tmsShipbillDetails = tmsShipbillDetailMapper.findByCriteria(criteria);

		List<WholeCarDeliveryItemDTO> dtos = Lists.newArrayList();
		for (TmsShipbillDetailModel tmsShipbillDetail : tmsShipbillDetails) {
			WholeCarDeliveryItemDTO dto = new WholeCarDeliveryItemDTO();
			BeanUtils.copyProperties(tmsShipbillDetail, dto);
			dtos.add(dto);
		}

		Page<WholeCarDeliveryItemDTO> page = PageUtils.getPage(dtos);
		if (tmsShipbillDetails instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillDetailModel> p0 = (com.github.pagehelper.Page<TmsShipbillDetailModel>) tmsShipbillDetails;
			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
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<WholeCarDeliveryPackDTO> queryPack(WholeCarDeliveryPackQueryVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY) && !user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			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();
		}

		Map<String, Object> queryMap = Maps.newHashMap();
		queryMap.put("loadingBillId", vo.getLoadingBillId());
		List<TmsLoadingBillDetailModel> list = tmsLoadingBillDetailMapper.findByMap(queryMap, TmsLoadingBillDetailModel.class);
		List<String> shipbillIds = Lists.newArrayList();
		for (TmsLoadingBillDetailModel tmsLoadingBillDetail : list) {
			shipbillIds.add(tmsLoadingBillDetail.getShipbillId());
		}

		Criteria criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		Restriction r1 = Restrictions.in("shipbillId", shipbillIds.toArray());
		criteria.setRestriction(r1);

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

		List<WholeCarDeliveryPackDTO> dtos = Lists.newArrayList();
		for (TmsShipbillPackageModel tmsShipbillPackageModel : tmsShipbillPackageModels) {
			WholeCarDeliveryPackDTO dto = new WholeCarDeliveryPackDTO();
			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());
			}
			dtos.add(dto);
		}

		Page<WholeCarDeliveryPackDTO> page = PageUtils.getPage(dtos);
		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;
	}

}
