xiangpei
2024-06-11 74b520065c267999e6a4cd61c8f7b0d516931f07
src/main/java/com/ycl/jxkg/service/impl/ExamTemplateServiceImpl.java
@@ -1,33 +1,51 @@
package com.ycl.jxkg.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.context.WebContext;
import com.ycl.jxkg.domain.entity.ExamTemplate;
import com.ycl.jxkg.domain.form.ExamTemplateForm;
import com.ycl.jxkg.domain.query.ExamTemplateQuery;
import com.ycl.jxkg.domain.vo.admin.exam.ExamTemplateVO;
import com.ycl.jxkg.domain.entity.User;
import com.ycl.jxkg.domain.question.TemplateQuestionDTO;
import com.ycl.jxkg.enums.QuestionTypeEnum;
import com.ycl.jxkg.enums.VisibilityEnum;
import com.ycl.jxkg.mapper.ExamTemplateMapper;
import com.ycl.jxkg.service.ExamTemplateService;
import com.ycl.jxkg.utils.PageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import com.ycl.jxkg.base.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.jxkg.domain.form.ExamTemplateForm;
import com.ycl.jxkg.domain.vo.ExamTemplateVO;
import com.ycl.jxkg.domain.query.ExamTemplateQuery;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
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;
/**
 * 随机试卷模板 服务实现类
 *
 * @author 开发人员名字
 * @since 2024-06-03
 * @author flq
 * @since 2024-06-05
 */
@Service
@RequiredArgsConstructor
public class ExamTemplateServiceImpl extends ServiceImpl<ExamTemplateMapper, ExamTemplate> implements ExamTemplateService {
    @Autowired
    protected WebContext webContext;
    private final ExamTemplateMapper examTemplateMapper;
@@ -39,6 +57,52 @@
    @Override
    public Result add(ExamTemplateForm form) {
        ExamTemplate entity = ExamTemplateForm.getEntityByForm(form, null);
        User currentUser = webContext.getCurrentUser();
        entity.setCreateUser(currentUser.getId());
        entity.setCreateTime(new Date());
        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);
        baseMapper.insert(entity);
        return Result.ok("添加成功");
    }
@@ -49,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("修改成功");
    }
@@ -88,12 +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);
        PageInfo<ExamTemplateVO> pageInfo = new PageInfo<>();
        pageInfo.setList(page.getRecords());
        pageInfo.setTotal(page.getTotal());
        return Result.ok(pageInfo);
        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);
    }
    /**
@@ -105,7 +233,7 @@
    public Result detail(Integer id) {
        ExamTemplateVO vo = baseMapper.getById(id);
        Assert.notNull(vo, "记录不存在");
        return Result.ok(vo);
        return Result.ok().data(vo);
    }
    /**
@@ -118,6 +246,6 @@
        List<ExamTemplateVO> vos = entities.stream()
                .map(entity -> ExamTemplateVO.getVoByEntity(entity, null))
                .collect(Collectors.toList());
        return Result.ok(vos);
        return Result.ok().data(vos);
    }
}