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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.DateUtils;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.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.SeriaNnumberConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.*;
import com.prolog.rdc.tms.model.dto.*;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPopDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseBranchDTO;
import com.prolog.rdc.tms.model.dto.message.SendMessageDTO;
import com.prolog.rdc.tms.model.entity.*;
import com.prolog.rdc.tms.model.enums.MessageOrderTypeEnum;
import com.prolog.rdc.tms.model.enums.MessageTypeEnum;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPopVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.BookingService;
import com.prolog.rdc.tms.service.ShipbillOperateLogService;
import com.prolog.rdc.tms.util.BusinessFlowUtil;
import com.prolog.rdc.tms.util.DataUtils;
import com.prolog.rdc.tms.util.MessageUtil;
import com.prolog.rdc.tms.util.PublicMethods;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 园区管理-预约管理实现层
 * <p>
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author zhangsj
 * @since 2022/7/5 10:30
 */
@Service
public class BookingServiceImpl implements BookingService {

	@Autowired
	private TmsAppointmentHeaderMapper tmsAppointmentHeaderMapper;

	@Autowired
	private TmsAppointmentDetailMapper tmsAppointmentDetailMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private TmsShipbillPackageMapper tmsShipbillPackageMapper;

	@Autowired
	private ShipbillOperateLogService shipbillOperateLogService;

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Autowired
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Autowired
	private TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	@Autowired
	private BusinessFlowUtil businessFlowUtil;
	/**
	 * 主界面分页查询
	 *
	 * @param bookingQueryVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<BookingQueryDTO> queryPageData(BookingQueryVO bookingQueryVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

		List<Map<String, Object>> data1 = Lists.newArrayList();
		List<Map<String, Object>> data2 = Lists.newArrayList();
		List<Map<String, Object>> data3 = Lists.newArrayList();

		// 预约状态
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_STATUS, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("预约状态翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 预约类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("预约类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		// 理货状态
		RestMessage<List<Map<String, Object>>> restMessage3 = maindataTmsResource.getDropDownById(DataDictionaryConstant.TALLY_STATUS, null, null, null);
		if (!restMessage3.isSuccess()) {
			throw new RuntimeException("理货状态翻译失败!");
		} else {
			data3 = restMessage3.getData();
		}

		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		Restriction r1 = ToolKits.isEmpty(bookingQueryVO.getAppointmentNo()) ? null : Restrictions.like("reserveNumber", "%" + bookingQueryVO.getAppointmentNo() + "%");
		Restriction r2 = ToolKits.isEmpty(bookingQueryVO.getBranchName()) ? null : Restrictions.like("branchName", "%" + bookingQueryVO.getBranchName() + "%");
		Restriction r3 = ToolKits.isEmpty(bookingQueryVO.getDriverName()) ? null : Restrictions.like("driverName", "%" + bookingQueryVO.getDriverName() + "%");
		Restriction r4 = ToolKits.isEmpty(bookingQueryVO.getDriverPhoneNumber()) ? null : Restrictions.like("driverPhoneNumber", "%" + bookingQueryVO.getDriverPhoneNumber() + "%");
		Restriction r5 = ToolKits.isEmpty(bookingQueryVO.getVehiclePlateNumber()) ? null : Restrictions.like("vehiclePlateNumber", "%" + bookingQueryVO.getVehiclePlateNumber() + "%");
		Restriction r6 = ToolKits.isEmpty(bookingQueryVO.getAppointmentType()) ? null : Restrictions.eq("appointmentType", bookingQueryVO.getAppointmentType());
		Restriction r7 = Restrictions.in("branchId", bookingQueryVO.getBranchId().split(","));
		Restriction r8 = Restrictions.ne("status", DataDictionaryConstant.APPOINTMENT_STATUS_QX);
		Restriction r9 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
		Restriction r10 = Restrictions.eq(TmsAppointmentHeaderModel::getStatus, bookingQueryVO.getStatus());
		Restriction r11 = ToolKits.isEmpty(bookingQueryVO.getStartDate()) ? null : Restrictions.ge(TmsAppointmentHeaderModel::getAppointmentDate, bookingQueryVO.getStartDate());
		Restriction r12 = ToolKits.isEmpty(bookingQueryVO.getEndDate()) ? null : Restrictions.le(TmsAppointmentHeaderModel::getAppointmentDate, bookingQueryVO.getEndDate());
		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end

		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12));
		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

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

		PageUtils.startPage(bookingQueryVO.getPageNum(), bookingQueryVO.getPageSize());
		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByCriteria(criteria);

		for (TmsAppointmentHeaderModel tmsAppointmentHeaderModel : tmsAppointmentHeaderModels) {
			BookingQueryDTO dto = new BookingQueryDTO();
			BeanUtils.copyProperties(tmsAppointmentHeaderModel, dto);

			// 数据翻译
			dto.setStatusName(PublicMethods.change(data1, String.valueOf(dto.getStatus())));
			dto.setAppointmentTypeName(PublicMethods.change(data2, String.valueOf(dto.getAppointmentType())));
			dto.setTallyStatusName(PublicMethods.change(data3, String.valueOf(dto.getTallyStatus())));

			dto.setReserveNumberTime(DateUtil.format(dto.getAppointmentDate(), "yyyy-MM-dd") + " " + dto.getAppointmentTime());
			// 封装数据集
			Map<String, Object> querymap = Maps.newHashMap();
			querymap.put("appointmentId", tmsAppointmentHeaderModel.getAppointmentId());
			List<TmsAppointmentDetailModel> tmsAppointmentDetails = tmsAppointmentDetailMapper.findByMap(querymap, TmsAppointmentDetailModel.class);
			// 设置预约单的派车单
			dto.setLoadingBillIds(DataUtils.getLoadingBillIds(tmsAppointmentDetails));
			// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
			if (ToolKits.isNotEmpty(bookingQueryVO.getShipbillNo())) {
				// 查询预约单明细
				for (TmsAppointmentDetailModel tmsAppointmentDetailModel : tmsAppointmentDetails) {
					if (ObjectUtil.contains(tmsAppointmentDetailModel.getShipbillNo(), bookingQueryVO.getShipbillNo())) {
						bookingQueryDTOS.add(dto);
						break;
					}
				}
			} else {
				bookingQueryDTOS.add(dto);
			}
			// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end
		}

		// 分页重组
		Page<BookingQueryDTO> page = PageUtils.getPage(bookingQueryDTOS);
		if (tmsAppointmentHeaderModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsAppointmentHeaderModel> p0 = (com.github.pagehelper.Page<TmsAppointmentHeaderModel>) tmsAppointmentHeaderModels;
			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 bookingAdvancedQueryVO
	 * @return
	 */
	@Override
	public Page<BookingQueryDTO> advancedQueryPageData(BookingAdvancedQueryVO bookingAdvancedQueryVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 预约状态
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_STATUS, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("预约状态翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 预约类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("预约类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

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

		// 返回数据集
		PageUtils.startPage(bookingAdvancedQueryVO.getPageNum(), bookingAdvancedQueryVO.getPageSize());
		List<BookingQueryDTO> bookingQueryDTOS = tmsAppointmentHeaderMapper.bookingAdvancedQuery(bookingAdvancedQueryVO);

		for (BookingQueryDTO dto : bookingQueryDTOS) {
			// 数据翻译
			dto.setStatusName(PublicMethods.change(data1, String.valueOf(dto.getStatus())));
			dto.setAppointmentTypeName(PublicMethods.change(data2, String.valueOf(dto.getAppointmentType())));
		}
		return PageUtils.getPage(bookingQueryDTOS);
	}

	/**
	 * 配载运单查询
	 *
	 * @param bookingGetShipbillVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<BookingGetShipbillDTO> getShipbillData(BookingGetShipbillVO bookingGetShipbillVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}
		// 数据字典翻译数据集合
		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();

		// 订单类型
		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();
		}

		// 封装运单明细
		Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.eq("appointmentId", bookingGetShipbillVO.getAppointmentId());
		criteria.setRestriction(r1);
		criteria.setOrder(Order.newInstance().asc(new String[] { "orderDate" }));

		PageUtils.startPage(bookingGetShipbillVO.getPageNum(), bookingGetShipbillVO.getPageSize());
		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);

		// 明细集合
		List<BookingGetShipbillDTO> list = Lists.newArrayList();

		for (TmsAppointmentDetailModel model : tmsAppointmentDetailModels) {

			BookingGetShipbillDTO bookingGetShipbillDTO = new BookingGetShipbillDTO();
			BeanUtils.copyProperties(model, bookingGetShipbillDTO);

			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				bookingGetShipbillDTO.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				bookingGetShipbillDTO.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				bookingGetShipbillDTO.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				bookingGetShipbillDTO.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

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

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

			// 获取背景颜色和字体颜色
			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(bookingGetShipbillDTO.getOrderTypeName()) ? bookingGetShipbillDTO.getOrderTypeName() : "")) {
							bookingGetShipbillDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingGetShipbillDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingGetShipbillDTO.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(bookingGetShipbillDTO.getOrderBusinessTypeName()) ? bookingGetShipbillDTO.getOrderBusinessTypeName() : "")) {
							bookingGetShipbillDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingGetShipbillDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingGetShipbillDTO.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(bookingGetShipbillDTO.getTemperatureRequirementName()) ? bookingGetShipbillDTO.getTemperatureRequirementName() : "")) {
							bookingGetShipbillDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingGetShipbillDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingGetShipbillDTO.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(bookingGetShipbillDTO.getDeliveryMethodTypeName()) ? bookingGetShipbillDTO.getDeliveryMethodTypeName() : "")) {
							bookingGetShipbillDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingGetShipbillDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingGetShipbillDTO.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(bookingGetShipbillDTO.getMemo()) ? bookingGetShipbillDTO.getMemo() : "")) {
							bookingGetShipbillDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingGetShipbillDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingGetShipbillDTO.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				}
			}

			list.add(bookingGetShipbillDTO);
		}
		// 分页重组
		Page<BookingGetShipbillDTO> page = PageUtils.getPage(list);
		if (tmsAppointmentDetailModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsAppointmentDetailModel> p0 = (com.github.pagehelper.Page<TmsAppointmentDetailModel>) tmsAppointmentDetailModels;
			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 bookingItemDetailVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<BookingItemDetailDTO> itemDetailData(BookingItemDetailVO bookingItemDetailVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

		// 数据字典翻译数据集合
		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();
		}

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

		// 查询预约单明细
		Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.eq("appointmentId", bookingItemDetailVO.getAppointmentId());
		criteria.setRestriction(r1);

		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);

		// 根据运单ID查询明细数据
		criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		r1 = Restrictions.in("shipbillId", tmsAppointmentDetailModels.stream().map(TmsAppointmentDetailModel::getShipbillId).distinct().collect(Collectors.toList()).toArray());

		criteria.setRestriction(r1);

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

		for (TmsShipbillDetailModel tmsShipbillDetailModel : tmsShipbillDetailModels) {
			BookingItemDetailDTO dto = new BookingItemDetailDTO();
			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<BookingItemDetailDTO> 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 bookingPackageDetailVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<BookingPackageDetailDTO> packageDetailData(BookingPackageDetailVO bookingPackageDetailVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

		// 数据字典翻译数据集合
		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();
		}

		// 查询预约单明细
		Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.eq("appointmentId", bookingPackageDetailVO.getAppointmentId());
		criteria.setRestriction(r1);

		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);

		// 根据运单ID查询明细数据
		criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		r1 = Restrictions.in("shipbillId", tmsAppointmentDetailModels.stream().map(TmsAppointmentDetailModel::getShipbillId).distinct().collect(Collectors.toList()).toArray());

		criteria.setRestriction(r1);

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

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

		for (TmsShipbillPackageModel tmsShipbillPackageModel : tmsShipbillPackageModels) {
			BookingPackageDetailDTO dto = new BookingPackageDetailDTO();
			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<BookingPackageDetailDTO> 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 appointmentIds
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void reviewData(List<String> appointmentIds) {
		ToolKits.isParamNull(appointmentIds, "预约单ID不能为空");

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

		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 查询预约单
		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		Restriction r1 = Restrictions.in("appointmentId", appointmentIds.toArray());
		criteria.setRestriction(r1);

		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByCriteria(criteria);
		ToolKits.isParamNull(tmsAppointmentHeaderModels, "未找到对应预约单!");

		// 获取是否有不为待审核的预约单
		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModelList = tmsAppointmentHeaderModels.stream().filter(o -> !DataDictionaryConstant.APPOINTMENT_STATUS_DSH.equals(o.getStatus())).collect(Collectors.toList());

		if (ToolKits.isNotEmpty(tmsAppointmentHeaderModelList)) {
			throw new PrologException("请勾选待审核的预约单审核!");
		}

		for (String appointmentId : appointmentIds) {
			// 预约单对象
			TmsAppointmentHeaderModel tmsAppointmentHeaderModel = tmsAppointmentHeaderModels.stream().filter(item -> item.getAppointmentId().equals(appointmentId)).findAny().orElse(null);

			// 查询预约单明细
			Criteria criteriaAppointment = Criteria.forClass(TmsAppointmentDetailModel.class);
			Restriction appointment = Restrictions.eq(TmsAppointmentDetailModel::getAppointmentId, appointmentId);
			criteriaAppointment.setRestriction(appointment);
			List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteriaAppointment);
			boolean tallyStatus = false;
			boolean hasTakeNumAndCallNum = false;
			boolean hasLoading = false;
			Criteria criteriaLoadingBillDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
			for (TmsAppointmentDetailModel detailModel : tmsAppointmentDetailModels) {
				if (ToolKits.isNotEmpty(detailModel.getFlowConfig())
						&& detailModel.getFlowConfig().contains(DataDictionaryConstant.FLOW_CONFIG_APPOINTMENT_TN_CN)) {
					hasTakeNumAndCallNum = true;
				}
				int nextStatus = DataDictionaryConstant.SHIPBILL_STATUS_APP_ING;
				if (ToolKits.isNotEmpty(detailModel.getFlowConfig()) && detailModel.getFlowConfig().contains(DataDictionaryConstant.FLOW_CONFIG_ARRANGE_GOODS)) {
					tallyStatus = true;
					nextStatus = DataDictionaryConstant.SHIPBILL_STATUS_LH_START;
				}
				if (ToolKits.isNotEmpty(detailModel.getFlowConfig()) && detailModel.getFlowConfig().contains(DataDictionaryConstant.FLOW_CONFIG_LOAD_UNLOAD)) {
					hasLoading = true;
				}
				int flowStatus = businessFlowUtil.judgmentNextFlowSts(detailModel.getFlowConfig(), DataDictionaryConstant.FLOW_CONFIG_APPOINTMENT);
				if (hasTakeNumAndCallNum &&
						NumberUtil.isGreater(BigDecimal.valueOf(flowStatus),BigDecimal.valueOf(DataDictionaryConstant.SHIPBILL_STATUS_ZX_START))) {
					nextStatus = flowStatus;
				} else if (!hasTakeNumAndCallNum ) {
					nextStatus = flowStatus;
				}

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

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

				// 判断预计提货时间是否为空
				if (ToolKits.isEmpty(detailModel.getEstimatedPickupStartTime())) {
					// 预约日期
					Date appointmentDate = tmsAppointmentHeaderModel.getAppointmentDate();
					// 预约时间段
					String appointmentTime = tmsAppointmentHeaderModel.getAppointmentTime();

					// 更新预约明细信息
					mapAppointmentDetail.put("estimatedPickupStartTime", DateUtil.parseDateTime(DateUtil.formatDate(appointmentDate) + " " + appointmentTime.split("-")[0] + ":00"));
					mapAppointmentDetail.put("estimatedPickupEndTime", DateUtil.parseDateTime(DateUtil.formatDate(appointmentDate) + " " + appointmentTime.split("-")[1] + ":00"));

					// 更新运单信息
					mapShipbillHead.put("estimatedPickupStartTime", DateUtil.parseDateTime(DateUtil.formatDate(appointmentDate) + " " + appointmentTime.split("-")[0] + ":00"));
					mapShipbillHead.put("estimatedPickupEndTime", DateUtil.parseDateTime(DateUtil.formatDate(appointmentDate) + " " + appointmentTime.split("-")[1] + ":00"));
				}
				mapAppointmentDetail.put("status", nextStatus);
				tmsAppointmentDetailMapper.updateMapById(detailModel.getId(), mapAppointmentDetail, TmsAppointmentDetailModel.class);

				mapShipbillHead.put("status", nextStatus);
				tmsShipbillHeaderMapper.updateMapById(detailModel.getShipbillId(), mapShipbillHead, TmsShipbillHeaderModel.class);

				// 更新装车单明细
				Restriction r3 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, detailModel.getShipbillId());
				Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
				criteriaLoadingBillDetail.setRestriction(Restrictions.and(r3, r4));
				criteriaLoadingBillDetail.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
				List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingBillDetail);

				if (ToolKits.isEmpty(tmsLoadingBillDetails)) {
					throw new PrologException("运单【" + detailModel.getShipbillId() + "】查询失败");
				}
				TmsLoadingBillDetailModel billDetailModel = tmsLoadingBillDetails.get(0);
				billDetailModel.setStatus(nextStatus);
				billDetailModel.setUpdateperson(user.getNickname());
				billDetailModel.setUpdatetime(date);
				tmsLoadingBillDetailMapper.update(billDetailModel);

				// 日志表
				ShipbillOperateLogSaveVO shipbillOperateLogSaveVO = new ShipbillOperateLogSaveVO();
				shipbillOperateLogSaveVO.setShipbillId(detailModel.getShipbillId());
				shipbillOperateLogSaveVO.setShipbillNo(detailModel.getShipbillNo());
				shipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YYSH);
				shipbillOperateLogSaveVO.setOldStatus(detailModel.getStatus());
				shipbillOperateLogSaveVO.setNewStatus(nextStatus);
				shipbillOperateLogService.addShipbillOperateLogData(shipbillOperateLogSaveVO);
			}
			Map<String, Object> map = Maps.newHashMap();
			if (!hasTakeNumAndCallNum) {
				if (tallyStatus) {
					map.put("status", DataDictionaryConstant.APPOINTMENT_STATUS_DLH);
				} else if (hasLoading) {
					map.put("status", DataDictionaryConstant.APPOINTMENT_STATUS_DZX);
				} else {
					map.put("status", DataDictionaryConstant.APPOINTMENT_STATUS_ZXWC);
				}
			} else {
				if (!hasLoading && !tallyStatus) {
					map.put("status", DataDictionaryConstant.APPOINTMENT_STATUS_ZXWC);
				} else {
					map.put("status", DataDictionaryConstant.APPOINTMENT_STATUS_YSH);
				}
			}
			map.put("updatetime", date);
			map.put("updateperson", user.getNickname());
			if (tallyStatus) {
				map.put("tallyStatus", DataDictionaryConstant.TALLY_STATUS_WLH);
			} else {
				map.put("tallyStatus", DataDictionaryConstant.TALLY_STATUS_LHWC);
			}
			// 修改预约单
			tmsAppointmentHeaderMapper.updateMapById(appointmentId, map, TmsAppointmentHeaderModel.class);
			MessageUtil.addMessage(new SendMessageDTO(MessageOrderTypeEnum.APPOINTMENT_NO.getCode(), MessageTypeEnum.APPOINTMENT_AUDIT.getCode(), Lists.newArrayList(appointmentId)));
		}
	}

	/**
	 * 预约单取消
	 *
	 * @param appointmentIds
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void cancelData(List<String> appointmentIds) {
		ToolKits.isParamNull(appointmentIds, "预约单ID不能为空");

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

		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 查询预约单
		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		Restriction r1 = Restrictions.in("appointmentId", appointmentIds.toArray());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByCriteria(criteria);
		ToolKits.isParamNull(tmsAppointmentHeaderModels, "未找到对应预约单!");

		for (TmsAppointmentHeaderModel tmsAppointmentHeader : tmsAppointmentHeaderModels) {
			TmsBaseBranchDTO data = null;
			Integer notCancelDura = null;
			RestMessage<TmsBaseBranchDTO> msg = maindataTmsResource.getOneBranch(tmsAppointmentHeader.getBranchId());
			if (!msg.isSuccess()) {
				throw new PrologException("仓库资料查询失败");
			} else {
				data = msg.getData();
				notCancelDura = data.getAptCanNotCancelDura();// 不可取消的时间范围(小时)
				if (ToolKits.isEmpty(notCancelDura)) {
					continue;
				} else {
					SimpleDateFormat sdf = new SimpleDateFormat();
					sdf.applyPattern("yyyy-MM-dd HH:mm");
					String now = sdf.format(new Date());

					SimpleDateFormat sdf2 = new SimpleDateFormat();
					sdf2.applyPattern("yyyy-MM-dd");
					String now2 = sdf2.format(new Date());

					/*
					 * if(ObjectUtil.notEqual(sdf2.format(tmsAppointmentHeader.getAppointmentDate())
					 * ,now2)){ continue; }
					 */

					// 判断当前时间是否在不可取消范围内
					String appointmentTime = tmsAppointmentHeader.getAppointmentTime();
					String startHourStr = appointmentTime.split("-")[0].split(":")[0];
					String startMinStr = appointmentTime.split("-")[0].split(":")[1];

					Integer startHour = Integer.parseInt(startHourStr);
					Integer notCancelHour = startHour - notCancelDura;

					Boolean b = false;
					if (notCancelHour < 0) {
						notCancelHour = 24 + notCancelHour;
						b = true;
					}

					String notCancelTime = null;
					if (b) {// 年月日减去一天
						notCancelTime = sdf2.format(new Date(new Date().getTime() - 24 * 60 * 60 * 1000)) + " " + (notCancelHour < 10 ? "0" + String.valueOf(notCancelHour) : String.valueOf(notCancelHour)) + ":" + startMinStr;
					} else {
						notCancelTime = now2 + " " + (notCancelHour < 10 ? "0" + String.valueOf(notCancelHour) : String.valueOf(notCancelHour)) + ":" + startMinStr;
					}

					int compareTo = now.compareTo(notCancelTime);
					if (compareTo > 0) {
						throw new PrologException("预约单【" + tmsAppointmentHeader.getAppointmentNo() + "】不可取消,当前时间已进入该预约单的不可取消时间段范围(" + notCancelTime + "以后)");
					}
				}
			}
		}

		// 获取是否有不为待审核的预约单
		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModelList = tmsAppointmentHeaderModels.stream().filter(o -> !DataDictionaryConstant.APPOINTMENT_STATUS_DSH.equals(o.getStatus())).collect(Collectors.toList());
		if (ToolKits.isNotEmpty(tmsAppointmentHeaderModelList)) {
			throw new PrologException("请勾选待审核的预约单");
		}

		// 查询预约明细
		Criteria appointmentDetail = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r11 = Restrictions.in("appointmentId", appointmentIds.toArray());
		Restriction r12 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		appointmentDetail.setRestriction(Restrictions.and(r11, r12));
		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(appointmentDetail);
		if (ToolKits.isNotEmpty(tmsAppointmentDetailModels)) {
			// 获取运单id
			List<String> shipbillIds = tmsAppointmentDetailModels.stream().filter(o -> ToolKits.isNotEmpty(o.getShipbillId())).map(TmsAppointmentDetailModel::getShipbillId).collect(Collectors.toList());

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

			// 修改运单状态
			Criteria shipbillHeader = Criteria.forClass(TmsShipbillHeaderModel.class);
			Restriction shipbillId = Restrictions.in("shipbillId", shipbillIds.toArray());
			shipbillHeader.setRestriction(shipbillId);
			tmsShipbillHeaderMapper.updateMapByCriteria(map, shipbillHeader);

			Criteria criteriaLoadingBillDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
			// 运单日志添加
			for (String id : shipbillIds) {
				// 更新装车单明细
				Restriction r3 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, id);
				Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
				criteriaLoadingBillDetail.setRestriction(Restrictions.and(r3, r4));
				criteriaLoadingBillDetail.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
				List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingBillDetail);

				if (ToolKits.isEmpty(tmsLoadingBillDetails)) {
					throw new PrologException("运单【" + id + "】查询失败");
				}
				TmsLoadingBillDetailModel billDetailModel = tmsLoadingBillDetails.get(0);
				billDetailModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_PENDING_APP);
				billDetailModel.setUpdateperson(user.getNickname());
				billDetailModel.setUpdatetime(date);
				tmsLoadingBillDetailMapper.update(billDetailModel);

				// 运单操作日志保存
				ShipbillOperateLogSaveVO shipbillOperateLogSaveVO = new ShipbillOperateLogSaveVO();
				shipbillOperateLogSaveVO.setShipbillId(id);
				shipbillOperateLogSaveVO.setShipbillNo(id);
				shipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YYQX);
				shipbillOperateLogSaveVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_PENDING_APP);
				shipbillOperateLogSaveVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_PENDING_APP);
				shipbillOperateLogService.addShipbillOperateLogData(shipbillOperateLogSaveVO);
			}
		}

		// 封装修改参数
		Map<String, Object> map = Maps.newHashMap();
		map.put("status", DataDictionaryConstant.APPOINTMENT_STATUS_QX);
		map.put("updatetime", date);
		map.put("updateperson", user.getNickname());
		// 取消预约单
		tmsAppointmentHeaderMapper.updateMapByCriteria(map, criteria);
		// 取消预约单明细
		tmsAppointmentDetailMapper.updateMapByCriteria(map, appointmentDetail);
	}

	/**
	 * 预约管理导出所有数据
	 *
	 * @param bookingQueryExportAllVO
	 * @return
	 */
	@Override
	public List<BookingExportDTO> exportAllData(BookingQueryExportAllVO bookingQueryExportAllVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 预约状态
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_STATUS, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("预约状态翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 预约类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("预约类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		// STD-BUG20220823 xiaoyf 23966导出bug修复 start
		Restriction r1 = ToolKits.isEmpty(bookingQueryExportAllVO.getAppointmentNo()) ? null : Restrictions.like("reserveNumber", "%" + bookingQueryExportAllVO.getAppointmentNo() + "%");
		// STD-BUG20220823 xiaoyf 23966导出bug修复 end
		Restriction r2 = ToolKits.isEmpty(bookingQueryExportAllVO.getBranchName()) ? null : Restrictions.like("branchName", "%" + bookingQueryExportAllVO.getBranchName() + "%");
		Restriction r3 = ToolKits.isEmpty(bookingQueryExportAllVO.getDriverName()) ? null : Restrictions.like("driverName", "%" + bookingQueryExportAllVO.getDriverName() + "%");
		Restriction r4 = ToolKits.isEmpty(bookingQueryExportAllVO.getDriverPhoneNumber()) ? null : Restrictions.like("driverPhoneNumber", "%" + bookingQueryExportAllVO.getDriverPhoneNumber() + "%");
		Restriction r5 = ToolKits.isEmpty(bookingQueryExportAllVO.getVehiclePlateNumber()) ? null : Restrictions.like("vehiclePlateNumber", "%" + bookingQueryExportAllVO.getVehiclePlateNumber() + "%");
		Restriction r6 = ToolKits.isEmpty(bookingQueryExportAllVO.getAppointmentType()) ? null : Restrictions.eq("appointmentType", bookingQueryExportAllVO.getAppointmentType());
		Restriction r7 = Restrictions.eq("branchId", bookingQueryExportAllVO.getBranchId());
		Restriction r8 = Restrictions.ne("status", DataDictionaryConstant.APPOINTMENT_STATUS_QX);
		Restriction r9 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
		Restriction r10 = Restrictions.eq(TmsAppointmentHeaderModel::getStatus, bookingQueryExportAllVO.getStatus());
		Restriction r11 = ToolKits.isEmpty(bookingQueryExportAllVO.getStartDate()) ? null : Restrictions.ge(TmsAppointmentHeaderModel::getAppointmentDate, bookingQueryExportAllVO.getStartDate());
		Restriction r12 = ToolKits.isEmpty(bookingQueryExportAllVO.getEndDate()) ? null : Restrictions.le(TmsAppointmentHeaderModel::getAppointmentDate, bookingQueryExportAllVO.getEndDate());
		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end

		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12));
		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

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

		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByCriteria(criteria);

		for (TmsAppointmentHeaderModel tmsAppointmentHeaderModel : tmsAppointmentHeaderModels) {
			BookingExportDTO dto = new BookingExportDTO();
			BeanUtils.copyProperties(tmsAppointmentHeaderModel, dto);

			// 数据翻译
			dto.setStatusName(PublicMethods.change(data1, String.valueOf(tmsAppointmentHeaderModel.getStatus())));
			dto.setAppointmentTypeName(PublicMethods.change(data2, String.valueOf(tmsAppointmentHeaderModel.getAppointmentType())));
			// STD-BUG20220823 xiaoyf 23972导出错误 start
			dto.setReserveNumberTime(DateUtil.format(tmsAppointmentHeaderModel.getAppointmentDate(), "yyyy-MM-dd") + " " + tmsAppointmentHeaderModel.getAppointmentTime());
			// STD-BUG20220823 xiaoyf 23972导出错误 end

			// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
			if (ToolKits.isNotEmpty(bookingQueryExportAllVO.getShipbillNo())) {
				// 查询预约单明细
				Map<String, Object> querymap = Maps.newHashMap();
				querymap.put("appointmentId", tmsAppointmentHeaderModel.getAppointmentId());
				List<TmsAppointmentDetailModel> tmsAppointmentDetails = tmsAppointmentDetailMapper.findByMap(querymap, TmsAppointmentDetailModel.class);
				for (TmsAppointmentDetailModel tmsAppointmentDetailModel : tmsAppointmentDetails) {
					if (ObjectUtil.equal(bookingQueryExportAllVO.getShipbillNo(), tmsAppointmentDetailModel.getShipbillNo())) {
						bookingExportDTOS.add(dto);
						break;
					}
				}
			} else {
				bookingExportDTOS.add(dto);
			}
			// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end
		}
		return bookingExportDTOS;
	}

	/**
	 * 预约管理导出当前页数据
	 *
	 * @param bookingQueryExportPageVO
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public List<BookingExportDTO> exportPageData(BookingQueryExportPageVO bookingQueryExportPageVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 预约状态
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_STATUS, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("预约状态翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 预约类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("预约类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		// STD-BUG20220823 xiaoyf 23966导出bug修复 start
		Restriction r1 = ToolKits.isEmpty(bookingQueryExportPageVO.getAppointmentNo()) ? null : Restrictions.like("reserveNumber", "%" + bookingQueryExportPageVO.getAppointmentNo() + "%");
		// STD-BUG20220823 xiaoyf 23966导出bug修复 end
		Restriction r2 = ToolKits.isEmpty(bookingQueryExportPageVO.getBranchName()) ? null : Restrictions.like("branchName", "%" + bookingQueryExportPageVO.getBranchName() + "%");
		Restriction r3 = ToolKits.isEmpty(bookingQueryExportPageVO.getDriverName()) ? null : Restrictions.like("driverName", "%" + bookingQueryExportPageVO.getDriverName() + "%");
		Restriction r4 = ToolKits.isEmpty(bookingQueryExportPageVO.getDriverPhoneNumber()) ? null : Restrictions.like("driverPhoneNumber", "%" + bookingQueryExportPageVO.getDriverPhoneNumber() + "%");
		Restriction r5 = ToolKits.isEmpty(bookingQueryExportPageVO.getVehiclePlateNumber()) ? null : Restrictions.like("vehiclePlateNumber", "%" + bookingQueryExportPageVO.getVehiclePlateNumber() + "%");
		Restriction r6 = ToolKits.isEmpty(bookingQueryExportPageVO.getAppointmentType()) ? null : Restrictions.eq("appointmentType", bookingQueryExportPageVO.getAppointmentType());
		Restriction r7 = Restrictions.eq("branchId", bookingQueryExportPageVO.getBranchId());
		Restriction r8 = Restrictions.ne("status", DataDictionaryConstant.APPOINTMENT_STATUS_QX);
		Restriction r9 = Restrictions.eq("enterpriseId", user.getEnterpriseId());

		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
		Restriction r10 = Restrictions.eq(TmsAppointmentHeaderModel::getStatus, bookingQueryExportPageVO.getStatus());
		Restriction r11 = ToolKits.isEmpty(bookingQueryExportPageVO.getStartDate()) ? null : Restrictions.ge(TmsAppointmentHeaderModel::getAppointmentDate, bookingQueryExportPageVO.getStartDate());
		Restriction r12 = ToolKits.isEmpty(bookingQueryExportPageVO.getEndDate()) ? null : Restrictions.le(TmsAppointmentHeaderModel::getAppointmentDate, bookingQueryExportPageVO.getEndDate());
		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end

		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12));
		// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

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

		PageUtils.startPage(bookingQueryExportPageVO.getPageNum(), bookingQueryExportPageVO.getPageSize());
		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByCriteria(criteria);

		for (TmsAppointmentHeaderModel tmsAppointmentHeaderModel : tmsAppointmentHeaderModels) {
			BookingExportDTO dto = new BookingExportDTO();
			BeanUtils.copyProperties(tmsAppointmentHeaderModel, dto);

			// 数据翻译
			dto.setStatusName(PublicMethods.change(data1, String.valueOf(tmsAppointmentHeaderModel.getStatus())));
			dto.setAppointmentTypeName(PublicMethods.change(data2, String.valueOf(tmsAppointmentHeaderModel.getAppointmentType())));
			// STD-BUG20220823 xiaoyf 23972导出错误 start
			dto.setReserveNumberTime(DateUtil.format(tmsAppointmentHeaderModel.getAppointmentDate(), "yyyy-MM-dd") + " " + tmsAppointmentHeaderModel.getAppointmentTime());
			// STD-BUG20220823 xiaoyf 23972导出错误 end

			// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 start
			if (ToolKits.isNotEmpty(bookingQueryExportPageVO.getShipbillNo())) {
				// 查询预约单明细
				Map<String, Object> querymap = Maps.newHashMap();
				querymap.put("appointmentId", tmsAppointmentHeaderModel.getAppointmentId());
				List<TmsAppointmentDetailModel> tmsAppointmentDetails = tmsAppointmentDetailMapper.findByMap(querymap, TmsAppointmentDetailModel.class);
				for (TmsAppointmentDetailModel tmsAppointmentDetailModel : tmsAppointmentDetails) {
					if (ObjectUtil.equal(bookingQueryExportPageVO.getShipbillNo(), tmsAppointmentDetailModel.getShipbillNo())) {
						bookingExportDTOS.add(dto);
						break;
					}
				}
			} else {
				bookingExportDTOS.add(dto);
			}
			// STD-DEVELOP20220823 xiaoyf 功能调整【任务26784】 end
		}

		Page<BookingExportDTO> page = PageUtils.getPage(bookingExportDTOS);
		if (tmsAppointmentHeaderModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsAppointmentHeaderModel> p0 = (com.github.pagehelper.Page<TmsAppointmentHeaderModel>) tmsAppointmentHeaderModels;
			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.getList();
	}

	/**
	 * 预约管理导出选择行数据
	 *
	 * @param appointmentIds
	 * @return
	 */
	@Override
	public List<BookingExportDTO> exportSelectLineData(List<String> appointmentIds) {
		ToolKits.isParamNull(appointmentIds, "预约单ID不能为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 预约状态
		RestMessage<List<Map<String, Object>>> restMessage1 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_STATUS, null, null, null);
		if (!restMessage1.isSuccess()) {
			throw new RuntimeException("预约状态翻译失败!");
		} else {
			data1 = restMessage1.getData();
		}
		// 预约类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.APPOINTMENT_TYPE, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("预约类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}
		// 返回数据集
		List<BookingExportDTO> bookingExportDTOS = Lists.newArrayList();

		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		Restriction r1 = Restrictions.in("appointmentId", appointmentIds.toArray());
		criteria.setRestriction(r1);

		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByCriteria(criteria);

		for (TmsAppointmentHeaderModel tmsAppointmentHeaderModel : tmsAppointmentHeaderModels) {
			BookingExportDTO dto = new BookingExportDTO();
			BeanUtils.copyProperties(tmsAppointmentHeaderModel, dto);

			// 数据翻译
			dto.setStatusName(PublicMethods.change(data1, String.valueOf(tmsAppointmentHeaderModel.getStatus())));
			dto.setAppointmentTypeName(PublicMethods.change(data2, String.valueOf(tmsAppointmentHeaderModel.getAppointmentType())));
			// STD-BUG20220823 xiaoyf 23972导出错误 start
			dto.setReserveNumberTime(DateUtil.format(tmsAppointmentHeaderModel.getAppointmentDate(), "yyyy-MM-dd") + " " + tmsAppointmentHeaderModel.getAppointmentTime());
			// STD-BUG20220823 xiaoyf 23972导出错误 end

			// 封装数据集
			bookingExportDTOS.add(dto);
		}
		return bookingExportDTOS;
	}

	/**
	 * 单个调整添加运单弹窗
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<BookingGetShipbillPopDTO> shipbillPopData(BookingByShipbillPopVO bookingByShipbillPopVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}
		// 数据字典翻译数据集合
		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();

		// 订单类型
		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();
		}

		TmsAppointmentHeaderModel tmsAppointmentHeaderModel = tmsAppointmentHeaderMapper.findById(bookingByShipbillPopVO.getAppointmentId(), TmsAppointmentHeaderModel.class);
		ToolKits.isParamNull(tmsAppointmentHeaderModel, "预约单ID[" + bookingByShipbillPopVO.getAppointmentId() + "]有误,未找到对应的预约单");

		Criteria criteria = Criteria.forClass(TmsLoadingBillDetailModel.class);
		Restriction r1 = Restrictions.eq(TmsLoadingBillDetailModel::getShipperType, DataDictionaryConstant.SHIPPER_TYPE_CK);
		Restriction r2 = Restrictions.eq(TmsLoadingBillDetailModel::getShipperUnitId, tmsAppointmentHeaderModel.getBranchId());
		Restriction r3 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
		Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_PENDING_APP);
		Restriction r5 = ToolKits.isEmpty(bookingByShipbillPopVO.getCargoOwnerName()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getCargoOwnerName, "%" + bookingByShipbillPopVO.getCargoOwnerName() + "%");
		Restriction r6 = ToolKits.isEmpty(bookingByShipbillPopVO.getOrderNo()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getOrderNo, "%" + bookingByShipbillPopVO.getOrderNo() + "%");
		Restriction r7 = ToolKits.isEmpty(bookingByShipbillPopVO.getShipbillNo()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getShipbillNo, "%" + bookingByShipbillPopVO.getShipbillNo() + "%");
		Restriction r8 = ToolKits.isEmpty(bookingByShipbillPopVO.getOrderBusinessType()) ? null : Restrictions.eq(TmsLoadingBillDetailModel::getOrderBusinessType, bookingByShipbillPopVO.getOrderBusinessType());
		Restriction r9 = ToolKits.isEmpty(bookingByShipbillPopVO.getShipperUnitName()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getShipperUnitName, "%" + bookingByShipbillPopVO.getShipperUnitName() + "%");
		Restriction r10 = ToolKits.isEmpty(bookingByShipbillPopVO.getConsigneeUnitName()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getConsigneeUnitName, "%" + bookingByShipbillPopVO.getConsigneeUnitName() + "%");
		Restriction r11 = ToolKits.isEmpty(bookingByShipbillPopVO.getOrderDateStart()) ? null : Restrictions.ge(TmsLoadingBillDetailModel::getOrderDate, bookingByShipbillPopVO.getOrderDateStart());
		Restriction r12 = ToolKits.isEmpty(bookingByShipbillPopVO.getOrderDateEnd()) ? null : Restrictions.le(TmsLoadingBillDetailModel::getOrderDate, bookingByShipbillPopVO.getOrderDateEnd());
		Restriction r13 = ToolKits.isEmpty(bookingByShipbillPopVO.getShipperName()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getShipperName, "%" + bookingByShipbillPopVO.getShipperName() + "%");
		Restriction r14 = ToolKits.isEmpty(bookingByShipbillPopVO.getConsigneeName()) ? null : Restrictions.like(TmsLoadingBillDetailModel::getConsigneeName, "%" + bookingByShipbillPopVO.getConsigneeName() + "%");
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14));
		List<TmsLoadingBillDetailModel> list = tmsLoadingBillDetailMapper.findByCriteria(criteria);

		List<BookingGetShipbillPopDTO> shipbillList = Lists.newArrayList();
		for (TmsLoadingBillDetailModel model : list) {
			// 通过装车单明细查询装车单汇总
			TmsLoadingBillHeaderModel tmsLoadingBillHeader = tmsLoadingBillHeaderMapper.findById(model.getLoadingBillId(), TmsLoadingBillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsLoadingBillHeader) && ObjectUtil.equal(sdf.format(tmsAppointmentHeaderModel.getAppointmentDate()), ToolKits.isEmpty(model.getEstimatedPickupStartTime()) ? DateUtil.parseDate(DateUtil.today()) : sdf.format(model.getEstimatedPickupStartTime()))) {
				if (ObjectUtil.equal(tmsAppointmentHeaderModel.getDriverId(), tmsLoadingBillHeader.getDriverId()) && ObjectUtil.equal(tmsAppointmentHeaderModel.getDriverPhoneNumber(), tmsLoadingBillHeader.getDriverPhoneNumber()) && ObjectUtil.equal(tmsAppointmentHeaderModel.getVehiclePlateNumber(), tmsLoadingBillHeader.getVehiclePlateNumber())) {
					BookingGetShipbillPopDTO bookingGetShipbillPopDTO = new BookingGetShipbillPopDTO();
					BeanUtils.copyProperties(model, bookingGetShipbillPopDTO);

					// 收货方位置(经纬度判断)
					if (ToolKits.isNotEmpty(bookingGetShipbillPopDTO.getConsigneeLongitude()) && ToolKits.isNotEmpty(bookingGetShipbillPopDTO.getConsigneeLatitude())) {
						bookingGetShipbillPopDTO.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
					} else {
						bookingGetShipbillPopDTO.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
					}
					// 发货方位置(经纬度判断)
					if (ToolKits.isNotEmpty(bookingGetShipbillPopDTO.getShipperLongitude()) && ToolKits.isNotEmpty(bookingGetShipbillPopDTO.getShipperLatitude())) {
						bookingGetShipbillPopDTO.setShipperPositioning(StatusConstant.POSITIONING_YDW);
					} else {
						bookingGetShipbillPopDTO.setShipperPositioning(StatusConstant.POSITIONING_WDW);
					}

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

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

					// 获取背景颜色和字体颜色
					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(bookingGetShipbillPopDTO.getOrderTypeName()) ? bookingGetShipbillPopDTO.getOrderTypeName() : "")) {
									bookingGetShipbillPopDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
									bookingGetShipbillPopDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

									bookingGetShipbillPopDTO.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(bookingGetShipbillPopDTO.getOrderBusinessTypeName()) ? bookingGetShipbillPopDTO.getOrderBusinessTypeName() : "")) {
									bookingGetShipbillPopDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
									bookingGetShipbillPopDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

									bookingGetShipbillPopDTO.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(bookingGetShipbillPopDTO.getTemperatureRequirementName()) ? bookingGetShipbillPopDTO.getTemperatureRequirementName() : "")) {
									bookingGetShipbillPopDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
									bookingGetShipbillPopDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

									bookingGetShipbillPopDTO.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(bookingGetShipbillPopDTO.getDeliveryMethodTypeName()) ? bookingGetShipbillPopDTO.getDeliveryMethodTypeName() : "")) {
									bookingGetShipbillPopDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
									bookingGetShipbillPopDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

									bookingGetShipbillPopDTO.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(bookingGetShipbillPopDTO.getMemo()) ? bookingGetShipbillPopDTO.getMemo() : "")) {
									bookingGetShipbillPopDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
									bookingGetShipbillPopDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

									bookingGetShipbillPopDTO.setSignConfigId(signconfigPopDTO.getId());

									break Signconfig;
								}
							}
							break;
						}
					}

					shipbillList.add(bookingGetShipbillPopDTO);
				}
			}
		}

		// 分页重组
		Page<BookingGetShipbillPopDTO> page = PageUtils.getPage(shipbillList);
		if (shipbillList instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<BookingGetShipbillPopDTO> p0 = (com.github.pagehelper.Page<BookingGetShipbillPopDTO>) shipbillList;
			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 bookingByIDVO
	 * @return
	 */
	@Override
	public BookingViewByOneDTO viewOneData(BookingByIDVO bookingByIDVO) {
		ToolKits.isParamNull(bookingByIDVO, "请求参数不为空");
		ToolKits.isParamNull(bookingByIDVO.getAppointmentId(), "预约单ID不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

		// 数据字典翻译数据集合
		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();

		// 订单类型
		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();
		}

		// 查询预约单
		TmsAppointmentHeaderModel tmsAppointmentHeaderModel = tmsAppointmentHeaderMapper.findById(bookingByIDVO.getAppointmentId(), TmsAppointmentHeaderModel.class);
		ToolKits.isParamNull(tmsAppointmentHeaderModel, "预约单ID[" + bookingByIDVO.getAppointmentId() + "]有误,未找到对应的预约单");
		Assert.isTrue(tmsAppointmentHeaderModel.getStatus().intValue() < DataDictionaryConstant.APPOINTMENT_STATUS_YSH.intValue(), "预约单【" + bookingByIDVO.getAppointmentId() + "】已审核不能调整");

		// 返回数据
		BookingViewByOneDTO bookingViewByOneDTO = new BookingViewByOneDTO();
		BeanUtils.copyProperties(tmsAppointmentHeaderModel, bookingViewByOneDTO);

		// 预约时间拼接
		StringBuffer appointmentFormatTime = new StringBuffer();
		appointmentFormatTime.append(bookingViewByOneDTO.getAppointmentDate());
		appointmentFormatTime.append(" ");
		appointmentFormatTime.append(bookingViewByOneDTO.getAppointmentTime());

		bookingViewByOneDTO.setAppointmentFormatTime(appointmentFormatTime.toString());

		// 查询明细
		Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.eq("appointmentId", bookingByIDVO.getAppointmentId());
		criteria.setRestriction(r1);
		criteria.setOrder(Order.newInstance().asc(new String[] { "orderDate" }));

		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);

		// 返回明细数据集
		List<BookingViewByOneDetailDTO> bookingViewByOneDetailDTOS = Lists.newArrayList();

		for (TmsAppointmentDetailModel model : tmsAppointmentDetailModels) {
			BookingViewByOneDetailDTO bookingViewByOneDetailDTO = new BookingViewByOneDetailDTO();
			BeanUtils.copyProperties(model, bookingViewByOneDetailDTO);

			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getConsigneeLongitude()) && ToolKits.isNotEmpty(model.getConsigneeLatitude())) {
				bookingViewByOneDetailDTO.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				bookingViewByOneDetailDTO.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(model.getShipperLongitude()) && ToolKits.isNotEmpty(model.getShipperLatitude())) {
				bookingViewByOneDetailDTO.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				bookingViewByOneDetailDTO.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

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

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

			// 获取背景颜色和字体颜色
			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(bookingViewByOneDetailDTO.getOrderTypeName()) ? bookingViewByOneDetailDTO.getOrderTypeName() : "")) {
							bookingViewByOneDetailDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingViewByOneDetailDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingViewByOneDetailDTO.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(bookingViewByOneDetailDTO.getOrderBusinessTypeName()) ? bookingViewByOneDetailDTO.getOrderBusinessTypeName() : "")) {
							bookingViewByOneDetailDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingViewByOneDetailDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingViewByOneDetailDTO.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(bookingViewByOneDetailDTO.getTemperatureRequirementName()) ? bookingViewByOneDetailDTO.getTemperatureRequirementName() : "")) {
							bookingViewByOneDetailDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingViewByOneDetailDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingViewByOneDetailDTO.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(bookingViewByOneDetailDTO.getDeliveryMethodTypeName()) ? bookingViewByOneDetailDTO.getDeliveryMethodTypeName() : "")) {
							bookingViewByOneDetailDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingViewByOneDetailDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingViewByOneDetailDTO.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(bookingViewByOneDetailDTO.getMemo()) ? bookingViewByOneDetailDTO.getMemo() : "")) {
							bookingViewByOneDetailDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							bookingViewByOneDetailDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

							bookingViewByOneDetailDTO.setSignConfigId(signconfigPopDTO.getId());

							break Signconfig;
						}
					}
					break;
				}
			}

			bookingViewByOneDetailDTOS.add(bookingViewByOneDetailDTO);
		}

		bookingViewByOneDTO.setBookingViewByOneDetailDTOS(bookingViewByOneDetailDTOS);

		return bookingViewByOneDTO;
	}

	/**
	 * 预约单调整(单个)
	 *
	 * @param bookingUpdateOneVO
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateOneData(BookingUpdateOneVO bookingUpdateOneVO) {
		ToolKits.isParamNull(bookingUpdateOneVO, "请求参数不为空");
		ToolKits.isParamNull(bookingUpdateOneVO.getAppointmentId(), "预约单ID不为空");
		ToolKits.isParamNull(bookingUpdateOneVO.getShipbillIds(), "预约明细不为空");

		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 查询预约单
		TmsAppointmentHeaderModel tmsAppointmentHeaderModel = tmsAppointmentHeaderMapper.findById(bookingUpdateOneVO.getAppointmentId(), TmsAppointmentHeaderModel.class);
		ToolKits.isParamNull(tmsAppointmentHeaderModel, "预约单ID[" + bookingUpdateOneVO.getAppointmentId() + "]有误,未找到对应的预约单");

		List<TmsShipbillHeaderModel> shipbillHeaderModels = tmsShipbillHeaderMapper.findByIds(TmsShipbillHeaderModel.class,bookingUpdateOneVO.getShipbillIds().stream().toArray(), null);
		if (ToolKits.isEmpty(shipbillHeaderModels)) {
			throw new PrologException(StrUtil.format("运单不存在"));
		}
		List<TmsShipbillHeaderModel> noTakeAndCallShipbillHeaderModels = shipbillHeaderModels.stream().filter(h->!h.getFlowConfig().contains(DataDictionaryConstant.FLOW_CONFIG_APPOINTMENT_TN_CN)).collect(Collectors.toList());
		if (ToolKits.isNotEmpty(noTakeAndCallShipbillHeaderModels) && noTakeAndCallShipbillHeaderModels.size() != bookingUpdateOneVO.getShipbillIds().size()) {
			throw new PrologException("请勾选相同预约流程的运单");
		}
		List<TmsShipbillHeaderModel> hasTakeAndCallShipbillHeaderModels = shipbillHeaderModels.stream().filter(h->h.getFlowConfig().contains(DataDictionaryConstant.FLOW_CONFIG_APPOINTMENT_TN_CN)).collect(Collectors.toList());
		if (ToolKits.isNotEmpty(hasTakeAndCallShipbillHeaderModels) && hasTakeAndCallShipbillHeaderModels.size() != bookingUpdateOneVO.getShipbillIds().size()) {
			throw new PrologException("请勾选相同预约流程的运单");
		}

		List<String> ids = Lists.newArrayList();
		ids.add(bookingUpdateOneVO.getAppointmentId());
		// 预约时间判断
		timeJudge(ids, bookingUpdateOneVO.getShipbillIds(), tmsAppointmentHeaderModel.getBranchId(), bookingUpdateOneVO.getAppointmentDate(), bookingUpdateOneVO.getAppointmentTime(), date, user.getEnterpriseId());

		BeanUtils.copyProperties(bookingUpdateOneVO, tmsAppointmentHeaderModel);
		if (ToolKits.isNotEmpty(bookingUpdateOneVO.getShipbillIds())) {
			for (String shipbillId : bookingUpdateOneVO.getShipbillIds()) {
				// 查询对应对应预约单明细
				Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
				Restriction r1 = Restrictions.eq("appointmentId", bookingUpdateOneVO.getAppointmentId());
				Restriction r2 = Restrictions.eq("shipbillId", shipbillId);
				criteria.setRestriction(Restrictions.and(r1, r2));
				long count = tmsAppointmentDetailMapper.findCountByCriteria(criteria);

				// 判断是否存在 没有就新增
				if (count == 0) {
					TmsShipbillHeaderModel tmsShipbillHeaderModel = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
					ToolKits.isParamNull(tmsShipbillHeaderModel, "运单ID[" + shipbillId + "]有误,查询为空");

					TmsAppointmentDetailModel tmsAppointmentDetailModel = new TmsAppointmentDetailModel();
					BeanUtils.copyProperties(tmsShipbillHeaderModel, tmsAppointmentDetailModel);

					// 获取预约明细表ID
					RestMessage<String> restMessageDetailId = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_APPOINTMENT_DETAIL_ID, "L");
					if (!restMessageDetailId.isSuccess()) {
						throw new RuntimeException("生成预约明细表ID失败");
					}
					String detailId = restMessageDetailId.getData();

					tmsAppointmentDetailModel.setId(detailId);
					tmsAppointmentDetailModel.setAppointmentId(tmsAppointmentHeaderModel.getAppointmentId());
					tmsAppointmentDetailModel.setAppointmentNo(tmsAppointmentHeaderModel.getAppointmentNo());
					tmsAppointmentDetailModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_APP_ING);

					// 新增
					tmsAppointmentDetailMapper.save(tmsAppointmentDetailModel);

					// 更新运单状态 为预约中
					Map<String, Object> mapUpdate = Maps.newHashMap();
					mapUpdate.put("status", DataDictionaryConstant.SHIPBILL_STATUS_APP_ING);
					mapUpdate.put("updateperson", user.getNickname());
					mapUpdate.put("updatetime", date);
					tmsShipbillHeaderMapper.updateMapById(shipbillId, mapUpdate, TmsShipbillHeaderModel.class);

					// 更新装车单明细状态 为预约中
					Criteria criteriaLoadingBillDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
					Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
					Restriction r5 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, shipbillId);
					criteriaLoadingBillDetail.setRestriction(Restrictions.and(r4, r5));
					tmsLoadingBillDetailMapper.updateMapByCriteria(mapUpdate, criteriaLoadingBillDetail);

					// 日志表
					ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
					shipbillOperateLogSaveNewVO.setShipbillId(shipbillId);
					shipbillOperateLogSaveNewVO.setShipbillNo(shipbillId);
					shipbillOperateLogSaveNewVO.setOperateDate(date);
					shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
					shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
					shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
					shipbillOperateLogSaveNewVO.setUserName(user.getNickname());
					shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YYQR);
					shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK);
					shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_APP_ING);
					shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);
				}
			}
		}

		// 查询多余明细
		Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.eq("appointmentId", bookingUpdateOneVO.getAppointmentId());
		Restriction r2 = ToolKits.isEmpty(bookingUpdateOneVO.getShipbillIds()) ? null : Restrictions.notIn("shipbillId", bookingUpdateOneVO.getShipbillIds().toArray());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);

		if (ToolKits.isNotEmpty(tmsAppointmentDetailModels)) {
			for (TmsAppointmentDetailModel tmsAppointmentDetailModel : tmsAppointmentDetailModels) {
				// 更新运单状态 为预约中
				Map<String, Object> mapUpdate = Maps.newHashMap();
				mapUpdate.put("status", DataDictionaryConstant.SHIPBILL_STATUS_PENDING_APP);
				mapUpdate.put("updateperson", user.getNickname());
				mapUpdate.put("updatetime", date);
				tmsShipbillHeaderMapper.updateMapById(tmsAppointmentDetailModel.getShipbillId(), mapUpdate, TmsShipbillHeaderModel.class);

				// 更新装车单明细状态 为预约中
				Criteria criteriaLoadingBillDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
				Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
				Restriction r5 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, tmsAppointmentDetailModel.getShipbillId());
				criteriaLoadingBillDetail.setRestriction(Restrictions.and(r4, r5));
				tmsLoadingBillDetailMapper.updateMapByCriteria(mapUpdate, criteriaLoadingBillDetail);

				// 日志表
				ShipbillOperateLogSaveNewVO shipbillOperateLogSaveNewVO = new ShipbillOperateLogSaveNewVO();
				shipbillOperateLogSaveNewVO.setShipbillId(tmsAppointmentDetailModel.getShipbillId());
				shipbillOperateLogSaveNewVO.setShipbillNo(tmsAppointmentDetailModel.getShipbillId());
				shipbillOperateLogSaveNewVO.setOperateDate(date);
				shipbillOperateLogSaveNewVO.setEnterpriseId(user.getEnterpriseId());
				shipbillOperateLogSaveNewVO.setEnterpriseName(user.getEnterpriseName());
				shipbillOperateLogSaveNewVO.setUserId(user.getUserid());
				shipbillOperateLogSaveNewVO.setUserName(user.getNickname());
				shipbillOperateLogSaveNewVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YYTC);
				shipbillOperateLogSaveNewVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_APP_ING);
				shipbillOperateLogSaveNewVO.setNewStatus(DataDictionaryConstant.SHIPBILL_STATUS_PENDING_APP);
				shipbillOperateLogService.addShipbillOperateLogNewData(shipbillOperateLogSaveNewVO);

				// 删除预约单明细
				tmsAppointmentDetailMapper.deleteById(tmsAppointmentDetailModel.getId(), TmsAppointmentDetailModel.class);
			}
		}
		r1 = Restrictions.eq("appointmentId", bookingUpdateOneVO.getAppointmentId());
		criteria.setRestriction(Restrictions.and(r1));
		List<TmsAppointmentDetailModel> newAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);
		int size = 0;
		int shipbillQuantity = 0;
		BigDecimal totPcs = BigDecimal.ZERO;
		BigDecimal totalVolume = BigDecimal.ZERO;
		BigDecimal totalWeight = BigDecimal.ZERO;
		if (ToolKits.isNotEmpty(newAppointmentDetailModels)) {
			size = newAppointmentDetailModels.size();
			shipbillQuantity = newAppointmentDetailModels.stream().filter(d->!DataDictionaryConstant.ORDER_TYPE_P.equals(d.getOrderType())).collect(Collectors.toList()).size();
			totPcs = newAppointmentDetailModels.stream().filter(d->ToolKits.isNotEmpty(d.getTotalPcs())).map(TmsAppointmentDetailModel::getTotalPcs).reduce(BigDecimal.ZERO,NumberUtil::add);
			totalVolume = newAppointmentDetailModels.stream().filter(d->ToolKits.isNotEmpty(d.getTotalVolume())).map(TmsAppointmentDetailModel::getTotalVolume).reduce(BigDecimal.ZERO,NumberUtil::add);
			totalWeight = newAppointmentDetailModels.stream().filter(d->ToolKits.isNotEmpty(d.getTotalWeight())).map(TmsAppointmentDetailModel::getTotalWeight).reduce(BigDecimal.ZERO,NumberUtil::add);
		}
		tmsAppointmentHeaderModel.setShipbillQuantity(size);
		tmsAppointmentHeaderModel.setUrgentQuantity(shipbillQuantity);
		tmsAppointmentHeaderModel.setTotalPcs(totPcs);
		tmsAppointmentHeaderModel.setTotalVolume(totalVolume);
		tmsAppointmentHeaderModel.setTotalWeight(totalWeight);
		tmsAppointmentHeaderMapper.update(tmsAppointmentHeaderModel);

	}

	/**
	 * 预约单调整(批量)
	 *
	 * @param bookingUpdateBatchVO
	 */
	@Override
	public void updateBatchData(BookingUpdateBatchVO bookingUpdateBatchVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号操作
		if (!user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			throw new PrologException("当前人员没有操作权限");
		}

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

		// 查询预约单
		List<TmsAppointmentHeaderModel> tmsAppointmentHeaderModels = tmsAppointmentHeaderMapper.findByIds(TmsAppointmentHeaderModel.class, bookingUpdateBatchVO.getAppointmentIds().toArray(), null);
		ToolKits.isParamNull(tmsAppointmentHeaderModels, "预约单ID有误,未找到对应的预约单");

		// 判断仓库是否统一
		List<Long> branchId = tmsAppointmentHeaderModels.stream().map(TmsAppointmentHeaderModel::getBranchId).distinct().collect(Collectors.toList());
		if (branchId.size() != 1) {
			throw new PrologException("预约单的预约地点不统一");
		}

		// 查询明细
		Criteria criteria = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.in("appointmentId", bookingUpdateBatchVO.getAppointmentIds().toArray());
		criteria.setRestriction(r1);
		List<TmsAppointmentDetailModel> tmsAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteria);
		ToolKits.isParamNull(tmsAppointmentDetailModels, "未找到对应明细");

		List<String> shipbillIds = tmsAppointmentDetailModels.stream().map(TmsAppointmentDetailModel::getShipbillId).distinct().collect(Collectors.toList());
		// 预约时间判断
		timeJudge(bookingUpdateBatchVO.getAppointmentIds(), shipbillIds, branchId.get(0), bookingUpdateBatchVO.getAppointmentDate(), bookingUpdateBatchVO.getAppointmentTime(), date, user.getEnterpriseId());

		// 修改预约单信息
		for (TmsAppointmentHeaderModel tmsAppointmentHeaderModel : tmsAppointmentHeaderModels) {
			BeanUtils.copyProperties(bookingUpdateBatchVO, tmsAppointmentHeaderModel);
			tmsAppointmentHeaderMapper.update(tmsAppointmentHeaderModel);
		}

	}

	/**
	 * 预约时间判断
	 *
	 * @param ids
	 * @param shipbillIds
	 * @param branchId
	 * @param appointmentDate
	 * @param appointmentTime
	 * @param aliAppDt
	 * @param enterpriseId
	 */
	public void timeJudge(List<String> ids, List<String> shipbillIds, Long branchId, Date appointmentDate, String appointmentTime, Date aliAppDt, Long enterpriseId) {
		// 获取仓库信息
		TmsBaseBranchDTO tmsBaseBranchDTO = new TmsBaseBranchDTO();
		RestMessage<TmsBaseBranchDTO> restMessageData = maindataTmsResource.viewBranch(branchId);
		if (!restMessageData.isSuccess()) {
			throw new RuntimeException("获取仓库的预约信息失败!");
		} else {
			tmsBaseBranchDTO = restMessageData.getData();
		}

		// 运输记录表数据查询
		Criteria criteriaTransLog = Criteria.forClass(TmsShipbillTransLogModel.class);
		Restriction r1 = Restrictions.eq(TmsShipbillTransLogModel::getEnterpriseId, enterpriseId);
		Restriction r2 = null;
		Restriction r3 = null;
		Integer aptTimeType = tmsBaseBranchDTO.getAptTime();
		Integer aptTimeUnit = tmsBaseBranchDTO.getAptUnit();

		// 可预约的时间
		if (ToolKits.isNotEmpty(aptTimeUnit) && DataDictionaryConstant.AVAILABLE_UNIT_HOUR.equals(aptTimeUnit)) {
			aliAppDt = DateUtil.offsetHour(aliAppDt, -(tmsBaseBranchDTO.getAptTimeDura().intValue()));
		} else if (ToolKits.isNotEmpty(aptTimeUnit) && DataDictionaryConstant.AVAILABLE_UNIT_DAY.equals(aptTimeUnit)) {
			aliAppDt = DateUtil.offsetDay(aliAppDt, -(tmsBaseBranchDTO.getAptTimeDura().intValue()));
		}
		for (String shipbillId : shipbillIds) {
			r2 = Restrictions.eq(TmsShipbillTransLogModel::getShipbillId, shipbillId);
			if (DataDictionaryConstant.AVAILABLE_TIME_PICK_UP.equals(aptTimeType)) {
				// r3 = Restrictions.gt(TmsShipbillTransLogModel::getEstimatedPickupStartTime,
				// aliAppDt);
			} else if (DataDictionaryConstant.AVAILABLE_TIME_ORDER.equals(aptTimeType)) {
				r3 = Restrictions.gt(TmsShipbillTransLogModel::getOrderDt, aliAppDt);
			} else if (DataDictionaryConstant.AVAILABLE_TIME_COMMIT.equals(aptTimeType)) {
				r3 = Restrictions.gt(TmsShipbillTransLogModel::getCommitDt, aliAppDt);
			} else if (DataDictionaryConstant.AVAILABLE_TIME_REVIEW.equals(aptTimeType)) {
				r3 = Restrictions.gt(TmsShipbillTransLogModel::getReviewDt, aliAppDt);
			} else if (DataDictionaryConstant.AVAILABLE_TIME_DISPATCH.equals(aptTimeType)) {
				r3 = Restrictions.gt(TmsShipbillTransLogModel::getDisDt, aliAppDt);
			} else if (DataDictionaryConstant.AVAILABLE_TIME_DISPATCH_CHECK.equals(aptTimeType)) {
				r3 = Restrictions.gt(TmsShipbillTransLogModel::getDisCheckDt, aliAppDt);
			}
			criteriaTransLog.setRestriction(Restrictions.and(r1, r2, r3));
			List<TmsShipbillTransLogModel> tmsShipbillHeaderModels = tmsShipbillTransportLogMapper.findByCriteria(criteriaTransLog);

			if (ToolKits.isEmpty(tmsShipbillHeaderModels)) {
				throw new PrologException("预约时间不在仓库【" + tmsBaseBranchDTO.getName() + "】的可预约时间段内");
			} else {
				TmsShipbillTransLogModel logModel = tmsShipbillHeaderModels.get(0);
				if (DataDictionaryConstant.AVAILABLE_TIME_PICK_UP.equals(aptTimeType) && ToolKits.isNotEmpty(logModel.getEstimatedPickupStartTime()) && logModel.getEstimatedPickupStartTime().before(aliAppDt)) {
					throw new PrologException("预约时间不在仓库【" + tmsBaseBranchDTO.getName() + "】的可预约时间段内");
				}
			}
		}

		// 不可预约时间段 开始时间
		Date nonAptSpecStartDate = tmsBaseBranchDTO.getNonAptSpecStartDate();
		// 不可预约时间段 结束时间
		Date nonAptSpecEndDate = tmsBaseBranchDTO.getNonAptSpecEndDate();

		String nonAptWeek = null;
		String nonAptMonth = null;
		if (DataDictionaryConstant.NON_ABL_DT_WK.equals(tmsBaseBranchDTO.getNonAptDt())) {
			// 不可预约日期 按周
			nonAptWeek = tmsBaseBranchDTO.getNonAptDura();
		} else if (DataDictionaryConstant.NON_ABL_DT_MT.equals(tmsBaseBranchDTO.getNonAptDt())) {
			// 不可预约日期 按月
			nonAptMonth = tmsBaseBranchDTO.getNonAptDura();
		}

		// 判断是否在不可预约时间段内
		if (ToolKits.isNotEmpty(nonAptSpecStartDate) && ToolKits.isNotEmpty(nonAptSpecEndDate) && DateUtil.compare(appointmentDate, nonAptSpecStartDate) >= 0 && DateUtil.compare(appointmentDate, nonAptSpecEndDate) < 0) {
			throw new PrologException("当前日期不可预约");
		}
		// 判断是否在不可预约 按月
		if (ToolKits.isNotEmpty(nonAptMonth) && nonAptMonth.contains(DateUtil.format(appointmentDate, "dd"))) {
			throw new PrologException("当前日期不可预约");
		}
		// 判断是否在不可预约 按周
		if (ToolKits.isNotEmpty(nonAptWeek) && nonAptWeek.contains(String.valueOf(DateUtil.dayOfWeek(appointmentDate)))) {
			throw new PrologException("当前日期不可预约");
		}

		// 查询已预约次数 (排除需要调整的预约单)
		Criteria criteria = Criteria.forClass(TmsAppointmentHeaderModel.class);
		Restriction r4 = Restrictions.notIn("appointmentId", ids.toArray());
		Restriction r5 = Restrictions.eq("branchId", branchId);
		Restriction r6 = Restrictions.eq("appointmentTime", appointmentTime);
		Restriction r7 = Restrictions.eq("appointmentDate", appointmentDate);
		Restriction r8 = Restrictions.ne("status", DataDictionaryConstant.APPOINTMENT_STATUS_QX);
		criteria.setRestriction(Restrictions.and(r4, r5, r6, r7, r8));
		long count = tmsAppointmentDetailMapper.findCountByCriteria(criteria);

		if (ToolKits.isNotEmpty(tmsBaseBranchDTO.getAptNum())) {
			Long appCnt = new Long(count);
			if (tmsBaseBranchDTO.getAptNum().intValue() < (appCnt.intValue() + ids.size())) {
				throw new PrologException("当前时间段可预约数不够");
			}
		} else {
			throw new PrologException("仓库可预约数为空");
		}
	}

	public void cancelAppointment(List<String> shipbillIds) {
		ToolKits.isParamNull(shipbillIds, "运单ID不能为空");

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

		Criteria appointmentDetail = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r1 = Restrictions.in(TmsAppointmentDetailModel::getShipbillId, shipbillIds);
		Restriction r3 = Restrictions.eq(TmsAppointmentDetailModel::getEnterpriseId, user.getEnterpriseId());
		appointmentDetail.setRestriction(Restrictions.and(r1,r3));
		List<TmsAppointmentDetailModel> appointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(appointmentDetail);
		if (ToolKits.isEmpty(appointmentDetailModels)) {
			return;
		}
		Map<String, List<TmsAppointmentDetailModel>> appointmentGroupmap = Maps.newHashMap();
		appointmentGroupmap = appointmentDetailModels.stream().collect(Collectors.groupingBy(o -> o.getAppointmentId()));

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

		for (String id:appointmentGroupmap.keySet()) {
			List<TmsAppointmentDetailModel> oneAppointmentDetailModels = appointmentGroupmap.get(id);
			r1 = Restrictions.in(TmsAppointmentDetailModel::getId, oneAppointmentDetailModels.stream().map(TmsAppointmentDetailModel::getId).collect(Collectors.toList()));
			appointmentDetail.setRestriction(Restrictions.and(r1));
			tmsAppointmentDetailMapper.deleteByCriteria(appointmentDetail);

			r1 = Restrictions.notIn(TmsAppointmentDetailModel::getId, oneAppointmentDetailModels.stream().map(TmsAppointmentDetailModel::getId).collect(Collectors.toList()));
			r3 = Restrictions.eq(TmsAppointmentDetailModel::getAppointmentId, id);
			appointmentDetail.setRestriction(Restrictions.and(r1,r3));
			List<TmsAppointmentDetailModel> otherAppointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(appointmentDetail);
			if (ToolKits.isEmpty(otherAppointmentDetailModels)) {
				tmsAppointmentHeaderMapper.updateMapById(id,map,TmsAppointmentHeaderModel.class);
			} else {
				TmsAppointmentHeaderModel appointmentHeaderModel = tmsAppointmentHeaderMapper.findById(id,TmsAppointmentHeaderModel.class);

				if (ToolKits.isEmpty(appointmentHeaderModel)) {
					continue;
				}
				appointmentHeaderModel.setUrgentQuantity(otherAppointmentDetailModels.stream().filter(d->!DataDictionaryConstant.ORDER_TYPE_P.equals(d.getOrderType())).collect(Collectors.toList()).size());
				appointmentHeaderModel.setShipbillQuantity(otherAppointmentDetailModels.size());
				appointmentHeaderModel.setTotalPcs(otherAppointmentDetailModels.stream().filter(d->ToolKits.isNotEmpty(d.getTotalPcs())).map(TmsAppointmentDetailModel::getTotalPcs).reduce(BigDecimal.ZERO,NumberUtil::add));
				appointmentHeaderModel.setTotalWeight(otherAppointmentDetailModels.stream().filter(d->ToolKits.isNotEmpty(d.getTotalWeight())).map(TmsAppointmentDetailModel::getTotalWeight).reduce(BigDecimal.ZERO,NumberUtil::add));
				appointmentHeaderModel.setTotalVolume(otherAppointmentDetailModels.stream().filter(d->ToolKits.isNotEmpty(d.getTotalVolume())).map(TmsAppointmentDetailModel::getTotalVolume).reduce(BigDecimal.ZERO,NumberUtil::add));
				appointmentHeaderModel.setUpdatetime(DateUtil.date());
				appointmentHeaderModel.setUpdateperson(user.getNickname());
				tmsAppointmentHeaderMapper.update(appointmentHeaderModel);
			}
		}
	}

}