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.service.out.PrintTaskService;
import com.prolog.eis.service.out.TimeTaskService;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restrictions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class PrintSchedule {

    private final TimeTaskService timeTaskService;

    private final PrintTaskService printTaskService;

    /**
     * 打印单据定时器（包含 调用WMS 订单绑定格口回告、出库复核结果回告、面单打印回告、全零发整打印）
     */
    @Scheduled(initialDelay = 3000, fixedDelay = 3000)
    public void print() throws Exception {
        RunTimeHelper.RunDispatch("回告wms信息", () -> {
            Criteria criteria = new Criteria(PrintTask.class);
            criteria.setOrder(Order.newInstance().asc("updateTime"));
            criteria.setRestriction(Restrictions.in("status", new Integer[]{0, 2}));
            List<PrintTask> printTaskList = printTaskService.findByCriteria(criteria);
            if (CollectionUtil.isEmpty(printTaskList)) {
                return;
            }
            for (PrintTask printTask : printTaskList) {
                try {
                    timeTaskService.doPrint(printTask);
                } catch (Exception e) {
                    log.error("回告wms信息异常:" + e.getMessage());
                }
            }
        });
    }

}
