package com.dxhy.core.task;

import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.service.ScheduleJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description  数据处理定时任务
 * @author  Allen
 * @date    2024/11/04 14:49
*/
@SuppressWarnings({"rawtypes", "DuplicatedCode"})
@Component("baseTask")
@Slf4j
public class BaseTask {
    @Resource
    private ScheduleJobService scheduleJobService;


    /**
     * 重置长时间处于执行状态的定时任务
     * @throws
     */
    public void timedTaskRestore() throws Exception {
        log.info("重置长时间处于执行状态的定时任务执行开始-");
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("baseTask", "timedTaskRestore");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                scheduleJobService.updateById(scheduleJobEntity);
                scheduleJobService.timedTaskRestore();
                log.info("重置长时间处于执行状态的定时任务执行结束--");
            } catch (Exception e) {
                log.debug("重置长时间处于执行状态的定时任务异常：{}", e);
            } finally {
                log.info("重置长时间处于执行状态的定时任务执行完成--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }
}
