|
@@ -0,0 +1,186 @@
|
|
|
|
|
+package com.dataeasy.server.task;
|
|
|
|
|
+
|
|
|
|
|
+import java.sql.Timestamp;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+import com.dataeasy.server.core.config.WxMessageProperties;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import com.dataeasy.server.atomic.entity.SubscriptionTaskConfig;
|
|
|
|
|
+import com.dataeasy.server.atomic.entity.SubscriptionUserConfig;
|
|
|
|
|
+import com.dataeasy.server.atomic.entity.SysScheduleTaskLog;
|
|
|
|
|
+import com.dataeasy.server.atomic.entity.User;
|
|
|
|
|
+import com.dataeasy.server.atomic.service.ISubscriptionTaskConfigService;
|
|
|
|
|
+import com.dataeasy.server.atomic.service.ISubscriptionUserConfigService;
|
|
|
|
|
+import com.dataeasy.server.atomic.service.ISysScheduleTaskLogService;
|
|
|
|
|
+import com.dataeasy.server.atomic.service.IUserService;
|
|
|
|
|
+import com.dataeasy.server.constant.ExecuteOptionEnum;
|
|
|
|
|
+import com.dataeasy.server.constant.PushOptionEnum;
|
|
|
|
|
+import com.dataeasy.server.constant.ScheduleTaskEnum;
|
|
|
|
|
+import com.dataeasy.server.constant.ScheduleTaskStatusEnum;
|
|
|
|
|
+import com.dataeasy.server.pojo.subscription.SubscriptionTaskConfigQuery;
|
|
|
|
|
+import com.dataeasy.server.pojo.subscription.SubscriptionUserConfigQuery;
|
|
|
|
|
+import com.dataeasy.server.service.manager.IWxManager;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.Builder;
|
|
|
|
|
+import lombok.Data;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
|
|
|
|
|
+import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author tyuio
|
|
|
|
|
+ * @version 1.0.0
|
|
|
|
|
+ * @description 定时任务 抽象类
|
|
|
|
|
+ * @date 2025/3/7 10:44
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public abstract class AbstractTask {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected ISysScheduleTaskLogService taskLogService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected ISubscriptionTaskConfigService taskConfigService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected ISubscriptionUserConfigService userConfigService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected IUserService userService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected IWxManager wxMpManager;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ protected WxMessageProperties wxMessageProperties;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 定时任务执行主逻辑
|
|
|
|
|
+ */
|
|
|
|
|
+ public void executeMain() {
|
|
|
|
|
+ ScheduleTaskEnum task = getTaskEnum();
|
|
|
|
|
+ log.info("======= {} 开始执行 =======", task.getName());
|
|
|
|
|
+ // 新增任务执行记录
|
|
|
|
|
+ SysScheduleTaskLog addTaskLog = task.buildTaskLog();
|
|
|
|
|
+ taskLogService.insert(addTaskLog);
|
|
|
|
|
+
|
|
|
|
|
+ // 查询需要执行的任务配置
|
|
|
|
|
+ SubscriptionTaskConfigQuery taskConfigQuery = new SubscriptionTaskConfigQuery();
|
|
|
|
|
+ taskConfigQuery.setTaskCode(task.toString());
|
|
|
|
|
+ taskConfigQuery.setExecuteOption(ExecuteOptionEnum.ENABLE.toString());
|
|
|
|
|
+ List<SubscriptionTaskConfig> taskConfigList = taskConfigService.getByCondition(taskConfigQuery);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(taskConfigList)) {
|
|
|
|
|
+ log.warn("任务:{},没有找到执行状态为启用的配置,结束执行", task.getName());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Long> subscriptionSourceIds = taskConfigList.stream().filter(v -> Objects.nonNull(v.getSubscriptionSourceId()))
|
|
|
|
|
+ .map(SubscriptionTaskConfig::getSubscriptionSourceId)
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ if (CollectionUtils.isEmpty(subscriptionSourceIds)) {
|
|
|
|
|
+ log.warn("任务:{},任务编码:{},没有找到有效的订阅源,结束执行", task.getName(), task);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 拉取数据并入库
|
|
|
|
|
+ fetchData();
|
|
|
|
|
+
|
|
|
|
|
+ // 寻找要推送数据的用户
|
|
|
|
|
+ SubscriptionUserConfigQuery userConfigQuery = new SubscriptionUserConfigQuery();
|
|
|
|
|
+ userConfigQuery.setSubscriptionSourceIds(subscriptionSourceIds);
|
|
|
|
|
+ userConfigQuery.setPushOption(PushOptionEnum.ENABLE);
|
|
|
|
|
+ userConfigQuery.setCurrentTime(new Timestamp(System.currentTimeMillis()));
|
|
|
|
|
+ List<SubscriptionUserConfig> userConfigList = userConfigService.getByCondition(userConfigQuery);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(userConfigList)) {
|
|
|
|
|
+ log.warn("任务:{},任务编码:{},没有找到推送状态为启用的用户配置,结束执行", task.getName(), task);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<Long> userIds = userConfigList.stream().filter(v -> Objects.nonNull(v.getUserId()))
|
|
|
|
|
+ .map(SubscriptionUserConfig::getUserId)
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ if (CollectionUtils.isEmpty(userIds)) {
|
|
|
|
|
+ log.warn("任务:{},任务编码:{},没有找到推送状态为启用的用户,结束执行", task.getName(), task);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<User> userList = userService.getByIds(userIds);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(userList)) {
|
|
|
|
|
+ log.warn("任务:{},任务编码:{},没有找到待推送的用户列表,结束执行", task.getName(), task);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Set<String> mpOpenIds = userList.stream().filter(v -> StringUtils.hasText(v.getMpOpenId()))
|
|
|
|
|
+ .map(User::getMpOpenId)
|
|
|
|
|
+ .collect(Collectors.toSet());
|
|
|
|
|
+ if (CollectionUtils.isEmpty(mpOpenIds)) {
|
|
|
|
|
+ log.warn("任务:{},任务编码:{},待推送的用户列表中没有找到服务号openid,结束执行", task.getName(), task);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取消息模板所需数据
|
|
|
|
|
+ TemplateMessage templateMessage = getTemplateMessage();
|
|
|
|
|
+
|
|
|
|
|
+ // 推送
|
|
|
|
|
+ for (String mpOpenId : mpOpenIds) {
|
|
|
|
|
+ // 生成模板信息
|
|
|
|
|
+ WxMpTemplateMessage mpTemplateMessage = WxMpTemplateMessage.builder()
|
|
|
|
|
+ .toUser(mpOpenId)
|
|
|
|
|
+ .templateId(templateMessage.getTemplateId())
|
|
|
|
|
+ .url(templateMessage.getUrl())
|
|
|
|
|
+ .data(templateMessage.getTemplateDataList())
|
|
|
|
|
+ .build();
|
|
|
|
|
+ wxMpManager.sendTemplateMessage(mpTemplateMessage);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 更新任务执行记录
|
|
|
|
|
+ SysScheduleTaskLog updateTaskLog = new SysScheduleTaskLog();
|
|
|
|
|
+ updateTaskLog.setId(addTaskLog.getId());
|
|
|
|
|
+ updateTaskLog.setProcessStatus(ScheduleTaskStatusEnum.SUCCESS);
|
|
|
|
|
+ taskLogService.updateById(updateTaskLog);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("======= {} 执行结束 =======", task.getName());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取定时任务枚举
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public abstract ScheduleTaskEnum getTaskEnum();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 拉取所需数据并入库
|
|
|
|
|
+ */
|
|
|
|
|
+ public abstract void fetchData();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取消息模板所需数据
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public abstract TemplateMessage getTemplateMessage();
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 定时任务中消息模板所需的数据
|
|
|
|
|
+ */
|
|
|
|
|
+ @Data
|
|
|
|
|
+ @Builder
|
|
|
|
|
+ public static class TemplateMessage {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 模板跳转链接
|
|
|
|
|
+ */
|
|
|
|
|
+ private String url;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 消息模板ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private String templateId;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 模板所需数据
|
|
|
|
|
+ */
|
|
|
|
|
+ private List<WxMpTemplateData> templateDataList;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|