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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillAbnormalMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillPackageMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillTransportLogMapper;
import com.prolog.rdc.tms.model.dto.AbnormalShipbillDTO;
import com.prolog.rdc.tms.model.dto.AbnormalShipbillItemDTO;
import com.prolog.rdc.tms.model.dto.AbnormalShipbillPackageDTO;
import com.prolog.rdc.tms.model.dto.AbnormalShipbillPageDTO;
import com.prolog.rdc.tms.model.dto.basicConfigure.SignconfigPopDTO;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillAbnormalModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillPackageModel;
import com.prolog.rdc.tms.model.vo.AbnormalShipbillAdvancedQueryVO;
import com.prolog.rdc.tms.model.vo.AbnormalShipbillItemQueryVO;
import com.prolog.rdc.tms.model.vo.AbnormalShipbillPackageQueryVO;
import com.prolog.rdc.tms.model.vo.AbnormalShipbillQueryVO;
import com.prolog.rdc.tms.model.vo.ShipbillOperateLogSaveNewVO;
import com.prolog.rdc.tms.model.vo.basicConfigure.SignconfigPopVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.AbnormalShipbillService;
import com.prolog.rdc.tms.service.ShipbillCommonService;
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;

/**
 * 运输管理-异常订单实现层
 * <p>
 * Company: 湖北普罗格科技股份有限公司
 *
 * @author xyf
 * @since 2022/6/9 14:01
 */
@Service
public class AbnormalShipbillServiceImpl implements AbnormalShipbillService {

	@Autowired
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Autowired
	private TmsShipbillPackageMapper tmsShipbillPackageMapper;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private ConvertorUtil convertorUtil;

	@Autowired
	private ShipbillCommonService shipbillCommonService;

	@Autowired
	private TmsShipbillAbnormalMapper tmsShipbillAbnormalMapper;

	@Autowired
	private ShipbillOperateLogService shipbillOperateLogService;

	@Autowired
	TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

