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

import com.prolog.cs.biz.inbound.constant.BizConstant;
import com.prolog.cs.biz.inbound.model.rcv.RcvHeaders;
import com.prolog.cs.biz.inbound.remote.ControlRemoteCaller;
import com.prolog.cs.biz.inbound.remote.StrategyClient;
import com.prolog.cs.biz.inbound.service.impl.processor.enums.ConclusionEnum;
import com.prolog.cs.biz.inbound.service.impl.processor.enums.RcvTypeStrategyEnum;
import com.prolog.cs.biz.inbound.service.impl.processor.enums.RqcStrategyEnum;
import com.prolog.cs.biz.inbound.service.rcv.RcvHeadersService;
import com.prolog.cs.biz.inbound.utils.SpringUtils;
import com.prolog.framework.bz.common.search.SearchApi;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.AssertUtils;
import com.prolog.framework.utils.ObjectUtils;
import com.prolog.framework.utils.StringUtils;
import com.prolog.upcloud.base.strategy.model.BusinessControl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * 任务策略执行帮助类
 * 
 * @author Xiong
 *
 */
@Service
public class TaskHelper {

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

	@Resource
	private SearchApi searchApi;

	@Resource
	private StrategyClient strategyClient;

	@Resource
	ControlRemoteCaller controlRemoteCaller;

	/**
	 * 获取结论
	 * 
	 * @param conclusion
	 * @return
	 */
	public ConclusionEnum getConclusionEnum(String conclusion) {
		switch (conclusion) {
		case "0":

			return ConclusionEnum.qualified;
		case "1":

			return ConclusionEnum.noQualified;
		case "2":

			return ConclusionEnum.pending;
		case "3":

			return ConclusionEnum.refuse;

		default:
			return ConclusionEnum.qualified;
		}

	}

	/**
	 * 获取质检策略
	 * 
	 * @param rcvHid
	 * @return
	 */
	public RqcStrategyEnum getRqcStrategyEnum(String rcvHid) {
		RcvHeadersService rcvHeadersService = SpringUtils.getBeansByType(RcvHeadersService.class);
		RcvHeaders findById = rcvHeadersService.findById(rcvHid);

		AssertUtils.notNull(findById, "根据收货单id查询的数据为空!");
		String rcvType = findById.getRcvType();
		String mtlOwnerId = findById.getMtlOwnerId();
		String warehouseId = findById.getWarehouseId();
		// 根据货主id 和仓库id去数据库查询策略
		if (rcvType.equals(BizConstant.HOSTED_IN)) {
			// 代管入库的处理,若是流通货主,自动验收,走货位推荐逻辑生生上架任务
			Map<String, Object> queryMap = new HashMap<>();
			queryMap.put("cargoOwnerId", mtlOwnerId);
			List<Map> searchForMap = null;
			try {
				searchForMap = searchApi.searchForMap("getCargoOwnerProp", queryMap);
			} catch (Exception e) {
				logger.info("获取货主属性失败,原因{}", e);
			}
			if (!ObjectUtils.isNull(searchForMap)) {
				Map map = searchForMap.get(0);
				String cargoOwnerProp = (String) map.get("cargoOwnerProp");
				Object isHostedRcvObj = map.get("isHostedRcv");
				Integer isHostedRcv = 1;
				if (isHostedRcvObj instanceof String) {
					isHostedRcv = Integer.parseInt((String) isHostedRcvObj);
				}
				if (isHostedRcvObj instanceof Integer) {
					isHostedRcv = (Integer) isHostedRcvObj;
				}
				if (isHostedRcv != null && 0 == isHostedRcv) {
					// 此货主不支持代管业务
					throw new BizException("此货主不支持代管业务!请设置!");
				}
				if (!StringUtils.isBlank(cargoOwnerProp) && "0".equals(cargoOwnerProp)) {
					// 流通货主
					return RqcStrategyEnum.rqcStrategy1;
				}
				if (!StringUtils.isBlank(cargoOwnerProp) && "1".equals(cargoOwnerProp)) {
					// 生产货主
					return RqcStrategyEnum.rqcStrategy4;
				}
			}
		}
		return RqcStrategyEnum.rqcStrategy4;

	}
	/*  添加收货获取质检 --- on 2022-07-25 by 陈浩 ---开始 */
	public RqcStrategyEnum getRqcRcvStrategyEnum(String rcvHid) {
		RcvHeadersService rcvHeadersService = SpringUtils.getBeansByType(RcvHeadersService.class);
		RcvHeaders findById = rcvHeadersService.findById(rcvHid);
		AssertUtils.notNull(findById, "根据收货单id查询的数据为空!");
		String rcvType = findById.getRcvType();
		String mtlOwnerId = findById.getMtlOwnerId();
		String warehouseId = findById.getWarehouseId();
		
	
			Map<String, Object> queryMap = new HashMap<>();
			queryMap.put("cargoOwnerId", mtlOwnerId);
			List<Map> searchForMap = null;
			try {
				searchForMap = searchApi.searchForMap("getCargoOwnerProp", queryMap);
			} catch (Exception e) {
				logger.info("获取货主属性失败,原因{}", e);
			}
			//调取质检策略
			String strageRseult=null;
			if(ObjectUtils.isNull(searchForMap)) {
				//无质检，自动生成验收任务单
				return RqcStrategyEnum.rqcStrategy1;

			}
		return RqcStrategyEnum.rqcStrategy4;

	}
	/*  添加收货获取质检 --- on 2022-07-25 by 陈浩 ---结束 */
	/**
	 * 获取收货策略
	 */
	public RcvTypeStrategyEnum getRcvTypeStrategyEnum(String rcvHid) {
		RcvHeadersService rcvHeadersService = SpringUtils.getBeansByType(RcvHeadersService.class);
		RcvHeaders findById = rcvHeadersService.findById(rcvHid);

		AssertUtils.notNull(findById, "根据收货单id查询的数据为空!");
		String rcvType = findById.getRcvType();
		if (ObjectUtils.isNotNull(rcvType)) {
			switch (rcvType) {
			case "rcv_in_type01":

				return RcvTypeStrategyEnum.generalRcv;

			default:
				return RcvTypeStrategyEnum.generalRcv; // 默认普通采购入库
			}
		} else {
			return RcvTypeStrategyEnum.generalRcv;
		}
	}

