Jelajahi Sumber

通知代码重构与优化

lideyin 6 tahun lalu
induk
melakukan
36ac06e2dd

+ 91 - 15
examcloud-task-service/src/main/java/cn/com/qmth/examcloud/task/service/job/DisposePublishingNoticeTask.java

@@ -1,12 +1,24 @@
 package cn.com.qmth.examcloud.task.service.job;
 
+import cn.com.qmth.examcloud.api.commons.enums.NoticeReceiverRuleType;
+import cn.com.qmth.examcloud.api.commons.enums.NoticeStatus;
+import cn.com.qmth.examcloud.core.examwork.dao.entity.NoticeRulePublishProgressEntity;
 import cn.com.qmth.examcloud.examwork.api.NoticeCloudService;
+import cn.com.qmth.examcloud.examwork.api.bean.NoticeRulePublishProgressBean;
+import cn.com.qmth.examcloud.examwork.api.request.DisposePublishingUserNoticeReq;
+import cn.com.qmth.examcloud.examwork.api.request.UpdateNoticeStatusReq;
+import cn.com.qmth.examcloud.examwork.api.response.DisposePublishingUserNoticeResp;
+import cn.com.qmth.examcloud.examwork.api.response.GetNoticeRulePublishProgressListResp;
 import cn.com.qmth.examcloud.web.task.AbstractTask;
 import cn.com.qmth.examcloud.web.task.ScheduleJob;
 import cn.com.qmth.examcloud.web.task.TaskTracker;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
+
 /**
  * @author lideyin
  * 处理发布中的通知
@@ -14,19 +26,83 @@ import org.springframework.stereotype.Component;
 @Component("disposePublishingNoticeTask")
 public class DisposePublishingNoticeTask extends AbstractTask {
 
-	@Autowired
-	TaskTracker taskTracker;
-	
-	@Autowired
-	private NoticeCloudService noticeCloudService;
-	
-	@Override
-	public void run(ScheduleJob scheduleJob) throws Exception {
-		noticeCloudService.disposePublishingUserNotice();
-	}
-
-	@Override
-	public TaskTracker getTaskTracker() {
-		return taskTracker;
-	}
+    @Autowired
+    TaskTracker taskTracker;
+
+    @Autowired
+    private NoticeCloudService noticeCloudService;
+    private static final Log log = LogFactory.getLog(DisposePublishingNoticeTask.class);
+
+    @Override
+    public void run(ScheduleJob scheduleJob) throws Exception {
+        GetNoticeRulePublishProgressListResp progressResponse = noticeCloudService.getToBeDisposedNoticeRulePublishProgressList();
+        List<NoticeRulePublishProgressBean> progressList = progressResponse.getList();
+        if (progressList == null || progressList.isEmpty()) {
+            return;
+        }
+        Long startUserId = 0L;
+        for (NoticeRulePublishProgressBean progress : progressList) {
+            int loopTimes = 0;
+            Long lastMaxUserId = getLastMaxUserId(progress.getNoticeReceiverRuleType(), progress);
+            while (true) {
+                try {
+                    loopTimes++;
+                    if (lastMaxUserId != null) {
+                        startUserId = lastMaxUserId + 1;
+                    }
+                    DisposePublishingUserNoticeReq disposeReq = new DisposePublishingUserNoticeReq();
+                    disposeReq.setStartUserId(startUserId);
+                    disposeReq.setNoticeRulePublishProgress(progress);
+                    DisposePublishingUserNoticeResp disposeResp = noticeCloudService.disposePublishingUserNotice(disposeReq);
+                    Long nextUserId = disposeResp.getNextUserId();
+
+                    if (nextUserId.equals(startUserId)) {
+                        updateNoticeStatus(progress.getNoticeId(), NoticeStatus.PUBLISHED);
+                        break;
+                    } else {
+                        startUserId = nextUserId;
+                        //处理中的状态只需更新一次
+                        if (loopTimes == 1) {
+                            updateNoticeStatus(progress.getNoticeId(), NoticeStatus.PUBLISHED);
+                        }
+                    }
+                } catch (Exception e) {
+                    // 特殊处理:此处为了不影响自动服务的其它正常数据,所以吃掉了异常
+                    log.error("[DISPOSE-NOTICE]:处理用户通知任务出现异常,rootOrgId=" + progress.getRootOrgId() + ",noticeId="
+                            + progress.getNoticeId(), e);
+                }
+            }
+        }
+
+
+    }
+
+    @Override
+    public TaskTracker getTaskTracker() {
+        return taskTracker;
+    }
+
+    private void updateNoticeStatus(Long noticeId, NoticeStatus noticeStatus) {
+        UpdateNoticeStatusReq updateNoticeStatusReq = new UpdateNoticeStatusReq();
+        updateNoticeStatusReq.setNoticeId(noticeId);
+        updateNoticeStatusReq.setNoticeStatus(noticeStatus);
+        noticeCloudService.updateNoticeStatus(updateNoticeStatusReq);
+    }
+
+    /**
+     * 获取上次更新的最大用户id
+     *
+     * @param ruleType
+     * @param process
+     * @return
+     */
+    private Long getLastMaxUserId(NoticeReceiverRuleType ruleType,
+                                  NoticeRulePublishProgressBean process) {
+        if (ruleType == NoticeReceiverRuleType.ALL_STUDENTS_OF_ROOT_ORG
+                || ruleType == NoticeReceiverRuleType.STUDENTS_OF_EXAM) {
+            return process.getMaxStudentId();
+        } else {
+            return process.getMaxCommonUserId();
+        }
+    }
 }