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

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.prolog.rdc.tms.constant.enums.intelligent.IntelligentLockEnum;
import com.prolog.rdc.tms.dao.mapper.*;
import com.prolog.rdc.tms.model.dto.*;
import com.prolog.rdc.tms.model.entity.*;
import com.prolog.rdc.tms.model.vo.*;
import com.prolog.rdc.tms.proxy.MainDataProxy;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
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.constant.enums.DriverReceiveStatusEnum;
import com.prolog.rdc.tms.constant.enums.ShipBillTypeEnum;
import com.prolog.rdc.tms.constant.enums.SignInStatusEnum;
import com.prolog.rdc.tms.constant.enums.UserTypeEnum;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPopDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCheckDTO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPopVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCheckVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.AllShipbillService;
import com.prolog.rdc.tms.service.ShipbillNodeTrackingService;
import com.prolog.rdc.tms.service.ShipbillOperateLogService;
import com.prolog.rdc.tms.util.ConvertorUtil;
import com.prolog.rdc.tms.util.PublicMethods;

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

/**
 * 运输管理——所有运单实现层
 *
 * @author xyf
 * @since 2022/4/18 12:43
 */
@Service
public class AllShipbillServiceImpl implements AllShipbillService {

	@Autowired
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Autowired
	private TmsAppointmentDetailMapper tmsAppointmentDetailMapper;

	@Autowired
	private TmsAppointmentHeaderMapper tmsAppointmentHeaderMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private TmsShipbillPackageMapper tmsShipbillPackageMapper;

	@Autowired
	private ShipbillNodeTrackingService shipbillNodeTrackingService;

	@Autowired
	private ShipbillOperateLogService shipbillOperateLogService;

	@Autowired
	private ConvertorUtil convertorUtil;

	@Autowired
	private TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

	@Autowired
	private TmsShipbillEvaluationMapper tmsShipbillEvaluationMapper;

	// STD-DEVELOP20220823 lhb 异常登记 20220914 start
	@Autowired
	private TmsShipbillAbnormalMapper tmsShipbillAbnormalMapper;
	// STD-DEVELOP20220823 lhb 异常登记 20220914 end

	@Autowired
	private MainDataProxy mainDataProxy;

	@Autowired
	private TmsTemperatureDataMapper tmsTemperatureDataMapper;

