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

import com.prolog.cs.biz.inbound.dto.rqc.RqcLinesDTO;
import com.prolog.cs.biz.inbound.service.impl.processor.Helper.TaskHelper;
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.impl.processor.enums.SubjectStrategyEnum;
import com.prolog.cs.biz.inbound.service.task.TaskInstockDetailService;
import com.prolog.cs.biz.inbound.service.task.TaskQcRecheckDetailService;
import com.prolog.cs.biz.inbound.service.task.TaskRejectedDetailService;
import com.prolog.framework.core.exception.BizException;
import org.springframework.stereotype.Service;

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

/**
 * 质检执行器操作类
 * 
 * @author Xiong
 *
 */
@Service
public class RqcTaskHandler implements TaskHandler {

	@Resource
	private TaskInstockDetailService taskInstockDetailService;

	@Resource
	private TaskQcRecheckDetailService taskQcRecheckDetailService;

	@Resource
	private TaskRejectedDetailService taskRejectedDetailService;

	@Resource
	private TaskHelper taskHelper;

	@Override
	public Object excute(SubjectStrategyEnum subjectStrategy, RcvTypeStrategyEnum rcvType, RqcStrategyEnum rqcStrategy,
			Map<String, Object> dataMap, ConclusionEnum conclusionEnum,boolean isAuto) {
		// 做具体逻辑
		String strategy = subjectStrategy.getStrategy();
		switch (strategy) {
		case "rcv_in_01":
			return filterSubject(rcvType, rqcStrategy, dataMap, conclusionEnum,isAuto);
		default:
			return null;
		}
	}

	private Object filterSubject(RcvTypeStrategyEnum rcvType, RqcStrategyEnum rqcStrategy, Map<String, Object> dataMap,
			ConclusionEnum conclusionEnum,boolean isAuto) {
		// 主体1 的逻辑
		String typeStrategy = rcvType.getTypeStrategy();
		switch (typeStrategy) {
		case "rcv_in_type01":
			// 可以配合policy包使用
			return filterRcvType(rqcStrategy, dataMap, conclusionEnum,isAuto);
		default:
			return null;
		}
	}

	private Object filterRcvType(RqcStrategyEnum rqcStrategy, Map<String, Object> dataMap,
			ConclusionEnum conclusionEnum,boolean isAuto) {
		// 普通采购入库 逻辑
		String typeStrategy = rqcStrategy.getTypeStrategy();
		switch (typeStrategy) {
		case "rqc_00":
			return filterRqcStrategy1(dataMap, conclusionEnum,isAuto);
		case "rqc_12":
			return filterRqcStrategy4(dataMap, conclusionEnum,isAuto);
		default:
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	private Object filterRqcStrategy4(Map<String, Object> dataMap, ConclusionEnum conclusionEnum,boolean isAuto) {
		// 质检策略4 需要走质检
		String conclusionval = conclusionEnum.getConclusionval();
		String conclusionval1 = ConclusionEnum.qualified.getConclusionval();
		String conclusionval2 = ConclusionEnum.noQualified.getConclusionval();
		String conclusionval3 = ConclusionEnum.pending.getConclusionval();
		String conclusionval4 = ConclusionEnum.refuse.getConclusionval();
		String mtlOwnerId = (String) dataMap.get("mtlOwnerId");
		String warehouseId = (String) dataMap.get("warehouseId");
		List<RqcLinesDTO> list = (List<RqcLinesDTO>) dataMap.get("data");
		Object excuteTask = null;
		if (conclusionval1.equals(conclusionval))
			// 合格不合格由上架任务池自身处理
			// 合格
			excuteTask = taskInstockDetailService.excuteTask(list, conclusionval,isAuto);
		else if (conclusionval2.equals(conclusionval)) {
			// 不合格
			for (RqcLinesDTO rqcLinesDTO : list) {
				rqcLinesDTO.setWhAreaCategory("NHK");
				rqcLinesDTO.setWarehouseCode("NHK");
			}
			excuteTask = taskInstockDetailService.excuteTask(list, conclusionval,isAuto);
		} else if (conclusionval3.equals(conclusionval)) {
			// 待处理(需要根据业务参数判断是否直接到复检并且进入代管子流程0否1是)
			String regValue = taskHelper.getInboundPendRerqcHostin(warehouseId, mtlOwnerId);

			excuteTask = taskQcRecheckDetailService.excuteTask(list, isAuto, regValue, warehouseId);
		}
		else if (conclusionval4.equals(conclusionval))
			// 拒绝
			excuteTask = taskRejectedDetailService.excuteTask2(list,isAuto);
		else
			throw new BizException("请选择正确的结论!");
//			excuteTask = taskInstockDetailService.excuteTask(list, "0");
		return excuteTask;
	}

	private Object filterRqcStrategy1(Map<String, Object> dataMap, ConclusionEnum conclusionEnum,boolean isAuto) {
		// 质检策略1 不进行质检
		// 具体质检不质检由物料本身结论决定

		return null;
	}

	@Override
	public Object rfExcute(SubjectStrategyEnum subjectStrategy, RcvTypeStrategyEnum rcvType,
			RqcStrategyEnum rqcStrategy, Map<String, Object> dataMap, ConclusionEnum conclusionEnum, boolean isAuto) {
		// TODO Auto-generated method stub
		return null;
	}

}
