Преглед изворни кода

完成教师端创建时段考试的任务定时与回调业务

zhaosongshan пре 7 месеци
родитељ
комит
4718b159a5

+ 5 - 2
snowy-plugin/snowy-plugin-exam/snowy-plugin-exam-func/src/main/java/vip/xiaonuo/exam/domain/TExam.java

@@ -82,6 +82,9 @@ public class TExam {
     @ApiModelProperty(value = "软删除", position = 12)
     private Integer deleted;
 
-    @ApiModelProperty(value = "任务ID", position = 13)
-    private String jobId;
+    @ApiModelProperty(value = "开始任务ID", position = 13)
+    private String startJobId;
+
+    @ApiModelProperty(value = "结束任务ID", position = 14)
+    private String endJobId;
 }

+ 35 - 2
snowy-plugin/snowy-plugin-exam/snowy-plugin-exam-func/src/main/java/vip/xiaonuo/exam/job/task/ExamJobTimerTaskRunner.java

@@ -1,8 +1,13 @@
 package vip.xiaonuo.exam.job.task;
 
+import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 import vip.xiaonuo.common.timer.CommonTimerTaskRunner;
+import vip.xiaonuo.dev.api.DevJobApi;
+import vip.xiaonuo.exam.domain.TExam;
+import vip.xiaonuo.exam.mapper.TExamMapper;
+import javax.annotation.Resource;
 
 /**
  * @PackageName:vip.xiaonuo.exam.job.task
@@ -15,6 +20,12 @@ import vip.xiaonuo.common.timer.CommonTimerTaskRunner;
 @Component
 public class ExamJobTimerTaskRunner implements CommonTimerTaskRunner {
 
+    @Resource
+    private DevJobApi devJobApi;
+
+    @Resource
+    private TExamMapper texamMapper;
+
     /**
      * 任务执行的具体内容
      *
@@ -22,7 +33,29 @@ public class ExamJobTimerTaskRunner implements CommonTimerTaskRunner {
      * @date 2022/8/15 16:09
      **/
     @Override
-    public void action(String extJson) {
-        log.info("extJson:{}",extJson);
+    public void action(String jobId) {
+        log.info("ExamJobTimerTaskRunner jobId:{} RUN...",jobId);
+        if(jobId != null && !jobId.isEmpty()){
+            JSONObject jb = devJobApi.queryEntity(jobId);
+            if(jb != null && jb.getIntValue("code") == 200){
+                String category = jb.getString("category");
+                JSONObject extJson = jb.getJSONObject("extJson");
+                log.info("ExamJobTimerTaskRunner jobId:{} category:{} extJson:{}",jobId,category,extJson);
+                TExam taskExam = texamMapper.selectById(extJson.getString("id"));
+
+                if(category.contains("start")){
+                    taskExam.setExamStatus(1);
+                    texamMapper.updateById(taskExam);
+                }
+                if(category.contains("end")){
+                    taskExam.setExamStatus(2);
+                    texamMapper.updateById(taskExam);
+                }
+            }else{
+                log.error("ExamJobTimerTaskRunner jobId:{} Query Error", jobId);
+            }
+        }else{
+            log.error("ExamJobTimerTaskRunner jobId IS Empty");
+        }
     }
 }

+ 50 - 41
snowy-plugin/snowy-plugin-exam/snowy-plugin-exam-func/src/main/java/vip/xiaonuo/exam/service/impl/TExamServiceImpl.java

@@ -18,6 +18,7 @@ import cn.hutool.core.collection.CollectionUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.oss.ServiceException;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -44,6 +45,8 @@ import vip.xiaonuo.exam.mapper.TExamMapper;
 import vip.xiaonuo.exam.service.ExamPaperService;
 import vip.xiaonuo.exam.service.TExamService;
 import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 import vip.xiaonuo.exam.utility.DateTimeUtil;
 
