package com.prolog.eis.schedule;

import cn.hutool.core.collection.CollectionUtil;
import com.prolog.eis.common.util.RunTimeHelper;
import com.prolog.eis.model.outbound.PrintTask;
import com.prolog.eis.model.shelves.AllotmentTaskRecord;
import com.prolog.eis.service.logic.WcsTaskService;
import com.prolog.eis.service.out.TimeTaskService;
import com.prolog.eis.service.shelves.AllotmentTaskRecordService;
import com.prolog.eis.utils.PalletDismantleBillTypeStrategyConfig;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.exception.AssertException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WcsSchedule {

    private final WcsTaskService wcsTaskService;

    private final AllotmentTaskRecordService allotmentTaskRecordService;

    /**
     * 交叉带分拣机mcs搬运任务发送
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 1000)
    public void sendMcsCarryTask() throws Exception {
        RunTimeHelper.RunDispatch("交叉带分拣机mcs搬运任务发送", () -> {
            Criteria criteria = new Criteria(AllotmentTaskRecord.class);
            criteria.setOrder(Order.newInstance().asc("scanDate"));
            criteria.setRestriction(Restrictions.in("sendStatus", new Integer[]{0, 2}));
            List<AllotmentTaskRecord> allotmentTaskRecordList = allotmentTaskRecordService.findByCriteria(criteria);
            if (CollectionUtil.isEmpty(allotmentTaskRecordList)) {
                return;
            }
            log.info("交叉带分拣机mcs搬运任务发送开始");
            for (AllotmentTaskRecord allotmentTaskRecord : allotmentTaskRecordList) {
                try {
                    wcsTaskService.sendMcsCarryTask(allotmentTaskRecord);
                } catch (Exception e) {
                    log.error("交叉带分拣机mcs搬运任务发送异常:" + e.getMessage());
                }
            }
            log.info("交叉带分拣机mcs搬运任务发送结束");
        });
    }

}
