定时任务

This commit is contained in:
liukang 2025-02-10 17:08:11 +08:00
parent c1ba9ee6a9
commit 5673e0b46a
10 changed files with 120 additions and 72 deletions

View File

@ -19,6 +19,8 @@ import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Apply;
import com.ruoyi.common.core.page.TableDataInfo;
import javax.websocket.server.PathParam;
/**
* 申请Controller
*
@ -76,4 +78,12 @@ public class ApplyController extends BaseController {
}
/**
* 确认收款
*/
@PreAuthorize("@ss.hasPermi('system:apply:confirm')")
@PostMapping("/confirm/{id}")
public AjaxResult confirm(@PathVariable("id") Long id) throws Exception {
return toAjax(applyService.confirm(id));
}
}

View File

@ -78,7 +78,7 @@ public class PerformController extends BaseController {
@Log(title = "演出", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody Perform perform) throws Exception {
return toAjax(performService.insertPerform(perform));
return toAjax(performService.insertPerform(perform,getUsername()));
}
/**

View File

@ -2,6 +2,7 @@ package com.ruoyi.quartz.service.impl;
import java.util.List;
import javax.annotation.PostConstruct;
import org.quartz.JobDataMap;
import org.quartz.JobKey;
import org.quartz.Scheduler;
@ -19,12 +20,11 @@ import com.ruoyi.quartz.util.ScheduleUtils;
/**
* 定时任务调度信息 服务层
*
*
* @author ruoyi
*/
@Service
public class SysJobServiceImpl implements ISysJobService
{
public class SysJobServiceImpl implements ISysJobService {
@Autowired
private Scheduler scheduler;
@ -35,55 +35,49 @@ public class SysJobServiceImpl implements ISysJobService
* 项目启动时初始化定时器 主要是防止手动修改数据库导致未同步到定时任务处理不能手动修改数据库ID和任务组名否则会导致脏数据
*/
@PostConstruct
public void init() throws SchedulerException, TaskException
{
public void init() throws SchedulerException, TaskException {
scheduler.clear();
List<SysJob> jobList = jobMapper.selectJobAll();
for (SysJob job : jobList)
{
for (SysJob job : jobList) {
ScheduleUtils.createScheduleJob(scheduler, job);
}
}
/**
* 获取quartz调度器的计划任务列表
*
*
* @param job 调度信息
* @return
*/
@Override
public List<SysJob> selectJobList(SysJob job)
{
public List<SysJob> selectJobList(SysJob job) {
return jobMapper.selectJobList(job);
}
/**
* 通过调度任务ID查询调度信息
*
*
* @param jobId 调度任务ID
* @return 调度任务对象信息
*/
@Override
public SysJob selectJobById(Long jobId)
{
public SysJob selectJobById(Long jobId) {
return jobMapper.selectJobById(jobId);
}
/**
* 暂停任务
*
*
* @param job 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int pauseJob(SysJob job) throws SchedulerException
{
public int pauseJob(SysJob job) throws SchedulerException {
Long jobId = job.getJobId();
String jobGroup = job.getJobGroup();
job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
int rows = jobMapper.updateJob(job);
if (rows > 0)
{
if (rows > 0) {
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
}
return rows;
@ -91,19 +85,17 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 恢复任务
*
*
* @param job 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int resumeJob(SysJob job) throws SchedulerException
{
public int resumeJob(SysJob job) throws SchedulerException {
Long jobId = job.getJobId();
String jobGroup = job.getJobGroup();
job.setStatus(ScheduleConstants.Status.NORMAL.getValue());
int rows = jobMapper.updateJob(job);
if (rows > 0)
{
if (rows > 0) {
scheduler.resumeJob(ScheduleUtils.getJobKey(jobId, jobGroup));
}
return rows;
@ -111,18 +103,16 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 删除任务后所对应的trigger也将被删除
*
*
* @param job 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int deleteJob(SysJob job) throws SchedulerException
{
public int deleteJob(SysJob job) throws SchedulerException {
Long jobId = job.getJobId();
String jobGroup = job.getJobGroup();
int rows = jobMapper.deleteJobById(jobId);
if (rows > 0)
{
if (rows > 0) {
scheduler.deleteJob(ScheduleUtils.getJobKey(jobId, jobGroup));
}
return rows;
@ -130,16 +120,14 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 批量删除调度信息
*
*
* @param jobIds 需要删除的任务ID
* @return 结果
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteJobByIds(Long[] jobIds) throws SchedulerException
{
for (Long jobId : jobIds)
{
public void deleteJobByIds(Long[] jobIds) throws SchedulerException {
for (Long jobId : jobIds) {
SysJob job = jobMapper.selectJobById(jobId);
deleteJob(job);
}
@ -147,21 +135,17 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 任务调度状态修改
*
*
* @param job 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int changeStatus(SysJob job) throws SchedulerException
{
public int changeStatus(SysJob job) throws SchedulerException {
int rows = 0;
String status = job.getStatus();
if (ScheduleConstants.Status.NORMAL.getValue().equals(status))
{
if (ScheduleConstants.Status.NORMAL.getValue().equals(status)) {
rows = resumeJob(job);
}
else if (ScheduleConstants.Status.PAUSE.getValue().equals(status))
{
} else if (ScheduleConstants.Status.PAUSE.getValue().equals(status)) {
rows = pauseJob(job);
}
return rows;
@ -169,13 +153,12 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 立即运行任务
*
*
* @param job 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean run(SysJob job) throws SchedulerException
{
public boolean run(SysJob job) throws SchedulerException {
boolean result = false;
Long jobId = job.getJobId();
String jobGroup = job.getJobGroup();
@ -184,8 +167,7 @@ public class SysJobServiceImpl implements ISysJobService
JobDataMap dataMap = new JobDataMap();
dataMap.put(ScheduleConstants.TASK_PROPERTIES, properties);
JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup);
if (scheduler.checkExists(jobKey))
{
if (scheduler.checkExists(jobKey)) {
result = true;
scheduler.triggerJob(jobKey, dataMap);
}
@ -194,17 +176,15 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 新增任务
*
*
* @param job 调度信息 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int insertJob(SysJob job) throws SchedulerException, TaskException
{
job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
public int insertJob(SysJob job) throws SchedulerException, TaskException {
//job.setStatus(ScheduleConstants.Status.PAUSE.getValue());
int rows = jobMapper.insertJob(job);
if (rows > 0)
{
if (rows > 0) {
ScheduleUtils.createScheduleJob(scheduler, job);
}
return rows;
@ -212,17 +192,15 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 更新任务的时间表达式
*
*
* @param job 调度信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public int updateJob(SysJob job) throws SchedulerException, TaskException
{
public int updateJob(SysJob job) throws SchedulerException, TaskException {
SysJob properties = selectJobById(job.getJobId());
int rows = jobMapper.updateJob(job);
if (rows > 0)
{
if (rows > 0) {
updateSchedulerJob(job, properties.getJobGroup());
}
return rows;
@ -230,17 +208,15 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 更新任务
*
* @param job 任务对象
*
* @param job 任务对象
* @param jobGroup 任务组名
*/
public void updateSchedulerJob(SysJob job, String jobGroup) throws SchedulerException, TaskException
{
public void updateSchedulerJob(SysJob job, String jobGroup) throws SchedulerException, TaskException {
Long jobId = job.getJobId();
// 判断是否存在
JobKey jobKey = ScheduleUtils.getJobKey(jobId, jobGroup);
if (scheduler.checkExists(jobKey))
{
if (scheduler.checkExists(jobKey)) {
// 防止创建时存在数据问题 先移除然后在执行创建操作
scheduler.deleteJob(jobKey);
}
@ -249,13 +225,12 @@ public class SysJobServiceImpl implements ISysJobService
/**
* 校验cron表达式是否有效
*
*
* @param cronExpression 表达式
* @return 结果
*/
@Override
public boolean checkCronExpressionIsValid(String cronExpression)
{
public boolean checkCronExpressionIsValid(String cronExpression) {
return CronUtils.isValid(cronExpression);
}
}

View File

@ -23,6 +23,12 @@
<artifactId>ruoyi-common</artifactId>
</dependency>
<!-- 定时任务-->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-quartz</artifactId>
<version>${ruoyi.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -44,6 +44,14 @@ public interface ApplyMapper
*/
public int updateApply(Apply apply);
/**
* 确认付款
*
* @param apply 申请
* @return 结果
*/
public int confirm(Apply apply);
/**
* 删除申请
*

View File

@ -40,7 +40,7 @@ public interface IPerformService {
* @param perform 演出
* @return 结果
*/
public int insertPerform(Perform perform) throws Exception;
public int insertPerform(Perform perform, String userName) throws Exception;
/**
* 修改演出

View File

@ -4,11 +4,14 @@ import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.quartz.domain.SysJob;
import com.ruoyi.quartz.service.ISysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PerformMapper;
import com.ruoyi.system.domain.Perform;
import com.ruoyi.system.service.IPerformService;
import org.springframework.transaction.annotation.Transactional;
/**
* 演出Service业务层处理
@ -20,6 +23,8 @@ import com.ruoyi.system.service.IPerformService;
public class PerformServiceImpl implements IPerformService {
@Autowired
private PerformMapper performMapper;
@Autowired
private ISysJobService jobService;
/**
* 查询演出
@ -60,13 +65,24 @@ public class PerformServiceImpl implements IPerformService {
* @return 结果
*/
@Override
public int insertPerform(Perform perform) throws Exception {
@Transactional
public int insertPerform(Perform perform, String userName) throws Exception {
if (perform.getStartTime().compareTo(new Date()) <= 0) {
throw new Exception("开始时间必须大于当前时间");
throw new Exception("开始时间必须大于当前时间");
}
perform.setCreateTime(DateUtils.getNowDate());
//未开始
perform.setStatus("1");
SysJob job = new SysJob();
job.setJobName(perform.getName() + " " + perform.getStartTime());
job.setCronExpression("* * * * * ?");
job.setInvokeTarget("ryTask.ryParams('ry')");
job.setJobGroup("DEFAULT");
job.setMisfirePolicy("2");
job.setStatus("0");
job.setCreateBy(userName);
jobService.insertJob(job);
return performMapper.insertPerform(perform);
}
@ -79,7 +95,7 @@ public class PerformServiceImpl implements IPerformService {
@Override
public int updatePerform(Perform perform) throws Exception {
if (perform.getStartTime().compareTo(new Date()) <= 0) {
throw new Exception("开始时间必须大于当前时间");
throw new Exception("开始时间必须大于当前时间");
}
perform.setUpdateTime(DateUtils.getNowDate());
return performMapper.updatePerform(perform);

View File

@ -58,4 +58,10 @@ public interface IApplyService
* @return 结果
*/
public int deleteApplyById(Long id);
/**
* 确认付款
*/
public int confirm(Long id) throws Exception;
}

View File

@ -52,7 +52,7 @@ public class ApplyServiceImpl implements IApplyService {
@Override
public int insertApply(Apply apply) throws Exception {
Apply a = applyMapper.selectApplyPerform(apply);
if(a !=null){
if (a != null) {
throw new Exception("该团您已经申请过,请勿重复申请");
}
apply.setCreateTime(DateUtils.getNowDate());
@ -99,4 +99,22 @@ public class ApplyServiceImpl implements IApplyService {
public int deleteApplyById(Long id) {
return applyMapper.deleteApplyById(id);
}
/**
* 确认付款
*
* @param id
* @return
*/
@Override
public int confirm(Long id) throws Exception {
Apply apply1 = applyMapper.selectApplyById(id);
if(!"0".equals(apply1.getStatus())){
throw new Exception("状态不为【未确认】");
}
Apply apply = new Apply();
apply.setId(id);
apply.setStatus("1");
return applyMapper.updateApply(apply);
}
}

View File

@ -150,6 +150,15 @@
where id = #{id}
</update>
<update id="confirm" parameterType="Apply">
update apply
set status = #{status}
where id = #{id}
and status = '0'
</update>
<delete id="deleteApplyById" parameterType="Long">
delete
from apply