package com.prolog.cs.biz.inbound.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.prolog.cs.basic.warehouse.model.BaseWmsWarehouseLocator;
import com.prolog.cs.biz.inbound.constant.BizConstant;
import com.prolog.cs.biz.inbound.dao.task.TaskInstockDetailMapper;
import com.prolog.cs.biz.inbound.dto.CargoChangeIn;
import com.prolog.cs.biz.inbound.dto.rcv.RcvInstockLinesDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcHeadersDTO;
import com.prolog.cs.biz.inbound.dto.rqc.RqcLinesDTO;
import com.prolog.cs.biz.inbound.dto.task.TaskInstockDetailDTO;
import com.prolog.cs.biz.inbound.remote.BaseWmsWarehouseCaller;
import com.prolog.cs.biz.inbound.remote.ControlRemoteCaller;
import com.prolog.cs.biz.inbound.service.impl.processor.Helper.TaskHelper;
import com.prolog.cs.biz.inbound.service.rcv.RcvInstockLinesService;
import com.prolog.cs.biz.inbound.vo.task.TaskQueryVO;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.beans.BeanUtils;
import com.prolog.framework.bz.common.search.SearchApi;
import com.prolog.framework.bz.core.util.ObjectUtils;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.framework.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.Map.Entry;

@Slf4j
@Lazy
@Service
public class AutoStockHandler {

	@Lazy
	@Resource
	private RcvInstockLinesService rcvInstockLinesService;

	@Resource
	private TaskInstockDetailMapper taskInstockDetailMapper;

	@Resource
	private BaseWmsWarehouseCaller baseWmsWarehouseCaller;

	@Resource
	private ControlRemoteCaller controlRemoteCaller;

	@Resource
	private SearchApi searchApi;

	@Value("${prolog.wms.enableAutoMatchStoreTemperature:false}")
	private boolean enableAutoMatchStoreTemperature;

	@Value("${prolog.wms.enableAutoMatchStoreTemperatureDefault:false}")
	private boolean enableAutoMatchStoreTemperatureDefault;

	@Resource
	private TaskHelper taskHelper;