	/**
	 * 运单分页查询
	 */
	@Override
	public Page<AllShipbillPageDTO> queryAllShipbillPage(AllShipbillQueryVO vo) {
		ToolKits.isParamNull(vo, "运单分页查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, // 车辆类别
				DataDictionaryConstant.LINE_DIRECTION, // 线路方向
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setUserType(user.getUserType());

		if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_ZY)) {// 自有账号
			vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? null : user.getDepts().stream().toArray(String[]::new));
			if (BussinessConstant.USER_DEPARTMENT_AUTH_Y.equals(user.getOwnedAllDataAuthFlag())) {// 部门权限
				vo.setIsDept("YES");
			} else {// 业务员权限
				vo.setIsDept("NO");
				// 货主
				vo.setConsignors(ToolKits.isEmpty(user.getConsignors()) ? null : user.getConsignors().stream().toArray(Long[]::new));
				// 客户
				vo.setCustomers(ToolKits.isEmpty(user.getCustomers()) ? null : user.getCustomers().stream().toArray(Long[]::new));
				// 供应商
				vo.setSuppliers(ToolKits.isEmpty(user.getSuppliers()) ? null : user.getSuppliers().stream().toArray(Long[]::new));
				// 仓库
				vo.setWsarehouses(ToolKits.isEmpty(user.getWarehouses()) ? null : user.getWarehouses().stream().toArray(Long[]::new));
			}
			// STD-DEVELOP20220823 lhb 货主账号 start
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主
			Long[] consignor = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setConsignors(consignor);
			// STD-DEVELOP20220823 lhb 货主账号 end
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {// 承运商账号
			vo.setCarrierId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setSuppliers(userId);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCustomers(userId);
		}

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<AllShipbillPageDTO> dtos = tmsShipbillHeaderMapper.queryAllShipbill(vo);
		for (AllShipbillPageDTO dto : dtos) {
			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ToolKits.isNotEmpty(dto.getCarrierTypeZiyou())) {
				if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierTypeZiyou())) {// 承运方类型为司机时展示司机姓名
					dto.setCarrierNameZiyou(dto.getDriverNameZiyou());
				}
				dto.setCarrierTypeNameZiyou(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierTypeZiyou())));
			}
			if (ToolKits.isNotEmpty(dto.getCarrierTypeCarrier())) {
				if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierTypeCarrier())) {// 承运方类型为司机时展示司机姓名
					dto.setCarrierNameCarrier(dto.getDriverNameCarrier());
				}
				dto.setCarrierTypeNameCarrier(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierTypeCarrier())));
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));

			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));
			dto.setVehicleTypeCategoryName(ConvertorUtil.getName(dicData, DataDictionaryConstant.VEHICLE_TYPE_CATEGORY, String.valueOf(dto.getVehicleTypeCategory())));
			dto.setLineDirectionName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_DIRECTION, String.valueOf(dto.getLineDirection())));

			dto.setLoadingBillNo(ToolKits.isEmpty(dto.getLoadingBillNoZiyou())?dto.getLoadingBillNoCarrier():dto.getLoadingBillNoZiyou());
			dto.setCarrierTypeName(ToolKits.isEmpty(dto.getCarrierTypeNameZiyou()) ? dto.getCarrierTypeNameCarrier():dto.getCarrierTypeNameZiyou());
			dto.setCarrierName(ToolKits.isEmpty(dto.getCarrierNameZiyou()) ? dto.getCarrierNameCarrier():dto.getCarrierNameZiyou());
			dto.setDriverPhoneNumber(dto.getCarrierPhoneNumber());
		}
		return PageUtils.getPage(dtos);
	}

	// STD-DEVELOP20220823 lhb 销售单模式 20220919 start

	/**
	 * 销售单模式查询
	 *
	 * @param
	 * @return
	 */
	public Page<AllShipbillPageDTO> querySellAllShipbillPage(AllShipbillQueryVO vo) {
		ToolKits.isParamNull(vo, "运单分页查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setUserType(user.getUserType());
		vo.setIsSell("YES");

		if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_ZY)) {// 自有账号
			vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? null : user.getDepts().stream().toArray(String[]::new));
			if (BussinessConstant.USER_DEPARTMENT_AUTH_Y.equals(user.getOwnedAllDataAuthFlag())) {// 部门权限
				vo.setIsDept("YES");
			} else {// 业务员权限
				vo.setIsDept("NO");
				// 货主
				vo.setConsignors(ToolKits.isEmpty(user.getConsignors()) ? null : user.getConsignors().stream().toArray(Long[]::new));
				// 客户
				vo.setCustomers(ToolKits.isEmpty(user.getCustomers()) ? null : user.getCustomers().stream().toArray(Long[]::new));
				// 供应商
				vo.setSuppliers(ToolKits.isEmpty(user.getSuppliers()) ? null : user.getSuppliers().stream().toArray(Long[]::new));
				// 仓库
				vo.setWsarehouses(ToolKits.isEmpty(user.getWarehouses()) ? null : user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主
			Long[] consignor = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setConsignors(consignor);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {// 承运商账号
			vo.setCarrierId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setSuppliers(userId);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCustomers(userId);
		}

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<AllShipbillPageDTO> dtos = tmsShipbillHeaderMapper.querySellAllShipbill(vo);
		for (AllShipbillPageDTO dto : dtos) {
			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));
			dto.setCarrierTypeNameCarrier(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierTypeCarrier())));
			dto.setCarrierTypeNameZiyou(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierTypeZiyou())));

			if (ToolKits.isEmpty(dto.getCarrierNameCarrier())) {
				dto.setCarrierNameCarrier(dto.getDriverNameCarrier());
			}
			if (ToolKits.isEmpty(dto.getCarrierNameZiyou())) {
				dto.setCarrierNameZiyou(dto.getDriverNameZiyou());
			}
		}
		return PageUtils.getPage(dtos);
	}

	/**
	 * 发货单信息查询
	 *
	 * @param
	 * @return
	 */
	@SuppressWarnings("resource")
	public Page<AllShipbillPageDTO> queryShipShipbill(AllShipbillSellQueryVO vo) {
		ToolKits.isParamNull(vo, "发货单信息查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		List<AllShipbillPageDTO> dtos = Lists.newArrayList();
		List<TmsShipbillHeaderModel> shipbillHeaderModels = Lists.newArrayList();
		if (vo.getSplitOrderType().equals(DataDictionaryConstant.SPLIT_ORDER_TYPE_UN) && vo.getCarrierSplitOrderType().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN)) {
			// 没拆过单的运单
			PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
			TmsShipbillHeaderModel shipbillHeaderModel = tmsShipbillHeaderMapper.findById(vo.getShipbillId(), TmsShipbillHeaderModel.class);
			if (shipbillHeaderModel.getStatus().intValue() > DataDictionaryConstant.SHIPBILL_STATUS_WS.intValue()) {
				shipbillHeaderModels.add(shipbillHeaderModel);
			}
		} else {
			// 拆单主单,查找下面的所有子单
			Criteria criteriaShipbillHeader = Criteria.forClass(TmsShipbillHeaderModel.class);
			Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getOriginalShipbillId, vo.getShipbillId());
			Restriction r2 = Restrictions.eq(TmsShipbillHeaderModel::getEnterpriseId, user.getEnterpriseId());
			Restriction r3 = Restrictions.gt(TmsShipbillHeaderModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_WS);

			Restriction r4 = Restrictions.eq(TmsShipbillHeaderModel::getSplitOrderStatus, DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			Restriction r5 = Restrictions.eq(TmsShipbillHeaderModel::getCarrierShipbillStatus, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
			Restriction r6 = Restrictions.or(r4, r5);

			Restriction r7 = Restrictions.lt(TmsShipbillHeaderModel::getSplitOrderType, DataDictionaryConstant.SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			Restriction r8 = Restrictions.lt(TmsShipbillHeaderModel::getCarrierSplitOrderType, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			Restriction r9 = Restrictions.or(r7, r8);

			criteriaShipbillHeader.setRestriction(Restrictions.and(r1, r2, r3, r6, r9));
			criteriaShipbillHeader.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
			PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
			shipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteriaShipbillHeader);
		}
		for (TmsShipbillHeaderModel shipbillHeaderModel : shipbillHeaderModels) {
			AllShipbillPageDTO dto = new AllShipbillPageDTO();
			BeanUtils.copyProperties(shipbillHeaderModel, dto);

			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));

			dtos.add(dto);
		}
		Page<AllShipbillPageDTO> page = PageUtils.getPage(dtos);
		if (dtos instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillHeaderModel> p0 = (com.github.pagehelper.Page<TmsShipbillHeaderModel>) shipbillHeaderModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 备货等待信息查询
	 *
	 * @param
	 * @return
	 */
	@SuppressWarnings("resource")
	public Page<AllShipbillPageDTO> queryStockingWaitingShipbill(AllShipbillSellQueryVO vo) {
		ToolKits.isParamNull(vo, "发货单信息查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		List<AllShipbillPageDTO> dtos = Lists.newArrayList();
		List<TmsShipbillHeaderModel> shipbillHeaderModels = Lists.newArrayList();
		if (vo.getSplitOrderType().equals(DataDictionaryConstant.SPLIT_ORDER_TYPE_UN) && vo.getCarrierSplitOrderType().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN)) {
			// 没拆过单的运单
			PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
			TmsShipbillHeaderModel shipbillHeaderModel = tmsShipbillHeaderMapper.findById(vo.getShipbillId(), TmsShipbillHeaderModel.class);
			if (shipbillHeaderModel.getStatus().intValue() <= DataDictionaryConstant.SHIPBILL_STATUS_QY.intValue()) {
				shipbillHeaderModels.add(shipbillHeaderModel);
			}
		} else {
			// 拆单主单,查找下面的所有子单
			Criteria criteriaShipbillHeader = Criteria.forClass(TmsShipbillHeaderModel.class);
			Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getOriginalShipbillId, vo.getShipbillId());
			Restriction r2 = Restrictions.eq(TmsShipbillHeaderModel::getEnterpriseId, user.getEnterpriseId());
			Restriction r3 = Restrictions.le(TmsShipbillHeaderModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_QY);

			Restriction r4 = Restrictions.eq(TmsShipbillHeaderModel::getSplitOrderStatus, DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			Restriction r5 = Restrictions.eq(TmsShipbillHeaderModel::getCarrierShipbillStatus, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
			Restriction r6 = Restrictions.or(r4, r5);

			Restriction r7 = Restrictions.lt(TmsShipbillHeaderModel::getSplitOrderType, DataDictionaryConstant.SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			Restriction r8 = Restrictions.lt(TmsShipbillHeaderModel::getCarrierSplitOrderType, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_MASTER);
			Restriction r9 = Restrictions.or(r7, r8);

			Restriction r10 = Restrictions.ne(TmsShipbillHeaderModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_CLOSE);

			criteriaShipbillHeader.setRestriction(Restrictions.and(r1, r2, r3, r6, r9, r10));
			criteriaShipbillHeader.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
			PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
			shipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteriaShipbillHeader);
		}
		for (TmsShipbillHeaderModel shipbillHeaderModel : shipbillHeaderModels) {
			AllShipbillPageDTO dto = new AllShipbillPageDTO();
			BeanUtils.copyProperties(shipbillHeaderModel, dto);

			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));

			dtos.add(dto);
		}
		Page<AllShipbillPageDTO> page = PageUtils.getPage(dtos);
		if (dtos instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillHeaderModel> p0 = (com.github.pagehelper.Page<TmsShipbillHeaderModel>) shipbillHeaderModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 关闭信息查询
	 *
	 * @param
	 * @return
	 */
	public Page<AllShipbillPageDTO> queryClosingShipbill(AllShipbillSellQueryVO vo) {
		ToolKits.isParamNull(vo, "发货单信息查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, // 承运商拆单类型
				DataDictionaryConstant.SHIPBILL_CLOSE_TYPE// 运单关闭类型
		});
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<AllShipbillPageDTO> shipbillHeaderModels = tmsShipbillHeaderMapper.queryCloseShipbill(vo);
		for (AllShipbillPageDTO dto : shipbillHeaderModels) {

			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));

			dto.setCloseTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_CLOSE_TYPE, String.valueOf(dto.getCloseType())));
		}
		return PageUtils.getPage(shipbillHeaderModels);
	}

	/**
	 * 评价信息查询
	 *
	 * @param
	 * @return
	 */
	@SuppressWarnings("resource")
	public Page<ShipbillEvaluationDTO> queryShipbillEvaluation(AllShipbillSellQueryVO vo) {
		ToolKits.isParamNull(vo, "评价信息查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.EVALUATION_TARGET, // 运单状态
				DataDictionaryConstant.EVALUATION_PROBLEM_TYPE // 运输状态
		});
		Criteria criteria = Criteria.forClass(TmsShipbillEvaluationModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", vo.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsShipbillEvaluationModel> list = tmsShipbillEvaluationMapper.findByCriteria(criteria);

		List<ShipbillEvaluationDTO> evaluationDTOS = Lists.newArrayList();
		for (TmsShipbillEvaluationModel model : list) {
			ShipbillEvaluationDTO evaluationDTO = new ShipbillEvaluationDTO();
			BeanUtils.copyProperties(model, evaluationDTO);
			evaluationDTO.setEvaluationTargetName(ConvertorUtil.getName(dicData, DataDictionaryConstant.EVALUATION_TARGET, String.valueOf(model.getEvaluationTarget())));
			evaluationDTO.setProblemTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.EVALUATION_PROBLEM_TYPE, String.valueOf(model.getProblemType())));
			evaluationDTO.setEvaluationUserTypeName(UserTypeEnum.getByCode(model.getEvaluationUserType()).getDesc());
			evaluationDTOS.add(evaluationDTO);
		}
		Page<ShipbillEvaluationDTO> page = PageUtils.getPage(evaluationDTOS);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillEvaluationModel> p0 = (com.github.pagehelper.Page<TmsShipbillEvaluationModel>) list;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 关闭运单
	 *
	 * @param
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public void closeShipbill(ShipbillCloseVO shipbillCloseVO) {
		ToolKits.isParamNull(shipbillCloseVO, "关闭运单请求不为空！");
		ToolKits.isParamNull(shipbillCloseVO.getShipbillId(), "关闭运单ID请求不为空！");
		ToolKits.isParamNull(shipbillCloseVO.getCloseType(), "关闭类型不为空");

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

		// 当前时间
		Date date = DateUtil.date();
		// 修改运单状态为关闭
		TmsShipbillHeaderModel shipbillHeaderModel = tmsShipbillHeaderMapper.findById(shipbillCloseVO.getShipbillId(), TmsShipbillHeaderModel.class);

		if (ToolKits.isEmpty(shipbillHeaderModel)) {
			throw new PrologException("运单【" + shipbillCloseVO.getShipbillId() + "】不存在");
		}
		if (shipbillHeaderModel.getStatus().equals(DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK) && ToolKits.isNotEmpty(shipbillHeaderModel.getCarrierShipbillStatus())) {
			if (shipbillHeaderModel.getCarrierShipbillStatus().intValue() > DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED.intValue()) {
				throw new PrologException("运单【" + shipbillCloseVO.getShipbillId() + "】已调度，不能关闭");
			}
		} else if (shipbillHeaderModel.getStatus().intValue() > DataDictionaryConstant.SHIPBILL_STATUS_REVIEW.intValue()) {
			throw new PrologException("运单【" + shipbillCloseVO.getShipbillId() + "】已调度(或者已自提登记)，不能关闭");
		}
		// 先调整运单明细商品件数
		BigDecimal goodsTotalPcs = new BigDecimal(0);
		// 商品总体积
		BigDecimal goodsTotalVolume = new BigDecimal(0);
		// 商品总重量
		BigDecimal goodsTotalWeight = new BigDecimal(0);
		if (ToolKits.isNotEmpty(shipbillCloseVO.getDetails())) {
			Long sumCnt = shipbillCloseVO.getDetails().stream().mapToInt(s -> s.getPcs().intValue()).summaryStatistics().getSum();
			if (sumCnt == 0) {
				throw new PrologException("关闭件数全部是0，不需要关闭");
			}
			for (ShipbillDetailModifyVO shipbillDetailModifyVO : shipbillCloseVO.getDetails()) {
				ToolKits.isParamNull(shipbillDetailModifyVO.getItemId(), "商品ID不为空");
				ToolKits.isParamNull(shipbillDetailModifyVO.getItemCode(), "商品编码不为空");
				ToolKits.isParamNull(shipbillDetailModifyVO.getItemName(), "商品名称不为空");
				ToolKits.isParamNull(shipbillDetailModifyVO.getPcs(), "商品件数不为空");
				ToolKits.isParamNull(shipbillDetailModifyVO.getApplyPcs(), "商品关闭件数不为空");

				TmsShipbillDetailModel origShipbillDetal = tmsShipbillDetailMapper.findById(shipbillDetailModifyVO.getId(), TmsShipbillDetailModel.class);
				if (ToolKits.isEmpty(origShipbillDetal)) {
					throw new PrologException("运单【" + shipbillCloseVO.getShipbillId() + "】的商品" + shipbillDetailModifyVO.getItemName() + "不存在");
				}

				if (shipbillDetailModifyVO.getPcs().intValue() < shipbillDetailModifyVO.getApplyPcs().intValue()) {
					throw new PrologException("运单【" + shipbillCloseVO.getShipbillId() + "】的商品" + shipbillDetailModifyVO.getItemName() + "的关闭件数不能超过件数");
				}

				// 更新商品信息
				// 关闭件数
				if (ToolKits.isEmpty(origShipbillDetal.getClosingPcs()) || origShipbillDetal.getClosingPcs().intValue() == 0) {
					origShipbillDetal.setClosingPcs(shipbillDetailModifyVO.getApplyPcs());
				} else {
					origShipbillDetal.setClosingPcs(origShipbillDetal.getClosingPcs().add(shipbillDetailModifyVO.getApplyPcs()));
				}
				// 计算商品的体积，重量
				ItemCheckVO itemCheckVO = new ItemCheckVO();
				itemCheckVO.setItemId(origShipbillDetal.getItemId());
				RestMessage<ItemCheckDTO> itemRestMessage = maindataTmsResource.getItem(itemCheckVO);

				if (!itemRestMessage.isSuccess()) {
					throw new PrologException("获取商品服务失败");
				}
				if (ToolKits.isEmpty(itemRestMessage.getData())) {
					throw new PrologException("未找到对应商品[商品:" + origShipbillDetal.getItemName() + "]");
				}
				ItemCheckDTO itemCheckDTO = itemRestMessage.getData();
				// 剩余件数
				origShipbillDetal.setPcs(origShipbillDetal.getOrigPcs().subtract(origShipbillDetal.getClosingPcs()).subtract(origShipbillDetal.getDeliveryPcs()));
				origShipbillDetal.setCloseDt(date);
				origShipbillDetal.setCloseType(shipbillCloseVO.getCloseType());
				origShipbillDetal.setCloseReason(shipbillCloseVO.getCloseReason());
				origShipbillDetal.setCloseperson(user.getNickname());
				origShipbillDetal.setWeight(origShipbillDetal.getPcs().multiply(itemCheckDTO.getWeight()));
				origShipbillDetal.setVolume(origShipbillDetal.getPcs().multiply(itemCheckDTO.getVolume()));
				origShipbillDetal.setPlanDeliveryPcs(origShipbillDetal.getPcs());
				tmsShipbillDetailMapper.update(origShipbillDetal);

				// STD-DEVELOP20220823 lhb 计算件重体 20220909 start
				// 关闭的件重体的合计
				goodsTotalPcs = goodsTotalPcs.add(shipbillDetailModifyVO.getApplyPcs());
				goodsTotalVolume = goodsTotalPcs.multiply(itemCheckDTO.getVolume());
				goodsTotalWeight = goodsTotalPcs.multiply(itemCheckDTO.getWeight());
				// STD-DEVELOP20220823 lhb 计算件重体 20220909 end

				// 更新父级运单的关闭件数
				String fatherShipbillIdForDetail = shipbillHeaderModel.getShipbillId();
				while (ToolKits.isNotEmpty(fatherShipbillIdForDetail)) {
					fatherShipbillIdForDetail = updateFatherShipbillDetail(fatherShipbillIdForDetail, shipbillDetailModifyVO, date, user, itemCheckDTO);
				}
			}

			// 运单剩余的件重体
			shipbillHeaderModel.setTotalWeight(shipbillHeaderModel.getTotalWeight().subtract(goodsTotalWeight));
			shipbillHeaderModel.setTotalVolume(shipbillHeaderModel.getTotalVolume().subtract(goodsTotalVolume));
			shipbillHeaderModel.setTotalPcs(shipbillHeaderModel.getTotalPcs().subtract(goodsTotalPcs));

			shipbillHeaderModel.setActualTotalWeight(shipbillHeaderModel.getTotalWeight());
			shipbillHeaderModel.setActualTotalVolume(shipbillHeaderModel.getTotalVolume());
			shipbillHeaderModel.setActualTotalPcs(shipbillHeaderModel.getTotalPcs());
			if (shipbillHeaderModel.getTotalPcs().intValue() == 0) {
				// 整单关闭了
				shipbillHeaderModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_CLOSE);
			}
		} else {
			shipbillHeaderModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_CLOSE);
		}

		shipbillHeaderModel.setUpdatetime(date);
		shipbillHeaderModel.setUpdateperson(user.getNickname());
		tmsShipbillHeaderMapper.update(shipbillHeaderModel);

		// 运单操作日志保存
		ShipbillOperateLogSaveVO shipbillOperateLogSaveVO = new ShipbillOperateLogSaveVO();
		shipbillOperateLogSaveVO.setShipbillId(shipbillHeaderModel.getShipbillId());
		shipbillOperateLogSaveVO.setShipbillNo(shipbillHeaderModel.getShipbillNo());
		shipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_CLOSING);
		shipbillOperateLogSaveVO.setOldStatus(shipbillHeaderModel.getStatus());
		shipbillOperateLogSaveVO.setNewStatus(shipbillHeaderModel.getStatus());
		shipbillOperateLogService.addShipbillOperateLogData(shipbillOperateLogSaveVO);
	}
	// STD-DEVELOP20220823 lhb 销售单模式 20220919 end

	/**
	 * 运单高级查询
	 *
	 * @param
	 * @return
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<AllShipbillPageDTO> pageAdvanced(AllShipbillAdsQueryVO adsQueryVO) {
		ToolKits.isParamNull(adsQueryVO, "运单高级查询请求不为空");
		ToolKits.isParamNull(adsQueryVO.getPageNum(), "当前页码不为空");
		ToolKits.isParamNull(adsQueryVO.getPageSize(), "每页条数不为空");

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

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE// 订单业务类型
		});

		List<SignconfigPopDTO> data8 = Lists.newArrayList();
		// 标记配置
		SignconfigPopVO signconfigPopVO = new SignconfigPopVO();
		RestMessage<List<SignconfigPopDTO>> restMessage8 = maindataTmsResource.getSignConfig(signconfigPopVO);
		if (!restMessage8.isSuccess()) {
			throw new RuntimeException("标记配置查询失败!");
		} else {
			data8 = restMessage8.getData();
		}
		// 获取高级查询配置
		String conditions = null;
		RestMessage<String> restMessageAdvancedCfg = maindataTmsResource.getQueryConfiguration(DataDictionaryConstant.BIZ_TYPE_ALL);
		if (!restMessageAdvancedCfg.isSuccess()) {
			// throw new RuntimeException("订单类型翻译失败!");
		} else {
			conditions = restMessageAdvancedCfg.getData();
		}
		adsQueryVO.setSql(conditions);
		adsQueryVO.setEnterpriseId(user.getEnterpriseId());

		PageUtils.startPage(adsQueryVO.getPageNum(), adsQueryVO.getPageSize());
		List<AllShipbillPageDTO> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.allShipbillAdvancedQuery(adsQueryVO);
		List<AllShipbillPageDTO> allShipbillPageDTOS = Lists.newArrayList();
		for (AllShipbillPageDTO tmsShipbillHeaderModel : tmsShipbillHeaderModels) {
			AllShipbillPageDTO allShipbillPageDTO = new AllShipbillPageDTO();
			BeanUtils.copyProperties(tmsShipbillHeaderModel, allShipbillPageDTO);
			// 收货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(tmsShipbillHeaderModel.getConsigneeLongitude()) && ToolKits.isNotEmpty(tmsShipbillHeaderModel.getConsigneeLatitude())) {
				allShipbillPageDTO.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
			} else {
				allShipbillPageDTO.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
			}
			// 发货方位置(经纬度判断)
			if (ToolKits.isNotEmpty(tmsShipbillHeaderModel.getShipperLongitude()) && ToolKits.isNotEmpty(tmsShipbillHeaderModel.getShipperLatitude())) {
				allShipbillPageDTO.setShipperPositioning(StatusConstant.POSITIONING_YDW);
			} else {
				allShipbillPageDTO.setShipperPositioning(StatusConstant.POSITIONING_WDW);
			}

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

			// 字典翻译
			allShipbillPageDTO.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(allShipbillPageDTO.getStatus())));
			allShipbillPageDTO.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(allShipbillPageDTO.getTransportStatus())));
			allShipbillPageDTO.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(allShipbillPageDTO.getAbnormalStatus())));
			allShipbillPageDTO.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(allShipbillPageDTO.getScanStatus())));
			allShipbillPageDTO.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(allShipbillPageDTO.getCarrierType())));
			allShipbillPageDTO.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(allShipbillPageDTO.getOrderType())));
			allShipbillPageDTO.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(allShipbillPageDTO.getOrderBusinessType())));
			allShipbillPageDTO.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(allShipbillPageDTO.getDeliveryMethodType())));
			allShipbillPageDTO.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(allShipbillPageDTO.getTransportType())));
			allShipbillPageDTO.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(allShipbillPageDTO.getTemperatureRequirement())));
			allShipbillPageDTO.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(allShipbillPageDTO.getShipperType())));
			allShipbillPageDTO.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(allShipbillPageDTO.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(allShipbillPageDTO.getOrderTypeName()) ? allShipbillPageDTO.getOrderTypeName() : "")) {
							allShipbillPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							allShipbillPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());

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

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

							allShipbillPageDTO.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(allShipbillPageDTO.getDeliveryMethodTypeName()) ? allShipbillPageDTO.getDeliveryMethodTypeName() : "")) {
							allShipbillPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							allShipbillPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());
							allShipbillPageDTO.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(allShipbillPageDTO.getMemo()) ? allShipbillPageDTO.getMemo() : "")) {
							allShipbillPageDTO.setBackground(ToolKits.isEmpty(signconfigPopDTO.getBackground()) ? "" : signconfigPopDTO.getBackground());
							allShipbillPageDTO.setColor(ToolKits.isEmpty(signconfigPopDTO.getColor()) ? "" : signconfigPopDTO.getColor());
							allShipbillPageDTO.setSignConfigId(signconfigPopDTO.getId());
							break Signconfig;
						}
					}
					break;
				}
			}
			allShipbillPageDTOS.add(allShipbillPageDTO);
		}

		// 分页重组
		Page<AllShipbillPageDTO> page = PageUtils.getPage(allShipbillPageDTOS);
		if (tmsShipbillHeaderModels instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<AllShipbillPageDTO> p0 = (com.github.pagehelper.Page<AllShipbillPageDTO>) tmsShipbillHeaderModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 运单商品查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<AllShipbillItemDTO> listItemDetailData(AllShipbillItemQueryVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.PACK_TYPE, // 包装类型
				DataDictionaryConstant.CARGO_THERMOSPHERE// 货物温存
		});

		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", vo.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsShipbillDetailModel> list = tmsShipbillDetailMapper.findByCriteria(criteria);

		List<AllShipbillItemDTO> dtos = new ArrayList<AllShipbillItemDTO>();
		for (TmsShipbillDetailModel model : list) {
			AllShipbillItemDTO dto = new AllShipbillItemDTO();
			BeanUtils.copyProperties(model, dto);
			dto.setPackTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.PACK_TYPE, String.valueOf(dto.getPackType())));
			dto.setCargoThermosphereName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARGO_THERMOSPHERE, String.valueOf(dto.getCargoThermosphere())));
			dtos.add(dto);
		}

		Page<AllShipbillItemDTO> page = PageUtils.getPage(dtos);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillDetailModel> p0 = (com.github.pagehelper.Page<TmsShipbillDetailModel>) list;
			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;
	}

	/**
	 * 运单包裹查询
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<AllShipbillPackageDTO> listPackageData(AllShipbillPackageQueryVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.PACKAGE_TYPE, // 包裹类型
				DataDictionaryConstant.DEVICE_COMPANY// 设备生产厂商
		});

		Criteria criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", vo.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<TmsShipbillPackageModel> list = tmsShipbillPackageMapper.findByCriteria(criteria);
		List<AllShipbillPackageDTO> dtos = Lists.newArrayList();

		for (TmsShipbillPackageModel model : list) {
			AllShipbillPackageDTO dto = new AllShipbillPackageDTO();
			BeanUtils.copyProperties(model, dto);
			dto.setPackageTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.PACKAGE_TYPE, String.valueOf(dto.getPackageType())));
			dto.setDeviceCompanyName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DEVICE_COMPANY, String.valueOf(dto.getDeviceCompany())));
			dtos.add(dto);
		}

		Page<AllShipbillPackageDTO> page = PageUtils.getPage(dtos);
		if (list instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillPackageModel> p0 = (com.github.pagehelper.Page<TmsShipbillPackageModel>) list;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 异常登记
	 *
	 * @param
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void abnormalRegister(AbnormalRegisterVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(vo.getIds(), "请选择要操作的数据");

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

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

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

		StringBuffer nodeEvent = new StringBuffer(DataDictionaryConstant.SHIPBILL_NODE_ABNORMAL_REGISTER);
		nodeEvent.append("异常类型为:");
		nodeEvent.append(PublicMethods.change(data1, String.valueOf(vo.getAbnormalType())));
		nodeEvent.append(",异常发生时间为:");
		nodeEvent.append(DateUtil.formatDateTime(vo.getAbnormalTime()));
		nodeEvent.append(",异常事件为:");
		nodeEvent.append(PublicMethods.change(data2, String.valueOf(vo.getAbnormalCase())));
		nodeEvent.append(",客户是否投诉为:");
		nodeEvent.append(ObjectUtil.equal(1, vo.getCustomerComplain()) ? "是" : "否");
		if (ToolKits.isNotEmpty(vo.getContent())) {
			nodeEvent.append(",异常内容为:");
			nodeEvent.append(vo.getContent());
		}

		for (String shipbillId : vo.getIds()) {
			// 更新运单异常状态为【异常】
			TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillHeader)) {
				tmsShipbillHeader.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS);
				tmsShipbillHeader.setUpdateperson(user.getNickname());
				tmsShipbillHeader.setUpdatetime(DateUtil.date());
				tmsShipbillHeaderMapper.update(tmsShipbillHeader);
			}

			// 更新运单运输记录
			Map<String, Object> queryMap = Maps.newHashMap();
			queryMap.put("shipbillId", shipbillId);
			List<TmsShipbillTransLogModel> tmsShipbillTransportLogs = tmsShipbillTransportLogMapper.findByMap(queryMap, TmsShipbillTransLogModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillTransportLogs) && tmsShipbillTransportLogs.size() > 0) {
				TmsShipbillTransLogModel tmsShipbillTransportLog = tmsShipbillTransportLogs.get(0);
				tmsShipbillTransportLog.setAbnormalTime(vo.getAbnormalTime());
				tmsShipbillTransportLog.setAbnormalType(vo.getAbnormalType());
				tmsShipbillTransportLog.setAbnormalEvent(vo.getAbnormalCase());
				tmsShipbillTransportLog.setAbnormalComplain(vo.getCustomerComplain());
				tmsShipbillTransportLog.setAbnormalContent(vo.getContent());
				tmsShipbillTransportLogMapper.update(tmsShipbillTransportLog);
			}

			// 运单节点保存
			ShipbillNodeTrackingSaveNewVO nodeTrackingVO = new ShipbillNodeTrackingSaveNewVO();
			nodeTrackingVO.setNodeDate(DateUtil.date());
			nodeTrackingVO.setShipbillId(tmsShipbillHeader.getShipbillId());
			nodeTrackingVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
			nodeTrackingVO.setStatus(tmsShipbillHeader.getStatus());
			nodeTrackingVO.setNodeEvent(nodeEvent.toString());
			nodeTrackingVO.setUserId(user.getUserid());
			nodeTrackingVO.setUserName(user.getNickname());
			nodeTrackingVO.setEnterpriseId(user.getEnterpriseId());
			nodeTrackingVO.setEnterpriseName(user.getEnterpriseName());
			nodeTrackingVO.setPicUrl(vo.getPictureUrl());
			shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingVO);

			// 运单日志保存
			ShipbillOperateLogSaveNewVO operateLogVO = new ShipbillOperateLogSaveNewVO();
			operateLogVO.setShipbillId(tmsShipbillHeader.getShipbillId());
			operateLogVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
			operateLogVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YCDJ);
			operateLogVO.setOldStatus(tmsShipbillHeader.getStatus());
			operateLogVO.setNewStatus(tmsShipbillHeader.getStatus());
			operateLogVO.setOperateDate(DateUtil.date());
			operateLogVO.setEnterpriseId(user.getEnterpriseId());
			operateLogVO.setEnterpriseName(user.getEnterpriseName());
			operateLogVO.setUserId(user.getUserid());
			operateLogVO.setUserName(user.getNickname());
			shipbillOperateLogService.addShipbillOperateLogNewData(operateLogVO);

			// 如果有对应装车单明细,也要更新异常状态为【异常】
			Map<String, Object> queryMap2 = Maps.newHashMap();
			queryMap2.put("shipbillId", shipbillId);
			List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByMap(queryMap2, TmsLoadingBillDetailModel.class);
			if (ToolKits.isNotEmpty(tmsLoadingBillDetails) && tmsLoadingBillDetails.size() > 0) {
				for (TmsLoadingBillDetailModel tmsLoadingBillDetail : tmsLoadingBillDetails) {
					tmsLoadingBillDetail.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS);
					tmsLoadingBillDetail.setUpdateperson(user.getNickname());
					tmsLoadingBillDetail.setUpdatetime(DateUtil.date());
					tmsLoadingBillDetailMapper.update(tmsLoadingBillDetail);
				}
			}

			// STD-DEVELOP20220823 lhb 异常登记 20220914 start
			// 获取运单异常表ID
			RestMessage<String> restMessageDetailId = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_ABNORMAL_ID, "L");
			if (!restMessageDetailId.isSuccess()) {
				throw new RuntimeException("生成运单异常表ID失败:" + restMessageDetailId.getMessage());
			}
			TmsShipbillAbnormalModel abnormalModel = new TmsShipbillAbnormalModel();
			abnormalModel.setId(restMessageDetailId.getData());
			abnormalModel.setShipbillId(shipbillId);
			abnormalModel.setShipbillNo(shipbillId);
			abnormalModel.setAbnormalTime(vo.getAbnormalTime());
			abnormalModel.setAbnormalType(vo.getAbnormalType());
			abnormalModel.setAbnormalEvent(vo.getAbnormalCase());
			abnormalModel.setAbnormalComplain(vo.getCustomerComplain());
			abnormalModel.setAbnormalContent(vo.getContent());
			abnormalModel.setAbnormalPhoto(vo.getPictureUrl());
			abnormalModel.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS);
			abnormalModel.setEnterpriseId(user.getEnterpriseId());
			abnormalModel.setEnterpriseName(user.getEnterpriseName());
			abnormalModel.setCreateperson(user.getNickname());
			abnormalModel.setCreatetime(DateUtil.date());
			tmsShipbillAbnormalMapper.save(abnormalModel);
			// STD-DEVELOP20220823 lhb 异常登记 20220914 end

			// 递归对上级运单进行异常登记
			String fatherShipbillId = tmsShipbillHeader.getFatherShipbillId();
			while (ToolKits.isNotEmpty(fatherShipbillId)) {
				fatherShipbillId = abnormalRegisterFather(fatherShipbillId, nodeTrackingVO, operateLogVO, vo, user);
			}
		}
	}

	/**
	 * 递归对上级运单进行异常登记
	 */
	private String abnormalRegisterFather(String shipbillId, ShipbillNodeTrackingSaveNewVO nodeTrackingVO, ShipbillOperateLogSaveNewVO operateLogVO, AbnormalRegisterVO vo, UserEntity user) {
		TmsShipbillHeaderModel fatherShipbill = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
		if (ToolKits.isNotEmpty(fatherShipbill.getFatherShipbillId()) && !fatherShipbill.getFatherShipbillId().equals(shipbillId)) {
			// 更新运单异常状态为【异常】
			fatherShipbill.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS);
			fatherShipbill.setUpdateperson(nodeTrackingVO.getUserName());
			fatherShipbill.setUpdatetime(DateUtil.date());
			tmsShipbillHeaderMapper.update(fatherShipbill);

			// 更新运单运输记录
			Map<String, Object> queryMap = Maps.newHashMap();
			queryMap.put("shipbillId", fatherShipbill.getShipbillId());
			List<TmsShipbillTransLogModel> tmsShipbillTransportLogs = tmsShipbillTransportLogMapper.findByMap(queryMap, TmsShipbillTransLogModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillTransportLogs) && tmsShipbillTransportLogs.size() > 0) {
				TmsShipbillTransLogModel tmsShipbillTransportLog = tmsShipbillTransportLogs.get(0);
				tmsShipbillTransportLog.setAbnormalTime(vo.getAbnormalTime());
				tmsShipbillTransportLog.setAbnormalType(vo.getAbnormalType());
				tmsShipbillTransportLog.setAbnormalEvent(vo.getAbnormalCase());
				tmsShipbillTransportLog.setAbnormalComplain(vo.getCustomerComplain());
				tmsShipbillTransportLog.setAbnormalContent(vo.getContent());
				tmsShipbillTransportLogMapper.update(tmsShipbillTransportLog);
			}

			// 运单节点保存
			nodeTrackingVO.setNodeDate(DateUtil.date());
			nodeTrackingVO.setShipbillId(fatherShipbill.getShipbillId());
			nodeTrackingVO.setShipbillNo(fatherShipbill.getShipbillId());
			shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingVO);

			// 运单日志保存
			operateLogVO.setOperateDate(DateUtil.date());
			operateLogVO.setShipbillId(fatherShipbill.getShipbillId());
			operateLogVO.setShipbillNo(fatherShipbill.getShipbillId());
			shipbillOperateLogService.addShipbillOperateLogNewData(operateLogVO);

			// STD-DEVELOP20220823 lhb 异常登记 20220914 start
			// 获取运单异常表ID
			RestMessage<String> restMessageDetailId = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_ABNORMAL_ID, "L");
			if (!restMessageDetailId.isSuccess()) {
				throw new RuntimeException("生成运单异常表ID失败:" + restMessageDetailId.getMessage());
			}
			TmsShipbillAbnormalModel abnormalModel = new TmsShipbillAbnormalModel();
			abnormalModel.setId(restMessageDetailId.getData());
			abnormalModel.setShipbillId(fatherShipbill.getShipbillId());
			abnormalModel.setShipbillNo(fatherShipbill.getShipbillId());
			abnormalModel.setAbnormalTime(vo.getAbnormalTime());
			abnormalModel.setAbnormalType(vo.getAbnormalType());
			abnormalModel.setAbnormalEvent(vo.getAbnormalCase());
			abnormalModel.setAbnormalComplain(vo.getCustomerComplain());
			abnormalModel.setAbnormalContent(vo.getContent());
			abnormalModel.setAbnormalPhoto(vo.getPictureUrl());
			abnormalModel.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS);
			abnormalModel.setEnterpriseId(user.getEnterpriseId());
			abnormalModel.setEnterpriseName(user.getEnterpriseName());
			abnormalModel.setCreateperson(user.getNickname());
			abnormalModel.setCreatetime(DateUtil.date());
			tmsShipbillAbnormalMapper.save(abnormalModel);
			// STD-DEVELOP20220823 lhb 异常登记 20220914 end

			return fatherShipbill.getFatherShipbillId();
		}
		return null;
	}

	/**
	 * 客服反馈
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void feedBack(FeedBackVO vo) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(vo.getIds(), "请选择要操作的数据");

		StringBuffer nodeEvent = new StringBuffer(DataDictionaryConstant.SHIPBILL_NODE_FEEDBACK);
		nodeEvent.append(":反馈时间为:");
		nodeEvent.append(vo.getFeedbackDate());
		if (ToolKits.isNotEmpty(vo.getAddress())) {
			nodeEvent.append(",实时地点为:");
			nodeEvent.append(vo.getAddress());
		}
		nodeEvent.append(",反馈内容为:");
		nodeEvent.append(vo.getFeedbackContent());

		for (String shipbillId : vo.getIds()) {
			TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillHeader)) {
				// 更新运单运输记录
				Map<String, Object> queryMap = Maps.newHashMap();
				queryMap.put("shipbillId", shipbillId);
				List<TmsShipbillTransLogModel> tmsShipbillTransportLogs = tmsShipbillTransportLogMapper.findByMap(queryMap, TmsShipbillTransLogModel.class);
				if (ToolKits.isNotEmpty(tmsShipbillTransportLogs) && tmsShipbillTransportLogs.size() > 0) {
					TmsShipbillTransLogModel tmsShipbillTransportLog = tmsShipbillTransportLogs.get(0);
					tmsShipbillTransportLog.setFeedBackTime(vo.getFeedbackDate());
					tmsShipbillTransportLog.setFeedBackPlace(vo.getAddress());
					tmsShipbillTransportLog.setFeedBackContent(vo.getFeedbackContent());
					tmsShipbillTransportLog.setUpdateperson(user.getNickname());
					tmsShipbillTransportLog.setUpdatetime(DateUtil.date());
					tmsShipbillTransportLogMapper.update(tmsShipbillTransportLog);
				}

				// 运单节点保存
				ShipbillNodeTrackingSaveNewVO nodeTrackingVO = new ShipbillNodeTrackingSaveNewVO();
				nodeTrackingVO.setNodeDate(DateUtil.date());
				nodeTrackingVO.setShipbillId(tmsShipbillHeader.getShipbillId());
				nodeTrackingVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
				nodeTrackingVO.setStatus(tmsShipbillHeader.getStatus());
				nodeTrackingVO.setNodeEvent(nodeEvent.toString());
				nodeTrackingVO.setUserId(user.getUserid());
				nodeTrackingVO.setUserName(user.getNickname());
				nodeTrackingVO.setEnterpriseId(user.getEnterpriseId());
				nodeTrackingVO.setEnterpriseName(user.getEnterpriseName());
				nodeTrackingVO.setPicUrl(vo.getFeedbackPicture());
				shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingVO);

				// 运单日志保存
				ShipbillOperateLogSaveNewVO operateLogVO = new ShipbillOperateLogSaveNewVO();
				operateLogVO.setShipbillId(tmsShipbillHeader.getShipbillId());
				operateLogVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
				operateLogVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_KFFK);
				operateLogVO.setOldStatus(tmsShipbillHeader.getStatus());
				operateLogVO.setNewStatus(tmsShipbillHeader.getStatus());
				operateLogVO.setOperateDate(DateUtil.date());
				operateLogVO.setEnterpriseId(user.getEnterpriseId());
				operateLogVO.setEnterpriseName(user.getEnterpriseName());
				operateLogVO.setUserId(user.getUserid());
				operateLogVO.setUserName(user.getNickname());
				shipbillOperateLogService.addShipbillOperateLogNewData(operateLogVO);

				// 递归对上级运单进行客服反馈
				String fatherShipbillId = tmsShipbillHeader.getFatherShipbillId();
				while (ToolKits.isNotEmpty(fatherShipbillId)) {
					fatherShipbillId = feedBackFather(fatherShipbillId, nodeTrackingVO, operateLogVO, vo);
				}
			}
		}
	}

	/**
	 * 递归对上级运单进行客服反馈
	 */
	private String feedBackFather(String shipbillId, ShipbillNodeTrackingSaveNewVO nodeTrackingVO, ShipbillOperateLogSaveNewVO operateLogVO, FeedBackVO vo) {
		TmsShipbillHeaderModel fatherShipbill = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
		if (ToolKits.isNotEmpty(fatherShipbill.getFatherShipbillId()) && !fatherShipbill.getFatherShipbillId().equals(shipbillId)) {
			// 更新运单运输记录
			Map<String, Object> queryMap = Maps.newHashMap();
			queryMap.put("shipbillId", shipbillId);
			List<TmsShipbillTransLogModel> tmsShipbillTransportLogs = tmsShipbillTransportLogMapper.findByMap(queryMap, TmsShipbillTransLogModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillTransportLogs) && tmsShipbillTransportLogs.size() > 0) {
				TmsShipbillTransLogModel tmsShipbillTransportLog = tmsShipbillTransportLogs.get(0);
				tmsShipbillTransportLog.setFeedBackTime(vo.getFeedbackDate());
				tmsShipbillTransportLog.setFeedBackPlace(vo.getAddress());
				tmsShipbillTransportLog.setFeedBackContent(vo.getFeedbackContent());
				tmsShipbillTransportLog.setUpdateperson(nodeTrackingVO.getUserName());
				tmsShipbillTransportLog.setUpdatetime(DateUtil.date());
				tmsShipbillTransportLogMapper.update(tmsShipbillTransportLog);
			}

			// 运单节点保存
			nodeTrackingVO.setNodeDate(DateUtil.date());
			nodeTrackingVO.setShipbillId(shipbillId);
			nodeTrackingVO.setShipbillNo(shipbillId);
			shipbillNodeTrackingService.addShipbillNodeTrackingNewData(nodeTrackingVO);

			// 运单日志保存
			operateLogVO.setOperateDate(DateUtil.date());
			operateLogVO.setShipbillId(shipbillId);
			operateLogVO.setShipbillNo(shipbillId);
			shipbillOperateLogService.addShipbillOperateLogNewData(operateLogVO);

			return fatherShipbill.getFatherShipbillId();
		}
		return null;
	}

	/**
	 * 运单修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(AllShipbillUpdateVO vo) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		if (ToolKits.isNotEmpty(vo.getItems()) && vo.getItems().size() > 0) {
			for (AllShipbillItemVO allShipbillItemVO : vo.getItems()) {
				TmsShipbillDetailModel tmsShipbillDetail = tmsShipbillDetailMapper.findById(allShipbillItemVO.getId(), TmsShipbillDetailModel.class);
				if (ToolKits.isNotEmpty(tmsShipbillDetail)) {
					BeanUtils.copyProperties(allShipbillItemVO, tmsShipbillDetail);
					tmsShipbillDetail.setUpdateperson(user.getNickname());
					tmsShipbillDetail.setUpdatetime(DateUtil.date());
					tmsShipbillDetailMapper.update(tmsShipbillDetail);
				}
			}
		}

		if (ToolKits.isNotEmpty(vo.getPacks()) && vo.getPacks().size() > 0) {
			for (AllShipbillPackageVO allShipbillPackageVO : vo.getPacks()) {
				TmsShipbillPackageModel tmsShipbillPackage = tmsShipbillPackageMapper.findById(allShipbillPackageVO.getPackageId(), TmsShipbillPackageModel.class);
				if (ToolKits.isNotEmpty(tmsShipbillPackage)) {
					BeanUtils.copyProperties(allShipbillPackageVO, tmsShipbillPackage);
					tmsShipbillPackage.setUpdateperson(user.getNickname());
					tmsShipbillPackage.setUpdatetime(DateUtil.date());
					tmsShipbillPackageMapper.update(tmsShipbillPackage);
				}
			}
		}
	}

	/**
	 * 所有运单导出
	 */
	@Override
	public List<AllShipbillExportDTO> export(AllShipbillQueryVO vo) throws Exception {
		ToolKits.isParamNull(vo, "运单分页查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setUserType(user.getUserType());

		if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_ZY)) {// 自有账号
			vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? null : user.getDepts().stream().toArray(String[]::new));
			if (BussinessConstant.USER_DEPARTMENT_AUTH_Y.equals(user.getOwnedAllDataAuthFlag())) {// 部门权限
				vo.setIsDept("YES");
			} else {// 业务员权限
				vo.setIsDept("NO");
				// 货主
				vo.setConsignors(ToolKits.isEmpty(user.getConsignors()) ? null : user.getConsignors().stream().toArray(Long[]::new));
				// 客户
				vo.setCustomers(ToolKits.isEmpty(user.getCustomers()) ? null : user.getCustomers().stream().toArray(Long[]::new));
				// 供应商
				vo.setSuppliers(ToolKits.isEmpty(user.getSuppliers()) ? null : user.getSuppliers().stream().toArray(Long[]::new));
				// 仓库
				vo.setWsarehouses(ToolKits.isEmpty(user.getWarehouses()) ? null : user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {// 承运商账号
			vo.setCarrierId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主
			Long[] consignor = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setConsignors(consignor);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setSuppliers(userId);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCustomers(userId);
		}
		List<AllShipbillPageDTO> dtos = tmsShipbillHeaderMapper.queryAllShipbill(vo);
		List<AllShipbillExportDTO> exportDTOs = Lists.newArrayList();
		for (AllShipbillPageDTO dto : dtos) {
			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));

			AllShipbillExportDTO exportDTO = new AllShipbillExportDTO();
			BeanUtils.copyProperties(dto, exportDTO);
			exportDTOs.add(exportDTO);
		}
		return exportDTOs;
	}

	/**
	 * 所有运单分页导出
	 */
	@SuppressWarnings("resource")
	@Override
	public List<AllShipbillExportDTO> exportPage(AllShipbillQueryVO vo) throws Exception {
		ToolKits.isParamNull(vo, "运单分页查询请求不为空");
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

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

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setUserType(user.getUserType());

		if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_ZY)) {// 自有账号
			vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? null : user.getDepts().stream().toArray(String[]::new));
			if (BussinessConstant.USER_DEPARTMENT_AUTH_Y.equals(user.getOwnedAllDataAuthFlag())) {// 部门权限
				vo.setIsDept("YES");
			} else {// 业务员权限
				vo.setIsDept("NO");
				// 货主
				vo.setConsignors(ToolKits.isEmpty(user.getConsignors()) ? null : user.getConsignors().stream().toArray(Long[]::new));
				// 客户
				vo.setCustomers(ToolKits.isEmpty(user.getCustomers()) ? null : user.getCustomers().stream().toArray(Long[]::new));
				// 供应商
				vo.setSuppliers(ToolKits.isEmpty(user.getSuppliers()) ? null : user.getSuppliers().stream().toArray(Long[]::new));
				// 仓库
				vo.setWsarehouses(ToolKits.isEmpty(user.getWarehouses()) ? null : user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {// 承运商账号
			vo.setCarrierId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主
			Long[] consignor = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setConsignors(consignor);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setSuppliers(userId);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCustomers(userId);
		}

		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<AllShipbillPageDTO> dtos = tmsShipbillHeaderMapper.queryAllShipbill(vo);
		List<AllShipbillExportDTO> exportDTOs = Lists.newArrayList();
		for (AllShipbillPageDTO dto : dtos) {
			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));

			AllShipbillExportDTO exportDTO = new AllShipbillExportDTO();
			BeanUtils.copyProperties(dto, exportDTO);
			exportDTOs.add(exportDTO);
		}

		Page<AllShipbillExportDTO> page = PageUtils.getPage(exportDTOs);
		if (dtos instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<AllShipbillPageDTO> p0 = (com.github.pagehelper.Page<AllShipbillPageDTO>) dtos;
			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();
	}

	/**
	 * 所有运单选择行导出
	 */
	@Override
	public List<AllShipbillExportDTO> exportSelected(List<String> ids) throws Exception {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		AllShipbillQueryVO vo = new AllShipbillQueryVO();
		if (ObjectUtil.notEqual(user.getUserType(), BussinessConstant.USER_TYPE_ZY) && ObjectUtil.notEqual(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {
			ToolKits.isParamNull(user, "当前人员没有操作权限");
		}

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_TYPE, // 收货方类型
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, // 承运商拆单状态
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		vo.setEnterpriseId(user.getEnterpriseId());
		vo.setUserType(user.getUserType());

		if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_ZY)) {// 自有账号
			vo.setDepts(ToolKits.isEmpty(user.getDepts()) ? null : user.getDepts().stream().toArray(String[]::new));
			if (BussinessConstant.USER_DEPARTMENT_AUTH_Y.equals(user.getOwnedAllDataAuthFlag())) {// 部门权限
				vo.setIsDept("YES");
			} else {// 业务员权限
				vo.setIsDept("NO");
				// 货主
				vo.setConsignors(ToolKits.isEmpty(user.getConsignors()) ? null : user.getConsignors().stream().toArray(Long[]::new));
				// 客户
				vo.setCustomers(ToolKits.isEmpty(user.getCustomers()) ? null : user.getCustomers().stream().toArray(Long[]::new));
				// 供应商
				vo.setSuppliers(ToolKits.isEmpty(user.getSuppliers()) ? null : user.getSuppliers().stream().toArray(Long[]::new));
				// 仓库
				vo.setWsarehouses(ToolKits.isEmpty(user.getWarehouses()) ? null : user.getWarehouses().stream().toArray(Long[]::new));
			}
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {// 承运商账号
			vo.setCarrierId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_HZ)) {
			// 货主
			Long[] consignor = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setConsignors(consignor);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setSuppliers(userId);
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			Long[] userId = { Long.valueOf(user.getLoginUserCompanyInfo().getId()) };
			vo.setCustomers(userId);
		}

		vo.setIds(ids.stream().toArray(String[]::new));
		List<AllShipbillPageDTO> dtos = tmsShipbillHeaderMapper.queryAllShipbill(vo);
		List<AllShipbillExportDTO> exportDTOs = Lists.newArrayList();
		for (AllShipbillPageDTO dto : dtos) {
			StringBuffer consigneeProvinceCityDistrict = new StringBuffer();
			// 目的地
			consigneeProvinceCityDistrict.append(dto.getConsigneeProvinceName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeCityName());
			consigneeProvinceCityDistrict.append(dto.getConsigneeDistrictName());
			dto.setConsigneeProvinceCityDistrict(consigneeProvinceCityDistrict.toString());

			// 始发地
			StringBuffer shipperProvinceCityDistrict = new StringBuffer();
			shipperProvinceCityDistrict.append(dto.getShipperProvinceName());
			shipperProvinceCityDistrict.append(dto.getShipperCityName());
			shipperProvinceCityDistrict.append(dto.getShipperDistrictName());
			dto.setShipperProvinceCityDistrict(shipperProvinceCityDistrict.toString());

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

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

			if (ObjectUtil.equal(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ, dto.getCarrierType())) {// 承运方类型为司机时展示司机姓名
				dto.setCarrierName(dto.getDriverName());
			}

			// 字典翻译
			dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
			dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
			dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
			dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
			dto.setCarrierTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LINE_CARRIER_TYPE, String.valueOf(dto.getCarrierType())));
			dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
			dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderBusinessType())));
			dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
			dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
			dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
			dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
			dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
			dto.setCarrierSplitOrderStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS, String.valueOf(dto.getCarrierSplitOrderStatus())));
			dto.setCarrierSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(dto.getCarrierSplitOrderType())));

			AllShipbillExportDTO exportDTO = new AllShipbillExportDTO();
			BeanUtils.copyProperties(dto, exportDTO);
			exportDTOs.add(exportDTO);
		}

		return exportDTOs;
	}

	// STD-DEVELOP20220823 lhb 运单件重体调整 20220913 start

	/**
	 * 运单修改保存
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void shipbillUpdate(ShipbillAdjustVO adjustVO) {
		ToolKits.isParamNull(adjustVO, "运单修改保存请求不为空！");
		ToolKits.isParamNull(adjustVO.getShipbillId(), "订单ID不为空");
		ToolKits.isParamNull(adjustVO.getDetails(), "商品信息不为空");

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

		// 当前时间
		Date date = DateUtil.date();
		TmsShipbillHeaderModel model = tmsShipbillHeaderMapper.findById(adjustVO.getShipbillId(), TmsShipbillHeaderModel.class);

		if (ToolKits.isEmpty(model)) {
			throw new PrologException("运单【" + adjustVO.getShipbillId() + "】不存在！");
		}
		if (ToolKits.isNotEmpty(model.getTransportStatus()) && !model.getTransportStatus().equals(DataDictionaryConstant.TRANSPORT_STATUS_PENDING_SHIP)) {
			throw new PrologException("运单【" + model.getShipbillNo() + "】已发货，不支持修改！");
		}
		if (model.getSplitOrderStatus().equals(DataDictionaryConstant.SPLIT_ORDER_STATUS_ED) || model.getCarrierSplitOrderStatus().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_ED)) {
			throw new PrologException("不支持拆单主单修改！");
		}
		if (model.getSplitOrderType().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_SUB) || model.getCarrierSplitOrderType().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_TRANSIT_SUB)) {
			throw new PrologException("不支持拆段子单修改！");
		}
		if (ToolKits.isNotEmpty(adjustVO.getDetails())) {
			updateShipbillDetail(model, adjustVO.getDetails(), user, date);
		}
		// 修改运单的扩展字段
		model.setExtStr1(adjustVO.getExtStr1());
		model.setExtStr2(adjustVO.getExtStr2());
		model.setExtStr3(adjustVO.getExtStr3());
		model.setExtStr4(adjustVO.getExtStr4());
		model.setExtStr5(adjustVO.getExtStr5());
		model.setExtStr6(adjustVO.getExtStr6());
		model.setExtStr7(adjustVO.getExtStr7());
		model.setExtStr8(adjustVO.getExtStr8());
		model.setExtStr9(adjustVO.getExtStr9());
		model.setExtStr10(adjustVO.getExtStr10());
		model.setExtDate1(adjustVO.getExtDate1());
		model.setExtDate2(adjustVO.getExtDate2());
		model.setExtDate3(adjustVO.getExtDate3());
		model.setExtDate4(adjustVO.getExtDate4());
		model.setExtDate5(adjustVO.getExtDate5());
		model.setExtNum1(adjustVO.getExtNum1());
		model.setExtNum2(adjustVO.getExtNum2());
		model.setExtNum3(adjustVO.getExtNum3());
		model.setExtNum4(adjustVO.getExtNum4());
		model.setExtNum5(adjustVO.getExtNum5());
		tmsShipbillHeaderMapper.update(model);

		// 运单操作日志保存
		ShipbillOperateLogSaveVO shipbillOperateLogSaveVO = new ShipbillOperateLogSaveVO();
		shipbillOperateLogSaveVO.setShipbillId(model.getShipbillId());
		shipbillOperateLogSaveVO.setShipbillNo(model.getShipbillNo());
		shipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_UPDATE);
		shipbillOperateLogSaveVO.setOldStatus(model.getStatus());
		shipbillOperateLogSaveVO.setNewStatus(model.getStatus());
		shipbillOperateLogService.addShipbillOperateLogData(shipbillOperateLogSaveVO);
	}

	private void updateShipbillDetail(TmsShipbillHeaderModel model, List<ShipbillDetailModifyVO> updateShipbillDetails, UserEntity user, Date date) {
		// String shipbillId = model.getShipbillId();
		Map<String, List<TmsShipbillDetailModel>> origShipbillDetailMap = Maps.newHashMap();
		List<ShipbillDetailModifyVO> subShipbillDetails = Lists.newArrayList();
		List<ShipbillDetailModifyVO> addShipbillDetails = Lists.newArrayList();
		Map<Object, List<ShipbillDetailModifyVO>> shipbillDetailGroupmap = Maps.newHashMap();

		Map<String, List<ShipbillDetailModifyVO>> shipbillGroupmap = Maps.newHashMap();
		shipbillGroupmap = updateShipbillDetails.stream().collect(Collectors.groupingBy(o -> o.getShipbillId()));
		for (String obj : shipbillGroupmap.keySet()) {
			List<ShipbillDetailModifyVO> shipbillList = shipbillGroupmap.get(obj);
			// 保存运单明细
			Criteria criteriaDetail = Criteria.forClass(TmsShipbillDetailModel.class);
			for (ShipbillDetailModifyVO detailInfoVO1 : shipbillList) {
				if (ToolKits.isNotEmpty(detailInfoVO1.getId())) {
					TmsShipbillDetailModel detailModel = tmsShipbillDetailMapper.findById(detailInfoVO1.getId(), TmsShipbillDetailModel.class);
					if (ToolKits.isEmpty(detailModel)) {
						throw new PrologException("运单【" + detailInfoVO1.getShipbillId() + "】明细【" + detailInfoVO1.getItemName() + "】不存在");
					}
					List<TmsShipbillDetailModel> shipbillDetailModels = Lists.newArrayList();
					shipbillDetailModels.add(detailModel);
					origShipbillDetailMap.put(detailInfoVO1.getId(), shipbillDetailModels);
				} else {
					Restriction r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, detailInfoVO1.getShipbillId());
					Restriction r2 = Restrictions.eq(TmsShipbillDetailModel::getItemId, detailInfoVO1.getItemId());
					criteriaDetail.setRestriction(Restrictions.and(r1, r2));
					List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteriaDetail);
					if (ToolKits.isEmpty(tmsShipbillDetailModels)) {
						throw new PrologException("运单【" + detailInfoVO1.getShipbillId() + "】明细【" + detailInfoVO1.getItemName() + "】不存在");
					}
					origShipbillDetailMap.put(detailInfoVO1.getShipbillId() + "_" + String.valueOf(detailInfoVO1.getItemId()), tmsShipbillDetailModels);
				}
			}
		}

		shipbillDetailGroupmap = updateShipbillDetails.stream().collect(Collectors.groupingBy(o -> o.getShipbillId() + "_" + String.valueOf(o.getItemId())));
		for (Object obj : shipbillDetailGroupmap.keySet()) {
			List<ShipbillDetailModifyVO> shipbillDetailList = shipbillDetailGroupmap.get(obj);
			long sumApplyPcs = shipbillDetailList.stream().mapToInt(o -> o.getApplyPcs().intValue()).summaryStatistics().getSum();

			ShipbillDetailModifyVO detailInfoVO = shipbillDetailList.get(0);
			Criteria criteriaDetail = Criteria.forClass(TmsShipbillDetailModel.class);
			if (ToolKits.isNotEmpty(model.getFatherShipbillId())) {
				// 取原始运单的件数
				Restriction r11 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, model.getFatherShipbillId());
				Restriction r22 = Restrictions.eq(TmsShipbillDetailModel::getItemId, detailInfoVO.getItemId());
				criteriaDetail.setRestriction(Restrictions.and(r11, r22));
				List<TmsShipbillDetailModel> origTmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteriaDetail);
				long origPcs = origTmsShipbillDetailModels.stream().mapToInt(o -> o.getPcs().intValue()).summaryStatistics().getSum();

				if (sumApplyPcs > origPcs) {
					throw new PrologException("运单【" + detailInfoVO.getShipbillId() + "】的合计申请件数超过原始剩余件数");
				}
			} else {
				if ((ToolKits.isEmpty(model.getCarrierShipbillStatus()) && model.getStatus().intValue() > DataDictionaryConstant.SHIPBILL_STATUS_REVIEW.intValue()) || (ToolKits.isNotEmpty(model.getCarrierShipbillStatus()) && (model.getStatus().intValue() > DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK.intValue() || model.getCarrierShipbillStatus().intValue() > DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED.intValue()))) {
					List<ShipbillDetailModifyVO> origShipbillDetails = shipbillDetailList.stream().filter(o -> ToolKits.isNotEmpty(o.getId())).collect(Collectors.toList());
					long origPcs = origShipbillDetails.stream().mapToInt(o -> o.getPcs().intValue()).summaryStatistics().getSum();
					if (sumApplyPcs > origPcs) {
						throw new PrologException("运单【" + detailInfoVO.getShipbillId() + "】的合计申请件数超过原始剩余件数");
					}
				}
			}
			Restriction r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, detailInfoVO.getShipbillId());
			Restriction r2 = Restrictions.eq(TmsShipbillDetailModel::getItemId, detailInfoVO.getItemId());
			criteriaDetail.setRestriction(Restrictions.and(r1, r2));
			List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteriaDetail);
			if (ToolKits.isEmpty(tmsShipbillDetailModels)) {
				throw new PrologException("运单【" + detailInfoVO.getShipbillId() + "】明细【" + detailInfoVO.getItemName() + "】不存在");
			}
			long sumPcs = tmsShipbillDetailModels.stream().mapToInt(o -> o.getPcs().intValue()).summaryStatistics().getSum();
			// 申请件数 < 件数
			if (sumApplyPcs < sumPcs) {
				detailInfoVO.setPcs(new BigDecimal(sumPcs - sumApplyPcs));
				// 需要调整出去的运单明细
				subShipbillDetails.add(detailInfoVO);
			} else if (sumApplyPcs > sumPcs) {
				// 申请件数 > 件数
				detailInfoVO.setPcs(new BigDecimal(sumApplyPcs - sumPcs));
				// 需要调整进来的运单明细
				addShipbillDetails.add(detailInfoVO);
			}

		}
		int seqno = 1;
		Map<Long, ItemCheckDTO> itemMap = Maps.newHashMap();
		List<TmsShipbillDetailModel> newShipbillbillDetails = Lists.newArrayList();
		// 作成新运单
		List<ShipbillDetailModifyVO> newShipbills = Lists.newArrayList();
		// 商品总件数
		BigDecimal goodsTotalPcs = new BigDecimal(0);
		// 商品总体积
		BigDecimal goodsTotalVolume = new BigDecimal(0);
		// 商品总重量
		BigDecimal goodsTotalWeight = new BigDecimal(0);
		List<String> selfDetailUpdateLog = Lists.newArrayList();
		for (ShipbillDetailModifyVO shipbillDetail : updateShipbillDetails) {

			TmsShipbillDetailModel tmsShipbillDetailModel = new TmsShipbillDetailModel();
			List<TmsShipbillDetailModel> tmsShipbillDetailModels = Lists.newArrayList();
			String tmsShipbillDetailId = shipbillDetail.getId();
			if (ToolKits.isNotEmpty(shipbillDetail.getId())) {
				tmsShipbillDetailModels = origShipbillDetailMap.get(shipbillDetail.getId());
			} else {
				// 获取运单明细ID
				RestMessage<String> restMessage2 = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_DETAIL_ID, "L");
				if (!restMessage2.isSuccess()) {
					throw new RuntimeException("生成运单明细ID失败");
				}
				tmsShipbillDetailId = restMessage2.getData();
				tmsShipbillDetailModels = origShipbillDetailMap.get(shipbillDetail.getShipbillId() + "_" + String.valueOf(shipbillDetail.getItemId()));
			}
			if (ToolKits.isEmpty(tmsShipbillDetailModels)) {
				throw new PrologException("运单【" + shipbillDetail.getShipbillId() + "】明细【" + shipbillDetail.getItemName() + "】不存在");
			}
			ItemCheckDTO rsItemInfo = itemMap.get(Long.valueOf(shipbillDetail.getItemId()));
			if (ToolKits.isEmpty(rsItemInfo)) {
				ItemCheckVO itemCheckVO = new ItemCheckVO();
				itemCheckVO.setItemId(shipbillDetail.getItemId());
				RestMessage<ItemCheckDTO> restMessage3 = maindataTmsResource.getItem(itemCheckVO);
				if (!restMessage3.isSuccess()) {
					throw new RuntimeException("取商品资料信息失败:" + restMessage3.getMessage());
				}
				rsItemInfo = restMessage3.getData();
				itemMap.put(Long.valueOf(shipbillDetail.getItemId()), rsItemInfo);
			}

			BeanUtils.copyProperties(tmsShipbillDetailModels.get(0), tmsShipbillDetailModel);
			// 明细赋值
			tmsShipbillDetailModel.setId(tmsShipbillDetailId);
			tmsShipbillDetailModel.setSeqno(seqno);
			seqno++;
			tmsShipbillDetailModel.setShipbillId(shipbillDetail.getShipbillId());
			tmsShipbillDetailModel.setShipbillNo(shipbillDetail.getShipbillId());
			tmsShipbillDetailModel.setEnterpriseId(user.getEnterpriseId());
			tmsShipbillDetailModel.setEnterpriseName(user.getEnterpriseName());
			tmsShipbillDetailModel.setCreateperson(user.getNickname());
			tmsShipbillDetailModel.setCreatetime(date);
			tmsShipbillDetailModel.setUpdateperson(user.getNickname());
			tmsShipbillDetailModel.setUpdatetime(date);

			tmsShipbillDetailModel.setPcs(shipbillDetail.getApplyPcs());
			tmsShipbillDetailModel.setVolume(tmsShipbillDetailModel.getPcs().multiply(rsItemInfo.getVolume()));
			tmsShipbillDetailModel.setWeight(tmsShipbillDetailModel.getPcs().multiply(rsItemInfo.getWeight()));

			tmsShipbillDetailModel.setExtStr1(shipbillDetail.getExtStr1());
			tmsShipbillDetailModel.setExtStr2(shipbillDetail.getExtStr2());
			tmsShipbillDetailModel.setExtStr3(shipbillDetail.getExtStr3());
			tmsShipbillDetailModel.setExtStr4(shipbillDetail.getExtStr4());
			tmsShipbillDetailModel.setExtStr5(shipbillDetail.getExtStr5());
			tmsShipbillDetailModel.setExtDate1(shipbillDetail.getExtDate1());
			tmsShipbillDetailModel.setExtDate2(shipbillDetail.getExtDate2());
			tmsShipbillDetailModel.setExtDate3(shipbillDetail.getExtDate3());
			tmsShipbillDetailModel.setExtDate4(shipbillDetail.getExtDate4());
			tmsShipbillDetailModel.setExtDate5(shipbillDetail.getExtDate5());
			tmsShipbillDetailModel.setExtNum1(shipbillDetail.getExtNum1());
			tmsShipbillDetailModel.setExtNum2(shipbillDetail.getExtNum2());
			tmsShipbillDetailModel.setExtNum3(shipbillDetail.getExtNum3());
			tmsShipbillDetailModel.setExtNum4(shipbillDetail.getExtNum4());
			tmsShipbillDetailModel.setExtNum5(shipbillDetail.getExtNum5());
			if (ToolKits.isNotEmpty(shipbillDetail.getId())) {
				// 保存原明细上的关闭件数，发货件数
				tmsShipbillDetailModel.setDeliveryPcs(ToolKits.isEmpty(shipbillDetail.getDeliveryPcs()) ? BigDecimal.ZERO : shipbillDetail.getDeliveryPcs());
				tmsShipbillDetailModel.setClosingPcs(ToolKits.isEmpty(shipbillDetail.getClosingPcs()) ? BigDecimal.ZERO : shipbillDetail.getClosingPcs());
				String log = "商品【" + shipbillDetail.getItemCode() + "】从" + tmsShipbillDetailModels.get(0).getPcs().intValue() + "修改为" + shipbillDetail.getApplyPcs().intValue();
				selfDetailUpdateLog.add(log);
				tmsShipbillDetailModel.setOrigPcs(tmsShipbillDetailModel.getPcs().add(tmsShipbillDetailModel.getClosingPcs().add(tmsShipbillDetailModel.getDeliveryPcs())));
				tmsShipbillDetailModel.setPlanDeliveryPcs(tmsShipbillDetailModel.getPcs());
				tmsShipbillDetailMapper.update(tmsShipbillDetailModel);
			} else {
				tmsShipbillDetailModel.setClosingPcs(BigDecimal.valueOf(0));
				tmsShipbillDetailModel.setDeliveryPcs(BigDecimal.valueOf(0));
				String log = "商品【" + shipbillDetail.getItemCode() + "】复制了一行,复制件数为" + shipbillDetail.getApplyPcs().intValue();
				selfDetailUpdateLog.add(log);

				tmsShipbillDetailModel.setOrigPcs(tmsShipbillDetailModel.getPcs().add(tmsShipbillDetailModel.getClosingPcs().add(tmsShipbillDetailModel.getDeliveryPcs())));
				tmsShipbillDetailModel.setPlanDeliveryPcs(tmsShipbillDetailModel.getPcs());
				tmsShipbillDetailModel.setScanStatus(DataDictionaryConstant.SCAN_STATUS_INIT);
				tmsShipbillDetailMapper.save(tmsShipbillDetailModel);
			}

			goodsTotalPcs = goodsTotalPcs.add(tmsShipbillDetailModel.getPcs());
			goodsTotalVolume = goodsTotalVolume.add(ToolKits.isEmpty(tmsShipbillDetailModel.getVolume()) ? BigDecimal.ZERO : tmsShipbillDetailModel.getVolume());
			goodsTotalWeight = goodsTotalWeight.add(ToolKits.isEmpty(tmsShipbillDetailModel.getWeight()) ? BigDecimal.ZERO : tmsShipbillDetailModel.getWeight());
			newShipbillbillDetails.add(tmsShipbillDetailModel);
		}

		// 修正运单的件重体
		// 总件数
		model.setTotalPcs(goodsTotalPcs);
		// 总体积
		model.setTotalVolume(goodsTotalVolume);
		// 总重量
		model.setTotalWeight(goodsTotalWeight);
		// 实际总件数
		model.setActualTotalPcs(goodsTotalPcs);
		// 实际总体积
		model.setActualTotalVolume(goodsTotalVolume);
		// 实际总重量
		model.setActualTotalWeight(goodsTotalWeight);
		tmsShipbillHeaderMapper.update(model);

		// 运单操作日志保存
		ShipbillOperateLogSaveVO shipbillOperateLogSaveVO = new ShipbillOperateLogSaveVO();
		shipbillOperateLogSaveVO.setShipbillId(model.getShipbillId());
		shipbillOperateLogSaveVO.setShipbillNo(model.getShipbillNo());
		shipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_OPERATENAME_TZ + ",修改详情:" + selfDetailUpdateLog.stream().collect(Collectors.joining(";")));
		shipbillOperateLogSaveVO.setOldStatus(model.getStatus());
		shipbillOperateLogSaveVO.setNewStatus(model.getStatus());
		shipbillOperateLogService.addShipbillOperateLogData(shipbillOperateLogSaveVO);

		// 修改调度单的件重体
		Criteria criteriaLoadingBillDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
		Restriction r3 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, model.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> loadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingBillDetail);
		if (ToolKits.isNotEmpty(loadingBillDetailModels)) {
			Set<String> loadingIdSet = Sets.newHashSet();
			// 修改第一条明细
			for (TmsLoadingBillDetailModel detailModel : loadingBillDetailModels) {
				// 修正运单的件重体
				// 总件数
				detailModel.setTotalPcs(goodsTotalPcs);
				// 总体积
				detailModel.setTotalVolume(goodsTotalVolume);
				// 总重量
				detailModel.setTotalWeight(goodsTotalWeight);
				// 实际总件数
				detailModel.setActualTotalPcs(goodsTotalPcs);
				// 实际总体积
				detailModel.setActualTotalVolume(goodsTotalVolume);
				// 实际总重量
				detailModel.setActualTotalWeight(goodsTotalWeight);
				tmsLoadingBillDetailMapper.update(detailModel);

				loadingIdSet.add(detailModel.getLoadingBillId());
				break;
			}
			for (String loadingId : loadingIdSet) {
				r3 = Restrictions.eq(TmsLoadingBillDetailModel::getLoadingBillId, loadingId);
				criteriaLoadingBillDetail.setRestriction(Restrictions.and(r3, r4));
				List<TmsLoadingBillDetailModel> loadingBillDetailModelList = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingBillDetail);
				if (ToolKits.isNotEmpty(loadingBillDetailModelList)) {

					BigDecimal hTotalPcs = loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getTotalPcs).reduce(BigDecimal.ZERO, BigDecimal::add);
					BigDecimal hTotalWeigth = loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getTotalWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
					BigDecimal hTotalVolume = loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getTotalVolume).reduce(BigDecimal.ZERO, BigDecimal::add);

					TmsLoadingBillHeaderModel loadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(loadingId, TmsLoadingBillHeaderModel.class);
					if (ToolKits.isNotEmpty(loadingBillHeaderModel)) {
						loadingBillHeaderModel.setTotalPcs(hTotalPcs);
						loadingBillHeaderModel.setTotalVolume(hTotalVolume);
						loadingBillHeaderModel.setTotalWeight(hTotalWeigth);
						loadingBillHeaderModel.setActualTotalPcs(loadingBillHeaderModel.getTotalPcs());
						loadingBillHeaderModel.setActualTotalVolume(loadingBillHeaderModel.getTotalVolume());
						loadingBillHeaderModel.setActualTotalWeight(loadingBillHeaderModel.getTotalWeight());
						tmsLoadingBillHeaderMapper.update(loadingBillHeaderModel);
					}
				}
			}
		}

		// 修改预约单的件重体
		Criteria criteriaAppointmentDetail = Criteria.forClass(TmsAppointmentDetailModel.class);
		Restriction r5 = Restrictions.eq(TmsAppointmentDetailModel::getShipbillId, model.getShipbillId());
		Restriction r6 = Restrictions.eq(TmsAppointmentDetailModel::getEnterpriseId, user.getEnterpriseId());
		criteriaAppointmentDetail.setRestriction(Restrictions.and(r5, r6));
		List<TmsAppointmentDetailModel> appointmentDetailModels = tmsAppointmentDetailMapper.findByCriteria(criteriaAppointmentDetail);
		if (ToolKits.isNotEmpty(appointmentDetailModels)) {
			Set<String> appointmentIdSet = Sets.newHashSet();
			for (TmsAppointmentDetailModel appointmentDetailModel : appointmentDetailModels) {
				// 修正运单的件重体
				// 总件数
				appointmentDetailModel.setTotalPcs(goodsTotalPcs);
				// 总体积
				appointmentDetailModel.setTotalVolume(goodsTotalVolume);
				// 总重量
				appointmentDetailModel.setTotalWeight(goodsTotalWeight);
				// 实际总件数
				appointmentDetailModel.setActualTotalPcs(goodsTotalPcs);
				// 实际总体积
				appointmentDetailModel.setActualTotalVolume(goodsTotalVolume);
				// 实际总重量
				appointmentDetailModel.setActualTotalWeight(goodsTotalWeight);
				tmsAppointmentDetailMapper.update(appointmentDetailModel);
				appointmentIdSet.add(appointmentDetailModel.getAppointmentId());
			}
			for (String appointmentId : appointmentIdSet) {
				r5 = Restrictions.eq(TmsAppointmentDetailModel::getAppointmentId, appointmentId);
				criteriaAppointmentDetail.setRestriction(Restrictions.and(r5, r6));
				List<TmsAppointmentDetailModel> appointmentDetailModelList = tmsAppointmentDetailMapper.findByCriteria(criteriaAppointmentDetail);
				if (ToolKits.isNotEmpty(appointmentDetailModelList)) {
					BigDecimal hTotalPcs = appointmentDetailModelList.stream().map(TmsAppointmentDetailModel::getTotalPcs).reduce(BigDecimal.ZERO, BigDecimal::add);
					BigDecimal hTotalWeigth = appointmentDetailModelList.stream().map(TmsAppointmentDetailModel::getTotalWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
					BigDecimal hTotalVolume = appointmentDetailModelList.stream().map(TmsAppointmentDetailModel::getTotalVolume).reduce(BigDecimal.ZERO, BigDecimal::add);

					TmsAppointmentHeaderModel appointmentHeaderModel = tmsAppointmentHeaderMapper.findById(appointmentId, TmsAppointmentHeaderModel.class);
					if (ToolKits.isNotEmpty(appointmentHeaderModel)) {
						appointmentHeaderModel.setTotalPcs(hTotalPcs);
						appointmentHeaderModel.setTotalVolume(hTotalVolume);
						appointmentHeaderModel.setTotalWeight(hTotalWeigth);
						tmsAppointmentHeaderMapper.update(appointmentHeaderModel);
					}
				}
			}
		}
		// 调度前或者自提登记前
		if (ToolKits.isEmpty(model.getFatherShipbillId())) {
			if (model.getStatus().intValue() < DataDictionaryConstant.SHIPBILL_STATUS_HD_ZT_APPROVAL.intValue() || (ToolKits.isNotEmpty(model.getCarrierShipbillStatus()) && model.getCarrierShipbillStatus().intValue() < DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_DISPATCHED.intValue())) {
				return;
			}
		}

		// 需要调整进来的运单明细
		if (ToolKits.isNotEmpty(addShipbillDetails)) {
			Map<Object, List<ShipbillDetailModifyVO>> addShipbillMap = Maps.newHashMap();
			addShipbillMap = addShipbillDetails.stream().collect(Collectors.groupingBy(o -> o.getShipbillId()));
			// 按运单，调整件数
			for (Object obj : addShipbillMap.keySet()) {
				List<ShipbillDetailModifyVO> addDetailModels = addShipbillMap.get(obj);
				if (ToolKits.isEmpty(addDetailModels)) {
					continue;
				}
				if (ToolKits.isEmpty(model.getFatherShipbillId())) {
					continue;
				}
				// 按运单明细，调整件数
				for (ShipbillDetailModifyVO addShibillDetail : addDetailModels) {
					BigDecimal adjustPcs = addShibillDetail.getPcs();
					Criteria criteriaShipbillHeader = Criteria.forClass(TmsShipbillHeaderModel.class);
					Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getFatherShipbillId, model.getFatherShipbillId());
					Restriction r2 = Restrictions.ne(TmsShipbillHeaderModel::getShipbillId, addShibillDetail.getShipbillId());
					r3 = Restrictions.gt(TmsShipbillHeaderModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_CLOSE);
					criteriaShipbillHeader.setRestriction(Restrictions.and(r1, r2, r3));
					List<TmsShipbillHeaderModel> brotherShipbillHeaderlist = tmsShipbillHeaderMapper.findByCriteria(criteriaShipbillHeader);

					// 运单商品总件数
					BigDecimal headerTotalPcs = new BigDecimal(0);

					for (TmsShipbillHeaderModel brotherShipbillHeader : brotherShipbillHeaderlist) {
						// 同级运单已调度，或者已自提登记，或者已拆分中转，不调整同级运单
						if (ToolKits.isNotEmpty(brotherShipbillHeader.getCarrierShipbillStatus())) {
							// 承运商的运单
							if (brotherShipbillHeader.getCarrierShipbillStatus().intValue() > DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED.intValue()) {
								continue;
							}
							if (!brotherShipbillHeader.getCarrierSplitOrderStatus().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN.intValue())) {
								continue;
							}
						} else {
							if (!brotherShipbillHeader.getSplitOrderStatus().equals(DataDictionaryConstant.SPLIT_ORDER_STATUS_UN.intValue())) {
								continue;
							}
							// 自提登记或者拆分
							if (brotherShipbillHeader.getStatus().intValue() > DataDictionaryConstant.SHIPBILL_STATUS_REVIEW.intValue()) {
								continue;
							}
						}

						Criteria criteriaShipbillDetail = Criteria.forClass(TmsShipbillDetailModel.class);
						r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, brotherShipbillHeader.getShipbillId());
						r2 = Restrictions.eq(TmsShipbillDetailModel::getItemId, addShibillDetail.getItemId());
						r3 = Restrictions.gt(TmsShipbillDetailModel::getPcs, BigDecimal.valueOf(0));
						criteriaShipbillDetail.setRestriction(Restrictions.and(r1, r2, r3));
						List<TmsShipbillDetailModel> brotherShipbillDetaillist = tmsShipbillDetailMapper.findByCriteria(criteriaShipbillDetail);
						if (ToolKits.isEmpty(brotherShipbillDetaillist)) {
							continue;
						}
						// 重新计算运单的件重体
						ItemCheckDTO rsItemInfo = itemMap.get(Long.valueOf(addShibillDetail.getItemId()));
						List<String> brotherDetailUpdateLog = Lists.newArrayList();
						for (TmsShipbillDetailModel addDetailModel : brotherShipbillDetaillist) {
							if (adjustPcs.intValue() == 0) {
								break;
							}
							if (addDetailModel.getPcs().intValue() > adjustPcs.intValue()) {
								// 一次调整完了
								String log = "商品【" + addDetailModel.getItemCode() + "】从" + addDetailModel.getPcs().intValue() + "修改为" + (addDetailModel.getPcs().subtract(adjustPcs)).intValue();
								brotherDetailUpdateLog.add(log);
								addDetailModel.setPcs(addDetailModel.getPcs().subtract(adjustPcs));
								addDetailModel.setOrigPcs(addDetailModel.getPcs().add(addDetailModel.getClosingPcs().add(addDetailModel.getDeliveryPcs())));
								addDetailModel.setUpdateperson(user.getNickname());
								addDetailModel.setUpdatetime(date);
								addDetailModel.setWeight(addDetailModel.getPcs().multiply(rsItemInfo.getWeight()));
								addDetailModel.setVolume(addDetailModel.getPcs().multiply(rsItemInfo.getVolume()));
								addDetailModel.setPlanDeliveryPcs(addDetailModel.getPcs());
								tmsShipbillDetailMapper.update(addDetailModel);
								headerTotalPcs = headerTotalPcs.add(adjustPcs);
								adjustPcs = BigDecimal.valueOf(0);
								break;
							} else {
								// 运单明细的剩余件数<=调整件数时
								String log = "商品【" + addDetailModel.getItemCode() + "】从" + addDetailModel.getPcs().intValue() + "修改为0";
								brotherDetailUpdateLog.add(log);
								adjustPcs = adjustPcs.subtract(addDetailModel.getPcs());
								headerTotalPcs = headerTotalPcs.add(addDetailModel.getPcs());

								addDetailModel.setPcs(BigDecimal.valueOf(0));
								addDetailModel.setOrigPcs(addDetailModel.getPcs().add(addDetailModel.getClosingPcs().add(addDetailModel.getDeliveryPcs())));
								addDetailModel.setUpdateperson(user.getNickname());
								addDetailModel.setUpdatetime(date);
								addDetailModel.setWeight(BigDecimal.valueOf(0));
								addDetailModel.setVolume(BigDecimal.valueOf(0));
								addDetailModel.setPlanDeliveryPcs(addDetailModel.getPcs());
								tmsShipbillDetailMapper.update(addDetailModel);
							}
						}
						// 修改兄弟运单
						r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, brotherShipbillHeader.getShipbillId());
						r2 = Restrictions.gt(TmsShipbillDetailModel::getPcs, BigDecimal.valueOf(0));
						criteriaShipbillDetail.setRestriction(Restrictions.and(r1, r2));
						brotherShipbillDetaillist = tmsShipbillDetailMapper.findByCriteria(criteriaShipbillDetail);

						if (ToolKits.isEmpty(brotherShipbillDetaillist)) {
							// 同级运单的明细都删除了
							brotherShipbillHeader.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_ZF);
							brotherShipbillHeader.setTotalPcs(BigDecimal.valueOf(0));
							brotherShipbillHeader.setTotalVolume(BigDecimal.valueOf(0));
							brotherShipbillHeader.setTotalWeight(BigDecimal.valueOf(0));

							brotherShipbillHeader.setActualTotalPcs(BigDecimal.valueOf(0));
							brotherShipbillHeader.setActualTotalVolume(BigDecimal.valueOf(0));
							brotherShipbillHeader.setActualTotalWeight(BigDecimal.valueOf(0));

							brotherShipbillHeader.setUpdateperson(user.getNickname());
							brotherShipbillHeader.setUpdatetime(date);
							tmsShipbillHeaderMapper.update(brotherShipbillHeader);
						} else {
							brotherShipbillHeader.setTotalPcs(brotherShipbillHeader.getTotalPcs().subtract(headerTotalPcs));
							brotherShipbillHeader.setTotalVolume(brotherShipbillHeader.getTotalPcs().multiply(rsItemInfo.getVolume()));
							brotherShipbillHeader.setTotalWeight(brotherShipbillHeader.getTotalPcs().multiply(rsItemInfo.getWeight()));

							brotherShipbillHeader.setActualTotalPcs(brotherShipbillHeader.getTotalPcs());
							brotherShipbillHeader.setActualTotalVolume(brotherShipbillHeader.getTotalVolume());
							brotherShipbillHeader.setActualTotalWeight(brotherShipbillHeader.getTotalWeight());

							brotherShipbillHeader.setUpdateperson(user.getNickname());
							brotherShipbillHeader.setUpdatetime(date);
							tmsShipbillHeaderMapper.update(brotherShipbillHeader);
						}

						// 运单操作日志保存
						ShipbillOperateLogSaveVO brotherShipbillLogVO = new ShipbillOperateLogSaveVO();
						brotherShipbillLogVO.setShipbillId(brotherShipbillHeader.getShipbillId());
						brotherShipbillLogVO.setShipbillNo(brotherShipbillHeader.getShipbillNo());
						brotherShipbillLogVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_OPERATENAME_TZ_BROTHER + ",修改详情:" + brotherDetailUpdateLog.stream().collect(Collectors.joining(";")));
						brotherShipbillLogVO.setOldStatus(brotherShipbillHeader.getStatus());
						brotherShipbillLogVO.setNewStatus(brotherShipbillHeader.getStatus());
						shipbillOperateLogService.addShipbillOperateLogData(brotherShipbillLogVO);

						// 调整结束
						if (adjustPcs.intValue() <= 0) {
							break;
						}
					}
					// 当条明细调整结束
					if (adjustPcs.intValue() <= 0) {
						continue;
					}
					if (adjustPcs.intValue() > 0) {
						throw new PrologException("件数增加时,同级子单已调度,或已自提登记,或已拆分,不支持修改件数");
					}
				}
			}
		}

		// 需要调整出去的运单明细
		if (ToolKits.isNotEmpty(subShipbillDetails)) {
			Map<Object, List<ShipbillDetailModifyVO>> subShipbillMap = Maps.newHashMap();
			subShipbillMap = subShipbillDetails.stream().collect(Collectors.groupingBy(o -> o.getShipbillId()));
			// 按运单，调整件数
			for (Object obj : subShipbillMap.keySet()) {
				List<ShipbillDetailModifyVO> subDetailModels = subShipbillMap.get(obj);
				if (ToolKits.isEmpty(subShipbillMap)) {
					continue;
				}
				// 按运单明细，调整件数
				for (ShipbillDetailModifyVO subShibillDetail : subDetailModels) {
					if (ToolKits.isEmpty(model.getFatherShipbillId())) {
						// 做新运单
						newShipbills.add(subShibillDetail);
						continue;
					}

					BigDecimal adjustPcs = subShibillDetail.getPcs();
					Criteria criteriaShipbillHeader = Criteria.forClass(TmsShipbillHeaderModel.class);
					Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getFatherShipbillId, model.getFatherShipbillId());
					Restriction r2 = Restrictions.ne(TmsShipbillHeaderModel::getShipbillId, subShibillDetail.getShipbillId());
					r3 = Restrictions.gt(TmsShipbillHeaderModel::getStatus, DataDictionaryConstant.SHIPBILL_STATUS_CLOSE);
					criteriaShipbillHeader.setRestriction(Restrictions.and(r1, r2, r3));
					List<TmsShipbillHeaderModel> brotherShipbillHeaderlist = tmsShipbillHeaderMapper.findByCriteria(criteriaShipbillHeader);

					for (TmsShipbillHeaderModel brotherShipbillHeader : brotherShipbillHeaderlist) {
						// 同级运单已调度，或者已自提登记，或者已拆分中转，不调整同级运单
						if (ToolKits.isNotEmpty(brotherShipbillHeader.getCarrierShipbillStatus())) {
							// 承运商的运单
							if (brotherShipbillHeader.getCarrierShipbillStatus().intValue() > DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED.intValue()) {
								continue;
							}
							if (!brotherShipbillHeader.getCarrierSplitOrderStatus().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN.intValue())) {
								continue;
							}
						} else {
							if (!brotherShipbillHeader.getSplitOrderStatus().equals(DataDictionaryConstant.SPLIT_ORDER_STATUS_UN.intValue())) {
								continue;
							}
							// 自提登记或者拆分
							if (brotherShipbillHeader.getStatus().intValue() > DataDictionaryConstant.SHIPBILL_STATUS_REVIEW.intValue()) {
								continue;
							}
						}
						Criteria criteriaShipbillDetail = Criteria.forClass(TmsShipbillDetailModel.class);
						r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, brotherShipbillHeader.getShipbillId());
						r2 = Restrictions.eq(TmsShipbillDetailModel::getItemId, subShibillDetail.getItemId());
						criteriaShipbillDetail.setRestriction(Restrictions.and(r1, r2));
						List<TmsShipbillDetailModel> brotherShipbillDetaillist = tmsShipbillDetailMapper.findByCriteria(criteriaShipbillDetail);
						if (ToolKits.isEmpty(brotherShipbillDetaillist)) {
							continue;
						}
						List<String> brotherDetailUpdateLog = Lists.newArrayList();
						// 重新计算运单的件重体
						ItemCheckDTO rsItemInfo = itemMap.get(Long.valueOf(subShibillDetail.getItemId()));
						TmsShipbillDetailModel subDetailModel = brotherShipbillDetaillist.get(0);

						String log = "商品【" + subDetailModel.getItemCode() + "】从" + subDetailModel.getPcs().intValue() + "修改为" + (subDetailModel.getPcs().add(adjustPcs)).intValue();
						brotherDetailUpdateLog.add(log);

						subDetailModel.setPcs(subDetailModel.getPcs().add(adjustPcs));
						subDetailModel.setOrigPcs(subDetailModel.getPcs().add(subDetailModel.getClosingPcs().add(subDetailModel.getDeliveryPcs())));
						subDetailModel.setUpdateperson(user.getNickname());
						subDetailModel.setUpdatetime(date);
						subDetailModel.setWeight(subDetailModel.getPcs().multiply(rsItemInfo.getWeight()));
						subDetailModel.setVolume(subDetailModel.getPcs().multiply(rsItemInfo.getVolume()));
						subDetailModel.setPlanDeliveryPcs(subDetailModel.getPcs());
						tmsShipbillDetailMapper.update(subDetailModel);

						brotherShipbillHeader.setTotalPcs(brotherShipbillHeader.getTotalPcs().add(adjustPcs));
						brotherShipbillHeader.setTotalVolume(brotherShipbillHeader.getTotalPcs().multiply(rsItemInfo.getVolume()));
						brotherShipbillHeader.setTotalWeight(brotherShipbillHeader.getTotalPcs().multiply(rsItemInfo.getWeight()));

						brotherShipbillHeader.setActualTotalPcs(brotherShipbillHeader.getTotalPcs());
						brotherShipbillHeader.setActualTotalVolume(brotherShipbillHeader.getTotalVolume());
						brotherShipbillHeader.setActualTotalWeight(brotherShipbillHeader.getTotalWeight());

						brotherShipbillHeader.setUpdateperson(user.getNickname());
						brotherShipbillHeader.setUpdatetime(date);
						tmsShipbillHeaderMapper.update(brotherShipbillHeader);
						adjustPcs = BigDecimal.valueOf(0);

						// 运单操作日志保存
						ShipbillOperateLogSaveVO brotherShipbillLogVO = new ShipbillOperateLogSaveVO();
						brotherShipbillLogVO.setShipbillId(brotherShipbillHeader.getShipbillId());
						brotherShipbillLogVO.setShipbillNo(brotherShipbillHeader.getShipbillNo());
						brotherShipbillLogVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_OPERATENAME_TZ_BROTHER + ",修改详情:" + brotherDetailUpdateLog.stream().collect(Collectors.joining(";")));
						brotherShipbillLogVO.setOldStatus(brotherShipbillHeader.getStatus());
						brotherShipbillLogVO.setNewStatus(brotherShipbillHeader.getStatus());
						shipbillOperateLogService.addShipbillOperateLogData(brotherShipbillLogVO);

						break;
					}
					if (adjustPcs.intValue() > 0) {
						// 做新运单
						newShipbills.add(subShibillDetail);
					}
				}
			}
		}

		// 作成新运单
		if (ToolKits.isNotEmpty(newShipbills) && newShipbills.size() > 0) {
			createNewShipbill(newShipbills, itemMap, user, date);
		}

	}

	/**
	 * 同步更新父运单商品明细
	 *
	 * @param shipbillId
	 * @param date
	 * @param user
	 * @return
	 */
	public String updateFatherShipbillDetail(String shipbillId, ShipbillDetailModifyVO closedShipbillDetal, Date date, UserEntity user, ItemCheckDTO itemCheckDTO) {
		// 查询运单
		TmsShipbillHeaderModel tmsShipbillHeaderModel = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
		// 判断是否为原始单号
		if (ToolKits.isNotEmpty(tmsShipbillHeaderModel.getFatherShipbillId()) && !tmsShipbillHeaderModel.getFatherShipbillId().equals(shipbillId)) {
			// 查找父级单号
			TmsShipbillHeaderModel fatherShipbillHeaderModel = tmsShipbillHeaderMapper.findById(tmsShipbillHeaderModel.getFatherShipbillId(), TmsShipbillHeaderModel.class);

			Criteria criteriaShipbillDetail = Criteria.forClass(TmsShipbillDetailModel.class);
			Restriction r11 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, fatherShipbillHeaderModel.getShipbillId());
			Restriction r12 = Restrictions.eq(TmsShipbillDetailModel::getEnterpriseId, user.getEnterpriseId());
			Restriction r13 = Restrictions.eq(TmsShipbillDetailModel::getItemId, closedShipbillDetal.getItemId());
			Restriction r14 = Restrictions.gt(TmsShipbillDetailModel::getPcs, BigDecimal.valueOf(0));
			criteriaShipbillDetail.setRestriction(Restrictions.and(r11, r12, r13, r14));
			List<TmsShipbillDetailModel> shipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteriaShipbillDetail);

			if (ToolKits.isEmpty(shipbillDetailModels)) {
				throw new PrologException("运单【" + shipbillId + "】的父运单的运单商品明细【" + closedShipbillDetal.getItemName() + "】不存在");
			}
			BigDecimal applyPcs = closedShipbillDetal.getApplyPcs();
			for (TmsShipbillDetailModel shipbillDetailModel : shipbillDetailModels) {
				if (applyPcs.intValue() <= 0) {
					break;
				}
				if (shipbillDetailModel.getPcs().intValue() >= applyPcs.intValue()) {
					shipbillDetailModel.setClosingPcs(shipbillDetailModel.getClosingPcs().add(applyPcs));

					// 剩余件数
					shipbillDetailModel.setPcs(shipbillDetailModel.getOrigPcs().subtract(shipbillDetailModel.getClosingPcs()).subtract(shipbillDetailModel.getDeliveryPcs()));
					shipbillDetailModel.setUpdateperson(user.getNickname());
					shipbillDetailModel.setUpdatetime(date);

					shipbillDetailModel.setVolume(shipbillDetailModel.getPcs().multiply(itemCheckDTO.getVolume()));
					shipbillDetailModel.setWeight(shipbillDetailModel.getPcs().multiply(itemCheckDTO.getWeight()));
					shipbillDetailModel.setPlanDeliveryPcs(shipbillDetailModel.getPcs());
					tmsShipbillDetailMapper.update(shipbillDetailModel);
					applyPcs = BigDecimal.valueOf(0);
					break;
				} else {
					shipbillDetailModel.setClosingPcs(shipbillDetailModel.getClosingPcs().add(shipbillDetailModel.getPcs()));

					// 剩余件数
					applyPcs = applyPcs.subtract(shipbillDetailModel.getPcs());

					shipbillDetailModel.setPcs(shipbillDetailModel.getOrigPcs().subtract(shipbillDetailModel.getClosingPcs()).subtract(shipbillDetailModel.getDeliveryPcs()));
					shipbillDetailModel.setUpdateperson(user.getNickname());
					shipbillDetailModel.setUpdatetime(date);

					shipbillDetailModel.setVolume(shipbillDetailModel.getPcs().multiply(itemCheckDTO.getVolume()));
					shipbillDetailModel.setWeight(shipbillDetailModel.getPcs().multiply(itemCheckDTO.getWeight()));
					shipbillDetailModel.setPlanDeliveryPcs(shipbillDetailModel.getPcs());
					tmsShipbillDetailMapper.update(shipbillDetailModel);
				}
			}

			// 更新运单上的件重体
			fatherShipbillHeaderModel.setTotalPcs(fatherShipbillHeaderModel.getTotalPcs().subtract(closedShipbillDetal.getApplyPcs()));
			fatherShipbillHeaderModel.setTotalWeight(fatherShipbillHeaderModel.getTotalPcs().multiply(itemCheckDTO.getWeight()));
			fatherShipbillHeaderModel.setTotalVolume(fatherShipbillHeaderModel.getTotalPcs().multiply(itemCheckDTO.getVolume()));

			fatherShipbillHeaderModel.setActualTotalWeight(fatherShipbillHeaderModel.getTotalWeight());
			fatherShipbillHeaderModel.setActualTotalVolume(fatherShipbillHeaderModel.getTotalVolume());
			fatherShipbillHeaderModel.setActualTotalPcs(fatherShipbillHeaderModel.getTotalPcs());
			tmsShipbillHeaderMapper.update(fatherShipbillHeaderModel);
			return fatherShipbillHeaderModel.getShipbillId();
		}
		return null;
	}
	// STD-DEVELOP20220823 lhb 运单件重体调整 20220913 end

	// 作成新运单
	public void createNewShipbill(List<ShipbillDetailModifyVO> addShibillDetail, Map<Long, ItemCheckDTO> itemMap, UserEntity user, Date date) {
		Map<Object, List<ShipbillDetailModifyVO>> addShipbillMap = Maps.newHashMap();
		addShipbillMap = addShibillDetail.stream().collect(Collectors.groupingBy(o -> o.getShipbillId()));
		for (Object obj : addShipbillMap.keySet()) {
			List<ShipbillDetailModifyVO> addDetailModels = addShipbillMap.get(obj);
			if (ToolKits.isEmpty(addDetailModels)) {
				continue;
			}
			// 作成运单明细
			// 获取运单汇总ID
			RestMessage<String> restMessage = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_HEADER_ID, "L");
			if (!restMessage.isSuccess()) {
				throw new RuntimeException("生成运单汇总ID失败");
			}
			String newShipbillId = restMessage.getData();
			// 保存每个子单中的明细信息
			int idx = 1;
			// 商品总件数
			BigDecimal newGoodsTotalPcs = new BigDecimal(0);
			// 商品总体积
			BigDecimal newGoodsTotalVolume = new BigDecimal(0);
			// 商品总重量
			BigDecimal newGoodsTotalWeight = new BigDecimal(0);
			for (ShipbillDetailModifyVO newShibillDetail : addDetailModels) {
				TmsShipbillDetailModel subTmsShipbillDetailModel = new TmsShipbillDetailModel();
				// 运单明细ID
				RestMessage<String> restMessage2 = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_DETAIL_ID, "L");
				if (!restMessage2.isSuccess()) {
					throw new RuntimeException("生成运单明细ID失败");
				}
				String subShipbillDetailId = restMessage2.getData();
				Criteria criteriaShipbillDetail = Criteria.forClass(TmsShipbillDetailModel.class);
				Restriction r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, newShibillDetail.getShipbillId());
				Restriction r2 = Restrictions.eq(TmsShipbillDetailModel::getItemId, newShibillDetail.getItemId());
				criteriaShipbillDetail.setRestriction(Restrictions.and(r1, r2));
				List<TmsShipbillDetailModel> shipbillDetaillist = tmsShipbillDetailMapper.findByCriteria(criteriaShipbillDetail);
				// 重新计算运单的件重体

				BeanUtils.copyProperties(shipbillDetaillist.get(0), subTmsShipbillDetailModel);
				// 取商品资料
				ItemCheckDTO rsItemInfo = itemMap.get(Long.valueOf(subTmsShipbillDetailModel.getItemId()));
				if (ToolKits.isEmpty(rsItemInfo)) {
					ItemCheckVO itemCheckVO = new ItemCheckVO();
					itemCheckVO.setItemId(subTmsShipbillDetailModel.getItemId());
					RestMessage<ItemCheckDTO> restMessage3 = maindataTmsResource.getItem(itemCheckVO);
					if (!restMessage3.isSuccess()) {
						throw new RuntimeException("取商品资料信息失败:" + restMessage3.getMessage());
					}
					rsItemInfo = restMessage3.getData();
					itemMap.put(Long.valueOf(subTmsShipbillDetailModel.getItemId()), rsItemInfo);
				}

				// ID
				subTmsShipbillDetailModel.setId(subShipbillDetailId);
				subTmsShipbillDetailModel.setSeqno(idx);
				idx++;
				subTmsShipbillDetailModel.setOriginalSeqno(idx);
				// 运单ID
				subTmsShipbillDetailModel.setShipbillId(newShipbillId);
				// 运单编号
				subTmsShipbillDetailModel.setShipbillNo(newShipbillId);

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

				subTmsShipbillDetailModel.setPcs(newShibillDetail.getPcs());
				subTmsShipbillDetailModel.setCreateperson(user.getNickname());
				subTmsShipbillDetailModel.setCreatetime(date);
				subTmsShipbillDetailModel.setUpdateperson(user.getNickname());
				subTmsShipbillDetailModel.setUpdatetime(date);
				subTmsShipbillDetailModel.setWeight(subTmsShipbillDetailModel.getPcs().multiply(rsItemInfo.getWeight()));
				subTmsShipbillDetailModel.setVolume(subTmsShipbillDetailModel.getPcs().multiply(rsItemInfo.getVolume()));
				subTmsShipbillDetailModel.setOrigPcs(subTmsShipbillDetailModel.getPcs());
				subTmsShipbillDetailModel.setClosingPcs(BigDecimal.valueOf(0));
				subTmsShipbillDetailModel.setDeliveryPcs(BigDecimal.valueOf(0));
				subTmsShipbillDetailModel.setScanStatus(DataDictionaryConstant.SCAN_STATUS_INIT);
				tmsShipbillDetailMapper.save(subTmsShipbillDetailModel);

				if (ToolKits.isNotEmpty(subTmsShipbillDetailModel.getPcs())) {
					newGoodsTotalPcs = newGoodsTotalPcs.add(subTmsShipbillDetailModel.getPcs());
				}
				if (ToolKits.isNotEmpty(subTmsShipbillDetailModel.getVolume())) {
					newGoodsTotalVolume = newGoodsTotalVolume.add(subTmsShipbillDetailModel.getVolume());
				}
				if (ToolKits.isNotEmpty(subTmsShipbillDetailModel.getWeight())) {
					newGoodsTotalWeight = newGoodsTotalWeight.add(subTmsShipbillDetailModel.getWeight());
				}
			}
			// 作成运单
			TmsShipbillHeaderModel newHeaderModel = new TmsShipbillHeaderModel();
			TmsShipbillHeaderModel oldHeaderModel = tmsShipbillHeaderMapper.findById(addDetailModels.get(0).getShipbillId(), TmsShipbillHeaderModel.class);
			BeanUtils.copyProperties(oldHeaderModel, newHeaderModel);
			newHeaderModel.setShipbillId(newShipbillId);
			newHeaderModel.setShipbillNo(newShipbillId);
			// 总件数
			newHeaderModel.setTotalPcs(newGoodsTotalPcs);
			// 总体积
			newHeaderModel.setTotalVolume(newGoodsTotalVolume);
			// 总重量
			newHeaderModel.setTotalWeight(newGoodsTotalWeight);
			// 实际总件数
			newHeaderModel.setActualTotalPcs(newGoodsTotalPcs);
			// 实际总体积
			newHeaderModel.setActualTotalVolume(newGoodsTotalVolume);
			// 实际总重量
			newHeaderModel.setActualTotalWeight(newGoodsTotalWeight);
			if (ToolKits.isNotEmpty(oldHeaderModel.getCarrierShipbillStatus())) {
				newHeaderModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_DISPATCH_CHECK);
				newHeaderModel.setCarrierShipbillStatus(DataDictionaryConstant.CARRIER_SHIPBILL_STATUS_AUDITED);
			} else {
				newHeaderModel.setStatus(DataDictionaryConstant.SHIPBILL_STATUS_REVIEW);
			}
			newHeaderModel.setSplitOrderStatus(DataDictionaryConstant.SPLIT_ORDER_STATUS_UN);
			newHeaderModel.setSplitOrderType(DataDictionaryConstant.SPLIT_ORDER_TYPE_UN);
			newHeaderModel.setCarrierSplitOrderStatus(DataDictionaryConstant.CARRIER_SPLIT_ORDER_STATUS_UN);
			newHeaderModel.setCarrierSplitOrderType(DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN);
			newHeaderModel.setCreateperson(user.getNickname());
			newHeaderModel.setCreatetime(date);
			newHeaderModel.setUpdateperson(user.getNickname());
			newHeaderModel.setUpdatetime(date);
			newHeaderModel.setShipbillType(ShipBillTypeEnum.NORMAL.getCode());
			newHeaderModel.setSignInStatus(SignInStatusEnum.NOT_SIGN.getCode());
			newHeaderModel.setDriverReceiveStatus(DriverReceiveStatusEnum.N.getCode());

			if (ToolKits.isNotEmpty(oldHeaderModel.getFatherShipbillId())) {
				newHeaderModel.setFatherShipbillId(oldHeaderModel.getOriginalShipbillId());
				newHeaderModel.setFatherShipbillNo(oldHeaderModel.getOriginalShipbillNo());
				newHeaderModel.setOriginalShipbillId(oldHeaderModel.getOriginalShipbillId());
				newHeaderModel.setOriginalShipbillNo(oldHeaderModel.getOriginalShipbillNo());
			}
			newHeaderModel.setIntelligentLock(IntelligentLockEnum.UN_LOCK.getCode());
			tmsShipbillHeaderMapper.save(newHeaderModel);

			// 初期化订单运输记录表
			saveTransportLog(newHeaderModel.getShipbillId(), date, newHeaderModel.getEstimatedPickupStartTime());

			// 子运单日志保存
			ShipbillOperateLogSaveNewVO subShipbillOperateLogSaveVO = new ShipbillOperateLogSaveNewVO();
			subShipbillOperateLogSaveVO.setShipbillId(newHeaderModel.getShipbillId());
			subShipbillOperateLogSaveVO.setShipbillNo(newHeaderModel.getShipbillNo());
			subShipbillOperateLogSaveVO.setOperateDate(date);
			subShipbillOperateLogSaveVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_OPERATENAME_UP_NEW);
			subShipbillOperateLogSaveVO.setOldStatus(DataDictionaryConstant.SHIPBILL_STATUS_YTJ);
			subShipbillOperateLogSaveVO.setNewStatus(newHeaderModel.getStatus());

			subShipbillOperateLogSaveVO.setUserId(user.getUserid());
			subShipbillOperateLogSaveVO.setUserName(user.getNickname());
			subShipbillOperateLogSaveVO.setEnterpriseId(user.getEnterpriseId());
			subShipbillOperateLogSaveVO.setEnterpriseName(user.getEnterpriseName());
			shipbillOperateLogService.addShipbillOperateLogNewData(subShipbillOperateLogSaveVO);

			// 运单节点保存
			ShipbillNodeTrackingSaveNewVO shipbillNodeTrackingSaveVO = new ShipbillNodeTrackingSaveNewVO();
			shipbillNodeTrackingSaveVO.setShipbillId(newHeaderModel.getShipbillId());
			shipbillNodeTrackingSaveVO.setShipbillNo(newHeaderModel.getShipbillId());
			shipbillNodeTrackingSaveVO.setNodeDate(date);
			shipbillNodeTrackingSaveVO.setStatus(newHeaderModel.getStatus());
			shipbillNodeTrackingSaveVO.setNodeEvent(DataDictionaryConstant.SHIPBILL_OPERATENAME_UP_NEW);

			shipbillNodeTrackingSaveVO.setUserId(user.getUserid());
			shipbillNodeTrackingSaveVO.setUserName(user.getNickname());
			shipbillNodeTrackingSaveVO.setEnterpriseId(user.getEnterpriseId());
			shipbillNodeTrackingSaveVO.setEnterpriseName(user.getEnterpriseName());
			shipbillNodeTrackingService.addShipbillNodeTrackingNewData(shipbillNodeTrackingSaveVO);
		}
	}

	/**
	 * 生成运输记录表
	 */
	public void saveTransportLog(String shipbillId, Date date, Date pickUpStartDt) {
		TmsShipbillTransLogModel logModel = new TmsShipbillTransLogModel();
		RestMessage<String> restMessage = maindataTmsResource.getCodeNumberByCode(SeriaNnumberConstant.TMS_SHIPBILL_TRANSPORT_LOG_ID, "L");
		if (!restMessage.isSuccess()) {
			throw new RuntimeException("生成运输记录ID失败");
		}
		String id = restMessage.getData();
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		logModel.setId(id);
		logModel.setShipbillId(shipbillId);
		logModel.setShipbillNo(shipbillId);

		logModel.setOrderDt(date);
		logModel.setCommitDt(date);
		logModel.setReviewDt(date);
		logModel.setEstimatedPickupStartTime(pickUpStartDt);
		logModel.setEnterpriseId(user.getEnterpriseId());
		logModel.setEnterpriseName(user.getEnterpriseName());
		logModel.setCreateperson(user.getNickname());
		logModel.setCreatetime(date);
		logModel.setUpdateperson(user.getNickname());
		logModel.setUpdatetime(date);

		tmsShipbillTransportLogMapper.save(logModel);
	}
	// WEICHAI-DEVELOP20220823 lhb 承运商审批后处理 20220921 end

	/**
	 * 关联运单
	 *
	 * @param relatedShipbillVO
	 */
	public List<RelatedShipbillDTO> queryRelatedShipbill(RelatedShipbillQueryVO relatedShipbillVO) {
		ToolKits.isParamNull(relatedShipbillVO, "关联运单查询请求不为空");

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

		List<RelatedShipbillDTO> relatedShipbillDTOS = Lists.newArrayList();
		List<TmsShipbillHeaderModel> list = Lists.newArrayList();

		if (ToolKits.isNotEmpty(relatedShipbillVO.getOriginalShipbillId())) {
			Criteria criteria = Criteria.forClass(TmsShipbillHeaderModel.class);
			Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getOriginalShipbillId, relatedShipbillVO.getOriginalShipbillId());
			Restriction r2 = Restrictions.eq(TmsShipbillHeaderModel::getEnterpriseId, user.getEnterpriseId());
			criteria.setRestriction(Restrictions.and(r1, r2));
			list = tmsShipbillHeaderMapper.findByCriteria(criteria);

			TmsShipbillHeaderModel origShipbill = tmsShipbillHeaderMapper.findById(relatedShipbillVO.getOriginalShipbillId(), TmsShipbillHeaderModel.class);
			list.add(origShipbill);
		} else {
			TmsShipbillHeaderModel origShipbill = tmsShipbillHeaderMapper.findById(relatedShipbillVO.getShipbillId(), TmsShipbillHeaderModel.class);
			list.add(origShipbill);
		}
		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SPLIT_ORDER_TYPE, // 拆单类型
				DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE// 承运商拆单类型
		});

		for (TmsShipbillHeaderModel headerModel : list) {
			RelatedShipbillDTO shipbillDTO = new RelatedShipbillDTO();
			BeanUtils.copyProperties(headerModel, shipbillDTO);
			if (!headerModel.getCarrierSplitOrderType().equals(DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE_UN)) {
				shipbillDTO.setSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CARRIER_SPLIT_ORDER_TYPE, String.valueOf(shipbillDTO.getCarrierSplitOrderType())));
			} else {
				shipbillDTO.setSplitOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SPLIT_ORDER_TYPE, String.valueOf(shipbillDTO.getSplitOrderType())));
			}
			relatedShipbillDTOS.add(shipbillDTO);
		}
		return relatedShipbillDTOS;
	}

	/**
	 * 运单温度轨迹信息查询
	 *
	 * @param
	 * @return
	 */
	public ShipBillTemperatureTrackDTO queryShipbillTemperature(ShipbillViewVO vo) {
		ToolKits.isParamNull(vo, "运单温度轨迹信息查询请求不为空");
		ToolKits.isParamNull(vo.getShipbillId(), "运单Id不为空");

		ShipBillTemperatureTrackDTO dto = new ShipBillTemperatureTrackDTO();
		dto.setShipbillId(vo.getShipbillId());
		dto.setShipbillNo(vo.getShipbillId());

		UserEntity user = PlgFxUsrUtils.getUserEntity();

		TmsShipbillHeaderModel shipbillHeaderModel = tmsShipbillHeaderMapper.findById(vo.getShipbillId(),TmsShipbillHeaderModel.class);
		if (ToolKits.isEmpty(shipbillHeaderModel)) {
			throw new PrologException(StrUtil.format("运单{}不存在",vo.getShipbillId()));
		}
		if (ToolKits.isEmpty(shipbillHeaderModel.getDepartureTime()) || ToolKits.isEmpty(shipbillHeaderModel.getVehicleDeviceCode())) {
			return dto;
		}
		Criteria criteriaTemperature = Criteria.forClass(TmsTemperatureData.class);
		Restriction r1 = Restrictions.eq(TmsTemperatureData::getDeviceCode, shipbillHeaderModel.getVehicleDeviceCode());
		Restriction r2 = Restrictions.ge(TmsTemperatureData::getTime, shipbillHeaderModel.getDepartureTime());
		Restriction r3 = ToolKits.isEmpty(shipbillHeaderModel.getDeliveryTime()) ? null:Restrictions.le(TmsTemperatureData::getTime, shipbillHeaderModel.getDeliveryTime());
		criteriaTemperature.setRestriction(Restrictions.and(r1,r2,r3));
		criteriaTemperature.setOrder(Order.newInstance().asc(new String[] { "time" }));
		List<TmsTemperatureData> tmsTemperatureDatas  = tmsTemperatureDataMapper.findByCriteria(criteriaTemperature);
		if (ToolKits.isEmpty(tmsTemperatureDatas)) {
			return dto;
		}
		if (ToolKits.isNotEmpty(vo.getTimeInterval())) {
			List<ShipBillTemperatureTrackDtlDTO> temperatureTrackDtlList = getTemperature(tmsTemperatureDatas, vo.getTimeInterval());
			dto.setTemperatureTrackDtlList(temperatureTrackDtlList);
		} else {
			// 默认1分钟
			List<ShipBillTemperatureTrackDtlDTO> temperatureTrackDtlList = Lists.newArrayList();
			tmsTemperatureDatas.stream().forEach(t->{
				ShipBillTemperatureTrackDtlDTO trackDtlDTO = new ShipBillTemperatureTrackDtlDTO();
				trackDtlDTO.setTemperature(t.getTemperature());
				trackDtlDTO.setLatitude(t.getLatitude());
				trackDtlDTO.setLongitude(t.getLongitude());
				trackDtlDTO.setVehiclePlateNumber(t.getVehiclePlateNumber());
				trackDtlDTO.setDeviceCode(t.getDeviceCode());
				trackDtlDTO.setTime(t.getTime());
				temperatureTrackDtlList.add(trackDtlDTO);
			});
			dto.setTemperatureTrackDtlList(temperatureTrackDtlList);
		}

		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		r1 = Restrictions.eq("shipbillId", vo.getShipbillId());
		r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<TmsShipbillDetailModel> tmsShipbillDetailModelList = tmsShipbillDetailMapper.findByCriteria(criteria);
		if (CollectionUtil.isNotEmpty(tmsShipbillDetailModelList)) {
			Optional<TmsShipbillDetailModel> maxStartTime = tmsShipbillDetailModelList.stream().filter(s->ToolKits.isNotEmpty(s.getTemperatureCtlLowerLimit())).max(Comparator.comparing(TmsShipbillDetailModel::getTemperatureCtlLowerLimit));
			if (maxStartTime.isPresent()) {
				dto.setTemperatureCtlLowerLimit(maxStartTime.get().getTemperatureCtlLowerLimit());
			}
			Optional<TmsShipbillDetailModel> minEndTime = tmsShipbillDetailModelList.stream().filter(s->ToolKits.isNotEmpty(s.getTemperatureCtlUpperLimit())).min(Comparator.comparing(TmsShipbillDetailModel::getTemperatureCtlUpperLimit));
			if (minEndTime.isPresent()) {
				dto.setTemperatureCtlUpperLimit(minEndTime.get().getTemperatureCtlUpperLimit());
			}
		} else {
			dto.setTemperatureCtlUpperLimit(BigDecimal.ZERO);
			dto.setTemperatureCtlLowerLimit(BigDecimal.ZERO);
		}
		return dto;
	}

	public List<ShipBillTemperatureTrackDtlDTO> getTemperature(List<TmsTemperatureData> tmsTemperatureDatas,Integer timeInterval) {
		Optional<TmsTemperatureData> maxStartTime = tmsTemperatureDatas.stream().filter(s->ToolKits.isNotEmpty(s.getTime())).max(Comparator.comparing(TmsTemperatureData::getTime));
		Date maxDate = null;
		if (maxStartTime.isPresent()) {
			maxDate = maxStartTime.get().getTime();
		}
		Optional<TmsTemperatureData> minEndTime = tmsTemperatureDatas.stream().filter(s->ToolKits.isNotEmpty(s.getTime())).min(Comparator.comparing(TmsTemperatureData::getTime));
		Date minDate = null;
		if (minEndTime.isPresent()) {
			minDate = minEndTime.get().getTime();
		}
		if (ToolKits.isEmpty(maxDate) || ToolKits.isEmpty(minDate)) {
			return null;
		}
		List<TemperatureIntervalDTO> intervalDTOS = Lists.newArrayList();
		while(minDate.before(maxDate)) {
			TemperatureIntervalDTO intervalDTO = new TemperatureIntervalDTO();
			intervalDTO.setMinDate(minDate);
			Date offsetTime = DateUtil.offsetMinute(minDate,timeInterval.intValue());
			intervalDTO.setMaxDate(offsetTime);
			intervalDTOS.add(intervalDTO);
			minDate = offsetTime;
		}
		intervalDTOS = intervalDTOS.stream().sorted(Comparator.comparing(TemperatureIntervalDTO::getMinDate)).collect(Collectors.toList());
		List<ShipBillTemperatureTrackDtlDTO> temperatureTrackDtlList = Lists.newArrayList();
		for (TemperatureIntervalDTO dto:intervalDTOS) {
			List<TmsTemperatureData> intervalTemperatures = tmsTemperatureDatas.stream().filter(t->t.getTime().after(dto.getMinDate()) && t.getTime().before(dto.getMaxDate())).collect(Collectors.toList());
			if (ToolKits.isEmpty(intervalTemperatures)) {
				continue;
			}
			intervalTemperatures = intervalTemperatures.stream().sorted(Comparator.comparing(TmsTemperatureData::getTime)).collect(Collectors.toList());
			TmsTemperatureData firstTemperature = intervalTemperatures.get(0);
			ShipBillTemperatureTrackDtlDTO trackDtlDTO = new ShipBillTemperatureTrackDtlDTO();
			trackDtlDTO.setTemperature(firstTemperature.getTemperature());
			trackDtlDTO.setLatitude(firstTemperature.getLatitude());
			trackDtlDTO.setLongitude(firstTemperature.getLongitude());
			trackDtlDTO.setVehiclePlateNumber(firstTemperature.getVehiclePlateNumber());
			trackDtlDTO.setDeviceCode(firstTemperature.getDeviceCode());
			trackDtlDTO.setTime(firstTemperature.getTime());
			temperatureTrackDtlList.add(trackDtlDTO);
		}
		return temperatureTrackDtlList;
	}
}
