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

import com.prolog.cs.biz.inbound.dto.rqc.RqcReLinesDTO;
import com.prolog.cs.biz.inbound.model.task.TaskQcRecheckDetail;
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.TaskQcDetailService;
import com.prolog.cs.biz.inbound.service.task.TaskQcRecheckDetailService;
import com.prolog.cs.biz.inbound.service.task.TaskRejectedDetailService;
import org.springframework.stereotype.Service;

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

/**
 * 复检执行器
 *
 * @author Xiong
 *
 */
@Service
public class RqcReTaskHandler implements TaskHandler {

	@Resource
	private TaskQcDetailService taskQcDetailService;

	@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<RqcReLinesDTO> list = (List<RqcReLinesDTO>) dataMap.get("data");
		Object excuteTask = null;
		Integer times = (Integer) dataMap.get("times");

		// 获取业务参数是否进入代管子流程0否1是
        String regValue = taskHelper.getInboundPendRerqcHostin(warehouseId, mtlOwnerId);
        // 如果需要进入代管子流程，则需要填充 ATTRIBUTE1 字段的值，用来辨识是否已进行过代管入库
        if ("1".equals(regValue)) {
            list = this.setHostedStatus(list);
        }

		if (conclusionval1.equals(conclusionval))
			// 合格不合格由上架任务池自身处理
			// 合格
			excuteTask = taskQcDetailService.excuteTask3(list, conclusionval, times,isAuto, regValue, warehouseId);
		else if (conclusionval2.equals(conclusionval))
			// 不合格
			excuteTask = taskQcDetailService.excuteTask3(list, conclusionval, times,isAuto, regValue, warehouseId);
		else if (conclusionval3.equals(conclusionval)) {
			// 这段代码是死代码,若是想看erp二次审核,参考复检明细保存
			// 待处理
			// 业务参数获取是否 复检待处理erp二次审核
			String toErpRegValue = taskHelper.getInboundRerqcPendToerp(warehouseId, mtlOwnerId);
			if ("1".equals(toErpRegValue)) {
				// TODO BY HUANG: 需要回传到erp进行审核，暂时没有和erp的交互方式
			} else {
				excuteTask = taskQcDetailService.excuteTask3(list, conclusionval, times,isAuto, regValue, warehouseId);
				//		excuteTask = taskQcRecheckDetailService.excuteTask2(list, times);
			}
		} else if (conclusionval4.equals(conclusionval))
			// 拒绝
			excuteTask = taskRejectedDetailService.excuteTask3(list,isAuto, regValue, warehouseId);
		else
			excuteTask = taskQcDetailService.excuteTask3(list, "0", times,isAuto, regValue, warehouseId);
		return excuteTask;
	}

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

		return null;
	}

    /**
     * 填充 refType
     *
     * @param list
     * @return
     */
    private List<RqcReLinesDTO> setHostedStatus(List<RqcReLinesDTO> list) {
        List<String> ids = list.stream().map(RqcReLinesDTO::getTaskReRqcId).collect(Collectors.toList());
        List<TaskQcRecheckDetail> tasks = taskQcRecheckDetailService.findByIds(ids);
        Map<String, String> refTypeMap = tasks.stream()
          .collect(Collectors.toMap(TaskQcRecheckDetail::getTaskRecheckId, TaskQcRecheckDetail::getAttribute1));

        for (RqcReLinesDTO rqcReLinesDTO : list) {
            if (refTypeMap.containsKey(rqcReLinesDTO.getTaskReRqcId())) {
                rqcReLinesDTO.setHostedStatus(refTypeMap.get(rqcReLinesDTO.getTaskReRqcId()));
            }
        }

        return list;
    }

	@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;
	}
}
