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

import com.prolog.cs.basic.warehouse.model.BaseWmsWarehouseLocator;
import com.prolog.cs.basic.warehouse.model.BaseWmsWhContainer;
import com.prolog.cs.biz.inbound.constant.BizConstant;
import com.prolog.cs.biz.inbound.remote.BaseWmsWarehouseCaller;
import com.prolog.cs.biz.inbound.utils.ObjectUtils;
import com.prolog.cs.biz.inbound.utils.UserUtils;
import com.prolog.cs.biz.inbound.vo.StrategyQueryVO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.utils.AssertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonRemoteService {

	@Resource
	private BaseWmsWarehouseCaller baseWmsWarehouseCaller;

	@Resource
	private UserUtils userUtils;

	private static Logger logger = LoggerFactory.getLogger(CommonRemoteService.class);

	/**
	 * 获取库位list
	 * 
	 * @param warehouseId
	 * @param warehouseRoomId
	 * @param warehouseAreaId
	 * @return
	 */
	public Object getLocatorList(String warehouseId, String warehouseLocatorCode, String warehouseLocatorName,
			Integer pageSize, Integer pageNum) {
		// 获取库位list
		List<BaseWmsWarehouseLocator> list = new ArrayList<BaseWmsWarehouseLocator>();
		RestMessage<Page<BaseWmsWarehouseLocator>> page = baseWmsWarehouseCaller.getLocatorPage(warehouseLocatorCode,
				warehouseLocatorName, null, warehouseId, pageSize, pageNum);
		boolean success = page.isSuccess();
		if (success) {
			// 记录日志
			if (0 == pageSize && 0 == pageNum) {
				list = page.getData().getList();
				return list;
			} else {
				return page.getData();
			}
		}
//		// 这里使用模拟的数据
//		for (int i = 0; i < 10; i++) {
//			BaseWmsWarehouseLocator baseWmsWarehouseLocator = excuteBuildLocatorModel(warehouseId, i);
//
//			list.add(baseWmsWarehouseLocator);
//		}
		return list;
	}

	private BaseWmsWarehouseLocator excuteBuildLocatorModel(String warehouseId, int i) {
		BaseWmsWarehouseLocator baseWmsWarehouseLocator = new BaseWmsWarehouseLocator();
		baseWmsWarehouseLocator.setAccessLevel("9");
		baseWmsWarehouseLocator.setWarehouseId(warehouseId);
		baseWmsWarehouseLocator.setCardPanelNumber(String.valueOf(i));
		baseWmsWarehouseLocator.setColumnNum(i);
		baseWmsWarehouseLocator.setDcCode("00" + i);
		baseWmsWarehouseLocator.setDcId(String.valueOf(i));
		baseWmsWarehouseLocator.setDcName("00" + i);
		baseWmsWarehouseLocator.setEnterpriseId("1");
		baseWmsWarehouseLocator.setFloorNum(i);
		baseWmsWarehouseLocator.setInIndex(i);
		baseWmsWarehouseLocator.setIsFclPalletRacking("Y");
		baseWmsWarehouseLocator.setItemPlaceType("垛堆");
		baseWmsWarehouseLocator.setLayerNum(i);
		baseWmsWarehouseLocator.setLocatorLaneNum("00" + i);
		baseWmsWarehouseLocator.setOutIndex(i);
		baseWmsWarehouseLocator.setRowNum(i);
		baseWmsWarehouseLocator.setStorageType("B");
		baseWmsWarehouseLocator.setWarehouseAreaCode("00" + i);
		baseWmsWarehouseLocator.setWarehouseAreaId("00" + i);
		baseWmsWarehouseLocator.setWarehouseAreaName("00" + i);
		baseWmsWarehouseLocator.setWarehouseCode("00" + i);
		baseWmsWarehouseLocator.setWarehouseLocatorCode("00" + i);
		baseWmsWarehouseLocator.setWarehouseLocatorDistance(i);
		baseWmsWarehouseLocator.setWarehouseLocatorId("00" + i);
		baseWmsWarehouseLocator.setWarehouseLocatorName("00" + i);
		baseWmsWarehouseLocator.setWarehouseLocatorType("1");
		baseWmsWarehouseLocator.setWarehouseLogicCode("00" + i);
		baseWmsWarehouseLocator.setWarehouseLogicId("00" + i);
		baseWmsWarehouseLocator.setWarehouseLogicName("00" + i);
		baseWmsWarehouseLocator.setWarehouseName("00" + i);
		baseWmsWarehouseLocator.setWarehouseRoomCode("00" + i);
		baseWmsWarehouseLocator.setWarehouseRoomId("00" + i);
		baseWmsWarehouseLocator.setWarehouseRoomName("00" + i);
		return baseWmsWarehouseLocator;
	}

	/**
	 * 获取容器
	 */
	public Page<BaseWmsWhContainer> getContainerPage(String containerCode, String containerType, String warehouseId,
			Integer pageSize, Integer pageNum) {
		AssertUtils.isNotBlank(warehouseId, "仓库id不可为空!");
		if (ObjectUtils.isNull(pageNum))
			pageNum = BizConstant.PAGE_NUM;
		if (ObjectUtils.isNull(pageSize))
			pageSize = BizConstant.PAGE_SIZE;
		Page<BaseWmsWhContainer> data = null;

		RestMessage<Page<BaseWmsWhContainer>> containerPage = baseWmsWarehouseCaller.getContainerPage(containerCode,
				containerType, warehouseId, pageSize, pageNum);
		boolean success = containerPage.isSuccess();
		if (!success) {
			// 记录日志
			String msg = String.format("远程调用容器服务失败!参数,仓库id[%s]", warehouseId);
			logger.error(msg);
			// 抛出错误
			throw new BizException(msg);
		}
		data = containerPage.getData();

		// 这里使用模拟的数据
//		String enterpriseId = userUtils.getEnterpriseId();
//		String userId = userUtils.getUserId();
//		String userName = userUtils.getUserName();
//		Date now = new Date();
//		data = new Page<BaseWmsWhContainer>();
//		List<BaseWmsWhContainer> mockList = new ArrayList<>();
//		for (int i = 0; i < 15; i++) {
//			BaseWmsWhContainer model = new BaseWmsWhContainer();
//			model.setContainerCode("00" + i);
//			model.setContainerHgt(BigDecimal.valueOf(10));
//			model.setContainerId("0000000000000" + i);
//			model.setContainerLen(BigDecimal.valueOf(10));
//			model.setContainerPercent(BigDecimal.valueOf(10));
//			model.setContainerStatus("0");
//			model.setContainerType("1");
//			model.setContainerVol(BigDecimal.valueOf(1000));
//			model.setContainerWgtlmt(BigDecimal.valueOf(100));
//			model.setContainerWid(BigDecimal.valueOf(10));
//			model.setEnterpriseId(enterpriseId);
//			model.setWarehouseId(warehouseId);
//			model.setGmtCreate(now);
//			model.setGmtModified(now);
//			model.setCreator(userId);
//			model.setCreatorName(userName);
//			model.setModifier(userId);
//			model.setModifierName(userName);
//			mockList.add(model);
//		}
//		data.setList(mockList);
//		data.setPageCount(BizConstant.PAGE_NUM);
//		data.setPageSize(BizConstant.PAGE_SIZE);
//		data.setTotalCount(BizConstant.PAGE_SIZE);
//		data.setPageNum(BizConstant.PAGE_NUM);

		return data;
	}

	public List<BaseWmsWarehouseLocator> getStrategyLocatorList(StrategyQueryVO strategyLocatorQueryVO) {
		String warehouseId = strategyLocatorQueryVO.getWarehouseId();
		RestMessage<Page<BaseWmsWarehouseLocator>> page = baseWmsWarehouseCaller.getLocatorPage(null, null, null,
				warehouseId, 1, 2);
		boolean success = page.isSuccess();
		List<BaseWmsWarehouseLocator> list = new ArrayList<BaseWmsWarehouseLocator>();
		if (success) {
			// 记录日志
			list = page.getData().getList();
		}

		// 模拟根据策略获取货位
		List<String> itemIdList = strategyLocatorQueryVO.getItemIdList();
		AssertUtils.notNull(itemIdList, "商品id列表不可为空");
//		String mtlOwnerId = strategyLocatorQueryVO.getMtlOwnerId();
		for (int i = 0; i < itemIdList.size(); i++) {
			BaseWmsWarehouseLocator excuteBuildLocatorModel = excuteBuildLocatorModel(warehouseId, i % 2);
			list.add(excuteBuildLocatorModel);
		}
		return list;
	}

	public List<Map<String, Object>> getStrategyAreaList(StrategyQueryVO strategyQueryVO) {
		// 获取推荐的库区和仓库列表
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		// 模拟根据策略获取库区和仓库列表
		List<String> itemIdList = strategyQueryVO.getItemIdList();
		AssertUtils.notNull(itemIdList, "商品id列表不可为空");
		for (int i = 0; i < itemIdList.size(); i++) {
			Map<String, Object> map = new HashMap<>();
//			String[] warehouseAreaCodes = { "HGQ", "BHGQ", "YCQ", "SJQ" };
			String s = "HGQ,BHGQ,YCQ,SJQ";
			map.put("warehouseArea", s);
			list.add(map);
		}
		return list;
	}
}
