xiangpei
2024-06-11 74b520065c267999e6a4cd61c8f7b0d516931f07
src/main/java/com/ycl/jxkg/service/impl/ExamTemplateServiceImpl.java
@@ -23,11 +23,13 @@
import lombok.RequiredArgsConstructor;
import com.ycl.jxkg.utils.PageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@@ -59,7 +61,7 @@
        entity.setCreateUser(currentUser.getId());
        entity.setCreateTime(new Date());
        if(!StringUtils.isEmpty(entity.getVisibility())) {
            entity.setVisibility(VisibilityEnum.fromCode(Integer.parseInt(entity.getVisibility())).getName());
            entity.setVisibility(VisibilityEnum.fromCode(entity.getVisibility()).getName());
        }
        List<TemplateQuestionDTO> questionList = form.getQuestionList();
        BigDecimal score = BigDecimal.ZERO;
@@ -111,13 +113,54 @@
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(ExamTemplateForm form) {
        ExamTemplate entity = baseMapper.selectById(form.getId());
        // 为空抛IllegalArgumentException,做全局异常处理
        Assert.notNull(entity, "记录不存在");
        BeanUtils.copyProperties(form, entity);
        baseMapper.updateById(entity);
        ExamTemplate entity = ExamTemplateForm.getEntityByForm(form, null);
        if(!StringUtils.isEmpty(entity.getVisibility())) {
            entity.setVisibility(VisibilityEnum.fromCode(entity.getVisibility()).getName());
        }
        List<TemplateQuestionDTO> questionList = form.getQuestionList();
        BigDecimal score = BigDecimal.ZERO;
        //设置题目信息
        for (TemplateQuestionDTO dto : questionList) {
            score = score.add(dto.getScore().multiply(BigDecimal.valueOf(dto.getNum())));
            Integer questionType = dto.getQuestionType();
            switch (QuestionTypeEnum.fromCode(questionType)){
                //单选
                case SingleChoice:
                    entity.setSingleChoice(dto.getNum());
                    entity.setSingleScore(dto.getScore());
                    break;
                //多选
                case MultipleChoice:
                    entity.setMultipleChoice(dto.getNum());
                    entity.setMultipleScore(dto.getScore());
                    break;
                //判断
                case TrueFalse:
                    entity.setTrueFalse(dto.getNum());
                    entity.setTrueFalseScore(dto.getScore());
                    break;
                //填空
                case GapFilling:
                    entity.setGapFilling(dto.getNum());
                    entity.setGapScore(dto.getScore());
                    break;
                //简答
                case ShortAnswer:
                    entity.setShortAnswer(dto.getNum());
                    entity.setShortAnswerScore(dto.getScore());
                    break;
                //计算
                case Calculation:
                    entity.setCalculation(dto.getNum());
                    entity.setCalculationScore(dto.getScore());
                    break;
            }
        }
        entity.setScore(score);
        examTemplateMapper.removeById(entity.getId());
        baseMapper.insert(entity);
        return Result.ok("修改成功");
    }
@@ -150,9 +193,35 @@
     */
    @Override
    public Result page(ExamTemplateQuery query) {
        IPage<ExamTemplateVO> page = PageUtil.getPage(query, ExamTemplateVO.class);
        IPage<ExamTemplate> page = PageUtil.getPage(query, ExamTemplate.class);
        baseMapper.getPage(page, query);
        return Result.ok().data(page.getRecords()).total(page.getTotal());
        List<ExamTemplate> records = page.getRecords();
        List<ExamTemplateVO> vos = new ArrayList<>();
        for (ExamTemplate examTemplate : records) {
            ExamTemplateVO vo = new ExamTemplateVO();
            BeanUtils.copyProperties(examTemplate,vo);
            String visibility = vo.getVisibility();
            vo.setVisibility(VisibilityEnum.valueOf(visibility).getCode()+"");
            //整理题目信息为集合
            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);
            vos.add(vo);
        }
        return Result.ok().data(vos).total(page.getTotal());
    }
    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);
    }
    /**