	/**
	 * 获取业务参数编码(待处理复检代管入)
	 *
	 * @param warehouseId
	 * @param mtlOwnerId
	 * @return
	 */
	public String getInboundPendRerqcHostin(String warehouseId, String mtlOwnerId) {
		return this.getBusinessControlValue(warehouseId, mtlOwnerId, "0", BizConstant.INBOUND_PEND_RERQC_HOSTIN);
	}

	/**
	 * 获取业务参数编码(复检待处理erp二次审核)
	 *
	 * @param warehouseId
	 * @param mtlOwnerId
	 * @return
	 */
	public String getInboundRerqcPendToerp(String warehouseId, String mtlOwnerId) {
		return this.getBusinessControlValue(warehouseId, mtlOwnerId, "0", BizConstant.INBOUND_RERQC_PEND_TOERP);
	}

	/**
	 * 获取业务配置参数的值
	 *
	 * @param warehouseId
	 * @param mtlOwnerId
	 * @param defaultVal
	 * @param regCode
	 * @return
	 */
	public String getBusinessControlValue(String warehouseId, String mtlOwnerId, String defaultVal, String regCode) {
		String regValue = defaultVal;
		RestMessage<BusinessControl> controlByCode = null;
		try {
			controlByCode = strategyClient.getControlByCode(regCode, warehouseId, mtlOwnerId, null, null);
		} catch (Exception e) {
		}
		if (com.prolog.cs.biz.inbound.utils.ObjectUtils.isNull(controlByCode) || !controlByCode.isSuccess()
				|| controlByCode.getData() == null) {
			if (!com.prolog.cs.biz.inbound.utils.ObjectUtils.isNull(controlByCode) && !controlByCode.isSuccess()) {
				logger.info("调用策略服务查询业务参数错误!原因{}", controlByCode.getMessage());
			}
			// throw new BizException("调用策略服务查询业务参数错误!请联系管理员重试!");
		} else if (controlByCode.getData() != null) {
			BusinessControl cdata = controlByCode.getData();
			regValue = cdata.getRegValue();
		}

		return regValue;
	}
}
