From 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期三, 14 五月 2025 15:50:57 +0800
Subject: [PATCH] bug:学员有状态不能修改问题

---
 src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java |  138 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 103 insertions(+), 35 deletions(-)

diff --git a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
index f434bc1..e0a83e9 100644
--- a/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
+++ b/src/main/java/com/mindskip/xzs/service/impl/ExamPaperServiceImpl.java
@@ -9,8 +9,10 @@
 import com.mindskip.xzs.domain.exam.ExamPaperQuestionItemObject;
 import com.mindskip.xzs.domain.exam.ExamPaperTitleItemObject;
 import com.mindskip.xzs.domain.other.KeyValue;
+import com.mindskip.xzs.domain.vo.ExamTemplatesVO;
 import com.mindskip.xzs.domain.vo.PaperExcelVO;
-import com.mindskip.xzs.queue.ExamPaperTimeTask;
+import com.mindskip.xzs.domain.vo.UserVO;
+import com.mindskip.xzs.repository.ExamPaperAnswerMapper;
 import com.mindskip.xzs.repository.ExamPaperMapper;
 import com.mindskip.xzs.repository.QuestionMapper;
 import com.mindskip.xzs.repository.UserDepartmentMapper;
@@ -20,22 +22,20 @@
 import com.mindskip.xzs.utility.ExamUtil;
 import com.mindskip.xzs.utility.JsonUtil;
 import com.mindskip.xzs.utility.ModelMapperSingle;
-import com.mindskip.xzs.utility.minio.DateUtils;
 import com.mindskip.xzs.viewmodel.admin.exam.*;
 import com.mindskip.xzs.viewmodel.admin.question.ExamQuestionVO;
 import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM;
 import com.mindskip.xzs.viewmodel.student.dashboard.PaperFilter;
 import com.mindskip.xzs.viewmodel.student.dashboard.PaperInfo;
 import com.mindskip.xzs.viewmodel.student.exam.ExamPaperPageVM;
+import org.apache.commons.lang3.ObjectUtils;
 import org.modelmapper.ModelMapper;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import javax.annotation.PostConstruct;
 import java.util.*;
-import java.util.concurrent.DelayQueue;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
@@ -44,6 +44,7 @@
 
     protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();
     private final ExamPaperMapper examPaperMapper;
+    private final ExamPaperAnswerMapper examPaperAnswerMapper;
     private final QuestionMapper questionMapper;
     private final TextContentService textContentService;
     private final QuestionService questionService;
@@ -55,29 +56,14 @@
     private final UserService userService;
     private final UserDepartmentMapper userDepartmentMapper;
     private final DepartmentService departmentService;
-    private final DelayQueue<ExamPaperTimeTask> QUEUE = new DelayQueue<>();
 
 
-    /**
-     * 灏嗚繘琛屼腑鐨勫畾鏃惰瘯鍗峰姞鍏ュ埌闃熷垪涓紝骞跺惎鍔ㄤ竴涓嚎绋嬫潵杞闃熷垪
-     */
-    @PostConstruct
-    public void pollQueue() {
-        examPaperMapper.selectTimeTaskPaper(DateUtils.getNowDate()).forEach(this::addTimeTask);
-        new Thread(() -> {
-            try {
-                ExamPaperTimeTask task = QUEUE.take();
-                saveMissExamUser(task.getExamPaperId());
-            } catch (InterruptedException e) {
-                Thread.currentThread().interrupt();
-            }
-        }).start();
-    }
 
     @Autowired
