package com.youchain.modules.quartz.task;

import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.service.PointService;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.repository.AgvTaskRepository;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.modules.quartz.utils.TimeNumberUtils;
import com.youchain.utils.BaseStatus;
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.ArrayList;
import java.util.List;

/**
 * 滚筒任务
 */
@Slf4j
@Service
public class gtcTask {

    @Autowired
    public AgvTaskRepository agvTaskRepository;

    @Autowired
    public AgvTaskService agvTaskService;

    @Autowired
    public PointService pointService;


    public void gtcTask() {
        //查询缓存输送线上的已扫描成功的满箱任务！
        List<AgvTask> agvTaskList1 = agvTaskRepository.queryByAgvTask(null, BizStatus.OPEN, BizStatus.PICK, 1, "ROLLER_MOVE");
        if (agvTaskList1.size() > 0) {
            if (agvTaskList1.size() > 2) {
                List<AgvTask> newAgvTask = agvTaskList1.subList(0, 2);
                this.isussTask(newAgvTask);
            } else {
                this.isussTask(agvTaskList1);
            }

        } else {
            log.info("无出库滚筒输送线任务!");
        }
    }

    public void isussTask(List<AgvTask> agvTaskList1) {
        List<AgvTask> agvTaskList2 = new ArrayList<>();//空箱任务
        for (AgvTask agvTask : agvTaskList1) {
            try {
                //满箱任务的终点是线边空箱的起点；获取线边点位
                Point startPoint = pointService.findByCode(agvTask.getEndSlotCode(), null, null, null, null);
                String taskCode = startPoint.getName() + "2";//线边名称；2代表空箱输送线；

                //出库缓存出库缓存滚筒输送线满箱接驳口为终点
                Point endPoint = pointService.getPoint("", BaseStatus.FREE, BaseStatus.BOX, "出库缓存滚筒输送线满箱接驳口");
                if (endPoint == null) {
                    log.info("出库缓存滚筒输送线满箱接驳口点位没有维护！");
                    return;
                }

                //线边任务执行中，不追加空箱任务
                if (agvTaskService.findByendSlotCode(agvTask.getEndSlotCode(), BizStatus.EMPTY_OUT, "ROLLER_MOVE")) {
                    int count = agvTaskService.QueryCount(taskCode);//查询线边空箱的数量
                    if (count > 2) {
                        count = 2;
                    }
                    if (count > 0) {
                        //生成反空箱滚筒任务任务
                        for (int i = 0; i < count; i++) {
                            if (agvTaskList2.size() < 2) {
                                AgvTask newAgvTask = new AgvTask(BizStatus.EMPTY_OUT, TimeNumberUtils.getKLXTaskCode(), startPoint.getCode(), endPoint.getCode(), BizStatus.OPEN, "ROLLER_MOVE");
                                agvTaskService.create(newAgvTask);
                                agvTaskList2.add(newAgvTask);
                            }
                        }
                    }
                } else {
                    log.info("线边任务执行中，不追加空箱任务！");
                }

            } catch (Exception e) {
                log.info(e.getMessage());
            }
        }
        String resultJson = agvTaskService.sendAgvTaskGTImpl(agvTaskList1, agvTaskList2);
        log.info(resultJson);
    }
}
