From f0f00e9ba8a755e4317e029d73b69a92ad9f9df1 Mon Sep 17 00:00:00 2001
From: kongzy <kongzy>
Date: 星期六, 14 九月 2024 17:02:41 +0800
Subject: [PATCH] update

---
 exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExExamPaperServiceImpl.java |  107 ++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 89 insertions(+), 18 deletions(-)

diff --git a/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExExamPaperServiceImpl.java b/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExExamPaperServiceImpl.java
index 5bd7e0d..0b7b717 100644
--- a/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExExamPaperServiceImpl.java
+++ b/exam-system/src/main/java/com/gkhy/exam/system/service/impl/ExExamPaperServiceImpl.java
@@ -14,20 +14,15 @@
 import com.gkhy.exam.system.domain.ExExamPaper;
 import com.gkhy.exam.system.domain.ExPaperQuestion;
 import com.gkhy.exam.system.domain.ExQuestion;
-import com.gkhy.exam.system.mapper.ExExamPaperMapper;
-import com.gkhy.exam.system.mapper.ExPaperQuestionMapper;
-import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
-import com.gkhy.exam.system.mapper.ExQuestionMapper;
+import com.gkhy.exam.system.domain.ExQuestionBank;
+import com.gkhy.exam.system.mapper.*;
 import com.gkhy.exam.system.service.ExExamPaperService;
 import com.gkhy.exam.system.utils.SequenceUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.Random;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -47,6 +42,8 @@
     private ExPaperQuestionMapper paperQuestionMapper;
     @Autowired
     private ExQuestionMapper questionMapper;
+    @Autowired
+    private ExQuestionBankMapper questionBankMapper;
 
     @Autowired
     private SequenceUtils sequenceUtils;
