package org.nl.system.controller.quartz;


import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.exception.BadRequestException;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.quartz.ISysQuartzJobService;
import org.nl.system.service.quartz.dao.SysQuartzJob;
import org.nl.system.service.quartz.dto.JobQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 * 定时任务表 前端控制器
 * </p>
 *
 * @author generator
 * @since 2022-12-19
 */
@Slf4j
@RestController
@RequestMapping("/api/jobs")
@Api(tags = "系统:定时任务管理")
public class SysQuartzJobController {
    private static final String ENTITY_NAME = "quartzJob";

    @Autowired
    private ISysQuartzJobService quartzJobService;

    @ApiOperation("查询定时任务")
    @GetMapping
    @SaCheckPermission("timing:list")
    public ResponseEntity<Object> query(JobQuery criteria, PageQuery pageable) {
        return new ResponseEntity<>(TableDataInfo.build(quartzJobService.queryAll(criteria, pageable)), HttpStatus.OK);
    }

    @ApiOperation("查询任务执行日志")
    @GetMapping(value = "/logs")
    @SaCheckPermission("timing:list")
    public ResponseEntity<Object> queryJobLog(JobQuery criteria, PageQuery pageable) {
        return new ResponseEntity<>(TableDataInfo.build(quartzJobService.queryAllLog(criteria, pageable)), HttpStatus.OK);
    }

    @Log("新增定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping
    @SaCheckPermission("timing:add")
    public ResponseEntity<Object> create(@Validated @RequestBody SysQuartzJob resources) {
        if (resources.getJob_id() != null) {
            throw new BadRequestException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        quartzJobService.createJob(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PutMapping
    @SaCheckPermission("timing:edit")
    public ResponseEntity<Object> update(@Validated @RequestBody SysQuartzJob resources) {
        quartzJobService.updateJob(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("更改定时任务状态")
    @ApiOperation("更改定时任务状态")
    @PutMapping(value = "/{id}")
    @SaCheckPermission("timing:edit")
    public ResponseEntity<Object> update(@PathVariable String id) {
        quartzJobService.updateIsPause(quartzJobService.getById(id));
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("执行定时任务")
    @ApiOperation("执行定时任务")
    @PutMapping(value = "/exec/{id}")
    @SaCheckPermission("timing:edit")
    public ResponseEntity<Object> execution(@PathVariable String id) {
        quartzJobService.execution(quartzJobService.getById(id));
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除定时任务")
    @ApiOperation("删除定时任务")
    @DeleteMapping
    @SaCheckPermission("timing:del")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        quartzJobService.deleteJob(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

