lin
2024-02-27 3501ca8469b1a09f22cf0d3dbbe1adf331ea814a
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.label.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springblade.common.node.TreeIntegerNode;
import org.springblade.common.utils.NodeTreeUtil;
import org.springblade.modules.label.entity.LabelEntity;
import org.springblade.modules.label.mapper.LabelMapper;
import org.springblade.modules.label.service.ILabelService;
import org.springblade.modules.label.vo.LabelVO;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 标签管理 服务实现类
 *
 * @author BladeX
 * @since 2023-10-28
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper, LabelEntity> implements ILabelService {
 
    @Override
    public IPage<LabelVO> selectLabelPage(IPage<LabelVO> page, LabelVO label) {
        return page.setRecords(baseMapper.selectLabelPage(page, label));
    }
 
    /**
     * 标签查询,按父id查询下级
     *
     * @param label
     * @return
     */
    @Override
    public Object getLabelList(LabelVO label) {
        Map<Integer, TreeIntegerNode> labelList = baseMapper.getLabelList(label);
        List<Integer> list = new ArrayList<>();
        // 遍历
        labelList.forEach((id, treeNode) -> {
            if (treeNode.getHasChildren()) {
                list.add(id);
            }
        });
        if (list.size() > 0) {
            // 查询子集
            Map<Integer, TreeIntegerNode> childrenLabelList = baseMapper.getChildrenLabelList(list);
            // 合并集合
            labelList.putAll(childrenLabelList);
        }
        // 处理并返回
        return NodeTreeUtil.getNodeTree(labelList);
    }
 
    @Override
    public List<TreeIntegerNode> tree(LabelVO label) {
        Map<Integer, TreeIntegerNode> labelTreeList = baseMapper.getLabelTreeList(label);
        List<TreeIntegerNode> nodeTree = NodeTreeUtil.getNodeTree(labelTreeList);
        nodeTree.forEach(node -> recursion(node));
        return nodeTree;
    }
 
    private void recursion(TreeIntegerNode node) {
        if (node.getChildren() != null && node.getChildren().size() > 0) {
//            BigDecimal bigDecimal = BigDecimal.valueOf(0);
//            node.setCount(node.getChildren().stream().map(item -> {
//                bigDecimal.add(BigDecimal.valueOf(item.getCount()));
//            }));
            node.getChildren().forEach(node2 -> recursion(node2));
        } else {
            node.setChildren(null);
        }
    }
}