yaowanxin
6 天以前 09e1c1155f4db6dc7a829e515337aa88ac4db81c
src/main/java/com/ruoyi/warehouse/service/impl/DocumentClassificationServiceImpl.java
@@ -3,6 +3,12 @@
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;
@@ -11,7 +17,9 @@
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
@@ -23,16 +31,65 @@
    implements DocumentClassificationService{
    @Autowired
    private DocumentationMapper documentationMapper;
    @Autowired
    private DocumentClassificationMapper documentClassificationMapper;
    @Override
    public boolean deleteByIds(List<Long> ids) {
        List<Documentation> documentations = documentationMapper.selectList(new LambdaQueryWrapper<Documentation>().in(Documentation::getDocumentClassificationId, ids));
        if(!CollectionUtils.isEmpty(documentations)){
            return false;
        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());
        }
        baseMapper.deleteBatchIds(ids);
        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;
    }
}