树形图后端代码

添加树形结构createTreeModel

思路:
在这里插入图片描述
关键代码:

@Override
    public boolean createTreeModel(List<QuestionClassifyModel> questionClassifyModelList) {

		// 分解试题分类集合
        List<QuestionClassifyModel> models = resolveQuestionClassify(questionClassifyModelList);
        // 实例化一个ArrayList集合teQuestionClassifyList
        List<TeQuestionClassify> teQuestionClassifyList = new ArrayList<>();
        // copyroperties model到questionClassifyEntity值
        for (QuestionClassifyModel model : models) {
            TeQuestionClassify teQuestionClassify = new TeQuestionClassify();
            // 将model 赋值给实体teQuestionClassify
            BeanUtils.copyProperties(model, teQuestionClassify);
            // 父id为空,赋值0
            if (StringUtils.isEmpty(teQuestionClassify.getParentId())) {
                teQuestionClassify.setParentId("0");
            }
            // 实体id为空,重新赋值
            if (StringUtils.isEmpty(teQuestionClassify.getId())) {
                teQuestionClassify.setId(IdWorker.getIdStr());
            }
            // 把实体添加到集合里
            teQuestionClassifyList.add(teQuestionClassify);

        }
        // 保存到数据库
        return questionClassifyService.saveBatch(teQuestionClassifyList);
    }



private List<QuestionClassifyModel> resolveQuestionClassify(List<QuestionClassifyModel> questionClassifyModelList) {
        // 实例化集合classifyModelList
        List<QuestionClassifyModel> classifyModelList = new ArrayList<>();
        // 通过增强for循环将试题分类集合赋值给model
        for (QuestionClassifyModel model : questionClassifyModelList){
            // 设置id
            model.setId(IdWorker.getIdStr());
            // 去掉null值
            model.getQuestionClassifyModel().remove(null);
            if (model.getQuestionClassifyModel() != null){
                for (QuestionClassifyModel sonModel : model.getQuestionClassifyModel()) {
                    // 子类设置父id
                    sonModel.setParentId(model.getId());
                }
                // 递归循环添加子类试题分类集合到实例化的集合classifyModelList,子类试题分类集合空递归结束
                classifyModelList.addAll(resolveQuestionClassify(model.getQuestionClassifyModel()));
            }
            classifyModelList.add(model);
        }
        return classifyModelList;
    }

在这里插入图片描述

在这里插入图片描述

关键代码:

@Override
    public TeQuestionClassify createTreeNode(QuestionClassifyModel questionClassifyModel) {
        
        TeQuestionClassify teQuestionClassify = new TeQuestionClassify();
        // 将questionClassifyModel赋值给teQuestionClassify
        BeanUtils.copyProperties(questionClassifyModel, teQuestionClassify);
        // 实体父id为空赋值0
        if (StringUtils.isEmpty(teQuestionClassify.getParentId())) {
            teQuestionClassify.setParentId("0");
        }
        // teQuestionClassify实体id为空,重新赋值
        if (StringUtils.isEmpty(teQuestionClassify.getId())) {
            teQuestionClassify.setId(IdWorker.getIdStr());
        }
        // 通过父id查询实体
        TeQuestionClassify teQuestionClassify1 = this.getById(questionClassifyModel.getParentId());
        // 实体为空或父id为0则赋值默认值Level为0
        if (teQuestionClassify1 == null || "0".equals(teQuestionClassify1.getParentId())) {
            teQuestionClassify.setLevel(0);
        } else {
            // 设置等级+1
            teQuestionClassify.setLevel(teQuestionClassify.getLevel() + 1);
        }
        // 插入实体到数据库
        questionClassifyDao.insert(teQuestionClassify);
        return teQuestionClassify;
    }

删除:
baseMapper的removeById(id)删除;

修改:
将参数QuestionClassifyModel赋值给实体
调用baseMapper的updateById(实体)方法修改;

查询:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
关键代码:

@Override
    public List<QuestionTypeClassifyModel> getQuestionClassify(String questionTypeId, String courseId, String operator) {

        /**
         * 1.查询该课程下所有该题型的所有分类
         * 2.将无子节点试题分类先放入集合中
         * 3.将所有parent_id去重得到一个父ids数组
         * 4.根据父id和当前结构递归调用方法查询试题分类的树形结构
         */
        List<QuestionTypeClassifyModel> treeList = new ArrayList<>();
        List<QuestionTypeClassifyModel> questionTypeClassifyModelList = questionClassifyDao.getQuestionClassify(questionTypeId, courseId, operator);
        // TODO: 换种方式-xsh
        List<QuestionTypeClassifyModel> listParent = questionTypeClassifyModelList.stream().filter(a -> "0".equals(a.getParentId())).collect(Collectors.toList());
        treeList.addAll(listParent);
        List<String> parentIdList = questionTypeClassifyModelList.stream().map(QuestionTypeClassifyModel::getParentId).collect(Collectors.toList());
        List<QuestionTypeClassifyModel> treeListChildren = getTreeQuestionClassify(questionTypeClassifyModelList, parentIdList, courseId);
        // 将子结构放入父结构数组中
        treeList.addAll(treeListChildren);
        return treeList;
    }



private List<QuestionTypeClassifyModel> getTreeQuestionClassify(List<QuestionTypeClassifyModel> questionTypeClassifyModelList, List<String> parentIdList, String courseId) {

        List<QuestionTypeClassifyModel> treeList = new ArrayList<>();
        HashSet<String> hs = new HashSet<>(parentIdList);
        parentIdList.clear();
        parentIdList.addAll(hs);

        parentIdList.remove(null);
        parentIdList.remove("0");

        if (!CollectionUtils.isEmpty(parentIdList)) {
            List<QuestionTypeClassifyModel> questionClassifyTree = questionClassifyDao.getQuestionClassifyTree(parentIdList, courseId);
            parentIdList.forEach(y->{
                if (!"0".equals(y)) {
                    List<QuestionTypeClassifyModel> listChildren = questionTypeClassifyModelList.stream().filter(a -> y.equals(a.getParentId())).collect(Collectors.toList());
                    // Option容器接收用于避免空指针异常
                    Optional<QuestionTypeClassifyModel> parent = questionClassifyTree.stream().filter(a -> y.equals(a.getKey())).findFirst();
                    parent.orElse(new QuestionTypeClassifyModel()).setChildren(listChildren);
                    treeList.add(parent.orElse(null));
                }
            });

            parentIdList = questionClassifyTree.stream().map(QuestionTypeClassifyModel::getParentId).collect(Collectors.toList());

            parentIdList.remove("0");
            // 递归此方法,出口为所有parentId为0,最上层节点出去
            return getTreeQuestionClassify(treeList, parentIdList, courseId);
        } else {
            // 当父id集合为空时,返回
            return questionTypeClassifyModelList;
        }
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值