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

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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.prolog.cs.platform.power.model.Organization;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Order;
import com.prolog.rdc.tms.app.dao.mapper.*;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillPackageModel;
import com.prolog.rdc.tms.model.dto.SySParameterDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.DriverCheckDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.VehicleTypeCheckDTO;
import com.prolog.rdc.tms.model.vo.BillDownloadVO;
import com.prolog.rdc.tms.model.vo.BmsShipBillPackVO;
import com.prolog.rdc.tms.model.vo.LoadingBillVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.DriverCheckVO;
import com.prolog.rdc.tms.model.vo.fleetManagement.VehicleTypeCheckVO;
import com.prolog.rdc.tms.rmi.feign.InterfaceFeign;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.rmi.feign.UserFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.product.core.framework.entity.UserEntity;
import com.prolog.product.core.framework.util.PlgFxUsrUtils;
import com.prolog.rdc.tms.app.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.app.constant.StatusConstant;
import com.prolog.rdc.tms.app.model.entity.TmsLoadingBillDetailModel;
import com.prolog.rdc.tms.app.model.entity.TmsLoadingBillHeaderModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillDetailModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.app.model.entity.TmsShipbillTransLogModel;
import com.prolog.rdc.tms.app.service.business.ShipbillCommonService;
import com.prolog.rdc.tms.model.dto.ShipbillResultDTO;
import com.prolog.rdc.tms.model.vo.BmsShipBillDetailVO;
import com.prolog.rdc.tms.model.vo.ShipBillVO;
import com.prolog.rdc.tms.rmi.feign.ShipBillRecordFeign;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 *
 * 运单公共处理实现层
 *
 * @className :ShipbillCommonServiceImpl
 * @author :fanzw
 * @creationTime :2022/09/08
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/09/08
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class ShipbillCommonServiceImpl implements ShipbillCommonService {

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private TmsShipbillDetailMapper tmsShipbillDetailMapper;

	@Autowired
	private TmsShipbillTransportLogMapper tmsShipbillTransportLogMapper;

	@Autowired
	private TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

	@Autowired
	private TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

	@Autowired
	private InterfaceFeign interfaceTmsResource;

	@Autowired
	private MaindataFeign maindataTmsResource;

	@Autowired
	private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

	@Autowired
	private UserFeign userTmsResource;

	@Autowired
	private TmsShipbillPackageMapper shipBillPackageMapper;

	/**
	 * 运单推送BMS
	 */
	@Override
	public void pushShipbillBmsData(List<String> ids) {
		log.info("运单:{}推送BMS开始", JSON.toJSONString(ids));
		List<TmsLoadingBillDetailModel> loadingBillDetailModels = tmsLoadingBillDetailMapper.findByIds(TmsLoadingBillDetailModel.class,ids.stream().toArray(), null);

		if (ToolKits.isEmpty(loadingBillDetailModels)) {
			log.info("派车单明细为空,不需要上传BMS");
			return;
		}
		UserEntity user = PlgFxUsrUtils.getUserEntity();
		ToolKits.isParamNull(user, "当前人员没有操作权限");

		Map<Object,List<TmsLoadingBillDetailModel>> loadingBillDtlMaps = Maps.newHashMap();
		loadingBillDtlMaps = loadingBillDetailModels.stream().collect(Collectors.groupingBy(o -> o.getLoadingBillId()));

		// 部门
		RestMessage<List<Organization>> departMents = userTmsResource.search(null, null, null, String.valueOf(user.getEnterpriseId()));
		if (!departMents.isSuccess()) {
			throw new BizException("部门查询失败：" + departMents.getMessage());
		}
		List<Organization> departmentData = departMents.getData();

		List<BillDownloadVO> billDownloadVOS = Lists.newArrayList();
		for (Object obj:loadingBillDtlMaps.keySet()) {
			List<TmsLoadingBillDetailModel> dlLoadingBillDetailModels = loadingBillDtlMaps.get(obj);
			TmsShipbillTransLogModel shipbillTransLogModel =null;
			DriverCheckDTO driverCheckDTO = null;
			TmsLoadingBillHeaderModel loadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(dlLoadingBillDetailModels.get(0).getLoadingBillId(), TmsLoadingBillHeaderModel.class);
			if (DataDictionaryConstant.LINE_CARRIER_TYPE_SJ.equals(loadingBillHeaderModel.getCarrierType())) {
				// 获取司机的隶属机构
				DriverCheckVO driverCheckVO = new DriverCheckVO();
				driverCheckVO.setId(loadingBillHeaderModel.getDriverId());
				RestMessage<DriverCheckDTO> driverCheckDTORestMessage = maindataFeign.getDriverExecute(driverCheckVO);
				if (!driverCheckDTORestMessage.isSuccess()) {
					log.info(StrUtil.format("司机{}的隶属机构获取失败",loadingBillHeaderModel.getDriverName()));
				} else {
					driverCheckDTO = driverCheckDTORestMessage.getData();
				}
			}
			BillDownloadVO billDownloadVO = new BillDownloadVO();
			List<ShipBillVO> dlShipBillVOS = Lists.newArrayList();
			for (TmsLoadingBillDetailModel loadingBillDetailModel:dlLoadingBillDetailModels) {
				TmsShipbillHeaderModel newShipBillHeaderModel = tmsShipbillHeaderMapper.findById(loadingBillDetailModel.getShipbillId(),TmsShipbillHeaderModel.class);
				if (DataDictionaryConstant.SHIPBILL_ABNORMAL_STATUS.equals(newShipBillHeaderModel.getAbnormalStatus())){
					continue;
				}
				// 请求汇总
				ShipBillVO shipBillVO = new ShipBillVO();
				BeanUtils.copyProperties(newShipBillHeaderModel, shipBillVO);
				shipBillVO.setSettlementDepartmentId(newShipBillHeaderModel.getDepartmentId());
				shipBillVO.setSettlementDepartmentCode(newShipBillHeaderModel.getDepartmentCode());
				shipBillVO.setSettlementDepartment(newShipBillHeaderModel.getDepartmentName());
				shipBillVO.setDepartmentId(newShipBillHeaderModel.getDepartmentId());
				shipBillVO.setDepartmentCode(newShipBillHeaderModel.getDepartmentCode());
				shipBillVO.setDepartmentName(newShipBillHeaderModel.getDepartmentName());
				shipBillVO.setNickname(user.getUsername());
				shipBillVO.setOrganizationId(user.getOrganizationId());
				shipBillVO.setOrganizationName(user.getOrgnaizationName());
				shipBillVO.setDeliveryAreas(newShipBillHeaderModel.getConsigneeStationAreas());
				shipBillVO.setDeliveryStationId(newShipBillHeaderModel.getConsigneeStationId());
				shipBillVO.setDeliveryStationCode(newShipBillHeaderModel.getConsigneeStationCode());
				shipBillVO.setDeliveryStationName(newShipBillHeaderModel.getConsigneeStationName());
				shipBillVO.setLineId(newShipBillHeaderModel.getLineId());
				shipBillVO.setLineCode(newShipBillHeaderModel.getLineCode());
				shipBillVO.setLineName(newShipBillHeaderModel.getLineName());

				// 查询运单记录表
				Criteria criteria = Criteria.forClass(TmsShipbillTransLogModel.class);
				Restriction r1 = Restrictions.eq(TmsShipbillTransLogModel::getShipbillId, newShipBillHeaderModel.getShipbillId());
				Restriction r2 = Restrictions.eq(TmsShipbillTransLogModel::getEnterpriseId, user.getEnterpriseId());
				criteria.setRestriction(Restrictions.and(r1, r2));
				List<TmsShipbillTransLogModel> shipbillTransLogModels = tmsShipbillTransportLogMapper.findByCriteria(criteria);
				Assert.isTrue(shipbillTransLogModels.size() > 0, "运单【" + newShipBillHeaderModel.getShipbillId() + "】运输记录未维护");
				Assert.isTrue(shipbillTransLogModels.size() == 1, "运单【" + newShipBillHeaderModel.getShipbillId() + "】运输记录维护重复");

				// 运单运输记录对象
				shipbillTransLogModel = shipbillTransLogModels.get(0);
				shipBillVO.setTotalPcs(newShipBillHeaderModel.getTotalDeliveryPcs());
				shipBillVO.setTotalVolume(newShipBillHeaderModel.getTotalDeliveryVolume());
				shipBillVO.setTotalWeight(newShipBillHeaderModel.getTotalDeliveryWeight());
				shipBillVO.setSubmitDate(shipbillTransLogModel.getCommitDt());
				shipBillVO.setAuditDate(shipbillTransLogModel.getReviewDt());
				shipBillVO.setLoadingDate(shipbillTransLogModel.getDisDt());
				shipBillVO.setLoadingAuditDate(shipbillTransLogModel.getDisCheckDt());
				shipBillVO.setStartDate(shipbillTransLogModel.getDepartureTime());
				shipBillVO.setDeliveryDate(shipbillTransLogModel.getDeliveryTime());
				shipBillVO.setReceiptDate(shipbillTransLogModel.getReceiptTime());
				shipBillVO.setOriginalBackDate(shipbillTransLogModel.getOrigReceiptReturnTime());
				shipBillVO.setTransportAging(newShipBillHeaderModel.getTransportAging());
				if (newShipBillHeaderModel.getReturnOrderFlag().intValue() == StatusConstant.WHETHER_YES.intValue()) {
					shipBillVO.setCompleteDate(shipbillTransLogModel.getOrigReceiptReturnTime());
				} else if (newShipBillHeaderModel.getReturnOrderFlag().intValue() == StatusConstant.WHETHER_NO.intValue() && newShipBillHeaderModel.getConsigneeReturnOrderPicture().intValue() == StatusConstant.WHETHER_YES.intValue()) {
					shipBillVO.setCompleteDate(shipbillTransLogModel.getReceiptTime());
				} else {
					shipBillVO.setCompleteDate(shipbillTransLogModel.getDeliveryTime());
				}
				// 查询装车单明细
				Criteria criteria1 = Criteria.forClass(TmsLoadingBillDetailModel.class);
				Restriction r11 = Restrictions.eq(TmsLoadingBillDetailModel::getShipbillId, newShipBillHeaderModel.getShipbillId());
				Restriction r13 = Restrictions.eq(TmsLoadingBillDetailModel::getEnterpriseId, user.getEnterpriseId());
				criteria1.setRestriction(Restrictions.and(r11, r13));
				criteria1.setOrder(Order.newInstance().desc(new String[] { "createtime" }));
				List<TmsLoadingBillDetailModel> loadingDetailModels = tmsLoadingBillDetailMapper.findByCriteria(criteria1);
				if (loadingDetailModels.size() > 0) {
					// 装车单查询
					if (loadingDetailModels.size() > 1) {
						// 承运商调度给司机
						shipBillVO.setCarrierId(loadingDetailModels.get(1).getCarrierId());
						shipBillVO.setCarrierCode(loadingDetailModels.get(1).getCarrierCode());
						shipBillVO.setCarrierName(loadingDetailModels.get(1).getCarrierName());
						shipBillVO.setCarrierType(loadingDetailModels.get(1).getCarrierType());
					} else {
						if (DataDictionaryConstant.LINE_CARRIER_TYPE_CYF.equals(loadingBillHeaderModel.getCarrierType())) {
							shipBillVO.setCarrierId(loadingBillHeaderModel.getCarrierId());
							shipBillVO.setCarrierCode(loadingBillHeaderModel.getCarrierCode());
							shipBillVO.setCarrierName(loadingBillHeaderModel.getCarrierName());
						} else if (DataDictionaryConstant.LINE_CARRIER_TYPE_SJ.equals(loadingBillHeaderModel.getCarrierType())) {
							// 获取司机的隶属机构
							shipBillVO.setCarrierId(driverCheckDTO.getAffiliationId());
							shipBillVO.setCarrierCode(driverCheckDTO.getAffiliationCode());
							shipBillVO.setCarrierName(driverCheckDTO.getAffiliationName());
						}
					}
					shipBillVO.setDriverId(loadingBillHeaderModel.getDriverId());
					shipBillVO.setDriverCode(loadingBillHeaderModel.getDriverCode());
					shipBillVO.setDriverName(loadingBillHeaderModel.getDriverName());
					shipBillVO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
					shipBillVO.setVehicleId(loadingBillHeaderModel.getVehicleId());
					shipBillVO.setVehicleCode(loadingBillHeaderModel.getVehicleCode());
					shipBillVO.setVehicleName(loadingBillHeaderModel.getVehiclePlateNumber());
					shipBillVO.setDeliveryName(loadingBillHeaderModel.getDeliverymanName());
					shipBillVO.setDeliveryTel(loadingBillHeaderModel.getDeliverymanPhoneNumber());
					shipBillVO.setTotalMileage(loadingBillHeaderModel.getTotalMileage());
					shipBillVO.setAffiliationId(loadingBillHeaderModel.getCarrierId());
					shipBillVO.setAffiliationCode(loadingBillHeaderModel.getCarrierCode());
					shipBillVO.setAffiliationName(loadingBillHeaderModel.getCarrierName());
					shipBillVO.setVehicleTypeId(loadingBillHeaderModel.getVehicleTypeId());
					shipBillVO.setVehicleTypeCode(loadingBillHeaderModel.getVehicleTypeCode());
					shipBillVO.setVehicleTypeName(loadingBillHeaderModel.getVehicleTypeName());
					shipBillVO.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillNo());

					// 请求明细集合
					List<BmsShipBillDetailVO> bmsShipBillDetailVOs = Lists.newArrayList();

					// 运单明细查询
					List<TmsShipbillDetailModel> tmsShipbillDetailModels = listShipbillDetailData(newShipBillHeaderModel.getShipbillId(), user);
					for (TmsShipbillDetailModel tmsShipbillDetailModel : tmsShipbillDetailModels) {
						// 请求明细对象
						BmsShipBillDetailVO bmsShipBillDetailVO = new BmsShipBillDetailVO();
						BeanUtils.copyProperties(tmsShipbillDetailModel, bmsShipBillDetailVO);
						bmsShipBillDetailVO.setPcs(tmsShipbillDetailModel.getDeliveryPcs());
						bmsShipBillDetailVO.setWeight(NumberUtil.mul(tmsShipbillDetailModel.getDeliveryPcs(),tmsShipbillDetailModel.getUnitWeight()));
						bmsShipBillDetailVO.setVolume(NumberUtil.mul(tmsShipbillDetailModel.getDeliveryPcs(),tmsShipbillDetailModel.getUnitVolume()));
						// 赋值
						bmsShipBillDetailVOs.add(bmsShipBillDetailVO);
					}
					shipBillVO.setBmsShipBillDetailList(bmsShipBillDetailVOs);
					List<TmsShipbillPackageModel> packageModelList = shipBillPackage(newShipBillHeaderModel.getShipbillId(), user);
					if (CollUtil.isNotEmpty(packageModelList)) {
						shipBillVO.setPackList(packageModelList.stream().map(packageModel -> BeanUtil.copyProperties(packageModel, BmsShipBillPackVO.class)).collect(Collectors.toList()));
					}
					dlShipBillVOS.add(shipBillVO);
				}
			}
			billDownloadVO.setShipBillVOList(dlShipBillVOS);

			// 运单，派车单 下传方式
//			RestMessage<SySParameterDTO> parameterModel = maindataTmsResource.queryParameter();
//			Integer tmsTransferOrderType = parameterModel.getData().getTmsTransferOrderType();
//			// 推送派车单
//			if (DataDictionaryConstant.TMS_TRANSFER_ORDER_TYPE_SHIP_LOADING_BILL.equals(tmsTransferOrderType)
//					&& loadingBillHeaderModel.getCarrierType().equals(DataDictionaryConstant.LINE_CARRIER_TYPE_SJ) &&
//					ToolKits.isNotEmpty(billDownloadVO.getShipBillVOList())) {
//				Criteria criteria2 = Criteria.forClass(TmsLoadingBillDetailModel.class);
//				Restriction r22 = Restrictions.eq(TmsLoadingBillDetailModel::getLoadingBillNo, loadingBillHeaderModel.getLoadingBillNo());
//				criteria2.setRestriction(Restrictions.and(r22));
//				List<TmsLoadingBillDetailModel> loadingBillDetailModelList = tmsLoadingBillDetailMapper.findByCriteria(criteria2);
//				if (ToolKits.isEmpty(loadingBillDetailModelList)) {
//					log.info(StrUtil.format("派车单{}明细不存在,不推送BMS",loadingBillHeaderModel.getLoadingBillNo()));
//					return;
//				}
//				String stationLines = null;
//				if (ToolKits.isNotEmpty(loadingBillHeaderModel.getGroupName()) &&
//						loadingBillHeaderModel.getGroupName().contains("站点")) {
//					stationLines = loadingBillHeaderModel.getDeliveryLines();
//				} else {
//					Map<Integer,String> stationMap = Maps.newTreeMap();
//					List<TmsLoadingBillDetailModel> loadingDtls = loadingBillDetailModelList.stream().filter(l->ToolKits.isNotEmpty(l.getShippingOrder()) && ToolKits.isNotEmpty(l.getPickUpOrder())).collect(Collectors.toList());
//					if (ToolKits.isNotEmpty(loadingDtls)) {
//						loadingDtls.stream().forEach(shipbill -> {
//							stationMap.put(shipbill.getPickUpOrder(), shipbill.getShipperStationName());
//							stationMap.put(shipbill.getShippingOrder(), shipbill.getConsigneeStationName());
//						});
//						List<String> stationNames = stationMap.values().stream().filter(val->ToolKits.isNotEmpty(val)).collect(Collectors.toList());
//						stationLines = stationNames.stream().collect(Collectors.joining("-"));
//					}
//				}
//				// 派车单结算部门
//				long size1 = loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getDepartmentId).distinct().collect(Collectors.toList()).size();
//				String settlementDepartmentId = null;
//				String settlementDepartmentCode = null;
//				String settlementDepartmentName = null;
//				if (ToolKits.isNotEmpty(size1) && 1 == size1) {
//					settlementDepartmentId = loadingBillDetailModelList.get(0).getDepartmentId();
//					settlementDepartmentCode = loadingBillDetailModelList.get(0).getDepartmentCode();
//					settlementDepartmentName = loadingBillDetailModelList.get(0).getDepartmentName();
//				} else {
//					settlementDepartmentId = departmentData.get(0).getId();
//					settlementDepartmentCode = departmentData.get(0).getCode();
//					settlementDepartmentName = departmentData.get(0).getName();
//				}
//				// 派车单提货区域
//				long size2 = loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getShipperStationAreas).distinct().collect(Collectors.toList()).size();
//				String shipperStationAreas = null;
//				if (ToolKits.isNotEmpty(size2) && 1 == size2) {
//					shipperStationAreas = loadingBillDetailModelList.get(0).getShipperStationAreas();
//				}
//				// 派车单送货区域
//				long size3 = loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getConsigneeStationAreas).distinct().collect(Collectors.toList()).size();
//				String consigneeStationAreas = null;
//				if (ToolKits.isNotEmpty(size3) && 1 == size3) {
//					consigneeStationAreas = loadingBillDetailModelList.get(0).getConsigneeStationAreas();
//				}
//				BigDecimal vehicleWeight = BigDecimal.ZERO;
//				BigDecimal vehicleVolume = BigDecimal.ZERO;
//				if (ToolKits.isNotEmpty(loadingBillHeaderModel.getVehicleTypeName())) {
//					VehicleTypeCheckVO vehicleTypeCheckVO = new VehicleTypeCheckVO();
//					vehicleTypeCheckVO.setVehicleTypeName(loadingBillHeaderModel.getVehicleTypeName());
//					RestMessage<VehicleTypeCheckDTO> vehicleTypeRestMessage = maindataFeign.getVehicleTypeByNameExecute(vehicleTypeCheckVO);
//					if (!vehicleTypeRestMessage.isSuccess()) {
//						log.info(StrUtil.format("车型{}的载重,载方获取失败",loadingBillHeaderModel.getVehicleTypeName()));
//					} else {
//						VehicleTypeCheckDTO vehicleTypeDTO = vehicleTypeRestMessage.getData();
//						vehicleWeight = vehicleTypeDTO.getWeight();
//						vehicleVolume = vehicleTypeDTO.getVolume();
//					}
//				}
//
//				// 按货主拆分派车单
//				Map<Object,List<TmsLoadingBillDetailModel>> hzLoadingBillDtlMaps = Maps.newHashMap();
//				hzLoadingBillDtlMaps = loadingBillDetailModelList.stream().collect(Collectors.groupingBy(o -> o.getCargoOwnerId()));
//				List<LoadingBillVO> loadingBillVOS = Lists.newArrayList();
//				for (Object key:hzLoadingBillDtlMaps.keySet()) {
//					List<TmsLoadingBillDetailModel> hzLoadingDtls = hzLoadingBillDtlMaps.get(key);
//
//					BigDecimal pcs1 = hzLoadingDtls.stream().filter(dto -> ToolKits.isNotEmpty(dto.getTotalDeliveryPcs())).map(TmsLoadingBillDetailModel::getTotalDeliveryPcs).reduce(BigDecimal.ZERO, BigDecimal::add);
//					BigDecimal pcs2 = hzLoadingDtls.stream().filter(dto -> ToolKits.isNotEmpty(dto.getPackageLclPiece())).map(TmsLoadingBillDetailModel::getPackageLclPiece).reduce(BigDecimal.ZERO, BigDecimal::add);
//					BigDecimal pcs3 = hzLoadingDtls.stream().filter(dto -> ToolKits.isNotEmpty(dto.getPackageWholePiece())).map(TmsLoadingBillDetailModel::getPackageWholePiece).reduce(BigDecimal.ZERO, BigDecimal::add);
//					BigDecimal weigth1 = hzLoadingDtls.stream().filter(dto -> ToolKits.isNotEmpty(dto.getTotalDeliveryWeight())).map(TmsLoadingBillDetailModel::getTotalDeliveryWeight).reduce(BigDecimal.ZERO, BigDecimal::add);
//					BigDecimal volume1 = hzLoadingDtls.stream().filter(dto -> ToolKits.isNotEmpty(dto.getTotalDeliveryVolume())).map(TmsLoadingBillDetailModel::getTotalDeliveryVolume).reduce(BigDecimal.ZERO, BigDecimal::add);
//
//					LoadingBillVO loadingBillVO = new LoadingBillVO();
//					BeanUtil.copyProperties(loadingBillHeaderModel,loadingBillVO);
//
//					loadingBillVO.setTotalPcs(pcs1);
//					loadingBillVO.setActualTotalPcs(pcs1);
//					loadingBillVO.setTotalLclPiece(pcs2);
//					loadingBillVO.setTotalWholePiece(pcs3);
//
//					loadingBillVO.setTotalWeight(weigth1);
//					loadingBillVO.setActualTotalWeight(weigth1);
//
//					loadingBillVO.setTotalVolume(volume1);
//					loadingBillVO.setActualTotalVolume(volume1);
//					if (ToolKits.isNotEmpty(shipbillTransLogModel)) {
//						loadingBillVO.setLoadingDate(shipbillTransLogModel.getDisDt());
//						loadingBillVO.setLoadingAuditDate(shipbillTransLogModel.getDisCheckDt());
//						loadingBillVO.setStartDate(shipbillTransLogModel.getDepartureTime());
//					}
//
//					loadingBillVO.setSettlementDepartmentId(settlementDepartmentId);
//					loadingBillVO.setSettlementDepartmentCode(settlementDepartmentCode);
//					loadingBillVO.setSettlementDepartment(settlementDepartmentName);
//
//					loadingBillVO.setCargoOwnerId(hzLoadingDtls.get(0).getCargoOwnerId());
//					loadingBillVO.setCargoOwnerCode(hzLoadingDtls.get(0).getCargoOwnerCode());
//					loadingBillVO.setCargoOwnerName(hzLoadingDtls.get(0).getCargoOwnerName());
//
//					loadingBillVO.setDeliveryLines(loadingBillHeaderModel.getDeliveryLines());
//					loadingBillVO.setStationLines(stationLines);
//					if (ToolKits.isNotEmpty(driverCheckDTO)) {
//						loadingBillVO.setCarrierId(driverCheckDTO.getAffiliationId());
//						loadingBillVO.setCarrierCode(driverCheckDTO.getAffiliationCode());
//						loadingBillVO.setCarrierName(driverCheckDTO.getAffiliationName());
//					} else {
//						loadingBillVO.setCarrierId(loadingBillHeaderModel.getCarrierId());
//						loadingBillVO.setCarrierCode(loadingBillHeaderModel.getCarrierCode());
//						loadingBillVO.setCarrierName(loadingBillHeaderModel.getCarrierName());
//					}
//					loadingBillVO.setCarrierType(loadingBillHeaderModel.getCarrierType());
//					loadingBillVO.setScheduleType(loadingBillHeaderModel.getScheduleType());
//
//					loadingBillVO.setPickUpAreas(shipperStationAreas);
//					loadingBillVO.setDeliveryAreas(consigneeStationAreas);
//
//					loadingBillVO.setVehicleVolume(vehicleVolume);
//					loadingBillVO.setVehicleWeight(vehicleWeight);
//					loadingBillVO.setVehicleTypeCategory(loadingBillHeaderModel.getVehicleTypeCategory());
//
//					loadingBillVO.setShipbillNos(hzLoadingDtls.stream().map(TmsLoadingBillDetailModel::getShipbillNo).collect(Collectors.joining(",")));
//
//					loadingBillVOS.add(loadingBillVO);
//				}
//				billDownloadVO.setLoadingBillVOList(loadingBillVOS);
//			}
			billDownloadVO.setUserName(user.getUsername());
			billDownloadVO.setNickName(user.getNickname());
			billDownloadVO.setEnterpriseName(user.getEnterpriseName());
			billDownloadVO.setEnterpriseId(user.getEnterpriseId());

			billDownloadVOS.add(billDownloadVO);
		}
		log.info("推送BMS报文:{}", JSONObject.toJSONString(billDownloadVOS));

		// 请求
		RestMessage<Object> restMessage = interfaceTmsResource.saveShipbillBmsExecute(billDownloadVOS);
		//RestMessage<Object> restMessage = bmsBillRecordFeign.downloadSave(billDownloadVOS);
		if (restMessage.isSuccess()) {
			log.info("】推送BMS成功");
		} else {
			log.info("推送BMS失败:" + restMessage.getMessage());
		}
	}

	/**
	 * 运单明细查询
	 */
	@Override
	public List<TmsShipbillDetailModel> listShipbillDetailData(String shipbillId, UserEntity user) {
		Criteria criteria = Criteria.forClass(TmsShipbillDetailModel.class);
		Restriction r1 = Restrictions.eq(TmsShipbillDetailModel::getShipbillId, shipbillId);
		Restriction r2 = Restrictions.eq(TmsShipbillDetailModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));

		List<TmsShipbillDetailModel> tmsShipbillDetailModels = tmsShipbillDetailMapper.findByCriteria(criteria);

		return tmsShipbillDetailModels;
	}

	public List<TmsShipbillPackageModel> shipBillPackage(String shipbillId, UserEntity user) {
		Criteria criteria = Criteria.forClass(TmsShipbillPackageModel.class);
		Restriction r1 = Restrictions.eq(TmsShipbillPackageModel::getShipbillId, shipbillId);
		Restriction r2 = Restrictions.eq(TmsShipbillPackageModel::getEnterpriseId, user.getEnterpriseId());
		criteria.setRestriction(Restrictions.and(r1, r2));
		return shipBillPackageMapper.findByCriteria(criteria);
	}

}
