package org.jeecg.modules.stm.audit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.stm.audit.entity.AuditFileContentCategories;
import org.jeecg.modules.stm.audit.mapper.AuditFileContentCategoriesMapper;
import org.jeecg.modules.stm.audit.service.IAuditFileContentCategoriesService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 条目内容分类
 * @Author: jeecg-boot
 * @Date:   2024-10-15
 * @Version: V1.0
 */
@Service
public class AuditFileContentCategoriesServiceImpl extends ServiceImpl<AuditFileContentCategoriesMapper, AuditFileContentCategories> implements IAuditFileContentCategoriesService {
    @Override
    public Map<Integer,String> queryAllDataByFileId(Integer fileId){
        QueryWrapper<AuditFileContentCategories> queryWrapper = new QueryWrapper<>();
        if(fileId!=null) {
            queryWrapper.eq("file_id", fileId);
        }
        List<AuditFileContentCategories> listData = this.list(queryWrapper);
        Map<Integer, String> resultMap = new HashMap<>();
        Map<Integer, AuditFileContentCategories> nodeMap = new HashMap<>();

        if(listData==null ||listData.size()==0) {
            return resultMap;
        }

        for (AuditFileContentCategories categorie : listData) {
            nodeMap.put(categorie.getId(), categorie);
        }
        for (AuditFileContentCategories categorie : listData) {
            StringBuilder path = new StringBuilder();
            AuditFileContentCategories currentNode = categorie;
            while ( currentNode!= null) {
                path.insert(0, currentNode.getName());
                if(currentNode.getParentId()!=null&&!currentNode.getParentId().equals("")) {
                    currentNode = nodeMap.get(currentNode.getParentId());
                    if (currentNode != null) {
                        path.insert(0, "/");
                    } else {
                        break;
                    }
                } else {
                    currentNode = null;
                    break;
                }
            }
            resultMap.put(categorie.getId(), path.toString());
        }

        return resultMap;
    }

    @Override
    public String queryFullPathNodeId(AuditFileContentCategories currentNode, Integer fileId) {
        QueryWrapper<AuditFileContentCategories> queryWrapper = new QueryWrapper<>();
        if (fileId != null) {
            queryWrapper.eq("file_id", fileId);
        }
        List<AuditFileContentCategories> listData = this.list(queryWrapper);
        if (listData == null || listData.isEmpty()) {
            return "";
        }
        Map<Integer, AuditFileContentCategories> nodeMap = listData.stream()
                .collect(Collectors.toMap(AuditFileContentCategories::getId, categorie -> categorie));

        List path = new ArrayList();
        while (currentNode != null) {
            path.add(0,currentNode.getName());
            Integer parentId = currentNode.getParentId();
            currentNode = (parentId != null && !parentId.equals("")) ? nodeMap.get(parentId) : null;
        }

        return String.join("/", path);
    }
}
