package com.prolog.eis.schedule;

import com.prolog.eis.common.util.RunTimeHelper;
import com.prolog.eis.init.OutboundDispatch;
import com.prolog.eis.service.carry.StackerService;
import com.prolog.eis.service.carry.TimeTaskService;
import com.prolog.eis.service.core.OutboundService;
import com.prolog.eis.service.outbound.BizOutboundTaskService;
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;

@Slf4j
@Component
@RequiredArgsConstructor
public class OutboundSchedule {

    private final OutboundService outboundService;

    private final BizOutboundTaskService bizOutboundTaskService;

    private final OutboundDispatch outboundDispatch;

    private final TimeTaskService timeTaskService;

    private final StackerService stackerService;

    /**
     * 出库逻辑
     */
    @Scheduled(initialDelay = 3000, fixedDelay = 3000)
    public void findPallet() throws Exception {

        RunTimeHelper.RunDispatch("校验库存", () -> {
            try {
                outboundService.checkStock();
            }catch (AssertException e) {
                log.warn("校验库存:" + e.getMessage());
            } catch (Exception ex) {
                log.error("校验库存异常：" + ex.getMessage(),ex);
            }
        });

        RunTimeHelper.RunDispatch("保存托盘出库任务", () -> {
            try {
                outboundDispatch.outbound();
            } catch (AssertException e) {
                log.warn("保存托盘出库任务:" + e.getMessage());
            }catch (Exception ex) {
                log.error("保存托盘出库任务异常：" + ex.getMessage(),ex);
            }
        });
    }



    /**
     * 四向库发送托盘任务
     */
    @Scheduled(initialDelay = 3000, fixedDelay = 3000)
    public void sendOutCarry() throws Exception {
        RunTimeHelper.RunDispatch("四向库发送托盘任务", () -> {
            try {
                log.info("发送托盘任务开始：" + System.currentTimeMillis());
                timeTaskService.sendOutCarry();
                log.info("发送托盘任务完成：" + System.currentTimeMillis());
            }catch (AssertException e) {
                log.warn("发送容器任务:" + e.getMessage());
            } catch (Exception e) {
                log.error("发送容器任务异常：" + e.getMessage(), e);
            }
        });
    }


    /**
     * 补货任务处理
     */
    @Scheduled(initialDelay = 3000, fixedDelay = 3000)
    public void supplyGen() throws Exception {
        RunTimeHelper.RunDispatch("补货任务处理", () -> {
            try {
                log.info("补货任务处理开始：" + System.currentTimeMillis());
                timeTaskService.supplyGen();
                log.info("补货任务处理完成：" + System.currentTimeMillis());
            } catch (Exception e) {
                log.error("补货任务处理异常：" + e.getMessage(), e);
            }
        });
    }

    /**
     * 四项库空托发往叠盘机
     */
    @Scheduled(initialDelay = 60000, fixedDelay = 60000)
    public void sendEmptyContainerToStacker() throws Exception {
        RunTimeHelper.RunDispatch("四项库空托发往叠盘机任务", () -> {
            try {
                stackerService.sendEmptyToStacker();
            } catch (AssertException e) {
                log.warn("四项库空托发往叠盘机任务:" + e.getMessage());
            }catch (Exception e) {
                log.error("四项库空托发往叠盘机任务异常:" + e.getMessage(), e);
            }

        });
    }

    /**
     * 站台空托发往叠盘机
     */
    @Scheduled(initialDelay = 5000, fixedDelay = 1000)
    public void sendStationEmptyContainerToStacker() throws Exception {
        RunTimeHelper.RunDispatch("站台空托发往叠盘机任务", () -> {
            try {
                stackerService.sendStaionEmptyToStacker();
            } catch (AssertException e) {
                log.warn("站台空托发往叠盘机任务:" + e.getMessage());
            }catch (Exception e) {
                log.error("站台空托发往叠盘机任务异常:" + e.getMessage(), e);
            }
        });
    }
    /**
     * 四项库空托垛发往1F收货区空托垛缓存位
     */
    @Scheduled(initialDelay = 60000, fixedDelay = 60000)
    public void sendEmptyContainerToCache() throws Exception {
        RunTimeHelper.RunDispatch("四项库空托垛发往1F收货区空托垛缓存位任务", () -> {
            try {
                bizOutboundTaskService.sendEmptyToCache();
            } catch (AssertException e) {
                log.error("四项库空托垛发往1F收货区空托垛缓存位任务异常:" + e.getMessage());
            }catch (Exception e) {
                log.error("四项库空托垛发往1F收货区空托垛缓存位任务异常:" + e.getMessage(), e);
            }

        });
    }

    /**
     * 1F上架位空托搬运到四项库拣选位
     */
//    @Scheduled(initialDelay = 60000, fixedDelay = 60000)
    public void moveEmptyContainer() throws Exception {
        RunTimeHelper.RunDispatch("1F上架位空托搬运到四项库拣选位任务", () -> {
            try {
                bizOutboundTaskService.moveEmptyContainer();
            } catch (AssertException e) {
                log.error("1F上架位空托搬运到四项库拣选位任务异常:" + e.getMessage());
            }catch (Exception e) {
                log.error("1F上架位空托搬运到四项库拣选位任务异常:" + e.getMessage(), e);
            }

        });
    }


}
