package com.youchain.modules.quartz.task;

import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.StockService;
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 StockService stockService;

    @Autowired
    public PickService pickService;

    @Autowired
    public TaskService taskService;

    @Autowired
    public AgvTaskService agvTaskService;

    @Autowired
    public KMReService kmReService;

    /**
     * 分配出库单
     */
    public void allocatePick() {
        List<String> statuses = Arrays.asList(BizStatus.OPEN, BizStatus.ASSIGN, BizStatus.PICKUP);
        List<Pick> pickList = pickService.findByPickStatus(statuses, true);
        if (pickList.isEmpty()) {
            throw new BadRequestException("无待分配的成品出库单,分配失败!");
        }
        for (Pick pick : pickList) {
            synchronized (pick) {
                taskService.allocatePick(pick);
            }
        }

        //生成备料任务
        taskService.materialPick();

    }



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

}
