package com.youchain.modules.quartz.task;

import com.youchain.basicdata.domain.Box;
import com.youchain.basicdata.domain.PointItem;
import com.youchain.basicdata.repository.BoxRepository;
import com.youchain.basicdata.repository.PointItemRepository;
import com.youchain.businessdata.domain.AgvTask;
import com.youchain.businessdata.domain.Inventory;
import com.youchain.businessdata.repository.AgvTaskRepository;
import com.youchain.businessdata.service.AgvTaskService;
import com.youchain.businessdata.service.MlsService;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.BizStatus;
import com.youchain.utils.HttpPostUtil;
import com.youchain.utils.UrlApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

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

    @Autowired
    public AgvTaskService agvTaskService;
    @Autowired
    public BoxRepository boxRepository;
    @Autowired
    public PointItemRepository pointItemRepository;
    @Autowired
    public MlsService mlsService;
    private static final int MAX_TASK_COUNT = 5;

    private static final long TIMEOUT_MS = 2*60*1000;
    public void pickLLJTask() {
        List<Box> boxList = boxRepository.getBoxAllData();
        for (Box box : boxList){
            List<String> dataValueList = box.getModbusInfo().getDataValueList();
            if (dataValueList.get(box.getSort()-1).equals(box.getWorkCode())){
                if (box.getWorkType().equals(BizStatus.PICK)){
                    List<PointItem> pointItemList=pointItemRepository.findByPoint(box.getPoint().getId());
                    // 获取 PointItem.item.code 集合
                    List<String> itemCodeList = pointItemList.stream()
                            .map(pointItem -> pointItem.getItem().getCode())
                            .collect(Collectors.toList());
                    // 出库
                    mlsService.getIssueInfo(itemCodeList, box.getPoint());
                }
            }
        }
    }
    public void pickTask() {
        // 下发出库任务
        List<AgvTask> agvTaskList = agvTaskService.queryByAgvTask(BizStatus.OPEN,BizStatus.PICK);
        if (agvTaskList.size() > 0) {
            /* 第一个任务的创建视角*/
            long lastTaskTime = agvTaskList.get(0).getCreateTime().getTime();
            if ((agvTaskList.size() >= MAX_TASK_COUNT)) {
                /* 数量大于等于5时*/
                // 取集合中的前五个任务合并发送
                List<AgvTask> agvTasks=agvTaskList.subList(0, MAX_TASK_COUNT);
                Result(agvTaskService.sendAgvTaskLXImpl(agvTasks));
            }else if((System.currentTimeMillis() - lastTaskTime >=TIMEOUT_MS) ){
                Result(agvTaskService.sendAgvTaskLXImpl(agvTaskList));
            } else {
                throw new RuntimeException("出库料箱任务未满足条件(不满足五个料箱需等待2分钟)!");
            }
        } else {
            throw new RuntimeException("无出库料箱任务!");
        }


    }
    public void yhTask() {
        /* 发送验货任务*/
        pickTaskByType(BizStatus.INV);
        /* 发送验货返库任务*/
        pickTaskByType(BizStatus.INV_BACK);
        System.out.println("发送验货任务");
    }

    private void pickTaskByType(String agvType) {
        // 下发验货任务
        List<AgvTask> agvTaskList = agvTaskService.queryByAgvTask(BizStatus.OPEN, agvType);
        if (agvTaskList.size() > 0) {
            /* 第一个任务的创建视角*/
            long lastTaskTime = agvTaskList.get(0).getCreateTime().getTime();
            if ((agvTaskList.size() >= MAX_TASK_COUNT)) {
                /* 数量大于等于5时*/
                // 取集合中的前五个任务合并发送
                List<AgvTask> agvTasks=agvTaskList.subList(0, MAX_TASK_COUNT);
                Result(agvTaskService.sendAgvTaskLXImpl(agvTasks));
            }else if((System.currentTimeMillis() - lastTaskTime >=1*60*1000) ){
                Result(agvTaskService.sendAgvTaskLXImpl(agvTaskList));
            } else {
//                throw new RuntimeException("出库料箱任务未满足条件(不满足五个料箱需等待2分钟)!");
            }
        } else {
//            throw new RuntimeException("无出库料箱任务!");
        }
    }

    private void Result(String result) {
        if(result==null ||result.length()==0){
            throw new RuntimeException("任务下发失败!");
        }
    }



}
