package com.ruoyi.utils;

import com.ruoyi.asset.mapper.TbLocationMapper;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RecursionUtil {
    public List<String> getNumberWithChildren(TbLocationMapper locationMapper, String number) {
        Set<String> result = new HashSet<>();
        Set<String> visited = new HashSet<>(); // 用于跟踪已访问过的节点

        processNumberWithChildren(locationMapper, number, result, visited); // 调用辅助方法处理节点及其子节点

        return new ArrayList<>(result);
    }

    private void processNumberWithChildren(TbLocationMapper locationMapper, String number, Set<String> result, Set<String> visited) {
        if (!visited.contains(number)) { // 避免重复处理节点
            visited.add(number);
            List<String> children = locationMapper.selectNumberByFather(number); // 调用mapper接口方法查询直接子节点

            // 递归处理每个子节点
            for (String child : children) {
                result.add(child);
                processNumberWithChildren(locationMapper, child, result, visited);
            }
        }
    }
}
