增加示例
This commit is contained in:
parent
6a0a4626fb
commit
ae1e12d31b
@ -0,0 +1,183 @@
|
||||
package com.yfd.platform.system.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yfd.platform.annotation.Log;
|
||||
import com.yfd.platform.config.ResponseResult;
|
||||
import com.yfd.platform.system.domain.QuartzJob;
|
||||
import com.yfd.platform.system.service.IQuartzJobService;
|
||||
import com.yfd.platform.system.service.impl.UserServiceImpl;
|
||||
import com.yfd.platform.utils.QuartzManage;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.quartz.CronExpression;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 定时任务 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author TangWei
|
||||
* @since 2023-03-19
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/system/quartzjob")
|
||||
@Tag(name = "定时任务")
|
||||
@Transactional
|
||||
public class QuartzJobController {
|
||||
|
||||
@Resource
|
||||
private IQuartzJobService quartzJobService;
|
||||
|
||||
@Resource
|
||||
private UserServiceImpl currentUser;
|
||||
|
||||
@Resource
|
||||
private QuartzManage quartzManage;
|
||||
|
||||
@Operation(summary = "查询定时任务")
|
||||
@GetMapping("/getQuartzJobList")
|
||||
public ResponseResult getQuartzJobList(Page<QuartzJob> page,
|
||||
String jobName) {
|
||||
LambdaQueryWrapper<QuartzJob> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (StrUtil.isNotBlank(jobName)) {
|
||||
queryWrapper.like(QuartzJob::getJobName, jobName);
|
||||
}
|
||||
queryWrapper.orderByAsc(QuartzJob::getOrderno);
|
||||
Page<QuartzJob> pageList = quartzJobService.page(page, queryWrapper);
|
||||
return ResponseResult.successData(pageList);
|
||||
}
|
||||
|
||||
@Log(module = "定时任务管理", value = "新增定时任务")
|
||||
@Operation(summary = "新增定时任务")
|
||||
@PostMapping("/addQuartzJob")
|
||||
public ResponseResult addQuartzJob(@RequestBody QuartzJob quartzJob) {
|
||||
if (quartzJob == null) {
|
||||
return ResponseResult.error("参数为空");
|
||||
}
|
||||
// 添加最近修改人
|
||||
quartzJob.setLastmodifier(currentUser.getUsername());
|
||||
// 添加最近修改时间
|
||||
quartzJob.setLastmodifydate(new Timestamp(System.currentTimeMillis()));
|
||||
if (StrUtil.isBlank(quartzJob.getJobCron()) || !CronExpression.isValidExpression(quartzJob.getJobCron())) {
|
||||
return ResponseResult.error("cron表达式格式错误");
|
||||
}
|
||||
quartzJob.setStatus("0");
|
||||
boolean ok = quartzJobService.addQuartzJob(quartzJob);
|
||||
quartzManage.addJob(quartzJob);
|
||||
if (ok) {
|
||||
return ResponseResult.success();
|
||||
} else {
|
||||
return ResponseResult.error("新增失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Log(module = "定时任务管理", value = "设置定时任务是否有效")
|
||||
@Operation(summary = "设置定时任务是否有效")
|
||||
@PostMapping("/setQuartzStatus")
|
||||
public ResponseResult setQuartzStatus(@RequestParam String id,
|
||||
@RequestParam String status) {
|
||||
if (StrUtil.isBlank(id) || StrUtil.isBlank(status)) {
|
||||
return ResponseResult.error("参数为空");
|
||||
}
|
||||
LambdaUpdateWrapper<QuartzJob> updateWrapper =
|
||||
new LambdaUpdateWrapper<>();
|
||||
//根据id 更新状态,最近修改人,最近修改时间
|
||||
updateWrapper.eq(QuartzJob::getId, id).set(QuartzJob::getStatus,
|
||||
status).set(
|
||||
QuartzJob::getLastmodifier, currentUser.getUsername()).set(QuartzJob::getLastmodifydate,
|
||||
LocalDateTime.now());
|
||||
boolean ok = quartzJobService.update(updateWrapper);
|
||||
QuartzJob quartzJob = quartzJobService.getById(id);
|
||||
if ("0".equals(quartzJob.getStatus())) {
|
||||
quartzManage.pauseJob(quartzJob);
|
||||
} else {
|
||||
quartzManage.resumeJob(quartzJob);
|
||||
}
|
||||
|
||||
if (ok) {
|
||||
return ResponseResult.success();
|
||||
} else {
|
||||
return ResponseResult.error();
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "根据ID查询定时任务")
|
||||
@GetMapping("/getQuartzJobById")
|
||||
public ResponseResult getQuartzJobById(String id) {
|
||||
QuartzJob quartzJob = quartzJobService.getById(id);
|
||||
return ResponseResult.successData(quartzJob);
|
||||
}
|
||||
|
||||
@Log(module = "定时任务管理", value = "修改定时任务")
|
||||
@Operation(summary = "修改定时任务")
|
||||
@PostMapping("/updateQuartzJob")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ResponseResult updateQuartzJob(@RequestBody QuartzJob quartzJob) {
|
||||
// 添加最近修改人
|
||||
quartzJob.setLastmodifier(currentUser.getUsername());
|
||||
// 添加最近修改时间
|
||||
quartzJob.setLastmodifydate(new Timestamp(System.currentTimeMillis()));
|
||||
if (StrUtil.isBlank(quartzJob.getJobCron()) || !CronExpression.isValidExpression(quartzJob.getJobCron())) {
|
||||
return ResponseResult.error("cron表达式格式错误");
|
||||
}
|
||||
boolean ok = quartzJobService.updateById(quartzJob);
|
||||
quartzManage.updateJobCron(quartzJob);
|
||||
if (ok) {
|
||||
return ResponseResult.success();
|
||||
} else {
|
||||
return ResponseResult.error("修改失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Log(module = "定时任务管理", value = "删除定时任务")
|
||||
@Operation(summary = "删除定时任务")
|
||||
@PostMapping("/deleteQuartzJob")
|
||||
public ResponseResult deleteQuartzJob(@RequestParam String id) {
|
||||
if (StrUtil.isBlank(id)) {
|
||||
return ResponseResult.error("参数为空");
|
||||
}
|
||||
boolean ok = quartzJobService.deleteQuartzJob(id);
|
||||
if (ok) {
|
||||
return ResponseResult.success();
|
||||
} else {
|
||||
return ResponseResult.error("删除失败");
|
||||
}
|
||||
}
|
||||
|
||||
@Log(module = "定时任务管理", value = "执行定时任务")
|
||||
@Operation(summary = "执行定时任务")
|
||||
@PostMapping("/execution")
|
||||
public ResponseResult execution(@RequestParam String id) {
|
||||
quartzJobService.execution(quartzJobService.getById(id));
|
||||
return ResponseResult.success();
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* 用途说明: 拖动修改定时顺序
|
||||
* 参数说明 fromID 当前ID toID 到达ID
|
||||
* 返回值说明: com.yfd.platform.config.ResponseResult 成功或者失败
|
||||
***********************************/
|
||||
@Log(module = "定时任务管理", value = "拖动定时任务")
|
||||
@PostMapping("/changeDictOrder")
|
||||
@Operation(summary = "拖动修改定时任务顺序")
|
||||
public ResponseResult changeQuartzOrder(@RequestParam String fromID,
|
||||
@RequestParam String toID) {
|
||||
|
||||
boolean ok = quartzJobService.changeDictOrder(fromID, toID);
|
||||
if (ok) {
|
||||
return ResponseResult.success();
|
||||
} else {
|
||||
return ResponseResult.error();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,114 @@
|
||||
package com.yfd.platform.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.yfd.platform.system.domain.QuartzJob;
|
||||
import com.yfd.platform.system.domain.SysDictionary;
|
||||
import com.yfd.platform.system.mapper.QuartzJobMapper;
|
||||
import com.yfd.platform.system.service.IQuartzJobService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yfd.platform.utils.QuartzManage;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 定时任务 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author TangWei
|
||||
* @since 2023-03-19
|
||||
*/
|
||||
@Service
|
||||
public class QuartzJobServiceImpl extends ServiceImpl<QuartzJobMapper,
|
||||
QuartzJob> implements IQuartzJobService {
|
||||
|
||||
@Resource
|
||||
private QuartzJobMapper quartzJobMapper;
|
||||
|
||||
@Resource
|
||||
private QuartzManage quartzManage;
|
||||
|
||||
/**********************************
|
||||
* 用途说明: 新增定时任务
|
||||
* 参数说明 quartzJob 定时对象
|
||||
* 返回值说明: boolean 是否成功
|
||||
***********************************/
|
||||
@Override
|
||||
public boolean addQuartzJob(QuartzJob quartzJob) {
|
||||
// 生成序号
|
||||
long orderNo = this.count() + 1L;
|
||||
quartzJob.setOrderno((int) orderNo);
|
||||
return this.save(quartzJob);
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* 用途说明: 删除定时任务
|
||||
* 参数说明 id id
|
||||
* 返回值说明: boolean 是否成功
|
||||
***********************************/
|
||||
@Override
|
||||
public boolean deleteQuartzJob(String id) {
|
||||
String[] split = id.split(",");
|
||||
Set<String> ids = Arrays.stream(split).collect(Collectors.toSet());
|
||||
for (String s : ids) {
|
||||
QuartzJob quartzJob = this.getById(s);
|
||||
quartzManage.deleteJob(quartzJob);
|
||||
this.removeById(s);
|
||||
}
|
||||
|
||||
// 查询所有定时任务
|
||||
List<QuartzJob> list =
|
||||
this.list(new LambdaQueryWrapper<QuartzJob>().orderByAsc(QuartzJob::getOrderno));
|
||||
// 更新序号
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
QuartzJob quartzJob = list.get(i);
|
||||
quartzJob.setOrderno(i + 1);
|
||||
this.updateById(quartzJob);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* 用途说明: 拖动修改定时任务顺序
|
||||
* 参数说明 fromID 当前ID toID 到达ID
|
||||
* 返回值说明: com.yfd.platform.config.ResponseResult 返回拖动成功或者失败
|
||||
***********************************/
|
||||
@Override
|
||||
public boolean changeDictOrder(String fromID, String toID) {
|
||||
QuartzJob fromQuartzJob =
|
||||
quartzJobMapper.selectById(fromID);
|
||||
QuartzJob toQuartzJob = quartzJobMapper.selectById(toID);
|
||||
// 如果数据字典不存在拖动失败
|
||||
if (fromQuartzJob == null || toQuartzJob == null) {
|
||||
return false;
|
||||
}
|
||||
Integer fromOrderNo = fromQuartzJob.getOrderno();
|
||||
Integer toOrderNo = toQuartzJob.getOrderno();
|
||||
// 如果数据字典的顺序号不存在拖动失败
|
||||
if (fromOrderNo == null || toOrderNo == null) {
|
||||
return false;
|
||||
}
|
||||
// 将顺序号放入字典对象中
|
||||
fromQuartzJob.setOrderno(toOrderNo);
|
||||
toQuartzJob.setOrderno(fromOrderNo);
|
||||
// 更改顺序号
|
||||
boolean fromBool = this.updateById(fromQuartzJob);
|
||||
boolean toBool = this.updateById(toQuartzJob);
|
||||
return fromBool && toBool;
|
||||
}
|
||||
|
||||
/**********************************
|
||||
* 用途说明: 执行定时任务
|
||||
* 参数说明 id id
|
||||
* 返回值说明: void
|
||||
***********************************/
|
||||
@Override
|
||||
public void execution(QuartzJob quartzJob) {
|
||||
quartzManage.runJobNow(quartzJob);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user