From 8bcc04c9623e0eee0995e1139dee6258a8df464d Mon Sep 17 00:00:00 2001
From: fuliqi <fuliqi@qq.com>
Date: 星期三, 12 六月 2024 09:14:24 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java |  162 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 150 insertions(+), 12 deletions(-)

diff --git a/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java b/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
index ccefd50..4da4b71 100644
--- a/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
+++ b/src/main/java/com/ycl/jxkg/service/impl/ExamPaperServiceImpl.java
@@ -1,12 +1,24 @@
 package com.ycl.jxkg.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ycl.jxkg.domain.entity.ExamTemplate;
+import com.ycl.jxkg.context.WebContext;
 import com.ycl.jxkg.domain.entity.TextContent;
-import com.ycl.jxkg.domain.enums.ExamPaperTypeEnum;
+import com.ycl.jxkg.domain.exam.ExamPaperTempDTO;
+import com.ycl.jxkg.domain.form.ExamPaperForm;
+import com.ycl.jxkg.domain.question.TemplateQuestionDTO;
+import com.ycl.jxkg.domain.vo.admin.exam.ExamResponseVO;
+import com.ycl.jxkg.enums.ExamPaperTypeEnum;
 import com.ycl.jxkg.domain.exam.ExamPaperQuestionItemObject;
 import com.ycl.jxkg.domain.exam.ExamPaperTitleItemObject;
 import com.ycl.jxkg.domain.other.KeyValue;
+import com.ycl.jxkg.enums.QuestionTypeEnum;
+import com.ycl.jxkg.enums.VisibilityEnum;
+import com.ycl.jxkg.enums.VisibilityEnum;
 import com.ycl.jxkg.mapper.ExamPaperMapper;
+import com.ycl.jxkg.mapper.ExamTemplateMapper;
 import com.ycl.jxkg.mapper.QuestionMapper;
 import com.ycl.jxkg.service.ExamPaperService;
 import com.ycl.jxkg.service.QuestionService;
@@ -15,27 +27,27 @@
 import com.ycl.jxkg.service.enums.ActionEnum;
 import com.ycl.jxkg.utils.DateTimeUtil;
 import com.ycl.jxkg.utils.JsonUtil;
-import com.ycl.jxkg.utils.ExamUtil;
 import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperEditRequestVO;
 import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperPageRequestVO;
 import com.ycl.jxkg.domain.vo.admin.exam.ExamPaperTitleItemVO;
-import com.ycl.jxkg.domain.vo.admin.question.QuestionEditRequestVO;
 import com.ycl.jxkg.domain.vo.student.dashboard.PaperFilter;
 import com.ycl.jxkg.domain.vo.student.dashboard.PaperInfo;
 import com.ycl.jxkg.domain.vo.student.exam.ExamPaperPageVO;
 import com.github.pagehelper.PageHelper;
 import com.github.pagehelper.PageInfo;
 import com.ycl.jxkg.domain.entity.ExamPaper;
-import com.ycl.jxkg.domain.entity.Question;
 import com.ycl.jxkg.domain.entity.User;
+import com.ycl.jxkg.utils.PageInfoHelper;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.BeanUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.util.Arrays;
+import java.math.BigDecimal;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
 
@@ -48,14 +60,121 @@
     private final TextContentService textContentService;
     private final QuestionService questionService;
     private final SubjectService subjectService;
+    private final ExamTemplateMapper examTemplateMapper;
+    private final WebContext webContext;
 
 
     @Override
-    public PageInfo<ExamPaper> page(ExamPaperPageRequestVO requestVM) {
-        return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
-                examPaperMapper.page(requestVM));
+    @Transactional(rollbackFor = Exception.class)
+    public void addPaper(ExamPaperForm form) {
+        ExamPaper examPaper = ExamPaperForm.getEntityByForm(form,null);
+        baseMapper.insert(examPaper);
+        if(ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())){
+            ExamTemplate examTemplate = getExamTemplate(form, examPaper);
+            examTemplateMapper.insert(examTemplate);
+            baseMapper.updateById(examPaper);
+        }
+        //TODO:闅忓簭璇曞嵎鐢熸垚棰樼洰
+
     }
 
