|
@@ -1,17 +1,25 @@
|
|
|
package com.qmth.sop.task.job.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.qmth.sop.business.cache.CommonCacheService;
|
|
|
+import com.qmth.sop.business.entity.SysConfig;
|
|
|
+import com.qmth.sop.business.entity.TSJobRemind;
|
|
|
+import com.qmth.sop.business.service.TSJobRemindService;
|
|
|
import com.qmth.sop.common.contant.SystemConstant;
|
|
|
+import com.qmth.sop.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.sop.common.enums.JobEnum;
|
|
|
+import com.qmth.sop.task.job.FlowTaskQuartzJob;
|
|
|
import com.qmth.sop.task.job.service.JobService;
|
|
|
import com.qmth.sop.task.service.QuartzService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.time.Duration;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: job service impl
|
|
@@ -27,12 +35,57 @@ public class JobServiceImpl implements JobService {
|
|
|
@Resource
|
|
|
QuartzService quartzService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ TSJobRemindService tsJobRemindService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ CommonCacheService commonCacheService;
|
|
|
+
|
|
|
/**
|
|
|
* 流程任务提醒task
|
|
|
*/
|
|
|
@Override
|
|
|
+ @Transactional
|
|
|
public void flowTaskRemindTask() {
|
|
|
-
|
|
|
+ List<TSJobRemind> tsJobRemindList = tsJobRemindService.list(new QueryWrapper<TSJobRemind>().lambda().eq(TSJobRemind::getExec, false).last("limit 0,100"));
|
|
|
+ if (!CollectionUtils.isEmpty(tsJobRemindList)) {
|
|
|
+ List<TSJobRemind> tsJobRemindUpdateList = new ArrayList<>(tsJobRemindList.size());
|
|
|
+ List<Long> tsJobRemindIdList = new ArrayList<>(tsJobRemindList.size());
|
|
|
+ for (TSJobRemind t : tsJobRemindList) {
|
|
|
+ if (Objects.nonNull(t.getEnable()) && t.getEnable()) {//新建定时任务
|
|
|
+ switch (t.getRemindType()) {
|
|
|
+ case BEFORE://提前提醒
|
|
|
+ SysConfig sysConfigBefore = commonCacheService.addSysConfigCache(SystemConstant.JOB_BEFORE_TIME);
|
|
|
+ Optional.ofNullable(sysConfigBefore).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("提前提醒任务时间未设置"));
|
|
|
+ Long execTime = SystemConstant.processMiniuteMinus(Duration.ofHours(Long.parseLong(sysConfigBefore.getConfigValue())));
|
|
|
+ t.updateExecInfo(execTime);
|
|
|
+ this.setJobTime(execTime, t.getObjId().toString(), t, FlowTaskQuartzJob.class, JobEnum.FLOW_TASK_REMIND_QUARTZ_JOB_GROUP);
|
|
|
+ tsJobRemindUpdateList.add(t);
|
|
|
+ break;
|
|
|
+ case AFTER://超时提醒
|
|
|
+ SysConfig sysConfigAfter = commonCacheService.addSysConfigCache(SystemConstant.JOB_AFTER_TIME);
|
|
|
+ Optional.ofNullable(sysConfigAfter).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("超时提醒任务时间未设置"));
|
|
|
+ execTime = SystemConstant.processMiniutePlus(Duration.ofHours(Long.parseLong(sysConfigAfter.getConfigValue())));
|
|
|
+ t.updateExecInfo(execTime);
|
|
|
+ this.setJobTime(execTime, t.getObjId().toString(), t, FlowTaskQuartzJob.class, JobEnum.FLOW_TASK_REMIND_QUARTZ_JOB_GROUP);
|
|
|
+ tsJobRemindUpdateList.add(t);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else if (Objects.nonNull(t.getEnable()) && !t.getEnable()) {
|
|
|
+ //禁用需删除定时任务
|
|
|
+ quartzService.deleteJob(t.getObjId().toString(), JobEnum.FLOW_TASK_REMIND_QUARTZ_JOB_GROUP.name());
|
|
|
+ tsJobRemindIdList.add(t.getId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(tsJobRemindUpdateList)) {
|
|
|
+ tsJobRemindService.saveOrUpdateBatch(tsJobRemindUpdateList);
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(tsJobRemindIdList)) {
|
|
|
+ tsJobRemindService.removeByIds(tsJobRemindIdList);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -40,11 +93,12 @@ public class JobServiceImpl implements JobService {
|
|
|
*
|
|
|
* @param finishTime
|
|
|
* @param jobName
|
|
|
+ * @param o
|
|
|
* @param cls
|
|
|
* @param jobEnum
|
|
|
*/
|
|
|
@Override
|
|
|
- public void setJobTime(Long finishTime, String jobName, Class cls, JobEnum jobEnum) {
|
|
|
+ public void setJobTime(Long finishTime, String jobName, Object o, Class cls, JobEnum jobEnum) {
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
calendar.setTimeInMillis(finishTime);
|
|
|
// calendar.add(Calendar.SECOND, 30);
|
|
@@ -60,6 +114,7 @@ public class JobServiceImpl implements JobService {
|
|
|
//执行一次性延时任务
|
|
|
Map mapJob = new HashMap();
|
|
|
mapJob.put(SystemConstant.NAME, jobName);
|
|
|
+ mapJob.put(SystemConstant.JOB_DATA, o);
|
|
|
quartzService.addJob(cls, jobName, jobEnum.name(), cron, mapJob);
|
|
|
}
|
|
|
}
|