From a316090e811c2a3f2b4a73f669131b439343d5cc Mon Sep 17 00:00:00 2001 From: wuchunlei Date: Wed, 4 Sep 2024 17:15:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=80=83=E8=AF=95=E5=91=A8?= =?UTF-8?q?=E6=97=B6=E9=95=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/peanut/config/DelayQueueConfig.java | 27 +++++- .../common/controller/ClassController.java | 4 + .../controller/ClassExamController.java | 21 ++++ .../modules/common/entity/ClassEntity.java | 2 +- .../modules/common/entity/ClassExamUser.java | 6 ++ .../modules/common/entity/ClassModel.java | 2 + .../common/service/ClassExamService.java | 6 ++ .../service/impl/ClassEntityServiceImpl.java | 33 ++++++- .../service/impl/ClassExamServiceImpl.java | 97 ++++++++++++++++--- .../modules/mq/Consumer/CommonConsumer.java | 55 +++++++++++ 10 files changed, 239 insertions(+), 14 deletions(-) create mode 100644 src/main/java/com/peanut/modules/mq/Consumer/CommonConsumer.java diff --git a/src/main/java/com/peanut/config/DelayQueueConfig.java b/src/main/java/com/peanut/config/DelayQueueConfig.java index da645339..63f1de10 100644 --- a/src/main/java/com/peanut/config/DelayQueueConfig.java +++ b/src/main/java/com/peanut/config/DelayQueueConfig.java @@ -64,6 +64,11 @@ public class DelayQueueConfig { public static final String COURSE_EXCHANGE = "course_exchange"; public static final String COURSE_ROUTING_KEY = "course_routingKey"; + //通用队列 + public static final String COMMON_QUEUE = "common_queue"; + public static final String COMMON_EXCHANGE = "common_exchange"; + public static final String COMMON_ROUTING_KEY = "common_routingKey"; + /** * 快递队列 */ @@ -248,7 +253,6 @@ public class DelayQueueConfig { .noargs(); } - //课程过期延迟队列 @Bean public Queue courseQueue() { @@ -270,6 +274,27 @@ public class DelayQueueConfig { .noargs(); } + //通用延迟队列 + @Bean + public Queue commonQueue() { + return new Queue(COMMON_QUEUE, true, false, false); + } + @Bean + public CustomExchange commonExchange() { + Map args = new HashMap<>(); + //自定义交换机的类型 + args.put("x-delayed-type", "direct"); + return new CustomExchange(COMMON_EXCHANGE, "x-delayed-message", true, false,args); + } + @Bean + public Binding commonBinding() { + return BindingBuilder + .bind(commonQueue()) + .to(commonExchange()) + .with(COMMON_ROUTING_KEY) + .noargs(); + } + @Bean diff --git a/src/main/java/com/peanut/modules/common/controller/ClassController.java b/src/main/java/com/peanut/modules/common/controller/ClassController.java index c0a29ddb..4ebc9cc3 100644 --- a/src/main/java/com/peanut/modules/common/controller/ClassController.java +++ b/src/main/java/com/peanut/modules/common/controller/ClassController.java @@ -274,6 +274,10 @@ public class ClassController { //新增作业 @RequestMapping("/addClassTaskAndQuesReply") public R addClassTaskAndQuesReply(@RequestBody ClassTaskAndQuesReply classTaskAndQuesReply){ + ClassEntity classEntity = classEntityService.getById(classTaskAndQuesReply.getClassId()); + if ("3".equals(classEntity.getState())) { + return R.error("考试周禁止提交作业"); + } classEntityService.addClassTaskAndQuesReply(classTaskAndQuesReply); return R.ok(); } diff --git a/src/main/java/com/peanut/modules/common/controller/ClassExamController.java b/src/main/java/com/peanut/modules/common/controller/ClassExamController.java index 7567a939..67ea87ff 100644 --- a/src/main/java/com/peanut/modules/common/controller/ClassExamController.java +++ b/src/main/java/com/peanut/modules/common/controller/ClassExamController.java @@ -166,6 +166,13 @@ public class ClassExamController { return classExamService.generateExamPaper(params); } + //提交选项 + @RequestMapping("/submitOption") + public R submitOption(@RequestBody Map params){ + classExamService.submitOption(params); + return R.ok(); + } + //提交试卷 @RequestMapping("/submitExamPaper") public R submitExamPaper(@RequestBody Map params){ @@ -173,6 +180,13 @@ public class ClassExamController { return R.ok().put("examPaper",examPaper); } + //试卷列表 + @RequestMapping("/getExamPaperList") + public R getExamPaperList(@RequestBody Map params){ + Object examPaper = classExamService.getExamPaperList(params); + return R.ok().put("examPaper",examPaper); + } + //试卷详情 @RequestMapping("/getExamPaperInfo") public R getExamPaperInfo(@RequestBody Map params){ @@ -226,6 +240,13 @@ public class ClassExamController { return R.ok(); } + //删除课程下全部题目 + @RequestMapping("/delSubjectByCourseId") + public R delSubjectByCourseId(@RequestBody Map params){ + classExamService.delSubjectByCourseId(params); + return R.ok(); + } + //删除选项 @RequestMapping("/delClassExamOption") public R delClassExamOption(@RequestBody Map params){ diff --git a/src/main/java/com/peanut/modules/common/entity/ClassEntity.java b/src/main/java/com/peanut/modules/common/entity/ClassEntity.java index eef18dce..2911e404 100644 --- a/src/main/java/com/peanut/modules/common/entity/ClassEntity.java +++ b/src/main/java/com/peanut/modules/common/entity/ClassEntity.java @@ -20,7 +20,7 @@ public class ClassEntity { private String title; - //小班状态 0待开班1进行中2完成 + //小班状态 0待开班1进行中2完成3考试中 private String state; private String icon; diff --git a/src/main/java/com/peanut/modules/common/entity/ClassExamUser.java b/src/main/java/com/peanut/modules/common/entity/ClassExamUser.java index 78981cd3..1c27f9f5 100644 --- a/src/main/java/com/peanut/modules/common/entity/ClassExamUser.java +++ b/src/main/java/com/peanut/modules/common/entity/ClassExamUser.java @@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; +import java.util.Date; + @Data @TableName("class_exam_user") public class ClassExamUser { @@ -24,6 +26,10 @@ public class ClassExamUser { private String answer; + private Date startTime; + + private Date endTime; + @TableLogic private Integer delFlag; } diff --git a/src/main/java/com/peanut/modules/common/entity/ClassModel.java b/src/main/java/com/peanut/modules/common/entity/ClassModel.java index 2b9d7664..f79bda07 100644 --- a/src/main/java/com/peanut/modules/common/entity/ClassModel.java +++ b/src/main/java/com/peanut/modules/common/entity/ClassModel.java @@ -25,6 +25,8 @@ public class ClassModel { private Integer days;//教学天数 + private Integer examDays;//考试周时长 + private Integer isQuestion; private Integer questionScore; private Integer isTask; diff --git a/src/main/java/com/peanut/modules/common/service/ClassExamService.java b/src/main/java/com/peanut/modules/common/service/ClassExamService.java index d05f30fd..5260bd60 100644 --- a/src/main/java/com/peanut/modules/common/service/ClassExamService.java +++ b/src/main/java/com/peanut/modules/common/service/ClassExamService.java @@ -22,14 +22,20 @@ public interface ClassExamService extends IService { void delClassExamSubject(Map params); + void delSubjectByCourseId(Map params); + void delClassExamOption(Map params); Page getClassExamSubjectList(Map params); R generateExamPaper(Map params); + void submitOption(Map params); + Object submitExamPaper(Map params); + Object getExamPaperList(Map params); + Object getExamPaperInfo(Map params); } diff --git a/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java index 6788c36d..70316b4a 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/ClassEntityServiceImpl.java @@ -1,5 +1,6 @@ package com.peanut.modules.common.service.impl; +import cn.hutool.core.date.DateUtil; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -8,6 +9,7 @@ import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.peanut.common.utils.DateUtils; import com.peanut.common.utils.R; import com.peanut.common.utils.ShiroUtils; +import com.peanut.config.DelayQueueConfig; import com.peanut.modules.common.dao.*; import com.peanut.modules.common.entity.*; import com.peanut.modules.common.service.ClassEntityService; @@ -15,12 +17,15 @@ import com.peanut.modules.sys.dao.SysUserDao; import com.peanut.modules.sys.entity.SysUserEntity; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; +import org.springframework.amqp.core.MessagePostProcessor; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.math.RoundingMode; +import java.time.LocalDate; import java.util.*; @Slf4j @@ -49,6 +54,8 @@ public class ClassEntityServiceImpl extends ServiceImpl params) { @@ -58,6 +65,9 @@ public class ClassEntityServiceImpl extends ServiceImpl classModelPage = classModelDao.selectPage(new Page<>(page, limit), wrapper); for (ClassModel classModel:classModelPage.getRecords()){ @@ -223,6 +233,7 @@ public class ClassEntityServiceImpl extends ServiceImpl params) { String state = params.get("state").toString(); ClassEntity classEntity = this.getBaseMapper().selectById(params.get("classId").toString()); @@ -296,11 +307,24 @@ public class ClassEntityServiceImpl extends ServiceImpl { + message.getMessageProperties().setDelay((int)date); + return message; + }; + } } diff --git a/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java index 9be60687..c057a895 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/ClassExamServiceImpl.java @@ -5,13 +5,17 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.yulichang.wrapper.MPJLambdaWrapper; +import com.peanut.common.utils.DateUtils; import com.peanut.common.utils.R; import com.peanut.common.utils.ShiroUtils; +import com.peanut.config.DelayQueueConfig; import com.peanut.modules.common.dao.*; import com.peanut.modules.common.entity.*; import com.peanut.modules.common.service.ClassExamService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; +import org.springframework.amqp.core.MessagePostProcessor; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @@ -30,6 +34,10 @@ public class ClassExamServiceImpl extends ServiceImpl i private ClassExamUserDao classExamUserDao; @Autowired private CourseDao courseDao; + @Autowired + private ClassEntityDao classEntityDao; + @Autowired + private RabbitTemplate rabbitTemplate; @Override public void addClassExamSubject(ClassExamSubject classExamSubject) { @@ -78,6 +86,30 @@ public class ClassExamServiceImpl extends ServiceImpl i @Override public void delClassExamSubject(Map params) { classExamSubjectDao.deleteById(params.get("subjectId").toString()); + classExamOptionDao.delete(new LambdaQueryWrapper() + .eq(ClassExamOption::getSubjectId,params.get("subjectId").toString())); + } + + @Override + public void delSubjectByCourseId(Map params) { + List list = classExamSubjectDao.selectList(new LambdaQueryWrapper() + .eq(ClassExamSubject::getCourseId,params.get("courseId").toString()).select(ClassExamSubject::getId)); + if (list.size() > 0) { + List sids = new ArrayList<>(); + List oids = new ArrayList<>(); + for (ClassExamSubject subject:list) { + sids.add(subject.getId()); + List os = classExamOptionDao.selectList(new LambdaQueryWrapper() + .eq(ClassExamOption::getSubjectId,subject.getId()).select(ClassExamOption::getId)); + if (os.size() > 0) { + for (ClassExamOption option:os) { + oids.add(option.getId()); + } + } + } + classExamSubjectDao.deleteBatchIds(sids); + classExamOptionDao.deleteBatchIds(oids); + } } @Override @@ -108,16 +140,20 @@ public class ClassExamServiceImpl extends ServiceImpl i @Override public R generateExamPaper(Map params) { + ClassEntity classEntity = classEntityDao.selectById(params.get("classId").toString()); MPJLambdaWrapper courseWrapper = new MPJLambdaWrapper<>(); courseWrapper.leftJoin(ClassModel.class,ClassModel::getId,ClassCourse::getModelId); courseWrapper.leftJoin(ClassEntity.class,ClassEntity::getModelId,ClassModel::getId); - courseWrapper.eq(ClassEntity::getId,params.get("classId")); + courseWrapper.eq(ClassEntity::getId,classEntity.getId()); courseWrapper.selectAll(ClassCourse.class); List courseList = classCourseDao.selectList(courseWrapper); List> resultList = new ArrayList<>(); + List answerList = new ArrayList<>(); List> sList = new ArrayList<>(); List> mList = new ArrayList<>(); if (courseList.size() > 0) { +// int stotal = 60;//单选题总数 +// int mtotal = 40;//多选题总数 int stotal = 5;//单选题总数 int mtotal = 5;//多选题总数 int snum = (int)Math.floor(stotal/courseList.size()); @@ -165,6 +201,7 @@ public class ClassExamServiceImpl extends ServiceImpl i //拼装选项 if (resultList.size()>0){ for (Map subject:resultList) { + answerList.add("");//预留答案位置 List> options = classExamOptionDao.selectMaps(new MPJLambdaWrapper() .selectAs(ClassExamOption::getId,"id") .selectAs(ClassExamOption::getContent,"content") @@ -175,28 +212,53 @@ public class ClassExamServiceImpl extends ServiceImpl i } } ClassExamUser classExamUser = new ClassExamUser(); - classExamUser.setClassId(Integer.parseInt(params.get("classId").toString())); + classExamUser.setClassId(classEntity.getId()); classExamUser.setSubject(JSONUtil.toJsonStr(resultList)); + classExamUser.setAnswer(JSONUtil.toJsonStr(answerList)); classExamUser.setUserId(ShiroUtils.getUId()); + Date startTime = new Date(); + classExamUser.setStartTime(startTime); classExamUserDao.insert(classExamUser); - return R.ok().put("examPaper",resultList).put("id",classExamUser.getId()); + //在考试结束时检查是否提交,未完成者自动提交 + //时间为2小时,再队列执行中也有设置,修改时要同步修改 + rabbitTemplate.convertAndSend( + DelayQueueConfig.COMMON_EXCHANGE, + DelayQueueConfig.COMMON_ROUTING_KEY, + "examSubmit"+","+classExamUser.getId(), + messagePostProcessor(DateUtils.addDateHours(startTime,2).getTime()) + ); + return R.ok().put("examPaper",resultList).put("id",classExamUser.getId()).put("startTime",classExamUser.getStartTime()); } + @Override + public void submitOption(Map params) { + ClassExamUser classExamUser = classExamUserDao.selectById(params.get("id").toString()); + List answerList = JSONUtil.parseArray(classExamUser.getAnswer()); + answerList.set((Integer)params.get("no")-1, params.get("answer").toString()); + classExamUser.setAnswer(JSONUtil.toJsonStr(answerList)); + classExamUserDao.updateById(classExamUser); + } + @Override public Object submitExamPaper(Map params) { ClassExamUser classExamUser = classExamUserDao.selectById(params.get("id").toString()); - List answerList = (List)params.get("answer"); - List resAnswerList = new ArrayList<>(); List subjectList = JSONUtil.parseArray(classExamUser.getSubject()); + List answerList = JSONUtil.parseArray(classExamUser.getAnswer()); + List resAnswerList = new ArrayList<>(); int score = 0; for (int i=0;i i boolean flag = true; for (ClassExamOption o:options){ if (o.getRightWrong()==1){ - if (answerList.get(i).contains(o.getId().toString())){ + if (answerList.get(i).toString().contains(o.getId().toString())){ }else { flag = false; } @@ -222,12 +284,13 @@ public class ClassExamServiceImpl extends ServiceImpl i classExamUser.setAnswer(JSONUtil.toJsonStr(resAnswerList)); classExamUser.setScore(score); classExamUser.setScoreSuccess(1); + classExamUser.setEndTime(new Date()); classExamUserDao.updateById(classExamUser); return classExamUser; } @Override - public Object getExamPaperInfo(Map params) { + public Object getExamPaperList(Map params) { List classExamUserList = classExamUserDao.selectList(new LambdaQueryWrapper() .eq(ClassExamUser::getClassId,params.get("classId")) .eq(ClassExamUser::getUserId,params.get("userId")) @@ -235,4 +298,16 @@ public class ClassExamServiceImpl extends ServiceImpl i return classExamUserList; } + @Override + public Object getExamPaperInfo(Map params) { + ClassExamUser classExamUser = classExamUserDao.selectById(params.get("id").toString()); + return classExamUser; + } + + private MessagePostProcessor messagePostProcessor(long date) { + return message -> { + message.getMessageProperties().setDelay((int)date); + return message; + }; + } } diff --git a/src/main/java/com/peanut/modules/mq/Consumer/CommonConsumer.java b/src/main/java/com/peanut/modules/mq/Consumer/CommonConsumer.java new file mode 100644 index 00000000..f82197cd --- /dev/null +++ b/src/main/java/com/peanut/modules/mq/Consumer/CommonConsumer.java @@ -0,0 +1,55 @@ +package com.peanut.modules.mq.Consumer; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.peanut.common.utils.DateUtils; +import com.peanut.config.DelayQueueConfig; +import com.peanut.modules.common.dao.ClassEntityDao; +import com.peanut.modules.common.dao.ClassExamUserDao; +import com.peanut.modules.common.entity.ClassEntity; +import com.peanut.modules.common.entity.ClassExamUser; +import com.peanut.modules.common.service.ClassExamService; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +//通用延迟队列 +@Component +public class CommonConsumer { + + @Autowired + private ClassEntityDao classEntityDao; + @Autowired + private ClassExamUserDao classExamUserDao; + @Autowired + private ClassExamService classexamService; + + @RabbitListener(queues = DelayQueueConfig.USERVIP_QUEUE) + public void commonConsumer(String typeAndParam) { + //参数为 业务模块 + , + 参数 + String[] typeAndParams = typeAndParam.split(","); + //考试周天数,根据设置的天数将班级状态从进行中设置成考试中 + if ("examDays".equals(typeAndParams[0])){ + ClassEntity classEntity = classEntityDao.selectById(typeAndParams[1]); + if (classEntity!=null&&!"3".equals(classEntity.getState())){ + classEntity.setState("3"); + classEntityDao.updateById(classEntity); + } + } + //在考试结束时检查是否提交,未完成者自动提交 + if ("examSubmit".equals(typeAndParams[0])){ + ClassExamUser classExamUser = classExamUserDao.selectById(typeAndParams[1]); + if (classExamUser!=null&&classExamUser.getScoreSuccess()==0){ + Map map = new HashMap<>(); + map.put("id",classExamUser.getId()); + classexamService.submitExamPaper(map); + } + } + + + } + +}