+    private ExamTemplate getExamTemplate(ExamPaperForm form, ExamPaper examPaper) {
+        //闅忔満璇曞嵎瀛樻ā鏉�
+        List<TemplateQuestionDTO> questionList = form.getQuestionList();
+        BigDecimal score = BigDecimal.ZERO;
+        Integer num=0;
+        ExamTemplate examTemplate = new ExamTemplate();
+        //璁剧疆棰樼洰淇℃伅
+        for (TemplateQuestionDTO dto : questionList) {
+            num += dto.getNum();
+            score = score.add(dto.getScore().multiply(BigDecimal.valueOf(dto.getNum())));
+            Integer questionType = dto.getQuestionType();
+            //鏁寸悊List涓哄璞�
+            switch (QuestionTypeEnum.fromCode(questionType)){
+                //鍗曢��
+                case SingleChoice:
+                    examTemplate.setSingleChoice(dto.getNum());
+                    examTemplate.setSingleScore(dto.getScore());
+                    break;
+                //澶氶��
+                case MultipleChoice:
+                    examTemplate.setMultipleChoice(dto.getNum());
+                    examTemplate.setMultipleScore(dto.getScore());
+                    break;
+                //鍒ゆ柇
+                case TrueFalse:
+                    examTemplate.setTrueFalse(dto.getNum());
+                    examTemplate.setTrueFalseScore(dto.getScore());
+                    break;
+                //濉┖
+                case GapFilling:
+                    examTemplate.setGapFilling(dto.getNum());
+                    examTemplate.setGapScore(dto.getScore());
+                    break;
+                //绠�绛�
+                case ShortAnswer:
+                    examTemplate.setShortAnswer(dto.getNum());
+                    examTemplate.setShortAnswerScore(dto.getScore());
+                    break;
+                //璁$畻
+                case Calculation:
+                    examTemplate.setCalculation(dto.getNum());
+                    examTemplate.setCalculationScore(dto.getScore());
+                    break;
+            }
+        }
+        examTemplate.setExamPaperId(examPaper.getId());
+        //鍥炲~
+        examPaper.setQuestionCount(num);
+        examPaper.setScore(score);
+        return examTemplate;
+    }
+
+    @Override
+    public void updateExamPaper(ExamPaperForm form) {
+        ExamPaper entity = ExamPaperForm.getEntityByForm(form,null);
+        //濡傛灉鏄殢鏈鸿瘯鍗蜂慨鏀箃emplate琛�
+        if(ExamPaperTypeEnum.Random.getCode().equals(form.getPaperType())){
+            ExamTemplate examTemplate = getExamTemplate(form, entity);
+            examTemplateMapper.removeById(form.getId());
+            examTemplateMapper.insert(examTemplate);
+        }
+        baseMapper.updateById(entity);
+    }
+
+    @Override
+    public PageInfo<ExamResponseVO> page(ExamPaperPageRequestVO requestVM) {
+        PageInfo<ExamPaperTempDTO> page = PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
+                examPaperMapper.page(requestVM));
+        PageInfo<ExamResponseVO> pageVO = PageInfoHelper.copyMap(page, e -> {
+            ExamResponseVO vo = new ExamResponseVO();
+            BeanUtils.copyProperties(e, vo);
+            vo.setVisibility(VisibilityEnum.valueOf(vo.getVisibility()).getCode() + "");
+            vo.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
+            if(ExamPaperTypeEnum.Random.getCode().equals(e.getPaperType())) {
+                //鏁寸悊棰樼洰淇℃伅涓洪泦鍚�
+                ExamTemplate examTemplate = e.getExamTemplate();
+                List<TemplateQuestionDTO> questionList = new ArrayList<>();
+                addQuestionList(questionList, QuestionTypeEnum.SingleChoice.getCode(), examTemplate.getSingleScore(), examTemplate.getSingleChoice());
+                addQuestionList(questionList, QuestionTypeEnum.MultipleChoice.getCode(), examTemplate.getMultipleScore(), examTemplate.getMultipleChoice());
+                addQuestionList(questionList, QuestionTypeEnum.TrueFalse.getCode(), examTemplate.getTrueFalseScore(), examTemplate.getTrueFalse());
+                addQuestionList(questionList, QuestionTypeEnum.GapFilling.getCode(), examTemplate.getGapScore(), examTemplate.getGapFilling());
+                addQuestionList(questionList, QuestionTypeEnum.ShortAnswer.getCode(), examTemplate.getShortAnswerScore(), examTemplate.getShortAnswer());
+                addQuestionList(questionList, QuestionTypeEnum.Calculation.getCode(), examTemplate.getCalculationScore(), examTemplate.getCalculation());
+                vo.setQuestionList(questionList);
+            }
+            return vo;
+        });
+        return pageVO;
+    }
+
+    private void addQuestionList(List<TemplateQuestionDTO> questionList, Integer code,BigDecimal score,Integer num) {
+        TemplateQuestionDTO questionDTO = new TemplateQuestionDTO();
+        questionDTO.setQuestionType(code);
+        questionDTO.setScore(score);
+        questionDTO.setNum(num);
+        questionList.add(questionDTO);
+    }
     @Override
     public PageInfo<ExamPaper> taskExamPage(ExamPaperPageRequestVO requestVM) {
         return PageHelper.startPage(requestVM.getPageIndex(), requestVM.getPageSize(), "id desc").doSelectPageInfo(() ->
@@ -91,7 +210,7 @@
             examPaperMapper.insert(examPaper);
         } else {
             examPaper = examPaperMapper.selectById(examPaperEditRequestVO.getId());
-            //TODO:寰呭畬鎴�
+            //TODO:
 //            TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
 //            frameTextContent.setContent(frameTextContentStr);
 //            textContentService.updateById(frameTextContent);
@@ -106,7 +225,7 @@
         ExamPaper examPaper = examPaperMapper.selectById(id);
         ExamPaperEditRequestVO vo = new ExamPaperEditRequestVO();
         BeanUtils.copyProperties(examPaper, vo);
-        //TODO:寰呭畬鎴�
+        //TODO:
 //        TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
 //        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), ExamPaperTitleItemObject.class);
 //        List<Integer> questionIds = examPaperTitleItemObjects.stream()
@@ -158,8 +277,10 @@
         }).collect(Collectors.toList());
     }
 