@@ -65,6 +62,27 @@
     @Override
     public ExExamPaper selectExamPaperById(Long paperId) {
         ExExamPaper examPaper= baseMapper.selectExamPaperById(paperId);
+        //获取题库名称
+        List<Long> bankIds=new ArrayList<>();
+        bankIds.add(examPaper.getSingleBankId());
+        bankIds.add(examPaper.getMultiBankId());
+        bankIds.add(examPaper.getJudgeBankId());
+        bankIds.add(examPaper.getEasyBankId());
+        List<ExQuestionBank> questionBanks=questionBankMapper.selectQuestionBankByIds(bankIds);
+        questionBanks.forEach(item -> {
+            if(Objects.equals(item.getId(), examPaper.getSingleBankId())){
+                examPaper.setSingleBankName(item.getName());
+            }
+            if(Objects.equals(item.getId(), examPaper.getMultiBankId())){
+                examPaper.setMultiBankName(item.getName());
+            }
+            if(Objects.equals(item.getId(), examPaper.getJudgeBankId())){
+                examPaper.setJudgeBankName(item.getName());
+            }
+            if(Objects.equals(item.getId(), examPaper.getEasyBankId())){
+                examPaper.setEasyBankName(item.getName());
+            }
+        });
         SysUser currentUser= SecurityUtils.getLoginUser().getUser();
         if(currentUser.getUserType().equals(UserTypeEnum.SYSTEM_USER.getCode())){
             return examPaper;
@@ -79,6 +97,7 @@
     @Override
     @Transactional(rollbackFor = RuntimeException.class)
     public int insertExamPaper(ExExamPaper examPaper) {
+        validatedData(examPaper);
         checkUserAllowed(examPaper);
         if(!checkNameUnique(examPaper)){
             throw new ApiException("试卷名称已存在");
@@ -87,18 +106,27 @@
         examPaper.setCode(sequenceUtils.getNextSequence(CodeTypeEnum.EXAM_PAPER.getCode()));
         examPaper.setCreateBy(SecurityUtils.getUsername());
         if(examPaper.getLimitTime()>0){
-            examPaper.setLimit(1);
+            examPaper.setLimited(1);
         }else{
-            examPaper.setLimit(0);
+            examPaper.setLimited(0);
         }
         int row=baseMapper.insert(examPaper);
         if(row<1){
             throw new ApiException("新增试卷失败");
         }
         //分配试题
-        assignSingleQuestion(examPaper);
-        assignMultiQuestion(examPaper);
-        assignJudgeQuestion(examPaper);
+        if(Optional.ofNullable(examPaper.getSingleNum()).orElse(0)>0) {
+            assignSingleQuestion(examPaper);
+        }
+        if(Optional.ofNullable(examPaper.getMultiNum()).orElse(0)>0) {
+            assignMultiQuestion(examPaper);
+        }
+        if(Optional.ofNullable(examPaper.getJudgeNum()).orElse(0)>0) {
+            assignJudgeQuestion(examPaper);
+        }
+        if(Optional.ofNullable(examPaper.getEasyNum()).orElse(0)>0) {
+            assignEasyQuestion(examPaper);
+        }
         return row;
     }
 
@@ -110,8 +138,37 @@
         if(currentUser.getUserType().equals(UserTypeEnum.STUDENT.getCode())){
             throw new ApiException("没有权限操作");
         }
-        if(!currentUser.getCompanyId().equals(examPaper.getCompanyId())){
+        if(examPaper.getCompanyId()!=null&&!currentUser.getCompanyId().equals(examPaper.getCompanyId())){
             throw new ApiException("没有权限操作其他企业试卷");
+        }
+    }
+
+    public void validatedData(ExExamPaper examPaper){
+        if(Optional.ofNullable(examPaper.getSingleNum()).orElse(0)<=0
+        &&Optional.ofNullable(examPaper.getMultiNum()).orElse(0)<=0
+        &&Optional.ofNullable(examPaper.getJudgeNum()).orElse(0)<=0
+        &&Optional.ofNullable(examPaper.getEasyNum()).orElse(0)<=0){
+            throw new ApiException("试卷题目数量不能为空");
+        }
+        if(Optional.ofNullable(examPaper.getSingleNum()).orElse(0)>0){
+            if(Optional.ofNullable(examPaper.getSingleScore()).orElse(0)<=0 || examPaper.getSingleMethod()==null||examPaper.getSingleBankId()==null){
+                throw new ApiException("单选题参数错误");
+            }
+        }
+        if(Optional.ofNullable(examPaper.getMultiNum()).orElse(0)>0){
+            if(Optional.ofNullable(examPaper.getMultiScore()).orElse(0)<=0 || examPaper.getMultiMethod()==null||examPaper.getMultiBankId()==null){
+                throw new ApiException("多选题参数错误");
+            }
+        }
+        if(Optional.ofNullable(examPaper.getJudgeNum()).orElse(0)>0){
+            if(Optional.ofNullable(examPaper.getJudgeScore()).orElse(0)<=0 || examPaper.getJudgeMethod()==null||examPaper.getJudgeBankId()==null){
+                throw new ApiException("判断题参数错误");
+            }
+        }
+        if(Optional.ofNullable(examPaper.getEasyNum()).orElse(0)>0){
+            if(Optional.ofNullable(examPaper.getEasyScore()).orElse(0)<=0 || examPaper.getEasyMethod()==null||examPaper.getEasyBankId()==null){
+                throw new ApiException("简答题参数错误");
+            }
         }
     }
 
@@ -136,6 +193,13 @@
         paperQuestionMapper.batchInsert(paperQuestionList);
     }
 
+    //分配简答题
+    public void assignEasyQuestion(ExExamPaper examPaper){
+        List<ExPaperQuestion> paperQuestionList = getPaperQuestions(examPaper.getId(), examPaper.getEasyBankId(),
+                examPaper.getEasyMethod(),examPaper.getEasyNum(), examPaper.getEasyScore(),QuestionTypeEnum.EASY);
+        paperQuestionMapper.batchInsert(paperQuestionList);
+    }
+
     private List<ExPaperQuestion> getPaperQuestions(Long paperId,Long bankId,Integer questionMethod,Integer questionCount,Integer questionScore, QuestionTypeEnum questionTypeEnum) {
         SysUser currentUser=SecurityUtils.getLoginUser().getUser();
         Integer totalQuestionCount=questionMapper.selectCountByBankId(currentUser.getCompanyId(), bankId, questionTypeEnum.getCode());
@@ -146,7 +210,7 @@
         if(Objects.equals(questionMethod, QuestionAssignEnum.RANDOM.getCode())){//随机分配
             questions=questionMapper.selectRandomQuestion(currentUser.getCompanyId(), bankId,questionTypeEnum.getCode(), questionCount);
         }else{//顺序分配
-            int totalPage=questionCount/questionMethod;//不能整除,忽略最后一页
+            int totalPage=totalQuestionCount/questionCount;//不能整除,忽略最后一页
             Random random=new Random();
             int startIndex=random.nextInt(totalPage)+1;
             questions=questionMapper.selectQuestionWithLimit(currentUser.getCompanyId(), bankId,questionTypeEnum.getCode(),startIndex, questionCount);
@@ -164,6 +228,7 @@
 
     @Override
     public int updateExamPaper(ExExamPaper examPaper) {
+        validatedData(examPaper);
         checkUserAllowed(examPaper);
         if(!checkNameUnique(examPaper)){
             throw new ApiException("试卷名称已存在");
@@ -174,9 +239,10 @@
         }
         examPaper.setCode(null);//编号不能修改
         if(examPaper.getLimitTime()>0){
-            examPaper.setLimit(1);
+            examPaper.setLimited(1);
         }else{
-            examPaper.setLimit(0);
+            examPaper.setLimited(0);
+            examPaper.setLimitTime(0);
         }
         int row=baseMapper.updateById(examPaper);
         if(row<1){
@@ -194,6 +260,10 @@
         if(examPaper.getJudgeRebuild()==1) {
            deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.JUDGE);
            assignJudgeQuestion(examPaper);
+        }
+        if(examPaper.getEasyRebuild()==1) {
+            deletePaperQuestion(examPaper.getId(),QuestionTypeEnum.EASY);
+            assignEasyQuestion(examPaper);
         }
         return row;
     }
@@ -229,8 +299,9 @@
 
     @Override
     public boolean checkNameUnique(ExExamPaper examPaper) {
+        SysUser currentUser=SecurityUtils.getLoginUser().getUser();
         Long paperId=examPaper.getId()==null?-1L:examPaper.getId();
-        ExExamPaper paper= baseMapper.checkNameUnique(examPaper.getName());
+        ExExamPaper paper= baseMapper.checkNameUnique(examPaper.getName(),currentUser.getCompanyId());
         if(paper!=null&&paper.getId().longValue()!=paperId.longValue()){
             return UserConstant.NOT_UNIQUE;
         }

--
Gitblit v1.9.2