Blame view

KnowledgeSubjectDictServiceImpl.java 6.99 KB
涂亚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
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;
    }

}