+
+
     private void examPaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, ExamPaper examPaper, List<ExamPaperTitleItemVO> titleItemsVM) {
-        //TODO:寰呭畬鎴�
+        //TODO:
 //        Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVO.getSubjectId());
 //        Integer questionCount = titleItemsVM.stream()
 //                .mapToInt(t -> t.getQuestionItems().size()).sum();
@@ -195,5 +316,22 @@
         }).collect(Collectors.toList());
     }
 
-
+    @Override
+    public List<ExamPaper> myExamPaper(Integer paperType) {
+        Integer userId = webContext.getCurrentUser().getId();
+        List<ExamPaper> list = new LambdaQueryChainWrapper<>(baseMapper)
+                .select(ExamPaper::getId, ExamPaper::getName, ExamPaper::getVisibility)
+                .eq(ExamPaper::getCreateUser, userId)
+                .eq(Objects.nonNull(paperType), ExamPaper::getPaperType, paperType)
+                .or()
+                .eq(ExamPaper::getVisibility, VisibilityEnum.Public.getName())
+                .eq(Objects.nonNull(paperType), ExamPaper::getPaperType, paperType)
+                .list();
+        list.stream().forEach(item -> {
+            if (VisibilityEnum.Public.getName().equals(item.getVisibility())) {
+                item.setName(item.getName() + " (鍏紑)");
+            }
+        });
+        return list;
+    }
 }

--
Gitblit v1.8.0