@@ -115,13 +118,29 @@ public class TExamServiceImpl extends ServiceImpl<TExamMapper, TExam> implements
             courseChapterPaperMapper.add(cp);
         }
         if(tExamAddParam.getStartTime() != null && tExamAddParam.getEndTime() != null){
-            this.addJob(tExam);
+            Date currentTime = new Date(System.currentTimeMillis());
+            // 开始时间与结束时间不能小于当前时间
+            if(currentTime.after(tExamAddParam.getStartTime()) || currentTime.after(tExamAddParam.getEndTime())){
+                throw new RuntimeException("开始时间与结束时间不能小于当前时间!!!");
+            }
+            this.addJob(tExam, "start");
+            this.addJob(tExam, "end");
         }
     }
 
     @Transactional(rollbackFor = Exception.class)
     @Override
     public void edit(TExamEditParam tExamEditParam) {
+        boolean updateJob = false;
+        if(tExamEditParam.getStartTime() !=null || tExamEditParam.getEndTime() !=null){
+            Date currentTime = new Date(System.currentTimeMillis());
+            // 开始时间与结束时间不能小于当前时间
+            if(currentTime.after(tExamEditParam.getStartTime()) || currentTime.after(tExamEditParam.getEndTime())){
+                throw new RuntimeException("开始时间与结束时间不能小于当前时间!!!");
+            }
+            updateJob = true;
+        }
+
         TExam tExam = this.queryEntity(tExamEditParam.getId().toString());
         if(tExamEditParam.getChapterId() != null && !tExamEditParam.getChapterId().isEmpty()){
             CourseChapterPaper cp = new CourseChapterPaper();
@@ -133,7 +152,7 @@ public class TExamServiceImpl extends ServiceImpl<TExamMapper, TExam> implements
             cp.setPaperId(tExamEditParam.getPaperId());
             courseChapterPaperMapper.add(cp);
         }
-        boolean updateJob = false;
+
         if(tExamEditParam.getPaperId() != null && !tExamEditParam.getPaperId().isEmpty()){
             ExamPaper examPaper = examPaperService.selectById(Integer.parseInt(tExamEditParam.getPaperId()));
             if(ExamPaperTypeEnum.TimeLimit.getCode() == examPaper.getPaperType()){
@@ -142,46 +161,25 @@ public class TExamServiceImpl extends ServiceImpl<TExamMapper, TExam> implements
                 updateJob = true;
             }
         }
-        if(tExamEditParam.getStartTime() !=null || tExamEditParam.getEndTime() !=null){
-            updateJob = true;
-        }
+
         if(updateJob){
             // 修改定时任务
-            JSONObject jobQuery = devJobApi.queryEntity(tExamEditParam.getJobId());
-            if(jobQuery != null && jobQuery.getInteger("code") == 200){
-                JSONObject jobEntity = jobQuery.getJSONObject("data");
-                if(jobEntity != null && jobEntity.getString("jobStatus") != null && jobEntity.getString("jobStatus").equals("STOPPED")){
-                    // 查的到任务已经结束了,删掉历史建新的
-                    List<String> jobIds = CollectionUtil.newArrayList(tExam.getJobId());
-                    JSONObject deleteJobResult = devJobApi.deleteJob(jobIds);
-                    if(deleteJobResult != null && deleteJobResult.getInteger("code") == 200){
-                        tExam = this.addJob(tExam);
-                    }else{
-                        log.error("任务修改失败,删除过期任务异常。{}",deleteJobResult);
-                        throw new CommonException("任务修改失败,删除过期任务异常。");
-                    }
+            if(tExam.getStartJobId() != null && tExam.getEndJobId() != null){
+                List<String> jobIds = new ArrayList<String>();
+                jobIds.add(tExam.getStartJobId());
+                jobIds.add(tExam.getEndJobId());
+                JSONObject deleteJobResult = devJobApi.deleteJob(jobIds);
+                if(deleteJobResult != null && deleteJobResult.getInteger("code") == 200){
+                    tExam = this.addJob(tExam, "start");
+                    tExam = this.addJob(tExam, "end");
                 }else{
-                    if(jobEntity == null || jobEntity.getString("jobStatus") == null){
-                        tExam = this.addJob(tExam);
-                    }else{
-                        // 查的到任务未结束,修改任务
-                        jobEntity.put("id", tExam.getJobId());
-                        jobEntity.put("name", tExam.getId()+"-"+tExam.getExamName()+"-"+tExam.getPaperId());
-                        String cronExpression = DateTimeUtil.generateCronExpression(tExam.getStartTime());
-                        jobEntity.put("cronExpression", cronExpression);
-                        jobEntity.put("sortCode", 99);
-                        jobEntity.put("extJson", JSONObject.toJSONString(tExam));
-                        JSONObject editJobResult = devJobApi.editJob(jobEntity);
-                        if(editJobResult != null && editJobResult.getInteger("code") == 200){
-                            tExam.setJobId(jobEntity.getString("id"));
-                        }else{
-                            throw new CommonException("任务修改失败");
-                        }
-                    }
+                    log.error("任务修改失败,删除过期任务异常。{}",deleteJobResult);
+                    throw new CommonException("任务修改失败,删除过期任务异常。");
                 }
             }else{
                 // 任务不存在,创建任务
-                tExam = this.addJob(tExam);
+                tExam = this.addJob(tExam, "start");
+                tExam = this.addJob(tExam, "end");
             }
         }
         BeanUtil.copyProperties(tExamEditParam, tExam);
@@ -210,18 +208,29 @@ public class TExamServiceImpl extends ServiceImpl<TExamMapper, TExam> implements
     }
 
 
-    public TExam addJob(TExam tExam){
+    public TExam addJob(TExam tExam, String startOnEnd){
         JSONObject jobParam = new JSONObject();
         jobParam.put("name", tExam.getId()+"-"+tExam.getExamName()+"-"+tExam.getPaperId());
-        jobParam.put("category", "exam");
+        jobParam.put("category", "exam-"+startOnEnd);
         jobParam.put("actionClass", "vip.xiaonuo.exam.job.task.ExamJobTimerTaskRunner");
-        String cronExpression = DateTimeUtil.generateCronExpression(tExam.getStartTime());
+        String cronExpression = "";
+        if("start".equals(startOnEnd)){
+            cronExpression = DateTimeUtil.generateCronExpression(tExam.getStartTime());
+        }
+        if("end".equals(startOnEnd)){
+            cronExpression = DateTimeUtil.generateCronExpression(tExam.getEndTime());
+        }
         jobParam.put("cronExpression", cronExpression);
         jobParam.put("sortCode", 99);
-        jobParam.put("extJson", JSONObject.toJSONString(tExam));
+        jobParam.put("extJson", JSONUtil.parseObj(tExam));
         JSONObject addJobResult = devJobApi.addJob(jobParam);
         if(addJobResult != null && addJobResult.getInteger("code") != null && addJobResult.getIntValue("code") == 200){
-            tExam.setJobId(addJobResult.getString("data"));
+            if("start".equals(startOnEnd)){
+                tExam.setStartJobId(addJobResult.getJSONObject("data").getString("id"));
+            }
+            if("end".equals(startOnEnd)){
+                tExam.setEndJobId(addJobResult.getJSONObject("data").getString("id"));
+            }
             this.save(tExam);
             return tExam;
         }else{