package com.friday.custody.controller;

import com.friday.custody.service.basicService.QuartzScheduleTaskService;
import com.friday.custody.dto.MergedNoticeDto;
import com.friday.custody.entity.ScheduleTask;
import com.friday.custody.quartz.MergedMessageNoticeByUserConfigJob;
import com.friday.custody.utils.JsonUtils;
import com.friday.custody.utils.QuartzUtils;
import com.friday.custody.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 手动添加定时任务
 * @author 021790
 */
@RestController
@RequestMapping("/custody/quartz/job/")
@Slf4j
public class DynamicScheduleController {
    @Resource
    private Scheduler scheduler;
    @Autowired
    private QuartzScheduleTaskService quartzScheduleTaskService;


    /**
     * cronExpression
     * jobClass
     * jobName
     * parameters
     *
     * jobStatus
     *
     * @param quartzBean
     * @return
     * @throws SchedulerException
     * @throws ClassNotFoundException
     */
    @PostMapping("/create")
    public R<String> createJob(@RequestBody ScheduleTask quartzBean) throws SchedulerException, ClassNotFoundException {
        MergedNoticeDto mergedNoticeDto = JsonUtils.X.parse(quartzBean.getParameters(), MergedNoticeDto.class);
        StringJoiner stringJoiner = new StringJoiner("-");
        stringJoiner.add("baclance-change")
                .add(mergedNoticeDto.getNotifierId().toString())
                .add(mergedNoticeDto.getNotifyTime());
        quartzBean.setJobName(stringJoiner.toString());
        quartzBean.setJobClass(MergedMessageNoticeByUserConfigJob.class.getCanonicalName());
        String cronExpression = QuartzUtils.getCronExpression(mergedNoticeDto.getNotifyTime());
        quartzBean.setCronExpression(cronExpression);
        if (!quartzScheduleTaskService.save(quartzBean)) {
            return R.error("create job failed");
        }
        QuartzUtils.createScheduleJob(scheduler, quartzBean);
        return R.ok("create job success");
    }


    @GetMapping("/list")
    public List<ScheduleTask> list() {
        List<ScheduleTask> list = quartzScheduleTaskService.list();
        return list;
    }

    @PostMapping("/pause")
    public R<String> pauseJob(String jobName) throws SchedulerException {
        QuartzUtils.pauseScheduleJob(scheduler, jobName);
        return R.ok("pause job success");
    }

    @PostMapping("/run")
    public R<String> runOnce(String jobName) throws SchedulerException {
        QuartzUtils.runOnce(scheduler, jobName);
        return R.ok(jobName + "run once success");
    }

    @PostMapping("/resume")
    public R<String> resume(String jobName) throws SchedulerException {
        QuartzUtils.resumeScheduleJob(scheduler, jobName);
        return R.ok("resume job success: " + jobName);
    }

    @PostMapping("/update")
    public R<String> update(@RequestBody ScheduleTask quartzBean) throws SchedulerException {
        QuartzUtils.updateScheduleJob(scheduler, quartzBean);
        return R.ok("update job success");
    }

    @PostMapping("/remove")
    public R<String> remove(@RequestBody ScheduleTask task) throws SchedulerException {
        if (!quartzScheduleTaskService.removeById(task.getId())) {
            return R.error("remove job failed");
        }
        QuartzUtils.deleteScheduleJob(scheduler, task.getJobName());
        return R.ok("remove job success");
    }
}
