KnowledgeSubjectDictServiceImpl.java 6.99 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.subject.GetSubjectAdministerDTO;
import com.meishu.dto.subject.GetSubjectTreeDTO;
import com.meishu.dto.subject.GetSubjectsDTO;
import com.meishu.mapper.AdministerMapper;
import com.meishu.mapper.SubjectTreeMapper;
import com.meishu.model.*;
import com.meishu.mapper.KnowledgeSubjectDictMapper;
import com.meishu.service.KnowledgeSubjectDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.util.tree.SerialNumber;
import com.meishu.vo.subject.GetSubjectTreeVO;
import com.meishu.vo.subject.SubjectTreeOrder;
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 Tuyp
 * @since 2021-04-27
 */
@Service
public class KnowledgeSubjectDictServiceImpl extends ServiceImpl<KnowledgeSubjectDictMapper, KnowledgeSubjectDictDO> implements KnowledgeSubjectDictService {

    @Autowired
    private SubjectTreeMapper subjectTreeMapper;

    @Autowired
    private AdministerMapper administerMapper;

    public IPage<KnowledgeSubjectDictDO> getSubjects(GetSubjectsDTO getSubjectsDTO) {
        Page pager = new Page(getSubjectsDTO.getPageNum(), getSubjectsDTO.getPageSize());
        return this.baseMapper.getAdministerSubjects(pager, getSubjectsDTO.getUserId(), getSubjectsDTO.getSubjectType(), getSubjectsDTO.getSubjectName());
    }

    public List<KnowledgeSubjectDictDO> getAllSubjects(GetSubjectsDTO getSubjectsDTO) {
        return this.baseMapper.selectList(new QueryWrapper<KnowledgeSubjectDictDO>()
                .lambda()
                .eq(false, KnowledgeSubjectDictDO::getSubjectName, getSubjectsDTO.getSubjectName())
                .eq(false, KnowledgeSubjectDictDO::getSubjectType, getSubjectsDTO.getSubjectType()));
    }

