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);
|
}
|
}
|