|
|
@@ -1,8 +1,20 @@
|
|
|
package cn.tr.module.smart.common.task;
|
|
|
|
|
|
-import cn.tr.module.smart.common.repository.BizMpPublishLogRepository;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.tr.core.exception.ServiceException;
|
|
|
+import cn.tr.core.exception.TRExcCode;
|
|
|
+import cn.tr.module.quartz.job.service.ISysJobService;
|
|
|
+import cn.tr.module.smart.common.po.BizMpPublishTaskPO;
|
|
|
+import cn.tr.module.smart.common.repository.BizMpPublishTaskRepository;
|
|
|
+import cn.tr.plugin.lock.annotation.DistributeLock;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
/**
|
|
|
* @author wangzl
|
|
|
@@ -10,10 +22,12 @@ import org.springframework.stereotype.Component;
|
|
|
* @date 2025/7/25 14:38
|
|
|
*/
|
|
|
@Component
|
|
|
+@Slf4j
|
|
|
public class QuartzTaskExecutor {
|
|
|
-
|
|
|
@Autowired
|
|
|
- private BizMpPublishLogRepository bizMpPublishLogRepository;
|
|
|
+ private BizMpPublishTaskRepository bizMpPublishTaskRepository;
|
|
|
+ @Autowired
|
|
|
+ private ISysJobService sysJobService;
|
|
|
|
|
|
/**
|
|
|
* @description: 定时任务执行逻辑
|
|
|
@@ -21,7 +35,32 @@ public class QuartzTaskExecutor {
|
|
|
* @author wangzl
|
|
|
* @date 2025/7/25
|
|
|
*/
|
|
|
- public void execute(String taskId) {
|
|
|
-
|
|
|
+ @DistributeLock(name = "#taskId")
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void execute(String taskId) {
|
|
|
+ BizMpPublishTaskPO bizMpPublishTaskPO = bizMpPublishTaskRepository.selectById(taskId);
|
|
|
+ if (ObjectUtil.isNull(bizMpPublishTaskPO)) {
|
|
|
+ log.info("任务不存在");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!Objects.equals(bizMpPublishTaskPO.getPublishCount(), bizMpPublishTaskPO.getTotalCount())) {
|
|
|
+ log.info("满足标准发送推送公众号");
|
|
|
+ //更新执行次数
|
|
|
+ bizMpPublishTaskRepository.update(null,new LambdaUpdateWrapper<BizMpPublishTaskPO>()
|
|
|
+ .set(BizMpPublishTaskPO::getPublishCount,bizMpPublishTaskPO.getPublishCount()+1)
|
|
|
+ .eq(BizMpPublishTaskPO::getId,taskId));
|
|
|
+ //执行推送数据
|
|
|
+ } else {
|
|
|
+ log.info("任务执行次数已完成");
|
|
|
+ //删除任务
|
|
|
+ bizMpPublishTaskRepository.deleteById(taskId);
|
|
|
+ //删除触发器
|
|
|
+ try {
|
|
|
+ sysJobService.removeSysJobByIds(new ArrayList<>());
|
|
|
+ throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"测试异常");
|
|
|
+ }catch (Exception e){
|
|
|
+ log.warn("删除定时任务 {}:执行异常了 :{}",taskId,e.getStackTrace());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|