    public String updateSubject(KnowledgeSubjectDictDO subjectDictDO) {
        this.baseMapper.updateById(subjectDictDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    public String addSubject(KnowledgeSubjectDictDO subjectDictDO) {
        this.baseMapper.insert(subjectDictDO);
        return ConstantUtils.ADD_SUCCESS;
    }

    public String deleteSubject(KnowledgeSubjectDictDO subjectDictDO) {
        this.baseMapper.deleteById(subjectDictDO.getId());
        return ConstantUtils.DELETE_SUCCESS;
    }


    public KnowledgeSubjectDictDO getSubject(KnowledgeSubjectDictDO subjectDictDO) {
        return this.baseMapper.selectById(subjectDictDO.getId());
    }


    public List<AdministerDO> getSubjectAdminister(GetSubjectAdministerDTO getSubjectAdministerDTO) {
        return this.baseMapper.getSubjectAdminister(getSubjectAdministerDTO.getSubjectId(), getSubjectAdministerDTO.getUserName());
    }

    /**
     * 递归遍历子节点
     *
     * @param getSubjectTreeVO 父节点填充子节点
     * @return
     */
    public List<GetSubjectTreeVO> getChildNodes(GetSubjectTreeVO getSubjectTreeVO, SubjectTreeOrder treeNode,Integer i,boolean flag,List<GetSubjectTreeVO> nodeList,Integer chapterId) {

        List<GetSubjectTreeVO> getSubjectTreeVOS = new ArrayList<>();

        i++;

        if (i==4 && !flag){     //只看前三层
            return null;
        }
        getSubjectTreeVO.setLayer(chapterId+"");
        if (!"10.1".equals(getSubjectTreeVO.getText()) && !"20.1".equals(getSubjectTreeVO.getText()) &&  !"30.1".equals(getSubjectTreeVO.getText()) &&  !"40.1".equals(getSubjectTreeVO.getText()) && !"50.1".equals(getSubjectTreeVO.getText())){
            getSubjectTreeVO.setText(getSubjectTreeVO.getText().replace("0.1",getSubjectTreeVO.getLayer()));
        }
        getSubjectTreeVO.setTreeNode(getSubjectTreeVO.getText());

        //查找该父节点下的子节点
        List<SubjectTreeDO> subjectTreeDOS = subjectTreeMapper.selectList(new QueryWrapper<SubjectTreeDO>()
                .lambda()
                .eq(SubjectTreeDO::getParentId, getSubjectTreeVO.getId())
                .orderByAsc(SubjectTreeDO::getTreeOrder));

        getSubjectTreeVO.setChildNodeCounts(subjectTreeDOS.size());

        for (SubjectTreeDO subjectTreeDO1 : subjectTreeDOS) {
//            GetSubjectTreeVO getSubjectTreeVO1 = new GetSubjectTreeVO();
            GetSubjectTreeVO getSubjectTreeVO1 =SubjectTreeOrder.getNextNode(treeNode,getSubjectTreeVO,nodeList);
            BeanUtils.copyProperties(subjectTreeDO1, getSubjectTreeVO1);
            getSubjectTreeVO1.setParentTreeName(getSubjectTreeVO.getTreeName());
            getSubjectTreeVOS.add(getSubjectTreeVO1);
            getSubjectTreeVO1.setChildren(getChildNodes(getSubjectTreeVO1, treeNode,i,flag,nodeList,chapterId));
        }
        if (subjectTreeDOS.size() == 0) {
            return null;
        }
        return getSubjectTreeVOS;
    }

    public List<GetSubjectTreeVO> getSubjectTree(GetSubjectTreeDTO getSubjectTreeDTO) {

        List<GetSubjectTreeVO> getSubjectTreeVOS = new ArrayList<>();

        SerialNumber number = new SerialNumber();
        SubjectTreeOrder treeNode = new SubjectTreeOrder(".", number);

        //判断老师角色
        List<RoleDictDO> roleDictDOS = administerMapper.getTeacherRoles(getSubjectTreeDTO.getUserId());

        boolean flag = false; //true 可以看全部的    false 没权限或者是授课老师 只能看三级

        for (RoleDictDO roleDictDO : roleDictDOS) {
            if (roleDictDO.getId() == 1 || roleDictDO.getId() == 2 || roleDictDO.getId() == 4) {
                flag = true;
            }
        }

        //获取科目的父节点
        List<SubjectTreeDO> subjectTreeDOS = subjectTreeMapper.selectList(new QueryWrapper<SubjectTreeDO>()
                .lambda()
                .eq(SubjectTreeDO::getSubjectId, getSubjectTreeDTO.getSubjectId())
                .isNull(SubjectTreeDO::getParentId)
                .orderByAsc(SubjectTreeDO::getTreeOrder));

        GetSubjectTreeVO root = treeNode.getRoot(treeNode.getNodeList());

        List<GetSubjectTreeVO> nodeList = new ArrayList<>();

        //章节id
        int chapterId = 1;

        for (SubjectTreeDO std : subjectTreeDOS) {
            GetSubjectTreeVO getSubjectTreeVO =  SubjectTreeOrder.getNextNode(treeNode,root,nodeList);
            BeanUtils.copyProperties(std, getSubjectTreeVO);
            int i = 1 ;     //第几层


            getSubjectTreeVO.setChildren(getChildNodes(getSubjectTreeVO, treeNode,i,flag,nodeList,chapterId));
            getSubjectTreeVO.setLayer(chapterId+"");
            chapterId++;
            getSubjectTreeVO.setText(getSubjectTreeVO.getText().replace("0.1",getSubjectTreeVO.getLayer()));
            getSubjectTreeVO.setTreeNode(getSubjectTreeVO.getText());
            getSubjectTreeVOS.add(getSubjectTreeVO);
        }
        return getSubjectTreeVOS;
    }

}