	// 自动质检&上架
	public void autoStock(RqcHeadersDTO rqcDTO, List<RqcLinesDTO> rqcLinesDTOList, UserDTO userDTO,
			Set<String> areaCategoryCodeSet, Date date) {
		TaskQueryVO queryVO = new TaskQueryVO();
		String rcvHeaderId = rqcDTO.getRcvHeaderId();
		queryVO.setRcvHeaderId(rcvHeaderId);
		String rcvType = rqcDTO.getRcvType();
		queryVO.setStatus("1");
		List<TaskInstockDetailDTO> list = taskInstockDetailMapper.findList(queryVO);
		if (ObjectUtils.isEmpty(list)) {
			log.info("上架任务为空!请联系管理员检查后重试!");
			if (rcvType.equals(BizConstant.STRAIGHT_IN)) {
				return;
			} else {
				throw new BizException("上架任务为空!请联系管理员检查后重试!");
			}

		}
		List<RcvInstockLinesDTO> lineList = new ArrayList<>();

		// 若是不进入特殊库别,就需要分三次取货位,整件货位,散件货位,整散合一货位,每个随机一个货位(包含多个逻辑区-商品大类(暂时实现不了))
		// 远程调用仓库服务获取货位(普通搜索)
		String warehouseId = rqcDTO.getWarehouseId();
		Map<String, Object> locatorQueryMap = new HashMap<>();
		locatorQueryMap.put("warehouseId", warehouseId);
		if (!ObjectUtils.isEmpty(areaCategoryCodeSet)) {
			locatorQueryMap.put("includeAreaCategoryCodes", new ArrayList<>(areaCategoryCodeSet));
		}
		try {
			log.info("自动选择空入货位查询条件==>" + JsonUtils.toString(locatorQueryMap));
		} catch (JsonProcessingException e2) {
		}
		RestMessage<Map<String, BaseWmsWarehouseLocator>> locations2Map = baseWmsWarehouseCaller
				.getRandomLocations2Map(locatorQueryMap);
		Map<String, BaseWmsWarehouseLocator> locatorMap = null;
		if (locations2Map.isSuccess())
			locatorMap = locations2Map.getData();
		if (ObjectUtils.isEmpty(locatorMap)) {// 库位数据为空
			log.error("(随机)货位没有合适的货位,请核对库别、库区、库位设置正确");
//			throw new BizException("没有合适的货位,请核对库别、库区、库位设置正确");
		}

		// 若是调账收货时未选择,是否需要远程调用库存服务匹配? pass,收货选择批号时带出相关货位(其他出)
		Date billDate = rqcLinesDTOList.get(0).getRqcDate();
		date = billDate;
		if (date == null) {
			date = new Date();
		}
		Integer downLimit = 15;
		Integer upLimit = 30;
		String autoTimeInterval = rqcDTO.getAutoTimeInterval();
		if (StringUtils.isBlank(autoTimeInterval)) {
			autoTimeInterval = taskHelper.getBusinessControlValue(rqcDTO.getWarehouseId(), rqcDTO.getMtlOwnerId(),
					"15-30", BizConstant.AUTO_BILL_INTERVAL_TIME);
			if (StringUtils.isBlank(autoTimeInterval)) {
				autoTimeInterval = "15-30";
			}
			String[] split = autoTimeInterval.split("-");
			try {
				downLimit = Integer.parseInt(split[0]);
				upLimit = Integer.parseInt(split[1]);
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			}
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, new Random().nextInt(downLimit) + upLimit - downLimit);

		String qcOperator1 = userDTO.getUsername();
		String enterpriseId = userDTO.getEnterpriseId();
		List<Map> searchForMap = null;
		try {
			Map<String, Object> queryMap = new HashMap<>();
			queryMap.put("enterpriseId", enterpriseId);
			queryMap.put("warehouseId", warehouseId);
			queryMap.put("bussinessType", "1011");
			searchForMap = searchApi.searchForMap("selectWorker", queryMap);
		} catch (Exception e) {
			log.info("随机获取上架操作员接口异常", e);
		}

		// 获取库存子状态
		String subStatus = getSubStatus(rcvType);

		for (int j = 0; j < list.size(); j++) {
			TaskInstockDetailDTO p = list.get(j);
			// 需要判断是否整件,散件,整散匹配不同的货位
			RcvInstockLinesDTO target = new RcvInstockLinesDTO();
			BeanUtils.copyProperties(p, target);
			BaseWmsWarehouseLocator BaseWmsWarehouseLocator = excuteGetLocator(locatorMap, target, p);
			if (BaseWmsWarehouseLocator == null) {
				// 二次获取
				BaseWmsWarehouseLocator = excuteGetLocator2(locatorMap, target);
			}

			String locatorId = p.getWhLocatorId();
			if (!StringUtils.isBlank(locatorId)) {
				BaseWmsWarehouseLocator = new BaseWmsWarehouseLocator();
				String whLocatorCode = p.getWhLocatorCode();
				if (StringUtils.isBlank(whLocatorCode)) {
					List<Map> searchForMap22 = null;
					try {
						Map<String, Object> queryMap22 = new HashMap<>();
						List<String> locatorIds = new ArrayList<>();
						locatorIds.add(locatorId);
						queryMap22.put("locatorIds", locatorIds);
						searchForMap22 = searchApi.searchForMap("locator_info_list", queryMap22);
					} catch (Exception e) {
						log.info("查询服务查询货位数据异常,货位id" + locatorId + "原因=>" + e.getMessage());
					}
					if (!ObjectUtils.isEmpty(searchForMap22)) {
						Map locatormap = searchForMap22.get(0);
						p.setWhLocatorCode((String)locatormap.get("whLocatorCode"));
						p.setWhLocatorName((String)locatormap.get("whLocatorName"));
						p.setWhAreaCode((String)locatormap.get("whAreaCode"));
						p.setWhAreaCategoryCode((String)locatormap.get("whAreaCategoryCode"));
					}

				}
				BaseWmsWarehouseLocator.setWarehouseLocatorName(p.getWhLocatorName());
				BaseWmsWarehouseLocator.setWarehouseLocatorId(p.getWhLocatorId());
				BaseWmsWarehouseLocator.setWarehouseLocatorCode(p.getWhLocatorCode());
				BaseWmsWarehouseLocator.setWarehouseAreaCode(p.getWhAreaCode());
				BaseWmsWarehouseLocator.setAreaCategory(p.getWhAreaCategoryCode());
				BaseWmsWarehouseLocator.setWarehouseAreaCategoryCode(p.getWhAreaCategoryCode());

			}

			if (rqcDTO.getAutoGen()) {
				// 货主转换
				target.setRealWhLocatorId(p.getWhLocatorId());
				target.setRealWhLocatorCode(p.getWhLocatorCode());
				target.setWhAreaCode(p.getWhAreaCode());
				target.setWhAreaCategoryCode(p.getWhAreaCategoryCode());
				target.setWhLocatorId(p.getWhLocatorId());
				target.setWhLocatorCode(p.getWhLocatorCode());
				target.setCargoChangeIn(new CargoChangeIn()); // 记录货主转换标识

			}
			if (BaseWmsWarehouseLocator == null) {
				log.error("没有合适的货位,请核对库别、库区、库位设置正确(二次获取)");
				throw new BizException("没有合适的货位,请核对库别、库区、库位设置正确");
			}
			p.setWhLocatorId(BaseWmsWarehouseLocator.getWarehouseLocatorId());
			p.setWhLocatorCode(BaseWmsWarehouseLocator.getWarehouseLocatorCode());
			p.setWhAreaCode(BaseWmsWarehouseLocator.getWarehouseAreaCode());
			p.setWhAreaCategoryCode(BaseWmsWarehouseLocator.getAreaCategory());
//			this.taskInstockDetailMapper.updateMapById(p.getTaskInboundId(),
//					MapUtils.put("whLocatorId", BaseWmsWarehouseLocator.getWarehouseLocatorId()).getMap(),
//					TaskInstockDetail.class);

			// 调账(有实物)收货需选择的货位
			if (rcvType.equals(BizConstant.BLANK_ENTITY_IN) || rcvType.equals(BizConstant.BLANK_RETURN_ENTITY_IN)
					|| rcvType.equals(BizConstant.ACCOUNT_REG_IN)) {
				// 调账的必须在收货的时候选择对应货位?若是没有,是否需要远程重新匹配?

				target.setRealWhLocatorId(p.getWhLocatorId());
				target.setRealWhLocatorCode(p.getWhLocatorCode());
				target.setWhAreaCode(p.getWhAreaCode());
				target.setWhAreaCategoryCode(p.getWhAreaCategoryCode());
				target.setWhLocatorId(p.getWhLocatorId());
				target.setWhLocatorCode(p.getWhLocatorCode());

			}

			String realWhLocatorId = target.getRealWhLocatorId();
			// 没有使用货位限定推荐货位
			if (StringUtils.isBlank(realWhLocatorId)) {
				target.setRealWhLocatorId(p.getWhLocatorId());
				target.setRealWhLocatorCode(p.getWhLocatorCode());
				target.setRealWhLocatorName(p.getWhLocatorName());
				target.setWhAreaCode(p.getWhAreaCode());
				target.setWhAreaCategoryCode(p.getWhAreaCategoryCode());

			}
			realWhLocatorId = target.getRealWhLocatorId();
			// 没有使用普通货位选择随机货位
			if (StringUtils.isBlank(realWhLocatorId)) {
				target.setRealWhLocatorId(BaseWmsWarehouseLocator.getWarehouseLocatorId());
				target.setRealWhLocatorCode(BaseWmsWarehouseLocator.getWarehouseLocatorCode());
				target.setRealWhLocatorName(BaseWmsWarehouseLocator.getWarehouseLocatorName());
				target.setWhLocatorId(BaseWmsWarehouseLocator.getWarehouseLocatorId());
				target.setWhLocatorCode(BaseWmsWarehouseLocator.getWarehouseLocatorCode());
				target.setWhLocatorName(BaseWmsWarehouseLocator.getWarehouseLocatorName());
			}

			// 匹配合适上架员
			if (!ObjectUtils.isNull(searchForMap)) {
				String opcode = getOpcode(searchForMap, BaseWmsWarehouseLocator);
				if (!StringUtils.isBlank(opcode)) {
					qcOperator1 = opcode;
				}
			}
			p.setExecutor(qcOperator1);
			target.setExecutorUser(qcOperator1);
			target.setTaskId(p.getTaskInboundId());
			target.setLineNum(new BigDecimal(j));
			target.setAutoTime(calendar.getTime());

			// 设置库存子状态
			target.setSubStatus(subStatus);

			lineList.add(target);
		}
		rcvInstockLinesService.batchAddAuto(lineList, userDTO);
	}

