package com.youchain.modules.quartz.task;

import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.domain.Pick;
import com.youchain.businessdata.domain.Task;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.businessdata.service.KMReService;
import com.youchain.businessdata.service.PickService;
import com.youchain.businessdata.service.TaskService;
import com.youchain.businessdata.service.dto.PickDto;
import com.youchain.exception.BadRequestException;
import com.youchain.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 料箱出库任务下发
 *
 * @author 92525
 */
@Slf4j
@Service
public class pickTask {

    @Autowired
    public PickService pickService;

    @Autowired
    public TaskService taskService;

    @Autowired
    public AgvTaskService agvTaskService;

    @Autowired
    public KMReService kmReService;

    /**
     * 轮询打开和分配中的成品出库单，分配库存
     */
    public void allocateCpPick() {
        List<String> statuses = Arrays.asList(BizStatus.OPEN, BizStatus.ASSIGN);
        List<Pick> pickList = pickService.findByPickStatus(statuses, false,BaseStatus.GD_TYPE_CT);
        if (pickList.isEmpty()) {
            throw new BadRequestException("无待分配的成品出库单,分配失败!");
        }
        for (Pick pick : pickList) {
            taskService.allocatePick(pick);

        }

    }

    /***
     * 轮询打开和分配中的单品品出库单，分配库存
     */
    public void allocateDpPick() {
        List<String> statuses = Arrays.asList(BizStatus.OPEN, BizStatus.ASSIGN);
        List<Pick> pickList = pickService.findByPickStatus(statuses, false, BaseStatus.GD_TYPE_DP);
        if (pickList.isEmpty()) {
            throw new BadRequestException("无待分配的单品出库单,分配失败!");
        }
        for (Pick pick : pickList) {
            taskService.allocatePick(pick);

        }

    }

    /**
     * 轮询已分配的出库单，生成备料任务
     */
    public void pickAgvTask() {
        /*查询分配完成的出库单*/
        List<String> statuses = Arrays.asList(BizStatus.ALLOCATE);
        List<Pick> pickList = pickService.findByPickStatus(statuses, false,null);
        if (pickList.isEmpty()) {
            throw new BadRequestException("无已分配的出库单,下发任务失败!");
        }

        for (Pick pick : pickList) {
            taskService.materialPick(pick);
        }
    }

    /**
     * 轮询拣货完成的出库单，自动叫料
     */
    public void callJlTask() {
        /*查询拣货完成完成的出库单*/
        List<String> statuses = Arrays.asList(BizStatus.PICK_ALL);
        List<Pick> pickList = pickService.findByPickStatus(statuses, true,null);
        if (pickList.isEmpty()) {
            throw new BadRequestException("无拣货完成的出库单或出库单未叫料,叫料失败!");
        }
        for (Pick pick : pickList) {
            taskService.callJlTask(pick);
        }
    }

    /**
     * 轮询下发备料任务、叫料任务、叫料返库任务
     */
    public void sendTaskBlAndJl() {
        List<AgvTask> agvTaskList = agvTaskService.findOpenByAgvTask();
        if (agvTaskList.isEmpty()) {
            throw new BadRequestException("无备料任务或无叫料任务!");
        }
        for (AgvTask agvTask : agvTaskList) {
            if (BizStatus.PICK.equals(agvTask.getType())) {
                kmReService.sendAgvTask(agvTask, kmReService.sendAgvTaskCcJson(agvTask));
            } else {
                kmReService.sendAgvTask(agvTask, kmReService.sendAgvTaskHjJson(agvTask));
            }
        }
    }

}
