package com.codvision.base.controller.manager;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.authcore.annotation.RequiresPermissions;
import com.codvision.base.entity.QuartzJob;
import com.codvision.base.service.IQuartzJobService;
import com.codvision.base.support.QuartzJobManager;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * quartz任务管理模块
 *
 * @author lingee
 * @date 2023-11-20
 */
@Slf4j
@RestController
@RequestMapping("/v1/manager/quartzJob")
@Api(tags = "平台管理端-quartz任务管理模块")
public class ManagerQuartzJobController {

    @Resource
    private IQuartzJobService quartzJobService;

    @Resource
    private QuartzJobManager quartzJobManager;

    @ApiOperation(value = "获取quartz任务管理分页列表", notes = "获取quartz任务管理分页列表接口")
    @PostMapping("/getJobList")
    @RequiresPermissions("tool_quartz_list")
    public ResponseEntity<IPage<QuartzJob>> getPageList(@ApiParam(value = "任务名称") @RequestParam(value = "jobName", required = false) String jobName,
                                                        @ApiParam(value = "任务分组") @RequestParam(value = "jobGroup", required = false) String jobGroup,
                                                        @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                        @RequestParam(value = "size", required = false, defaultValue = "10") int size) {
        QuartzJob queryBean = new QuartzJob();
        queryBean.setJobName(jobName);
        queryBean.setJobGroup(jobGroup);
        IPage<QuartzJob> pageResult = quartzJobService.getPageResult(new QueryPage<>(queryBean, page, size));
        return new ResponseEntity<IPage<QuartzJob>>().ok().data(pageResult);
    }

    @ApiOperation(value = "新增定时任务", notes = "新增定时任务接口")
    @PostMapping("/addJob")
    @RequiresPermissions("tool_quartz_add")
    public ResponseEntity<QuartzJob> addJob(@Valid @RequestBody QuartzJob quartzJob) {
        QuartzJob result = quartzJobService.addQuartzJob(quartzJob);

        return new ResponseEntity<QuartzJob>().ok().data(result).message("添加成功");
    }

    @PostMapping("/updateJob")
    @ApiOperation("更新定时任务")
    @RequiresPermissions("tool_quartz_edit")
    public ResponseEntity<QuartzJob> updateJob(@ApiParam("定时任务信息") @RequestBody QuartzJob quartzJob) {
        ResponseEntity<QuartzJob> responseEntity = new ResponseEntity<>();
        try {
            responseEntity.setData(quartzJobService.updateJob(quartzJob));
        } catch (Exception e) {
            throw new BusinessException(1000, e.getMessage());
        }
        return responseEntity.ok();
    }

    @PostMapping("/triggerJob")
    @ApiOperation("触发/执行定时任务")
    @RequiresPermissions("tool_quartz_list")
    public ResponseEntity<Boolean> triggerJob(@ApiParam(value = "任务名称", required = true) @RequestParam(value = "jobName") String jobName,
                                              @ApiParam(value = "任务分组", required = true) @RequestParam(value = "jobGroup") String jobGroup) {
        ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
        quartzJobManager.triggerJob(jobName, jobGroup);
        responseEntity.setData(true);
        return responseEntity;
    }

    @PostMapping("/pauseJob")
    @ApiOperation("停止任务")
    @RequiresPermissions("tool_quartz_list")
    public ResponseEntity<Boolean> pauseJob(@ApiParam(value = "任务名称", required = true) @RequestParam(value = "jobName") String jobName,
                                            @ApiParam(value = "任务分组", required = true) @RequestParam(value = "jobGroup") String jobGroup) {
        ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
        quartzJobManager.pauseJob(jobName, jobGroup);
        responseEntity.setData(true);
        return responseEntity;
    }

    @PostMapping("/resumeJob")
    @ApiOperation("恢复任务")
    @RequiresPermissions("tool_quartz_list")
    public ResponseEntity<Boolean> resumeJob(@ApiParam(value = "任务名称", required = true) @RequestParam(value = "jobName") String jobName,
                                             @ApiParam(value = "任务分组", required = true) @RequestParam(value = "jobGroup") String jobGroup) {
        ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
        quartzJobManager.resumeJob(jobName, jobGroup);
        responseEntity.setData(true);
        return responseEntity;
    }

    @PostMapping("/batchDeleteJob")
    @ApiOperation("删除任务")
    @RequiresPermissions("tool_quartz_del")
    public ResponseEntity<Boolean> batchDeleteJob(@ApiParam(value = "任务信息", required = true) @RequestBody List<QuartzJob> quartzJobs) {
        ResponseEntity<Boolean> responseEntity = new ResponseEntity<>();
        quartzJobService.batchDeleteJob(quartzJobs);
        responseEntity.setData(true);
        return responseEntity;
    }

    @PostMapping("/getAllMethod")
    @ApiOperation("获取定时类中所有方法")
    @RequiresPermissions("tool_quartz_add")
    public ResponseEntity<List<String>> getAllMethod(@ApiParam(value = "类名称", required = true) @RequestParam(value = "className") String className) {
        ResponseEntity<List<String>> responseEntity = new ResponseEntity<>();
        try {
            responseEntity.setData(quartzJobService.getAllMethod(className));
        } catch (Exception e) {
            log.error(e.toString(), e);
            responseEntity.setCode(10000).setMessage("获取执行类失败！");
        }
        return responseEntity;
    }
}
