liding
2 天以前 2da4c045299aad5898dea78d7c9371491ce2c155
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
package com.ruoyi.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.dto.TreeDto;
import com.ruoyi.business.entity.Tree;
import com.ruoyi.business.mapper.TreeMapper;
import com.ruoyi.business.service.TreeService;
import com.ruoyi.business.vo.TreeVo;
import com.ruoyi.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * <p>
 * 档案信息表,记录系统中各类档案的基本信息 服务实现类
 * </p>
 *
 * @author ruoyi
 * @since 2025-06-10
 */
@Service
@RequiredArgsConstructor
public class TreeServiceImpl extends ServiceImpl<TreeMapper, Tree> implements TreeService {
 
    private final TreeMapper treeMapper;
 
    @Override
    public List<TreeVo> selectTreeList(TreeDto treeDto) {
        // 查询根节点(parentId 为 null)
        LambdaQueryWrapper<Tree> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Tree::getParentId);
 
        // 如果有产品名称条件,添加到查询中
        if (treeDto.getName() != null && !treeDto.getName().isEmpty()) {
            queryWrapper.like(Tree::getName, treeDto.getName());
        }
 
        // 查询根节点列表
        List<Tree> rootTrees = treeMapper.selectList(queryWrapper);
 
        // 转换为树节点并递归构建子树
        List<TreeVo> tree = new ArrayList<>();
        for (Tree tr : rootTrees) {
            TreeVo node = convertToTreeDto(tr);
            node.setChildren(buildChildrenNodes(tr.getId()));
            tree.add(node);
        }
        return tree;
    }
 
    private TreeVo convertToTreeDto(Tree tree) {
        TreeVo dto = new TreeVo();
        BeanUtils.copyProperties(tree, dto);
        dto.setLabel(tree.getName());
        dto.setChildren(new ArrayList<>());
        return dto;
    }
 
    // 递归构建子节点
    private List<TreeVo> buildChildrenNodes(Long parentId) {
        // 查询当前父节点的子节点
        LambdaQueryWrapper<Tree> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tree::getParentId, parentId);
        List<Tree> childProducts = treeMapper.selectList(queryWrapper);
 
        // 转换子节点并递归构建它们的子树
        List<TreeVo> children = new ArrayList<>();
        for (Tree child : childProducts) {
            TreeVo childNode = convertToTreeDto(child);
            childNode.setChildren(buildChildrenNodes(child.getId()));
            children.add(childNode);
        }
 
        return children;
    }
 
    @Override
    public int addOrEditProduction(TreeDto treeDto) {
        if (treeDto.getId() == null) {
            // 新增tree逻辑
            if (treeDto.getParentId() == null) {
                // 若未指定父节点,默认为根节点(parentId 设为 null)
                treeDto.setParentId(null);
            } else {
                // 检查父节点是否存在(可选,根据业务需求)
                Tree parent = treeMapper.selectById(treeDto.getParentId());
                if (parent == null) {
                    throw new IllegalArgumentException("父节点不存在,无法添加子tree");
                }
            }
            return treeMapper.insert(treeDto);
        } else {
            // 编辑tree逻辑
            Tree existingProduct = treeMapper.selectById(treeDto.getId());
            if (existingProduct == null) {
                throw new IllegalArgumentException("要编辑的tree不存在");
            }
            return treeMapper.updateById(treeDto);
        }
    }
 
    @Override
    public int delByIds(Long[] ids) {
        // 检查参数
        if (ids == null || ids.length == 0) {
            return 0;
        }
        // 构造更新条件
        UpdateWrapper<Tree> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids)
                .set("deleted", 1);  // 设置 deleted 为 1 表示已删除
        // 执行批量逻辑删除
        return treeMapper.update(null, updateWrapper);
    }
}