package com.yunzhupaas.flowable.job;

import com.yunzhupaas.flowable.model.time.FlowTimeModel;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.RandomUtil;
import com.yunzhupaas.util.RedisUtil;
import com.yunzhupaas.flowable.util.TimeUtil;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.Date;
import java.util.List;

/**
 * 类的描述
 *
 * @author YUNZHUPAASYUNZHUPAAS开发组
 * @version 5.0.x
 * @since 2024/5/23 17:49
 */
// @Component
public class JobRunner implements ApplicationRunner {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 获取缓存中定时相关信息
        List<FlowTimeModel> list = FlowJobUtil.getOperator(redisUtil);
        for (FlowTimeModel timeModel : list) {
            Date date = new Date();
            // 判断当前时间是否已经 超过了结束时间
            if (date.getTime() > timeModel.getEndDate().getTime()) {
                continue;
            }
            if (timeModel.getOn()) {
                timeModel.setId(RandomUtil.uuId());
                JobDataMap jobDataMap = new JobDataMap();
                jobDataMap.putAll(JsonUtil.entityToMap(timeModel));
                FlowJobUtil.insertModel(timeModel, redisUtil);
                TimeUtil.addJob(timeModel.getId(), timeModel.getDuring(), FlowTime.class, jobDataMap,
                        timeModel.getStartDate(), timeModel.getEndDate());
            }
        }

    }
}
