package com.sky.ioc.controller.scene;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sky.ioc.entity.QuartzJob;
import com.sky.ioc.service.job.IQuartzJobService;
import com.sky.ioc.tool.ReturnMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags ="定时任务")
@Slf4j
@RestController
@RequestMapping("/quartz_job")
public class QuartzJobController {

    @Autowired
    private IQuartzJobService quartzJobService;

    @ApiOperation("创建定时任务")
    @PostMapping(value = "/addjob")
    public ReturnMsg addjob(@RequestBody QuartzJob quartzJob){
        List<QuartzJob> list = quartzJobService.list(new QueryWrapper<QuartzJob>().eq("job_class_name", quartzJob.getJobClassName()));
        if (list != null && list.size() > 0) {
            return ReturnMsg.fail("该定时任务类名已存在");
        }
        quartzJobService.saveAndScheduleJob(quartzJob);
        return ReturnMsg.ok();
    }


    @ApiOperation("定时任务列表")
    @GetMapping(value = "/getJobList")
    public ReturnMsg getJobList(@RequestParam(name = "name", required = false) String name,
                                @RequestParam(name = "group", required = false) Integer group,
                                @RequestParam(name = "status", required = false) Integer status,
                                @RequestParam(name = "page", required = false,defaultValue = "1") Integer page,
                                @RequestParam(name = "pageSize", required = false,defaultValue = "10") Integer pageSize) {
        return  quartzJobService.pageList(name,group,status,page,pageSize);
    }


    @ApiOperation("编辑定时任务")
    @PostMapping(value = "/editJob")
    public ReturnMsg<?> editJob(@RequestBody QuartzJob quartzJob) {
        //  QuartzJob quartzJob = quartzJobService.getById(id);
        if (quartzJob == null) {
            return ReturnMsg.fail("未找到对应实体");
        }else {
            try {
                quartzJobService.editAndScheduleJob(quartzJob);
                return ReturnMsg.ok();
            } catch (SchedulerException e) {
               // throw new RuntimeException(e);
                return ReturnMsg.fail("失败!"+e.getMessage());
            }

        }
    }

    @ApiOperation("删除定时任务")
    @PostMapping(value = "/deleteJob")
    public ReturnMsg<?> deleteJob(@RequestParam(name = "id", required = true) Integer id) {
        QuartzJob quartzJob = quartzJobService.getById(id);
        if (quartzJob == null) {
            return ReturnMsg.fail("未找到对应实体");
        }else {
            quartzJobService.deleteAndStopJob(quartzJob);
            return ReturnMsg.ok();
        }
    }

    @ApiOperation("启动/停止定时任务")
    @PostMapping(value = "/stopAndScheduleJob")
    public ReturnMsg<?> stopAndScheduleJob(@RequestParam(name = "id", required = true) Integer id,
                                           @RequestParam(name = "status", required = true) Integer status) {
        QuartzJob quartzJob = quartzJobService.getById(id);
        quartzJob.setStatus(status);
        if (quartzJob == null) {
            return ReturnMsg.fail("未找到对应实体");
        }else {
            if (status==0){
                //启动
                quartzJobService.resumeJob(quartzJob);
                return ReturnMsg.ok("操作成功!");
            }else{
                //暂停
                try {
                    quartzJobService.editAndScheduleJob(quartzJob);
                    return ReturnMsg.ok();
                } catch (SchedulerException e) {
                    // throw new RuntimeException(e);
                    return ReturnMsg.fail("失败!"+e.getMessage());
                }
            }
        }
    }

}