	/**
	 * 主界面分页查询
	 *
	 * @param vo
	 * @return abnormalShipbillPageDTO
	 */
	@Override
	public Page<AbnormalShipbillPageDTO> queryAbnormalShipbillPage(AbnormalShipbillQueryVO 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)) {
			throw new PrologException("当前人员没有操作权限");
		}

		List<SysRefdatadModel> dicData = convertorUtil.getDictionary(new String[] { DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
				DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
				DataDictionaryConstant.LINE_CARRIER_TYPE, // 承运方类型
				DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
				DataDictionaryConstant.SCAN_STATUS, // 扫描状态
				DataDictionaryConstant.ORDER_TYPE, // 订单类型
				DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
				DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
				DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
				DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
				DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
				DataDictionaryConstant.CONSIGNEE_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 (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			vo.setCargoOwnerUserId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			vo.setSupplierUserId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			vo.setCustomerUserId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		}
		PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
		List<AbnormalShipbillPageDTO> list = tmsShipbillHeaderMapper.queryAbnormalShipbillPage(vo);
		Criteria criteriaLoadingBillDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
		for (AbnormalShipbillPageDTO dto : list) {
			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.getOrderType())));
			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())));

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

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

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

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

			// 调度信息获取
			Restriction r1 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, dto.getShipbillId());
			Restriction r2 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
			criteriaLoadingBillDetail.setRestriction(Restrictions.and(r1, r2));
			criteriaLoadingBillDetail.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
			List<TmsLoadingBillDetailModel> tmsLoadingBillDetails = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingBillDetail);
			if (ToolKits.isNotEmpty(tmsLoadingBillDetails)) {
				TmsLoadingBillDetailModel tmsLoadingBillDetail = tmsLoadingBillDetails.get(0);
				TmsLoadingBillHeaderModel loadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(tmsLoadingBillDetail.getLoadingBillId(), TmsLoadingBillHeaderModel.class);
				if (ToolKits.isNotEmpty(loadingBillHeaderModel)) {
					dto.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillNo());
					dto.setDeliverymanName(loadingBillHeaderModel.getDeliverymanName());
					dto.setVehiclePlateNumber(loadingBillHeaderModel.getVehiclePlateNumber());
				}
			}
		}
		return PageUtils.getPage(list);
	}

	@Override
	public Page<AbnormalShipbillPageDTO> advancedQueryAbnormalShipbillPage(AbnormalShipbillAdvancedQueryVO abnormalShipbillAdvancedQueryVO) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		// 权限控制 仅支持自有账号和承运商账号操作
		if (ObjectUtil.notEqual(user.getUserType(), BussinessConstant.USER_TYPE_ZY) && ObjectUtil.notEqual(user.getUserType(), BussinessConstant.USER_TYPE_CYS)) {
			throw new PrologException("当前人员没有操作权限");
		}
		// 权限控制
		if (user.getUserType().equals(BussinessConstant.USER_TYPE_ZY)) {
			// 判断是否是部门权限
			if (user.getOwnedAllDataAuthFlag().equals(BussinessConstant.USER_DEPARTMENT_AUTH_Y)) {
				abnormalShipbillAdvancedQueryVO.setIsDept(StatusConstant.IS_DEPT_YES);
				abnormalShipbillAdvancedQueryVO.setDepts(user.getDepts().toArray(new String[] {}));
			} else {
				abnormalShipbillAdvancedQueryVO.setIsDept(StatusConstant.IS_DEPT_NO);
				// 设置部门权限
				abnormalShipbillAdvancedQueryVO.setDepts(user.getDepts().toArray(new String[] {}));
				// 设置数据权限
				abnormalShipbillAdvancedQueryVO.setConsignors(user.getConsignors().toArray(new Long[] {}));
				abnormalShipbillAdvancedQueryVO.setWsarehouses(user.getWarehouses().toArray(new Long[] {}));
				abnormalShipbillAdvancedQueryVO.setCustomers(user.getCustomers().toArray(new Long[] {}));
				abnormalShipbillAdvancedQueryVO.setSuppliers(user.getSuppliers().toArray(new Long[] {}));
			}
		} else if (user.getUserType().equals(BussinessConstant.USER_TYPE_CYS)) {
			abnormalShipbillAdvancedQueryVO.setUnitId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_HZ)) {// 货主账号
			abnormalShipbillAdvancedQueryVO.setCargoOwnerUserId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_GYS)) {// 供应商账号
			abnormalShipbillAdvancedQueryVO.setSupplierUserId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else if (ObjectUtil.equal(user.getUserType(), BussinessConstant.USER_TYPE_KH)) {// 客户账号
			abnormalShipbillAdvancedQueryVO.setCustomerUserId(Long.valueOf(user.getLoginUserCompanyInfo().getId()));
		} else {
			throw new PrologException("当前人员没有操作权限");
		}
		abnormalShipbillAdvancedQueryVO.setUserType(user.getUserType());
		abnormalShipbillAdvancedQueryVO.setEnterpriseId(user.getEnterpriseId());

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

		PageUtils.startPage(abnormalShipbillAdvancedQueryVO.getPageNum(), abnormalShipbillAdvancedQueryVO.getPageSize());
		List<AbnormalShipbillPageDTO> list = tmsLoadingBillDetailMapper.advancedQueryAbnormalShipbillPage(abnormalShipbillAdvancedQueryVO);

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

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

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

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

							dto.setSignConfigId(signconfigPopDTO.getId());

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

							dto.setSignConfigId(signconfigPopDTO.getId());

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

							dto.setSignConfigId(signconfigPopDTO.getId());

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

							dto.setSignConfigId(signconfigPopDTO.getId());

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

		return PageUtils.getPage(list);
	}

	/**
	 * 运单商品查询
	 *
	 * @param
	 * @return abnormalShipbillItemDTO
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<AbnormalShipbillItemDTO> listItemDetailData(AbnormalShipbillItemQueryVO abnormalShipbillItemQueryVO) {
		ToolKits.isParamNull(abnormalShipbillItemQueryVO, "运单商品查询请求不为空");
		ToolKits.isParamNull(abnormalShipbillItemQueryVO.getShipbillId(), "运单ID不为空");

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

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

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

		// 获取商品信息
		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", abnormalShipbillItemQueryVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

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

		List<AbnormalShipbillItemDTO> abnormalShipbillItemDTOS = new ArrayList<AbnormalShipbillItemDTO>();

		if (ToolKits.isNotEmpty(tmsShipbillDetailModels)) {
			for (TmsShipbillDetailModel tmsShipbillDetailModel : tmsShipbillDetailModels) {
				AbnormalShipbillItemDTO abnormalShipbillItemDTO = new AbnormalShipbillItemDTO();
				BeanUtils.copyProperties(tmsShipbillDetailModel, abnormalShipbillItemDTO);
				abnormalShipbillItemDTO.setPackTypeName(PublicMethods.change(data1, String.valueOf(abnormalShipbillItemDTO.getPackType())));
				abnormalShipbillItemDTO.setCargoThermosphereName(PublicMethods.change(data2, String.valueOf(abnormalShipbillItemDTO.getCargoThermosphere())));
				abnormalShipbillItemDTOS.add(abnormalShipbillItemDTO);
			}
		}
		Page<AbnormalShipbillItemDTO> page = PageUtils.getPage(abnormalShipbillItemDTOS);
		if (abnormalShipbillItemDTOS instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillDetailModel> p0 = (com.github.pagehelper.Page<TmsShipbillDetailModel>) tmsShipbillDetailModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	// STD-DEVELOP20220823 lhb 异常关闭 20220926 start

	/**
	 * 运单异常查询
	 *
	 * @param abnormalShipbillItemQueryVO
	 * @return
	 */
	@SuppressWarnings("resource")
	public Page<AbnormalShipbillDTO> listAbnormalData(AbnormalShipbillItemQueryVO abnormalShipbillItemQueryVO) {
		ToolKits.isParamNull(abnormalShipbillItemQueryVO, "运单异常查询请求不为空");
		ToolKits.isParamNull(abnormalShipbillItemQueryVO.getShipbillId(), "运单ID不为空");

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

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

		// 异常类型
		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_STATUS, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("异常状态翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

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

		// 客户投诉
		RestMessage<List<Map<String, Object>>> restMessage4 = maindataTmsResource.getDropDownById(DataDictionaryConstant.ABNORMAL_COMPLAIN, null, null, null);
		if (!restMessage4.isSuccess()) {
			throw new RuntimeException("客户投诉翻译失败!");
		} else {
			data4 = restMessage4.getData();
		}

		// 获取运单异常信息
		Criteria criteria = Criteria.forClass(TmsShipbillAbnormalModel.class);
		Restriction r1 = Restrictions.eq(TmsShipbillAbnormalModel::getShipbillId, abnormalShipbillItemQueryVO.getShipbillId());
		Restriction r2 = Restrictions.eq(TmsShipbillAbnormalModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

		PageUtils.startPage(abnormalShipbillItemQueryVO.getPageNum(), abnormalShipbillItemQueryVO.getPageSize());
		List<TmsShipbillAbnormalModel> tmsShipbillAbnormalModels = tmsShipbillAbnormalMapper.findByCriteria(criteria);

		List<AbnormalShipbillDTO> abnormalShipbillDTOS = new ArrayList<AbnormalShipbillDTO>();

		if (ToolKits.isNotEmpty(tmsShipbillAbnormalModels)) {
			for (TmsShipbillAbnormalModel shipbillAbnormalModel : tmsShipbillAbnormalModels) {
				AbnormalShipbillDTO abnormalShipbillDTO = new AbnormalShipbillDTO();
				BeanUtils.copyProperties(shipbillAbnormalModel, abnormalShipbillDTO);
				abnormalShipbillDTO.setAbnormalTypeName(PublicMethods.change(data1, String.valueOf(abnormalShipbillDTO.getAbnormalType())));
				abnormalShipbillDTO.setAbnormalStatusNanme(PublicMethods.change(data2, String.valueOf(abnormalShipbillDTO.getAbnormalStatus())));
				abnormalShipbillDTO.setAbnormalEventName(PublicMethods.change(data3, String.valueOf(abnormalShipbillDTO.getAbnormalEvent())));
				abnormalShipbillDTO.setAbnormalComplainName(PublicMethods.change(data4, String.valueOf(abnormalShipbillDTO.getAbnormalComplain())));
				abnormalShipbillDTOS.add(abnormalShipbillDTO);
			}
		}
		Page<AbnormalShipbillDTO> page = PageUtils.getPage(abnormalShipbillDTOS);
		if (abnormalShipbillDTOS instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillAbnormalModel> p0 = (com.github.pagehelper.Page<TmsShipbillAbnormalModel>) tmsShipbillAbnormalModels;
			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;
	}
	// STD-DEVELOP20220823 lhb 异常关闭 20220926 end

	/**
	 * 运单包裹查询
	 *
	 * @param
	 * @return abnormalShipbillPackageDTO
	 */
	@SuppressWarnings("resource")
	@Override
	public Page<AbnormalShipbillPackageDTO> listPackageData(AbnormalShipbillPackageQueryVO abnormalShipbillPackageQueryVO) {
		ToolKits.isParamNull(abnormalShipbillPackageQueryVO, "运单包裹查询请求不为空");
		ToolKits.isParamNull(abnormalShipbillPackageQueryVO.getShipbillId(), "运单ID不为空");

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

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

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

		// 设备类型
		RestMessage<List<Map<String, Object>>> restMessage2 = maindataTmsResource.getDropDownById(DataDictionaryConstant.DEVICE_COMPANY, null, null, null);
		if (!restMessage2.isSuccess()) {
			throw new RuntimeException("设备类型翻译失败!");
		} else {
			data2 = restMessage2.getData();
		}

		// 添加查询条件
		Criteria criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		Restriction r1 = Restrictions.eq("shipbillId", abnormalShipbillPackageQueryVO.getShipbillId());
		Restriction r2 = Restrictions.eq("enterpriseId", user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		criteria.setOrder(Order.newInstance().desc(new String[] { "updatetime" }));

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

		// 响应对象集合
		List<AbnormalShipbillPackageDTO> abnormalShipbillPackageDTOs = new ArrayList<AbnormalShipbillPackageDTO>();

		if (ToolKits.isNotEmpty(tmsShipbillPackageModels)) {
			for (TmsShipbillPackageModel tmsShipbillPackageModel : tmsShipbillPackageModels) {
				AbnormalShipbillPackageDTO abnormalShipbillPackageDTO = new AbnormalShipbillPackageDTO();
				BeanUtils.copyProperties(tmsShipbillPackageModel, abnormalShipbillPackageDTO);
				abnormalShipbillPackageDTO.setPackageTypeName(PublicMethods.change(data1, String.valueOf(abnormalShipbillPackageDTO.getPackageType())));
				abnormalShipbillPackageDTO.setDeviceCompanyName(PublicMethods.change(data2, String.valueOf(abnormalShipbillPackageDTO.getDeviceCompany())));
				abnormalShipbillPackageDTOs.add(abnormalShipbillPackageDTO);
			}
		}
		Page<AbnormalShipbillPackageDTO> page = PageUtils.getPage(abnormalShipbillPackageDTOs);
		if (abnormalShipbillPackageDTOs instanceof com.github.pagehelper.Page) {
			com.github.pagehelper.Page<TmsShipbillPackageModel> p0 = (com.github.pagehelper.Page<TmsShipbillPackageModel>) tmsShipbillPackageModels;
			page.setPageCount(p0.getPages());
			page.setPageNum(p0.getPageNum());
			page.setPageSize(p0.getPageSize());
			page.setTotalCount(p0.getTotal());
			page.setStartRow(p0.getStartRow());
			page.setEndRow(p0.getEndRow());
		}
		return page;
	}

	/**
	 * 关闭运单全部异常
	 *
	 * @param ids
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void closeShipbill(List<String> ids) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(ids, "请选择要操作的运单数据");

		// 当前时间
		Date date = DateUtil.date();
		List<String> loadingDetailIds = Lists.newArrayList();
		for (String shipbillId : ids) {
			TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);
			if (ToolKits.isNotEmpty(tmsShipbillHeader)) {
				tmsShipbillHeader.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
				tmsShipbillHeader.setUpdatetime(date);
				tmsShipbillHeader.setUpdateperson(user.getNickname());
				tmsShipbillHeaderMapper.update(tmsShipbillHeader);

				// 关闭运单的所有异常
				Criteria criteria = Criteria.forClass(TmsShipbillAbnormalModel.class);
				Restriction r1 = Restrictions.eq(TmsShipbillAbnormalModel::getEnterpriseId, user.getEnterpriseId());
				Restriction r2 = Restrictions.eq(TmsShipbillAbnormalModel::getShipbillId, shipbillId);
				criteria.setRestriction(Restrictions.and(r1, r2));

				Map<String, Object> map = Maps.newHashMap();
				map.put("abnormalStatus", DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
				map.put("abnormalCloseTime", date);
				map.put("updateperson", user.getNickname());
				map.put("updatetime", DateUtil.date());
				tmsShipbillAbnormalMapper.updateMapByCriteria(map, criteria);

				// 更新装车单明细异常状态
				Criteria criteriaLoadingDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
				Restriction r3 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
				Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, shipbillId);
				criteriaLoadingDetail.setRestriction(Restrictions.and(r3, r4));
				criteriaLoadingDetail.setOrder(Order.newInstance().desc(new String[]{"createtime"}));
				List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingDetail);
				for (TmsLoadingBillDetailModel tmsLoadingBillDetailModel : tmsLoadingBillDetailModels) {
					tmsLoadingBillDetailModel.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
					tmsLoadingBillDetailModel.setUpdateperson(user.getNickname());
					tmsLoadingBillDetailModel.setUpdatetime(date);
					tmsLoadingBillDetailMapper.update(tmsLoadingBillDetailModel);
				}

				// 运单日志保存
				ShipbillOperateLogSaveNewVO operateLogVO = new ShipbillOperateLogSaveNewVO();
				operateLogVO.setShipbillId(tmsShipbillHeader.getShipbillId());
				operateLogVO.setShipbillNo(tmsShipbillHeader.getShipbillId());
				operateLogVO.setOperateEvent(DataDictionaryConstant.SHIPBILL_STATUSNAME_YCGB);
				operateLogVO.setOldStatus(tmsShipbillHeader.getStatus());
				operateLogVO.setNewStatus(tmsShipbillHeader.getStatus());
				operateLogVO.setOperateDate(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 = abnormalCloseFather(fatherShipbillId, operateLogVO, user);
				}
				// 运单同步BMS
				if (DataDictionaryConstant.SHIPBILL_STATUS_END.equals(tmsShipbillHeader.getStatus())
						&& ToolKits.isNotEmpty(tmsLoadingBillDetailModels)) {
					loadingDetailIds.add(tmsLoadingBillDetailModels.get(0).getId());
					//shipbillCommonService.pushShipbillBmsData(tmsShipbillHeader);
				}
			} else {
				throw new PrologException("未查询到运单");
			}
		}

		// 运单同步BMS
		if (ToolKits.isNotEmpty(loadingDetailIds)) {
			shipbillCommonService.pushShipbillBmsData(loadingDetailIds);
		}
	}

	// STD-DEVELOP20220823 lhb 异常关闭 20220926 start

	/**
	 * 关闭运单的一个异常
	 *
	 * @param ids
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void closeAbnormalShipbill(List<String> ids) {
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");
		ToolKits.isParamNull(ids, "请选择要操作的运单的异常信息");

		// 当前时间
		Date date = DateUtil.date();
		List<String> loadingDetailIds = Lists.newArrayList();
		for (String abnormalId : ids) {
			TmsShipbillAbnormalModel shipbillAbnormalModel = tmsShipbillAbnormalMapper.findById(abnormalId, TmsShipbillAbnormalModel.class);
			if (ToolKits.isEmpty(shipbillAbnormalModel)) {
				throw new PrologException("运单【" + shipbillAbnormalModel.getShipbillNo() + "】的异常不存在");
			}

			TmsShipbillHeaderModel tmsShipbillHeader = tmsShipbillHeaderMapper.findById(shipbillAbnormalModel.getShipbillId(), TmsShipbillHeaderModel.class);

			if (ToolKits.isEmpty(tmsShipbillHeader)) {
				throw new PrologException("运单【" + shipbillAbnormalModel.getShipbillNo() + "】不存在");
			}

			if (ToolKits.isNotEmpty(shipbillAbnormalModel)) {
				shipbillAbnormalModel.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
				shipbillAbnormalModel.setAbnormalCloseTime(date);
				shipbillAbnormalModel.setUpdatetime(date);
				shipbillAbnormalModel.setUpdateperson(user.getNickname());
				tmsShipbillAbnormalMapper.update(shipbillAbnormalModel);

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

				// 添加查询条件
				Criteria criteria = Criteria.forClass(TmsShipbillAbnormalModel.class);
				Restriction r1 = Restrictions.eq(TmsShipbillAbnormalModel::getShipbillId, shipbillAbnormalModel.getShipbillId());
				Restriction r2 = Restrictions.eq(TmsShipbillAbnormalModel::getEnterpriseId, user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsShipbillAbnormalModel> shipbillAbnormalModels = tmsShipbillAbnormalMapper.findByCriteria(criteria);

				if (ToolKits.isEmpty(shipbillAbnormalModels)) {
					continue;
				}
				int size = shipbillAbnormalModels.stream().filter(o -> o.getAbnormalStatus().intValue() < DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS.intValue()).collect(Collectors.toList()).size();
				if (size == 0) {
					// 更新运单的异常状态
					tmsShipbillHeader.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
					tmsShipbillHeader.setUpdatetime(date);
					tmsShipbillHeader.setUpdateperson(user.getNickname());
					tmsShipbillHeaderMapper.update(tmsShipbillHeader);

					// 更新装车单明细异常状态
					Criteria criteriaLoadingDetail = Criteria.forClass(TmsLoadingBillDetailModel.class);
					Restriction r3 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
					Restriction r4 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, shipbillAbnormalModel.getShipbillId());
					criteriaLoadingDetail.setRestriction(Restrictions.and(r3, r4));
					criteriaLoadingDetail.setOrder(Order.newInstance().desc(new String[]{"createtime"}));
					List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteriaLoadingDetail);
					for (TmsLoadingBillDetailModel tmsLoadingBillDetailModel : tmsLoadingBillDetailModels) {
						tmsLoadingBillDetailModel.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
						tmsLoadingBillDetailModel.setUpdateperson(user.getNickname());
						tmsLoadingBillDetailModel.setUpdatetime(date);
						tmsLoadingBillDetailMapper.update(tmsLoadingBillDetailModel);
					}

					// 关闭主单的异常状态
					String fatherShipbillId = tmsShipbillHeader.getFatherShipbillId();
					while (ToolKits.isNotEmpty(fatherShipbillId)) {
						fatherShipbillId = abnormalCloseFather(fatherShipbillId, operateLogVO, user);
					}

					// 运单同步BMS
					if (DataDictionaryConstant.SHIPBILL_STATUS_END.equals(tmsShipbillHeader.getStatus())
							&& ToolKits.isNotEmpty(tmsLoadingBillDetailModels)) {
						loadingDetailIds.add(tmsLoadingBillDetailModels.get(0).getId());
						//shipbillCommonService.pushShipbillBmsData(tmsShipbillHeader);
					}
				}
			} else {
				throw new PrologException("未查询到运单异常明细");
			}
		}
		// 运单同步BMS
		if (ToolKits.isNotEmpty(loadingDetailIds)) {
			shipbillCommonService.pushShipbillBmsData(loadingDetailIds);
		}
	}

	/**
	 * 递归对上级运单进行异常关闭
	 */
	private String abnormalCloseFather(String shipbillId, ShipbillOperateLogSaveNewVO operateLogVO, UserEntity user) {
		TmsShipbillHeaderModel fatherShipbill = tmsShipbillHeaderMapper.findById(shipbillId, TmsShipbillHeaderModel.class);

		// 查找父单下面的所有子单
		Criteria criteriaFatherShipbillHeader = Criteria.forClass(TmsShipbillHeaderModel.class);
		Restriction r1 = Restrictions.eq(TmsShipbillHeaderModel::getFatherShipbillId, fatherShipbill.getShipbillId());
		Restriction r2 = Restrictions.eq(TmsShipbillHeaderModel::getEnterpriseId, user.getEnterpriseId());
		criteriaFatherShipbillHeader.setRestriction(Restrictions.and(r1, r2));
		List<TmsShipbillHeaderModel> tmsShipbillHeaderModels = tmsShipbillHeaderMapper.findByCriteria(criteriaFatherShipbillHeader);
		if (ObjectUtil.isNotEmpty(tmsShipbillHeaderModels)) {
			List<TmsShipbillHeaderModel> filterList = tmsShipbillHeaderModels.stream().filter(dto -> DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS.equals(dto.getAbnormalStatus())).collect(Collectors.toList());
			// 所有子单都已异常关闭
			if (filterList.size() == tmsShipbillHeaderModels.size()) {
				// 更新运单
				fatherShipbill.setUpdatetime(DateUtil.date());
				fatherShipbill.setUpdateperson(user.getNickname());
				fatherShipbill.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
				tmsShipbillHeaderMapper.update(fatherShipbill);

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

				// 关闭运单的所有异常
				Criteria criteria = Criteria.forClass(TmsShipbillAbnormalModel.class);
				Restriction r3 = Restrictions.eq(TmsShipbillAbnormalModel::getEnterpriseId, user.getEnterpriseId());
				Restriction r4 = Restrictions.eq(TmsShipbillAbnormalModel::getShipbillId, shipbillId);
				criteria.setRestriction(Restrictions.and(r3, r4));
				List<TmsShipbillAbnormalModel> tmsShipbillAbnormals = tmsShipbillAbnormalMapper.findByCriteria(criteria);
				for (TmsShipbillAbnormalModel tmsShipbillAbnormal : tmsShipbillAbnormals) {
					tmsShipbillAbnormal.setAbnormalStatus(DataDictionaryConstant.SHIPBILL_ABNORMAL_CLOSE_STATUS);
					tmsShipbillAbnormal.setAbnormalCloseTime(DateUtil.date());
					tmsShipbillAbnormal.setUpdatetime(DateUtil.date());
					tmsShipbillAbnormal.setUpdateperson(user.getNickname());
					tmsShipbillAbnormalMapper.update(tmsShipbillAbnormal);
				}

				return fatherShipbill.getFatherShipbillId();
			}
		}

		return null;
	}
	// STD-DEVELOP20220823 lhb 异常关闭 20220926 end

}
