ExerciseDictServiceImpl.java 13.1 KB
package com.meishu.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.meishu.dto.exercise.AddExerciseDTO;
import com.meishu.dto.exercise.CheckExerciseDTO;
import com.meishu.dto.exercise.GetTreeExercisesDTO;
import com.meishu.dto.voddict.GetToCheckVodCountsDTO;
import com.meishu.dto.voddict.PublicCheckingDTO;
import com.meishu.mapper.*;
import com.meishu.model.*;
import com.meishu.service.ExerciseDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.vo.exercise.GetExerciseDetailVO;
import com.meishu.vo.exercise.GetTreeExercisesVO;
import com.meishu.vo.voddict.GetToCheckCountsVO;
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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

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

    @Autowired
    private ExerciseTreeMappingMapper exerciseTreeMappingMapper;

    @Autowired
    private KnowledgeSubjectDictMapper knowledgeSubjectDictMapper;

    @Autowired
    private AdministerMapper administerMapper;

    public IPage<GetTreeExercisesVO> getTreeExercises(GetTreeExercisesDTO getTreeExercisesDTO) {

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

        KnowledgeSubjectDictDO subjectDictDO = knowledgeSubjectDictMapper.selectById(getTreeExercisesDTO.getSubjectId());

        if ("1".equals(getTreeExercisesDTO.getShareStatus())) {
            getTreeExercisesDTO.setUserId(null);
        }

        //查看该老师的科目信息
        List<KnowledgeSubjectDictDO> subjectDictDOS = administerMapper.getTeacherSubjects(getTreeExercisesDTO.getUserId());

        List<String> strings = new ArrayList<>();
        for (KnowledgeSubjectDictDO subjectDictDO1 : subjectDictDOS) {
            strings.add(subjectDictDO1.getSubjectEn());
        }

        IPage<GetTreeExercisesVO> getTreeExercisesVOIPage = this.baseMapper.getTreeExercises(pager, getTreeExercisesDTO.getUserId(), subjectDictDO.getSubjectEn(), getTreeExercisesDTO.getTreeId(), getTreeExercisesDTO.getExerciseType(), getTreeExercisesDTO.getDifficulty(), getTreeExercisesDTO.getTitle(), getTreeExercisesDTO.getShareStatus(),getTreeExercisesDTO.getCheckStatus(),strings);

        List<GetTreeExercisesVO> getTreeExercisesVOS = getTreeExercisesVOIPage.getRecords();
        for (GetTreeExercisesVO getTreeExercisesVO : getTreeExercisesVOS) {
            //找知识树
            List<SubjectTreeDO> subjectTreeDOS = this.baseMapper.getTrees(getTreeExercisesVO.getId());
            getTreeExercisesVO.setSubjectTreeDOS(subjectTreeDOS);
        }
        getTreeExercisesVOIPage.setRecords(getTreeExercisesVOS);
        return getTreeExercisesVOIPage;
    }

    public IPage<GetTreeExercisesVO> getCheckTreeExercises(GetTreeExercisesDTO getTreeExercisesDTO){

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

        KnowledgeSubjectDictDO subjectDictDO = knowledgeSubjectDictMapper.selectById(getTreeExercisesDTO.getSubjectId());

        //查看该老师的科目信息
        List<KnowledgeSubjectDictDO> subjectDictDOS = administerMapper.getTeacherSubjects(getTreeExercisesDTO.getUserId());

        List<String> strings = new ArrayList<>();
        for (KnowledgeSubjectDictDO subjectDictDO1 : subjectDictDOS) {
            strings.add(subjectDictDO1.getSubjectEn());
        }

        IPage<GetTreeExercisesVO> getTreeExercisesVOIPage = this.baseMapper.getTreeExercises(pager, null, subjectDictDO.getSubjectEn(), getTreeExercisesDTO.getTreeId(), getTreeExercisesDTO.getExerciseType(), getTreeExercisesDTO.getDifficulty(), getTreeExercisesDTO.getTitle(), getTreeExercisesDTO.getShareStatus(),getTreeExercisesDTO.getCheckStatus(),strings);

        List<GetTreeExercisesVO> getTreeExercisesVOS = getTreeExercisesVOIPage.getRecords();
        for (GetTreeExercisesVO getTreeExercisesVO : getTreeExercisesVOS) {
            //找知识树
            List<SubjectTreeDO> subjectTreeDOS = this.baseMapper.getTrees(getTreeExercisesVO.getId());
            getTreeExercisesVO.setSubjectTreeDOS(subjectTreeDOS);
        }
        getTreeExercisesVOIPage.setRecords(getTreeExercisesVOS);
        return getTreeExercisesVOIPage;

    }

    @Transactional(rollbackFor = Exception.class)
    public String deleteExercise(ExerciseDictDO exerciseDictDO) {

//        Integer count = exerciseCourseTreeMappingMapper.selectCount(new QueryWrapper<ExerciseCourseTreeMappingDO>()
//                .lambda()
//                .eq(ExerciseCourseTreeMappingDO::getExerciseId, exerciseDictDO.getId()));
//
//        if (count > 0) {
//            throw new HttpException(30003);
//        }
//
        this.baseMapper.deleteById(exerciseDictDO.getId());

        //删相关树
        exerciseTreeMappingMapper.delete(new QueryWrapper<ExerciseTreeMappingDO>()
                .lambda()
                .eq(ExerciseTreeMappingDO::getExerciseId, exerciseDictDO.getId()));

//        //删相关做题记录
//        exerciseDoneHistoryMapper.delete(new QueryWrapper<ExerciseDoneHistoryDO>()
//                .lambda()
//                .eq(ExerciseDoneHistoryDO::getExerciseId, exerciseDictDO.getId()));

        return ConstantUtils.DELETE_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String addExercise(AddExerciseDTO addExerciseDTO) {
        ExerciseDictDO exerciseDictDO = new ExerciseDictDO();
        KnowledgeSubjectDictDO subjectDictDO = knowledgeSubjectDictMapper.selectById(addExerciseDTO.getSubjectId());
        BeanUtils.copyProperties(addExerciseDTO, exerciseDictDO);
        exerciseDictDO.setShareStatus("0");
        exerciseDictDO.setCheckStatus("0");
        exerciseDictDO.setSubjectId(subjectDictDO.getSubjectEn());
        this.baseMapper.insert(exerciseDictDO);

        List<Long> longs = addExerciseDTO.getTreeIds();
        for (Long lg : longs) {
            ExerciseTreeMappingDO exerciseTreeMappingDO = new ExerciseTreeMappingDO();
            exerciseTreeMappingDO.setTreeId(lg);
            exerciseTreeMappingDO.setExerciseId(exerciseDictDO.getId());
            exerciseTreeMappingMapper.insert(exerciseTreeMappingDO);

//            //新增的题目也要加到courseTree里去
//            List<CourseTreeDO> courseTreeDOS = courseTreeMapper.selectList(new QueryWrapper<CourseTreeDO>()
//                    .lambda()
//                    .eq(CourseTreeDO::getTreeId, lg));
//
//            for (CourseTreeDO courseTreeDO : courseTreeDOS) {
//                ExerciseCourseTreeMappingDO exerciseCourseTreeMappingDO = new ExerciseCourseTreeMappingDO();
//                exerciseCourseTreeMappingDO.setExerciseId(exerciseDictDO.getId());
//                exerciseCourseTreeMappingDO.setStatus("0");
//                exerciseCourseTreeMappingDO.setCourseTreeId(courseTreeDO.getId());
//                exerciseCourseTreeMappingDO.setCourseId(lg);
//                exerciseCourseTreeMappingMapper.insert(exerciseCourseTreeMappingDO);
//            }
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String updateExercise(AddExerciseDTO addExerciseDTO) {
        ExerciseDictDO exerciseDictDO = new ExerciseDictDO();
        BeanUtils.copyProperties(addExerciseDTO, exerciseDictDO);
        KnowledgeSubjectDictDO subjectDictDO = knowledgeSubjectDictMapper.selectById(addExerciseDTO.getSubjectId());
        if (null!=subjectDictDO){
            exerciseDictDO.setSubjectId(subjectDictDO.getSubjectEn());
        }
        this.baseMapper.updateById(exerciseDictDO);

        exerciseTreeMappingMapper.delete(new QueryWrapper<ExerciseTreeMappingDO>()
                .lambda()
                .in(ExerciseTreeMappingDO::getExerciseId, addExerciseDTO.getId()));

        List<Long> longs = addExerciseDTO.getTreeIds();
        for (Long lg : longs) {
            ExerciseTreeMappingDO exerciseTreeMappingDO = new ExerciseTreeMappingDO();
            exerciseTreeMappingDO.setTreeId(lg);
            exerciseTreeMappingDO.setExerciseId(exerciseDictDO.getId());
            exerciseTreeMappingMapper.insert(exerciseTreeMappingDO);
        }
        return ConstantUtils.SUCCESS_UPDATE;
    }

    public GetToCheckCountsVO getToCheckExeCounts(GetToCheckVodCountsDTO getToCheckVodCountsDTO) {

        GetToCheckCountsVO getToCheckCountsVO = new GetToCheckCountsVO();

        KnowledgeSubjectDictDO subjectDictDO = knowledgeSubjectDictMapper.selectById(getToCheckVodCountsDTO.getSubjectId());

        Integer count = this.baseMapper.selectCount(new QueryWrapper<ExerciseDictDO>()
                .lambda()
                .eq(ExerciseDictDO::getCheckStatus, "2")
                .eq(ExerciseDictDO::getSubjectId, subjectDictDO.getSubjectEn()));
        getToCheckCountsVO.setToCheckCounts(count);

        //已审核  包括已通过 未通过
        Integer counts1 = this.baseMapper.selectCount(new QueryWrapper<ExerciseDictDO>()
                .lambda()
                .eq(ExerciseDictDO::getSubjectId, subjectDictDO.getSubjectEn())
                .in(ExerciseDictDO::getCheckStatus, new String[]{"1", "3"}));
        getToCheckCountsVO.setCheckCounts(counts1);

        return getToCheckCountsVO;
    }

    public String checkExercise(CheckExerciseDTO checkExerciseDTO) {

        //审核通过
        if ("1".equals(checkExerciseDTO.getCheckStatus())) {
            ExerciseDictDO exerciseDictDO = this.baseMapper.selectById(checkExerciseDTO.getId());

            exerciseDictDO.setCheckStatus("1");// 已通过审核
            this.baseMapper.updateById(exerciseDictDO);

            exerciseDictDO.setShareStatus("1"); //已共享
            exerciseDictDO.setDifficulty(checkExerciseDTO.getDifficulty());
            exerciseDictDO.setAdviceLength(checkExerciseDTO.getAdviceLength());
            exerciseDictDO.setCreateDate(LocalDateTime.now());
            exerciseDictDO.setUpdateDate(LocalDateTime.now());
            this.baseMapper.insert(exerciseDictDO);

            List<Long> ids = checkExerciseDTO.getTreeIds();

//            //取消之前的关联
//            exerciseTreeMappingMapper.delete(new QueryWrapper<ExerciseTreeMappingDO>()
//                    .lambda()
//                    .eq(ExerciseTreeMappingDO::getExerciseId, checkExerciseDTO.getId()));

            for (Long lg : ids) {
                //知识点关联
                ExerciseTreeMappingDO exerciseTreeMappingDO = new ExerciseTreeMappingDO();
                exerciseTreeMappingDO.setExerciseId(exerciseDictDO.getId());
                exerciseTreeMappingDO.setTreeId(lg);
                exerciseDictDO.setStatus("0");     //不是关联习题
                exerciseTreeMappingMapper.insert(exerciseTreeMappingDO);
            }
        } else {
            //不通过
            ExerciseDictDO exerciseDictDO = new ExerciseDictDO();
            BeanUtils.copyProperties(checkExerciseDTO, exerciseDictDO);
            this.baseMapper.updateById(exerciseDictDO);
        }

        return ConstantUtils.CHECK_STATUS;
    }

    public IPage<ExerciseDictDO> publicUpload(PublicCheckingDTO publicCheckingDTO) {

        Page pager = new Page(publicCheckingDTO.getPageNum(), publicCheckingDTO.getPageSize());
        return this.baseMapper.selectPage(pager, new QueryWrapper<ExerciseDictDO>()
                .lambda()
                .eq(ExerciseDictDO::getAdministerId, publicCheckingDTO.getUserId())
                .eq(ExerciseDictDO::getCheckStatus, publicCheckingDTO.getCheckStatus()));
    }

    public String withdraw(ExerciseDictDO exerciseDictDO) {
        exerciseDictDO.setShareStatus("0");
        exerciseDictDO.setCheckStatus("0");
        this.baseMapper.updateById(exerciseDictDO);
        return ConstantUtils.SET_SUCCESS;
    }

    public GetExerciseDetailVO getExerciseDetail(ExerciseDictDO exerciseDictDO) {

        GetExerciseDetailVO getExerciseDetailVO = new GetExerciseDetailVO();
        ExerciseDictDO exerciseDictDO1 = this.baseMapper.selectById(exerciseDictDO.getId());
        BeanUtils.copyProperties(exerciseDictDO1, getExerciseDetailVO);

        List<SubjectTreeDO> subjectTreeDOS = this.baseMapper.getTrees(exerciseDictDO.getId());
        getExerciseDetailVO.setSubjectTreeDOS(subjectTreeDOS);
        return getExerciseDetailVO;
    }

    public String setRelatedExercise(ExerciseTreeMappingDO exerciseTreeMappingDO){

        ExerciseTreeMappingDO exerciseTreeMappingDO1 = new ExerciseTreeMappingDO();
        exerciseTreeMappingDO1.setStatus(exerciseTreeMappingDO.getStatus());

        exerciseTreeMappingMapper.update(exerciseTreeMappingDO1,new QueryWrapper<ExerciseTreeMappingDO>()
        .lambda()
        .eq(ExerciseTreeMappingDO::getExerciseId,exerciseTreeMappingDO.getExerciseId()));

        return ConstantUtils.SUCCESS_UPDATE;
    }

}