package com.youchain.modules.quartz.task;

import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.domain.Stock;
import com.youchain.basicdata.service.StockService;
import com.youchain.businessdata.domain.AgvTask;
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.exception.BadRequestException;
import com.youchain.utils.BizStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 备料任务
 */
@Slf4j
@Service
public class jlTask {


    @Autowired
    public StockService stockService;

    @Autowired
    public PickService pickService;

    @Autowired
    public TaskService taskService;

    @Autowired
    public AgvTaskService agvTaskService;

    @Autowired
    public KMReService kmReService;

    // 定义异常信息常量
    private static final String NO_EMPTY_SHELVES_ERROR_MSG = "没有叫料任务!";

    // 定义一个对象作为锁
    private final Object lock = new Object();

    /**
     * 下发叫料任务
     */
    public void sendAgvJl() {
        List<AgvTask> agvTaskList = agvTaskService.findOpenByAgvTask(BizStatus.CALL_PICK);
        if (agvTaskList.isEmpty()) {
            throw new BadRequestException(NO_EMPTY_SHELVES_ERROR_MSG);
        }
        synchronized (lock) {
            for (AgvTask agvTask : agvTaskList) {
                processAgvTask(agvTask);
            }
        }

    }

    private void processAgvTask(AgvTask agvTask) {
        kmReService.sendAgvTask(agvTask, kmReService.sendAgvTaskHjJson(agvTask));
    }
}