	/**
	 * 二次匹配 若是空,则直接匹配第一个
	 * 
	 * @param locatorMap
	 * @param target
	 * @return
	 */
	private BaseWmsWarehouseLocator excuteGetLocator2(Map<String, BaseWmsWarehouseLocator> locatorMap,
			RcvInstockLinesDTO target) {
		BaseWmsWarehouseLocator value = null;
		if (ObjectUtils.isNull(locatorMap)) {
			return value;
		}
		for (Entry<String, BaseWmsWarehouseLocator> entity : locatorMap.entrySet()) {
			value = entity.getValue();
			if (value != null) {
				break;
			}
		}
		return value;
	}

	/**
	 * 根据单据类型获取库存子状态
	 * 
	 * @param rcvType
	 * @return
	 */
	private String getSubStatus(String rcvType) {
		String subStatus = "";
		if (rcvType.equals(BizConstant.BLANK_IN) || rcvType.equals(BizConstant.BLANK_RETURN_IN)) {
			subStatus = "2";
		}
		if (rcvType.equals(BizConstant.BLANK_ENTITY_IN) || rcvType.equals(BizConstant.BLANK_RETURN_ENTITY_IN)
				|| rcvType.equals(BizConstant.ACCOUNT_REG_IN)) {
			subStatus = "3";
		}
		if (rcvType.equals(BizConstant.STRAIGHT_IN)) {
			subStatus = "4";
		}
//		if (rcvType.equals(BizConstant.OVERSTEP_IN)) {
//			subStatus = "5";
//		}

		return subStatus;
	}

