package com.prolog.eis.schedule;

import com.prolog.eis.common.util.RunTimeHelper;
import com.prolog.eis.constants.base.BaseBillTypeConstants;
import com.prolog.eis.constants.sys.SysParamConstants;
import com.prolog.eis.model.sys.SysParam;
import com.prolog.eis.service.bindgrid.BindGridDispatchService;
import com.prolog.eis.service.bindgridex.BindGridExDispatchService;
import com.prolog.eis.service.buildwave.BuildWaveDispatchService;
import com.prolog.eis.service.out.TimeTaskService;
import com.prolog.eis.service.sys.SysParamService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Version: 1.0
 * @Author: wuhan
 * @Created: 2024/7/30 15:53
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class DispatchSchedule {
    private final BuildWaveDispatchService buildWaveDispatchService;
    private final TimeTaskService timeTaskService;
    private final BindGridDispatchService bindGridDispatchService;
    private final BindGridExDispatchService bindGridExDispatchService;
    private final SysParamService sysParamService;

    @Scheduled(initialDelay = 1000 * 5, fixedDelay = 1000 * 10)
    public void buildWave() throws Exception {
        List<SysParam> sysParamList = sysParamService.findAll();
        if (!sysParamService.findValFromList(sysParamList, SysParamConstants.OLD_WAVE_SWITCH, "0").equals("0")) {
            System.out.println("旧波次调度开关打开，新组波调度停止执行");
            return;
        }

        RunTimeHelper.RunDispatch("新组波调度", () -> {
            if (sysParamService.findValFromList(sysParamList, SysParamConstants.NEW_WAVE_SWITCH, "0").equals("0")) {
                System.out.println("新组波调度开关关闭");
                return;
            }

            try {
                int buildOrderCount = buildWaveDispatchService.dispatch();
                System.out.println("新组波调度组波订单数：" + buildOrderCount);
            } catch (Exception e) {
                log.error("新拆零组波调度失败:{}", e.getMessage(), e);
            }
        });

        RunTimeHelper.RunDispatch("新拆零出库库存定位调度", () -> {
            if (sysParamService.findValFromList(sysParamList, SysParamConstants.NEW_STORE_SWITCH, "0").equals("0")) {
                System.out.println("新拆零出库库存定位调度开关关闭");
                return;
            }

            try {
                timeTaskService.findPallet(String.valueOf(BaseBillTypeConstants.OUTBOUND_TYPE));
            } catch (Exception ex) {
                log.error("拆零库存定位调度异常：{}", ex.getMessage(), ex);
            }
        });
    }

    @Scheduled(initialDelay = 1000 * 5, fixedDelay = 1000 * 5)
    public void bindGrid() throws Exception {
        List<SysParam> sysParamList = sysParamService.findAll();
        if (!sysParamService.findValFromList(sysParamList, SysParamConstants.OLD_WAVE_SWITCH, "0").equals("0")) {
            System.out.println("旧波次调度开关打开，新格口绑定调度停止执行");
            return;
        }

        RunTimeHelper.RunDispatch("新格口绑定", () -> {
            String newBindGridSwitch = sysParamService.findValFromList(sysParamList, SysParamConstants.NEW_BIND_GRID_SWITCH, "0");
            if (newBindGridSwitch.equals("0")) {
                System.out.println("新格口绑定调度开关关闭");
                return;
            }

            try {
                if (newBindGridSwitch.equals("1")) {
                    //新版格口绑定调度（允许部分绑定波次格口）
                    bindGridExDispatchService.dispatch();
                } else if (newBindGridSwitch.equals("2")) {
                    //旧版格口绑定调度（只允许一个波次全部绑定格口）
                    bindGridDispatchService.dispatch();
                }
            } catch (Exception e) {
                log.error("新格口绑定调度失败:{}", e.getMessage(), e);
            }
        });
    }
}
