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 |  269 ++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 202 insertions(+), 67 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 c213d8d..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,13 +1,24 @@
 package com.ycl.jxkg.service.impl;
 
-import com.baomidou.mybatisplus.core.conditions.Wrapper;
+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.TextContent;
-import com.ycl.jxkg.domain.enums.ExamPaperTypeEnum;
+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.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;
@@ -16,30 +27,28 @@
 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.vo.admin.exam.ExamPaperEditRequestVO;
-import com.ycl.jxkg.vo.admin.exam.ExamPaperPageRequestVO;
-import com.ycl.jxkg.vo.admin.exam.ExamPaperTitleItemVO;
-import com.ycl.jxkg.vo.admin.question.QuestionEditRequestVO;
-import com.ycl.jxkg.vo.student.dashboard.PaperFilter;
-import com.ycl.jxkg.vo.student.dashboard.PaperInfo;
-import com.ycl.jxkg.vo.student.exam.ExamPaperPageVO;
+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.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.ExamPaper;
-import com.ycl.jxkg.domain.Question;
-import com.ycl.jxkg.domain.User;
+import com.ycl.jxkg.domain.entity.ExamPaper;
+import com.ycl.jxkg.domain.entity.User;
+import com.ycl.jxkg.utils.PageInfoHelper;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.BeanUtils;
-import org.springframework.beans.factory.annotation.Autowired;
 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.function.Function;
 import java.util.stream.Collectors;
 
 @Service
@@ -51,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(() ->
@@ -88,17 +204,16 @@
             frameTextContent.setContent(frameTextContentStr);
             frameTextContent.setCreateTime(now);
             textContentService.save(frameTextContent);
-            examPaper.setFrameTextContentId(frameTextContent.getId());
             examPaper.setCreateTime(now);
             examPaper.setCreateUser(user.getId());
-            examPaper.setDeleted(false);
             examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM);
             examPaperMapper.insert(examPaper);
         } else {
             examPaper = examPaperMapper.selectById(examPaperEditRequestVO.getId());
-            TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
-            frameTextContent.setContent(frameTextContentStr);
-            textContentService.updateById(frameTextContent);
+            //TODO:
+//            TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
+//            frameTextContent.setContent(frameTextContentStr);
+//            textContentService.updateById(frameTextContent);
             examPaperFromVM(examPaperEditRequestVO, examPaper, titleItemsVM);
             examPaperMapper.updateById(examPaper);
         }
@@ -110,32 +225,32 @@
         ExamPaper examPaper = examPaperMapper.selectById(id);
         ExamPaperEditRequestVO vo = new ExamPaperEditRequestVO();
         BeanUtils.copyProperties(examPaper, vo);