	/**
	 * 匹配合适的货位
	 * 
	 * @param locatorMap
	 * @param target
	 * @param p
	 */
	private BaseWmsWarehouseLocator excuteGetLocator(Map<String, BaseWmsWarehouseLocator> locatorMap,
			RcvInstockLinesDTO target, TaskInstockDetailDTO p) {
		if (ObjectUtils.isNull(locatorMap)) {
			return null;
		}
		Set<Entry<String, BaseWmsWarehouseLocator>> entrySet = locatorMap.entrySet();
		for (Entry<String, BaseWmsWarehouseLocator> entity : entrySet) {
			BaseWmsWarehouseLocator locator = entity.getValue();
			if (locator == null) {
				log.error("没有合适的货位,请核对库别、库区、库位设置正确");
				return null;
//				throw new BizException("没有合适的货位,请核对库别、库区、库位设置正确");
			}
			String isCaseOrPiece = getCaseOrPiece(target);

			String itemIsPieceOrCase = p.getItemIsPieceOrCase();
			String whAreaCategoryCode = p.getWhAreaCategoryCode();
			if (!StringUtils.isBlank(whAreaCategoryCode)) {
				// 库别不为空,匹配库别
				String areaCategory = locator.getAreaCategory();
				String warehouseAreaCategoryCode = locator.getWarehouseAreaCategoryCode();
				if (StringUtils.isBlank(areaCategory)) {
					areaCategory = warehouseAreaCategoryCode;
				}
				if (!whAreaCategoryCode.equalsIgnoreCase(areaCategory)) {
					continue;
				}
			} else {
				// 库别为空,商品整散属性为整散合一,默认匹配货位整散合一
				String warehouseLocatorType = locator.getWarehouseLocatorType();
				if (!StringUtils.isBlank(itemIsPieceOrCase) && "0".equals(itemIsPieceOrCase)
						&& !StringUtils.isBlank(warehouseLocatorType) && !"2".equals(warehouseLocatorType)) {
					continue;
				}
			}
			if (!StringUtils.isBlank(itemIsPieceOrCase) && "0".equals(itemIsPieceOrCase)) {
				// 商品属性整散合一,只需要匹配货位为整散合一以及温度即可
				String warehouseLocatorType = locator.getWarehouseLocatorType();
				if (enableAutoMatchStoreTemperature) {
					if ("2".equals(warehouseLocatorType) && matchStoreTemperature(target, locator)) {
						return locator;
					} else {
						if ("2".equals(warehouseLocatorType)) {
							return locator;
						}
					}
				} else {
					if ("2".equals(warehouseLocatorType)) {
						return locator;
					}
				}
			} else {
				// 商品属性整散分离,正常匹配
				// 整件类型
				if ("0".equals(isCaseOrPiece)) {
					String warehouseLocatorType = locator.getWarehouseLocatorType();
					if (enableAutoMatchStoreTemperature) {
						if ("0".equals(warehouseLocatorType) && matchStoreTemperature(target, locator)) {
							return locator;
						} else {
							if ("0".equals(warehouseLocatorType)) {
								return locator;
							}
						}
					} else {
						if ("0".equals(warehouseLocatorType)) {
							return locator;
						}
					}
				}
				// 散件类型
				if ("1".equals(isCaseOrPiece)) {
					String warehouseLocatorType = locator.getWarehouseLocatorType();
					if (enableAutoMatchStoreTemperature) {
						if ("1".equals(warehouseLocatorType) && matchStoreTemperature(target, locator)) {
							return locator;
						} else {
							if ("1".equals(warehouseLocatorType)) {
								return locator;
							}
						}
					} else {
						if ("1".equals(warehouseLocatorType)) {
							return locator;
						}
					}
				}
				// 整散合一类型
				if ("2".equals(isCaseOrPiece)) {
					String warehouseLocatorType = locator.getWarehouseLocatorType();
					if (enableAutoMatchStoreTemperature) {
						if ("2".equals(warehouseLocatorType) && matchStoreTemperature(target, locator)) {
							return locator;
						} else {
							if ("2".equals(warehouseLocatorType)) {
								return locator;
							}
						}
					} else {
						if ("2".equals(warehouseLocatorType)) {
							return locator;
						}
					}
				}
			}

		}

		// 啥都没匹配到,则优先匹配存储条件>货位类型>合一类型
		for (Entry<String, BaseWmsWarehouseLocator> entity : entrySet) {
			BaseWmsWarehouseLocator locator = entity.getValue();
			String warehouseLocatorType = locator.getWarehouseLocatorType();
			// 货位整散合一类型为2都可满足
			if (enableAutoMatchStoreTemperature) {
				if ("2".equals(warehouseLocatorType) && matchStoreTemperature(target, locator)) {
					return locator;
				} else {
					// 还是啥都没有,直接合一类型
					if ("2".equals(warehouseLocatorType)) {
						return locator;
					}
				}
			} else {
				if ("2".equals(warehouseLocatorType)) {
					return locator;
				}
			}
			// 若是还是没匹配到,默认选一个货位
			return locator;
		}

		return null;

	}

