yaowanxin
3 天以前 b33e6f53dd537abaebad066de12474580fcc33b2
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
package com.ruoyi.warehouse.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.dto.ProductTreeDto;
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.warehouse.dto.DocumentClassificationDto;
import com.ruoyi.warehouse.dto.DocumentClassificationTreeDto;
import com.ruoyi.warehouse.dto.DocumentationDto;
import com.ruoyi.warehouse.mapper.DocumentationMapper;
import com.ruoyi.warehouse.pojo.DocumentClassification;
import com.ruoyi.warehouse.pojo.Documentation;
import com.ruoyi.warehouse.service.DocumentClassificationService;
import com.ruoyi.warehouse.mapper.DocumentClassificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
/**
* @author 86151
* @description 针对表【document_classification(文档分类表)】的数据库操作Service实现
* @createDate 2025-08-15 10:44:23
*/
@Service
public class DocumentClassificationServiceImpl extends ServiceImpl<DocumentClassificationMapper, DocumentClassification>
    implements DocumentClassificationService{
    @Autowired
    private DocumentationMapper documentationMapper;
    @Autowired
    private DocumentClassificationMapper documentClassificationMapper;
    @Override
    public boolean deleteByIds(List<Long> ids) {
        List<DocumentClassification> list = documentClassificationMapper.selectList(new LambdaQueryWrapper<DocumentClassification>().in(DocumentClassification::getId, ids));
        for (DocumentClassification documentClassification : list) {
          //如果每项的父id为空,说明是根节点,需要删除文档信息表中的数据
            List<DocumentationDto> documentationDtos = documentationMapper.listByDocumentClassificationId(documentClassification.getId());
            if (CollectionUtils.isNotEmpty(documentationDtos)){
                throw new RuntimeException("存在文档信息,不能删除");
            }
            documentClassificationMapper.deleteById(documentClassification.getId());
        }
        return true;
    }
 
    @Override
    public List<DocumentClassificationTreeDto> selectDocumentClassificationList() {
        // 查询根节点(parentId 为 null)
        LambdaQueryWrapper<DocumentClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(DocumentClassification::getParentId);
 
        // 查询根节点列表
        List<DocumentClassification> rootList = baseMapper.selectList(queryWrapper);
 
        // 转换为树节点并递归构建子树
        List<DocumentClassificationTreeDto> tree = new ArrayList<>();
        for (DocumentClassification documentClassification : rootList) {
            DocumentClassificationTreeDto node = convertToTreeDto(documentClassification);
            node.setChildren(buildDocumentChildrenNodes(documentClassification.getId()));
            tree.add(node);
        }
        return tree;
    }
    // 递归构建子节点
    private List<DocumentClassificationTreeDto> buildDocumentChildrenNodes(Long parentId) {
        // 查询当前父节点的子节点
        LambdaQueryWrapper<DocumentClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DocumentClassification::getParentId, parentId);
        List<DocumentClassification> childList = baseMapper.selectList(queryWrapper);
 
        // 转换子节点并递归构建它们的子树
        List<DocumentClassificationTreeDto> children = new ArrayList<>();
        for (DocumentClassification child : childList) {
            DocumentClassificationTreeDto childNode = convertToTreeDto(child);
            childNode.setChildren(buildDocumentChildrenNodes(child.getId()));
            children.add(childNode);
        }
 
        return children;
    }
    // 将 DocumentClassification 转换为 DocumentClassificationTreeDto
    private DocumentClassificationTreeDto convertToTreeDto(DocumentClassification documentClassification) {
        DocumentClassificationTreeDto dto = new DocumentClassificationTreeDto();
        BeanUtils.copyProperties(documentClassification, dto);
        dto.setCategory(documentClassification.getCategory()); // 设置 label 为产品名称
        dto.setChildren(new ArrayList<>());
        return dto;
    }
}