-    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService, ExamPaperUserService examPaperUserService, UserService userService, UserDepartmentMapper userDepartmentMapper, DepartmentService departmentService) {
+    public ExamPaperServiceImpl(ExamPaperMapper examPaperMapper, ExamPaperAnswerMapper examPaperAnswerMapper, QuestionMapper questionMapper, TextContentService textContentService, QuestionService questionService, SubjectService subjectService, ExamPaperDepartmentService examPaperDepartmentService, ExamPaperSubjectService examPaperSubjectService, QuestionSubjectService questionSubjectService, ExamPaperUserService examPaperUserService, UserService userService, UserDepartmentMapper userDepartmentMapper, DepartmentService departmentService) {
         super(examPaperMapper);
         this.examPaperMapper = examPaperMapper;
+        this.examPaperAnswerMapper = examPaperAnswerMapper;
         this.questionMapper = questionMapper;
         this.textContentService = textContentService;
         this.questionService = questionService;
@@ -135,6 +121,7 @@
         String frameTextContentStr = JsonUtil.toJsonStr(frameTextContentList);
 
         ExamPaper examPaper;
+        Integer[] userIds = examPaperEditRequestVM.getUserIds();
         if (actionEnum == ActionEnum.ADD) {
             examPaper = modelMapper.map(examPaperEditRequestVM, ExamPaper.class);
             TextContent frameTextContent = new TextContent(frameTextContentStr, now);
@@ -152,7 +139,10 @@
             TextContent frameTextContent = textContentService.selectById(examPaper.getFrameTextContentId());
             frameTextContent.setContent(frameTextContentStr);
             textContentService.updateByIdFilter(frameTextContent);
+            examPaperEditRequestVM.setScore(null);
+            examPaperEditRequestVM.setUserIds(null);
             modelMapper.map(examPaperEditRequestVM, examPaper);
+            examPaperEditRequestVM.setUserIds(userIds);
             examPaperFromVM(examPaperEditRequestVM, examPaper, titleItemsVM);
             examPaperMapper.updateByPrimaryKeySelective(examPaper);
             //鎵归噺淇敼
@@ -168,6 +158,9 @@
     @Override
     public ExamPaperEditRequestVO examPaperToVM(Integer id) {
         ExamPaper examPaper = examPaperMapper.selectByPrimaryKey(id);
+        if (Objects.isNull(examPaper)) {
+            throw new RuntimeException("璇ヨ�冭瘯宸茶鍒犻櫎锛屾棤娉曟煡鐪�");
+        }
         ExamPaperEditRequestVO vm = modelMapper.map(examPaper, ExamPaperEditRequestVO.class);
         vm.setLevel(examPaper.getGradeLevel());
         vm.setMenuIds(examPaper.getUserIds());
@@ -177,7 +170,11 @@
                 .flatMap(t -> t.getQuestionItems().stream()
                         .map(q -> q.getId()))
                 .collect(Collectors.toList());
+        if (ObjectUtils.isEmpty(questionIds)) {
+            throw new RuntimeException("璇ヨ瘯鍗锋病鏈夐鐩�");
+        }
         List<Question> questions = questionMapper.selectByIds(questionIds);
+
         //鍗曢�夋暟閲�
         Integer singleChoice = questions.stream().filter(e -> e.getQuestionType() == 1).collect(Collectors.toList()).size();
         //澶氶�夋暟閲�
@@ -193,6 +190,7 @@
             List<ExamQuestionVO> questionItemsVM = t.getQuestionItems().stream().map(i -> {
                         Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
                         ExamQuestionVO questionEditRequestVM = questionService.getQuestionEditRequestVM(question);
+
                         questionEditRequestVM.setTitle("(" + QuestionTypeEnum.fromCode(questionEditRequestVM.getQuestionType()).getName() + ") " + questionEditRequestVM.getTitle());
                         questionEditRequestVM.setItemOrder(generateRandomNumber(questionEditRequestVM.getQuestionType() == 1 ? 0 : ((questionEditRequestVM.getQuestionType() == 2 ? singleChoice : multipleChoice + singleChoice)),
                                 questionEditRequestVM.getQuestionType() == 1 ? singleChoice : ((questionEditRequestVM.getQuestionType() == 2 ? multipleChoice + singleChoice : trueFalse + multipleChoice + singleChoice)),
@@ -218,12 +216,18 @@
         vm.setDepartmentIds(examPaperDepartmentService.getByExamPaperId(examPaper.getId())
                 .stream().map(ExamPaperDepartment::getDepartmentId).toArray(Integer[]::new));
         List<ExamPaperUser> examPaperUsers = examPaperUserService.getByExamPaperId(examPaper.getId());
-        Integer[][] userIds = new Integer[examPaperUsers.size()][2];
+        List<Integer> userIds = new ArrayList();
+        List<String> userNames = new ArrayList();
         for (int i = 0; i < examPaperUsers.size(); i++) {
-            Integer[] userId = {userService.getUserById(examPaperUsers.get(i).getUserId()).getUserLevel(), examPaperUsers.get(i).getUserId()};
-            userIds[i] = userId;
+            User user = userService.getUserById(examPaperUsers.get(i).getUserId());
+            if (ObjectUtils.isNotEmpty(user)) {
+                Integer userId = examPaperUsers.get(i).getUserId();
+                userIds.add(userId);
+                userNames.add(user.getRealName());
+            }
         }
-        vm.setUserId(userIds);
+        vm.setUserIds(userIds);
+        vm.setUserNames(userNames);
         return vm;
     }
 
@@ -234,8 +238,8 @@
 
 
     @Override
-    public Integer selectAllCount() {
-        return examPaperMapper.selectAllCount();
+    public Integer selectAllCount(List<Integer> deptIds) {
+        return examPaperMapper.selectAllCount(deptIds);
     }
 
     @Override
@@ -270,10 +274,36 @@
             if(userDepartments.size() != 0){
                 Department byId = departmentService.getById(userDepartments.get(0).getDepartmentId());
                 e.setDepartmentName(byId.getName());
+                e.setDeptId(byId.getId());
             }
             return e;
         }).collect(Collectors.toList());
         return paperExcel;
+    }
+
+    @Override
+    public List<PaperExcelVO> getRandomPaperExcelById(Integer id) {
+        List<PaperExcelVO> paperExcel = examPaperMapper.getRandomPaperExcelById(id);
+        // 濡傛灉鏌愯�冪敓瀛樺湪澶氫釜閮ㄩ棬锛屾瘡涓儴闂ㄩ兘鍔犱竴閬嶆暟鎹�
+        List<PaperExcelVO> needAdd = new ArrayList<>();
+        List<PaperExcelVO> needRemove = new ArrayList<>();
+        paperExcel = paperExcel.stream().map(e->{
+            e.setPaperScore(ExamUtil.scoreToVM(Integer.parseInt(e.getPaperScore())));
+            e.setUserScore(ExamUtil.scoreToVM(Integer.parseInt(e.getUserScore())));
+            List<Department> userDepartments = userDepartmentMapper.selectDeptByUserId(Integer.parseInt(e.getUserId()));
+            for (Department dept : userDepartments) {
+                PaperExcelVO vo = new PaperExcelVO();
+                BeanUtils.copyProperties(e, vo);
+                vo.setDepartmentName(dept.getName());
+                vo.setDeptId(dept.getId());
+                needAdd.add(vo);
+                needRemove.add(e);
+            }
+            return e;
+        }).collect(Collectors.toList());
+        paperExcel.removeAll(needRemove);
+        paperExcel.addAll(needAdd);
+        return paperExcel.stream().filter(e -> Objects.nonNull(e.getDeptId())).collect(Collectors.toList());
     }
 
     private void examPaperFromVM(ExamPaperEditRequestVM examPaperEditRequestVM, ExamPaper examPaper, List<ExamPaperTitleItemVM> titleItemsVM) {
@@ -528,9 +558,10 @@
         Integer order = 0;
         for (QuestionTypeVM questionTypeVM : questionTypeVMList) {
 
-            List<Integer> questions = questionSubjectService.getSubject(questionTypeVM.getSubjectId())
+            List<Integer> questions = questionSubjectService.getSubjectBySubjectIds(examPaperEditRequestVM.getSubjectId())
                     .stream().map(QuestionSubject::getQuestionId).collect(Collectors.toList());
             List<Question> list = questionService.selectByIds(questions);
+            // List<Question> list = questionService.getAll();
 
             Map<Integer, Integer> multiple = new HashMap<>();
             //澶氶��
@@ -617,15 +648,52 @@
     }
 
     @Override
-    public void addTimeTask(ExamPaper examPaper) {
-        ExamPaperTimeTask examPaperTimeTask = new ExamPaperTimeTask();
-        examPaperTimeTask.setExamPaperId(examPaper.getId());
-        examPaperTimeTask.setExpiry(examPaper.getLimitEndTime().getTime());
-        QUEUE.add(examPaperTimeTask);
+    @Transactional
+    public void missExam(ExamPaperEditRequestVM model) {
+        // 淇敼鍘熸潵鐨勮瘯鍗锋椂闂�
+        if (ObjectUtils.isNotEmpty(model.getLimitDateTime())) {
+            ExamPaper examPaper = new ExamPaper();
+            examPaper.setId(model.getExamPaperId());
+            examPaper.setLimitStartTime(DateTimeUtil.parse(model.getLimitDateTime().get(0), DateTimeUtil.STANDER_FORMAT));
+            examPaper.setLimitEndTime(DateTimeUtil.parse(model.getLimitDateTime().get(1), DateTimeUtil.STANDER_FORMAT));
+            examPaperMapper.updateByPrimaryKeySelective(examPaper);
+        }
+        // 鏍规嵁鑰冭瘯id灏嗛�夋嫨鐨勮ˉ鑰冭�冪敓鐨勮�冭瘯鎴愮哗璁剧疆涓烘棤鏁�
+        examPaperAnswerMapper.setMissExam(model);
+
+        // 琛ヨ�冩椂灏嗘病鏈夐�夋嫨鐨勫苟涓旀病鏈夊弬鍔犺繃鑰冭瘯鐨勮�冪敓澧炲姞涓�鏉¢浂鍒嗚�冭瘯璁板綍锛屽惁鍒欐病鏈夊弬鍔犺繃鑰冭瘯鐨勫嵆浣夸笉琚�夋嫨涔熷彲浠ュ弬鍔犺ˉ鑰�
+        examPaperAnswerMapper.insertDefault(model);
     }
 
     @Override
-    public void saveMissExamUser(long examPaperId) {
-        examPaperMapper.saveMissExamUser(examPaperId);
+    @Transactional
+    public void missExamByTemplateId(ExamTemplatesVO model) {
+        List<Integer> id = examPaperMapper.getExamPaperByTemplateId(model);
+        model.setPaperIds(id);
+        // 鏍规嵁鑰冭瘯id灏嗛�夋嫨鐨勮ˉ鑰冭�冪敓鐨勮�冭瘯鎴愮哗璁剧疆涓烘棤鏁�
+        examPaperAnswerMapper.setMissExamByTemplate(model);
+    }
+
+    @Override
+    public List<ExamPaper> list(List<Integer> deptIds) {
+        List<ExamPaper> list = examPaperMapper.list(deptIds);
+        List<ExamPaper> template = examPaperMapper.template(deptIds);
+        list.addAll(template);
+        return list.stream().sorted(Comparator.comparing(ExamPaper::getCreateTime).reversed()).collect(Collectors.toList());
+    }
+
+    @Override
+    public PageInfo<UserVO> selectStudent(UserVO userVO, List<Integer> adminDeptIds) {
+        userVO.setDeptIds(ObjectUtils.isNotEmpty(userVO.getDeptIds()) ? userVO.getDeptIds() : adminDeptIds);
+        return PageHelper.startPage(userVO.getPageIndex(), userVO.getPageSize()).doSelectPageInfo(() ->
+                examPaperMapper.selectStudent(userVO));
+    }
+
+    @Override
+    public List<ExamPaper> queryCondition() {
+        List<ExamPaper> examPaperList = examPaperMapper.getExamPaper();
+        List<ExamPaper> templateList = examPaperMapper.getTemplate();
+        examPaperList.addAll(templateList);
+        return examPaperList.stream().sorted(Comparator.comparing(ExamPaper::getCreateTime).reversed()).collect(Collectors.toList());
     }
 }

--
Gitblit v1.8.0