	/**
	 * 匹配存储条件
	 * 
	 * @param target
	 * @param locator
	 * @return
	 */
	private boolean matchStoreTemperature(RcvInstockLinesDTO target, BaseWmsWarehouseLocator locator) {
		boolean flag = false;
		String storeTemperature = target.getStoreTemperature();
		String attribute2 = locator.getAttribute2();

		if (enableAutoMatchStoreTemperatureDefault) {
			if (StringUtils.isBlank(storeTemperature)) {
				storeTemperature = "常温";
			}
			if (StringUtils.isBlank(attribute2)) {
				attribute2 = "常温";
			}
		}

		if ("常温".equals(storeTemperature) && ("常温".equals(attribute2) || "阴凉".equals(attribute2))) {
			flag = true;
		}
		if ("阴凉".equals(storeTemperature) && "阴凉".equals(attribute2)) {
			flag = true;
		}
		if ("冷藏".equals(storeTemperature) && "冷藏".equals(attribute2)) {
			flag = true;
		}
		if ("冷冻".equals(storeTemperature) && "冷冻".equals(attribute2)) {
			flag = true;
		}
		// 货位存储条件为空,默认匹配所有
		if (StringUtils.isBlank(attribute2)) {
			flag = true;
		}

		return flag;
	}

	/**
	 * 判断整散属性
	 * 
	 * @param target
	 * @return
	 */
	private String getCaseOrPiece(RcvInstockLinesDTO target) {
		// 是否整散
		BigDecimal pieceQty = target.getPieceQty();
		BigDecimal conversionRate = target.getConversionRate();
		BigDecimal oddCaseQty = pieceQty.divide(conversionRate, 0, RoundingMode.DOWN);
		BigDecimal oddPieceQty = pieceQty.subtract(oddCaseQty.multiply(conversionRate));
		// 判断数据的整散
		if (null != oddPieceQty && oddPieceQty.compareTo(BigDecimal.ZERO) == 0) {
			// 为整数
			return "0";
		}

		if (null != oddCaseQty && oddCaseQty.compareTo(BigDecimal.ZERO) == 0 && null != oddPieceQty
				&& oddPieceQty.compareTo(BigDecimal.ZERO) == 1) {
			// 零散散
			return "1";
		}

		if (null != oddCaseQty && oddCaseQty.compareTo(BigDecimal.ZERO) == 1 && null != oddPieceQty
				&& oddPieceQty.compareTo(BigDecimal.ZERO) == 1) {
			// 整散
			return "2";
		}
		return "0";
	}

