package org.ibase4j.core.support.scheduler;

import com.github.pagehelper.Page;
import org.ibase4j.dao.scheduler.TaskFireLogMapper;
import org.ibase4j.model.scheduler.TaskFireLog;
import org.ibase4j.model.scheduler.TaskScheduled;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ShenHuaJie
 * @version 2016年7月1日 上午11:34:59
 */
public class SchedulerService {
    @Autowired
    private TaskFireLogMapper logMapper;
    @Autowired
    private SchedulerManager schedulerManager;

    // 获取所有作业
    public List<TaskScheduled> getAllTaskDetail() {
        return schedulerManager.getAllJobDetail();
    }

    // 执行作业
    public void execTask(String taskGroup, String taskName) {
        TaskScheduled taskScheduler = new TaskScheduled();
        taskScheduler.setTaskGroup(taskGroup);
        taskScheduler.setTaskName(taskName);
        schedulerManager.runJob(taskScheduler);
    }

    // 暂停/恢复作业
    public void openCloseTask(String taskGroup, String taskName, String status) {
        TaskScheduled taskScheduler = new TaskScheduled();
        taskScheduler.setTaskGroup(taskGroup);
        taskScheduler.setTaskName(taskName);
        if ("start".equals(status)) {
            schedulerManager.resumeJob(taskScheduler);
        } else if ("stop".equals(status)) {
            schedulerManager.stopJob(taskScheduler);
        }
    }

    // 删除作业
    public void delTask(String taskGroup, String taskName) {
        TaskScheduled taskScheduler = new TaskScheduled();
        taskScheduler.setTaskGroup(taskGroup);
        taskScheduler.setTaskName(taskName);
        schedulerManager.delJob(taskScheduler);
    }

    // 修改任务
    public void updateTask(TaskScheduled taskScheduled) {
        schedulerManager.updateTask(taskScheduled);
    }

    @Cacheable("taskFireLog")
    public TaskFireLog getFireLogById(Long id) {
        return null;
    }

    @Transactional
    @CachePut("taskFireLog")
    public TaskFireLog updateLog(TaskFireLog record) {
        return null;
    }

    public Page<TaskFireLog> queryLog(Map<String, Object> params) {
        return null;
    }
}