-        vo.setLevel(examPaper.getGradeLevel());
-        TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
-        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), ExamPaperTitleItemObject.class);
-        List<Integer> questionIds = examPaperTitleItemObjects.stream()
-                .flatMap(t -> t.getQuestionItems().stream()
-                        .map(q -> q.getId()))
-                .collect(Collectors.toList());
-        List<Question> questions = questionMapper.selectByIds(questionIds);
-        List<ExamPaperTitleItemVO> examPaperTitleItemVOS = examPaperTitleItemObjects.stream().map(t -> {
-            ExamPaperTitleItemVO tTitleVM = new ExamPaperTitleItemVO();
-            BeanUtils.copyProperties(t, tTitleVM);
-            List<QuestionEditRequestVO> questionItemsVM = t.getQuestionItems().stream().map(i -> {
-                Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
-                QuestionEditRequestVO questionEditRequestVO = questionService.getQuestionEditRequestVM(question);
-                questionEditRequestVO.setItemOrder(i.getItemOrder());
-                return questionEditRequestVO;
-            }).collect(Collectors.toList());
-            tTitleVM.setQuestionItems(questionItemsVM);
-            return tTitleVM;
-        }).collect(Collectors.toList());
-        vo.setTitleItems(examPaperTitleItemVOS);
-        vo.setScore(ExamUtil.scoreToVM(examPaper.getScore()));
-        if (ExamPaperTypeEnum.TimeLimit == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
-            List<String> limitDateTime = Arrays.asList(DateTimeUtil.dateFormat(examPaper.getLimitStartTime()), DateTimeUtil.dateFormat(examPaper.getLimitEndTime()));
-            vo.setLimitDateTime(limitDateTime);
-        }
+        //TODO:
+//        TextContent frameTextContent = textContentService.getById(examPaper.getFrameTextContentId());
+//        List<ExamPaperTitleItemObject> examPaperTitleItemObjects = JsonUtil.toJsonListObject(frameTextContent.getContent(), ExamPaperTitleItemObject.class);
+//        List<Integer> questionIds = examPaperTitleItemObjects.stream()
+//                .flatMap(t -> t.getQuestionItems().stream()
+//                        .map(q -> q.getId()))
+//                .collect(Collectors.toList());
+//        List<Question> questions = questionMapper.selectByIds(questionIds);
+//        List<ExamPaperTitleItemVO> examPaperTitleItemVOS = examPaperTitleItemObjects.stream().map(t -> {
+//            ExamPaperTitleItemVO tTitleVM = new ExamPaperTitleItemVO();
+//            BeanUtils.copyProperties(t, tTitleVM);
+//            List<QuestionEditRequestVO> questionItemsVM = t.getQuestionItems().stream().map(i -> {
+//                Question question = questions.stream().filter(q -> q.getId().equals(i.getId())).findFirst().get();
+//                QuestionEditRequestVO questionEditRequestVO = questionService.getQuestionEditRequestVM(question);
+//                questionEditRequestVO.setItemOrder(i.getItemOrder());
+//                return questionEditRequestVO;
+//            }).collect(Collectors.toList());
+//            tTitleVM.setQuestionItems(questionItemsVM);
+//            return tTitleVM;
+//        }).collect(Collectors.toList());
+//        vo.setTitleItems(examPaperTitleItemVOS);
+//        vo.setScore(ExamUtil.scoreToVM(examPaper.getScore()));
+//        if (ExamPaperTypeEnum.Random == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
+//            List<String> limitDateTime = Arrays.asList(DateTimeUtil.dateFormat(examPaper.getLimitStartTime()), DateTimeUtil.dateFormat(examPaper.getLimitEndTime()));
+//            vo.setLimitDateTime(limitDateTime);
+//        }
         return vo;
     }
 
@@ -162,22 +277,25 @@
         }).collect(Collectors.toList());
     }
 
+
+
     private void examPaperFromVM(ExamPaperEditRequestVO examPaperEditRequestVO, ExamPaper examPaper, List<ExamPaperTitleItemVO> titleItemsVM) {
-        Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVO.getSubjectId());
-        Integer questionCount = titleItemsVM.stream()
-                .mapToInt(t -> t.getQuestionItems().size()).sum();
-        Integer score = titleItemsVM.stream().
-                flatMapToInt(t -> t.getQuestionItems().stream()
-                        .mapToInt(q -> ExamUtil.scoreFromVM(q.getScore()))
-                ).sum();
-        examPaper.setQuestionCount(questionCount);
-        examPaper.setScore(score);
-        examPaper.setGradeLevel(gradeLevel);
-        List<String> dateTimes = examPaperEditRequestVO.getLimitDateTime();
-        if (ExamPaperTypeEnum.TimeLimit == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
-            examPaper.setLimitStartTime(DateTimeUtil.parse(dateTimes.get(0), DateTimeUtil.STANDER_FORMAT));
-            examPaper.setLimitEndTime(DateTimeUtil.parse(dateTimes.get(1), DateTimeUtil.STANDER_FORMAT));
-        }
+        //TODO:
+//        Integer gradeLevel = subjectService.levelBySubjectId(examPaperEditRequestVO.getSubjectId());
+//        Integer questionCount = titleItemsVM.stream()
+//                .mapToInt(t -> t.getQuestionItems().size()).sum();
+//        Integer score = titleItemsVM.stream().
+//                flatMapToInt(t -> t.getQuestionItems().stream()
+//                        .mapToInt(q -> ExamUtil.scoreFromVM(q.getScore()))
+//                ).sum();
+//        examPaper.setQuestionCount(questionCount);
+//        examPaper.setScore(score);
+//        examPaper.setGradeLevel(gradeLevel);
+//        List<String> dateTimes = examPaperEditRequestVO.getLimitDateTime();
+//        if (ExamPaperTypeEnum.Random == ExamPaperTypeEnum.fromCode(examPaper.getPaperType())) {
+//            examPaper.setLimitStartTime(DateTimeUtil.parse(dateTimes.get(0), DateTimeUtil.STANDER_FORMAT));
+//            examPaper.setLimitEndTime(DateTimeUtil.parse(dateTimes.get(1), DateTimeUtil.STANDER_FORMAT));
+//        }
     }
 
     private List<ExamPaperTitleItemObject> frameTextContentFromVM(List<ExamPaperTitleItemVO> titleItems) {
@@ -198,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