|
|
@@ -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{
|