ExerciseDictServiceImpl.java 4.67 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.dto.exercise.GetAllExercisesDTO;
import com.subsidy.dto.exercise.SubmitDTO;
import com.subsidy.mapper.ExerciseDoneResultMapper;
import com.subsidy.model.CourseDictDO;
import com.subsidy.model.ExerciseDictDO;
import com.subsidy.mapper.ExerciseDictMapper;
import com.subsidy.model.ExerciseDoneResultDO;
import com.subsidy.service.ExerciseDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.course.ExerciseVO;
import com.subsidy.vo.exercise.MemberExerciseVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 习题库 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-10-12
 */
@Service
public class ExerciseDictServiceImpl extends ServiceImpl<ExerciseDictMapper, ExerciseDictDO> implements ExerciseDictService {

    @Autowired
    private ExerciseDoneResultMapper exerciseDoneResultMapper;

    public IPage<ExerciseDictDO> getAllExercises(GetAllExercisesDTO getAllExercisesDTO) {

        Page pager = new Page(getAllExercisesDTO.getPageNum(), getAllExercisesDTO.getPageSize());

        return this.baseMapper.selectPage(pager, new QueryWrapper<ExerciseDictDO>()
                .lambda()
                .eq(ExerciseDictDO::getCourseId, getAllExercisesDTO.getCourseId())
                .like(ExerciseDictDO::getDifficulty, getAllExercisesDTO.getDifficulty())
                .like(ExerciseDictDO::getExerciseType, getAllExercisesDTO.getExerciseType())
                .like(ExerciseDictDO::getTitle, getAllExercisesDTO.getTitle())
                .orderByAsc(ExerciseDictDO::getOrderNo));
    }

    public String addExercise(ExerciseDictDO exerciseDictDO){

        List<ExerciseDictDO> exerciseDictDOS = this.baseMapper.selectList(new QueryWrapper<ExerciseDictDO>()
        .lambda()
        .eq(ExerciseDictDO::getCourseId,exerciseDictDO.getCourseId())
        .orderByDesc(ExerciseDictDO::getOrderNo));

        int orderNo = 1;

        if (exerciseDictDOS.size() >0){
            orderNo = exerciseDictDOS.get(0).getOrderNo()+1;
        }
        exerciseDictDO.setOrderNo(orderNo);
        this.baseMapper.insert(exerciseDictDO);
        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteExercise(ExerciseDictDO exerciseDictDO){
        this.baseMapper.deleteById(exerciseDictDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateExercise(ExerciseDictDO exerciseDictDO){
        this.baseMapper.updateById(exerciseDictDO);
        return ConstantUtils.SET_SUCCESS;
    }

    public List<ExerciseVO> getCourseExercises(CourseDictDO courseDictDO){

        List<ExerciseVO> exerciseVOS = new ArrayList<>();

        List<ExerciseDictDO> exerciseDictDOS = this.baseMapper.selectList(new QueryWrapper<ExerciseDictDO>()
        .lambda()
        .eq(ExerciseDictDO::getCourseId,courseDictDO.getId()));

        for (ExerciseDictDO exerciseDictDO : exerciseDictDOS){
            ExerciseVO exerciseVO = new ExerciseVO();
            BeanUtils.copyProperties(exerciseDictDO,exerciseVO);
            exerciseVOS.add(exerciseVO);
        }
        return exerciseVOS;
    }

    public String submit(SubmitDTO submitDTO){

        ExerciseDoneResultDO exerciseDoneResultDO = new ExerciseDoneResultDO();

        List<MemberExerciseVO> memberExerciseVOS = submitDTO.getMemberExerciseVOS();

        int rightCounts = 0 ;

        for (MemberExerciseVO exerciseVO : memberExerciseVOS){
            ExerciseDictDO exerciseDictDO = this.baseMapper.selectById(exerciseVO.getId());
            if (exerciseDictDO.getRightAnswer().equals(exerciseVO.getSelectAnswer())){
                rightCounts++;
            }
        }

        exerciseDoneResultDO.setClassId(submitDTO.getClassId());
        exerciseDoneResultDO.setRightCounts(rightCounts);
        exerciseDoneResultDO.setTotalCounts(memberExerciseVOS.size());
        exerciseDoneResultDO.setCourseId(submitDTO.getCourseId());
        exerciseDoneResultDO.setMemberId(submitDTO.getMemberId());

        exerciseDoneResultMapper.delete(new QueryWrapper<ExerciseDoneResultDO>()
        .lambda()
        .eq(ExerciseDoneResultDO::getMemberId,submitDTO.getMemberId())
        .eq(ExerciseDoneResultDO::getCourseId,submitDTO.getCourseId()));

        exerciseDoneResultMapper.insert(exerciseDoneResultDO);

        return ConstantUtils.COMMIT_SUCCESS;
    }

}