huminmin
23 小时以前 271f4a25576ac6c2c8f2c4eb4c9782d6eef85124
src/main/java/com/ruoyi/warehouse/service/impl/DocumentClassificationServiceImpl.java
@@ -3,14 +3,17 @@
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.common.utils.bean.BeanUtils;
import com.ruoyi.warehouse.dto.DocumentClassificationTreeDto;
import com.ruoyi.warehouse.dto.DocumentationDto;
import com.ruoyi.warehouse.mapper.DocumentClassificationMapper;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
@@ -19,20 +22,69 @@
* @createDate 2025-08-15 10:44:23
*/
@Service
public class DocumentClassificationServiceImpl extends ServiceImpl<DocumentClassificationMapper, DocumentClassification>
    implements DocumentClassificationService{
    @Autowired
    private DocumentationMapper documentationMapper;
@RequiredArgsConstructor
public class DocumentClassificationServiceImpl extends ServiceImpl<DocumentClassificationMapper, DocumentClassification> implements DocumentClassificationService{
    private final DocumentationMapper documentationMapper;
    private final 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;
    }
}