QuartzJobController.java 3.7 KB
Newer Older
1
package me.zhengjie.modules.quartz.rest;
2

3 4
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
5
import lombok.extern.slf4j.Slf4j;
6 7 8 9
import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.quartz.domain.QuartzJob;
import me.zhengjie.modules.quartz.service.QuartzJobService;
10
import me.zhengjie.modules.quartz.service.dto.JobQueryCriteria;
11 12 13 14 15 16 17 18
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
19
 * @author Zheng Jie
20 21 22 23
 * @date 2019-01-07
 */
@Slf4j
@RestController
24 25
@Api(tags = "系统:定时任务管理")
@RequestMapping("/api/jobs")
26 27 28 29
public class QuartzJobController {

    private static final String ENTITY_NAME = "quartzJob";

30 31 32 33 34
    private final QuartzJobService quartzJobService;

    public QuartzJobController(QuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }
35

36
    @Log("查询定时任务")
37 38
    @ApiOperation("查询定时任务")
    @GetMapping
39
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_SELECT')")
40
    public ResponseEntity getJobs(JobQueryCriteria criteria, Pageable pageable){
41
        return new ResponseEntity<>(quartzJobService.queryAll(criteria,pageable), HttpStatus.OK);
42 43
    }

44 45
    @ApiOperation("查询任务执行日志")
    @GetMapping(value = "/logs")
46
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_SELECT')")
47
    public ResponseEntity getJobLogs(JobQueryCriteria criteria, Pageable pageable){
48
        return new ResponseEntity<>(quartzJobService.queryAllLog(criteria,pageable), HttpStatus.OK);
49 50
    }

51
    @Log("新增定时任务")
52 53
    @ApiOperation("新增定时任务")
    @PostMapping
54 55 56 57 58
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_CREATE')")
    public ResponseEntity create(@Validated @RequestBody QuartzJob resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
59
        return new ResponseEntity<>(quartzJobService.create(resources),HttpStatus.CREATED);
60 61
    }

62
    @Log("修改定时任务")
63 64
    @ApiOperation("修改定时任务")
    @PutMapping
65
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')")
66
    public ResponseEntity update(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources){
67 68 69 70
        quartzJobService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

71
    @Log("更改定时任务状态")
72 73
    @ApiOperation("更改定时任务状态")
    @PutMapping(value = "/{id}")
74 75 76 77 78 79
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')")
    public ResponseEntity updateIsPause(@PathVariable Long id){
        quartzJobService.updateIsPause(quartzJobService.findById(id));
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

80
    @Log("执行定时任务")
81 82
    @ApiOperation("执行定时任务")
    @PutMapping(value = "/exec/{id}")
83 84 85 86 87 88
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_EDIT')")
    public ResponseEntity execution(@PathVariable Long id){
        quartzJobService.execution(quartzJobService.findById(id));
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

89
    @Log("删除定时任务")
90 91
    @ApiOperation("删除定时任务")
    @DeleteMapping(value = "/{id}")
92 93 94 95 96 97
    @PreAuthorize("hasAnyRole('ADMIN','JOB_ALL','JOB_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        quartzJobService.delete(quartzJobService.findById(id));
        return new ResponseEntity(HttpStatus.OK);
    }
}