	/**
	 * 获取合适的上架员
	 * 
	 * @param searchForMap
	 * @param baseWmsWarehouseLocator
	 * @return
	 */
	private String getOpcode(List<Map> searchForMap, BaseWmsWarehouseLocator baseWmsWarehouseLocator) {
		String personName = null;
		for (Map smap : searchForMap) {
			String workareas = (String) smap.get("workareas");
			if (!StringUtils.isBlank(workareas)) {
				String[] split = workareas.split(",");
				List<String> asList = Arrays.asList(split);
				if (asList.contains(baseWmsWarehouseLocator.getWarehouseAreaCode())) {
					personName = (String) smap.get("personName");
					return personName;
				}
			}
		}
		return personName;
	}

//	
//	//自动质检&上架
//	public void autoStock(RqcHeadersDTO rcvDTO, List<RqcLinesDTO> rqcLinesDTOList, UserDTO userDTO, String warehouseCategoryCode, Date date){
//		TaskQueryVO queryVO = new TaskQueryVO();
//		String rcvHeaderId = rcvDTO.getRcvHeaderId();
//		queryVO.setRcvHeaderId(rcvHeaderId);
//		List<TaskInstockDetailDTO> list = taskInstockDetailMapper.findList(queryVO);
////		List<TaskInstockDetail> list = taskInstockDetailMapper.findByMap(MapUtils.put("rcvHeaderId",rcvDTO.getRcvHeaderId()).getMap(),TaskInstockDetail.class);
//		List<RcvInstockLinesDTO> lineList=new ArrayList<>();
//		// 远程调用仓库服务
//		Map<String, Object> locatorQueryMap = new HashMap<>();
//		locatorQueryMap.put("warehouseId", rcvDTO.getWarehouseId());
//		locatorQueryMap.put("warehouseCategoryCode", warehouseCategoryCode);
//		RestMessage<Map<String, BaseWmsWarehouseLocator>> locations2Map = baseWmsWarehouseCaller.getLocations2Map(locatorQueryMap);
//		Map<String, BaseWmsWarehouseLocator> locatorMap = null;
//		if (locations2Map.isSuccess())
//			locatorMap = locations2Map.getData();
//		if (ObjectUtils.isEmpty(locatorMap)){// 库位数据为空
//			throw new BizException("没有合适的货位,请核对库别、库区、库位设置正确");
//		}
//		Map.Entry entry=locatorMap.entrySet().stream().findFirst().orElse(null);
//		if(entry==null){
//			throw new BizException("没有合适的货位,请核对库别、库区、库位设置正确");
//		}
//		
//		if (date == null) {
//			date = new Date();
//		}
//		Calendar calendar = Calendar.getInstance();
//		calendar.setTime(date);
//		calendar.add(Calendar.MINUTE, new Random().nextInt(15) + 15);
//		
//		String qcOperator1=userDTO.getUsername();
//		try {
//			RestMessage<UserDTO> rest = controlRemoteCaller.getRandomUserDTOByWorkGroup("1011",
//					rcvDTO.getWarehouseId(),userDTO.getEnterpriseId());
//			if (rest != null && rest.isSuccess() && rest.getData()!=null && StringUtils.isNotBlank(rest.getData().getUsername())) {
//				qcOperator1=rest.getData().getUsername();
//			}
//		} catch (Exception e) {
//			log.error("随机获取操作员接口异常",e);
//		}
//		for (int j = 0; j < list.size(); j++) {
//			TaskInstockDetail p=list.get(j);
//			RcvInstockLinesDTO target = new RcvInstockLinesDTO();
//			BeanUtils.copyProperties(p, target);
//			BaseWmsWarehouseLocator BaseWmsWarehouseLocator=(BaseWmsWarehouseLocator)entry.getValue();
//			target.setTaskId(p.getTaskInboundId());
//			target.setRealWhLocatorId(BaseWmsWarehouseLocator.getWarehouseLocatorId());
//			target.setRealWhLocatorCode(BaseWmsWarehouseLocator.getWarehouseLocatorCode());
//			target.setRealWhLocatorName(BaseWmsWarehouseLocator.getWarehouseLocatorName());
//			target.setWhLocatorId(BaseWmsWarehouseLocator.getWarehouseLocatorId());
//			target.setWhLocatorCode(BaseWmsWarehouseLocator.getWarehouseLocatorCode());
//			target.setWhLocatorName(BaseWmsWarehouseLocator.getWarehouseLocatorName());
//			target.setLineNum(new BigDecimal(j));
//			RqcLinesDTO itemBaseInfoVO = rqcLinesDTOList.stream().filter(item -> item.getRcvLineId().equals(p.getRcvLineId())).findFirst().orElse(null);
//			if (itemBaseInfoVO != null) {
//				target.setBatchExpiredDate(itemBaseInfoVO.getBatchExpiredDate());
//				target.setBatchProductionDate(itemBaseInfoVO.getBatchProductionDate());
//			}
//			if(p.getQty().compareTo(BigDecimal.ONE)<0){
//				target.setOddPieceQty(p.getPieceQty());
//				target.setOddCaseQty(BigDecimal.ZERO);
//			}else{
//				target.setOddPieceQty(BigDecimal.ZERO);
//				target.setOddCaseQty(p.getQty());
//			}
//			p.setExecutor(qcOperator1);
//			target.setAutoTime(calendar.getTime());
//			lineList.add(target);
//		}
//		rcvInstockLinesService.batchAdd(lineList);
//	}
//	

}
