# -*- coding: utf-8 -*-
# @Time : 2022/5/24 22:07
# @Author : wangyinghao
# @Site : 
# @File : api_management_utils.py
# @Software: PyCharm
from automated_main.models.api_automation.api_management import ApiManagement
from django.forms import model_to_dict
from automated_main.models.api_automation.api_interfaces_case import ApiInterfacesCase


class ApiManagementUtils:

    @classmethod
    def get_api_management_tree_recursion(cls, parent):
        res = []
        results = ApiManagement.objects.filter(parent=parent)
        parent_name = ""
        if 0 != parent:
            obj = ApiManagement.objects.get(id=parent)
            parent_name = obj.name
        for s in results:
            tmp = model_to_dict(s)
            tmp['children'] = ApiManagementUtils.get_api_management_tree_recursion(s.id)
            tmp['parent_name'] = parent_name
            res.append(tmp)

        return res

    @classmethod
    def get_interfaces_case_recursion(cls, parent):
        res = []
        results = ApiManagement.objects.filter(parent=parent)
        if 0 != parent:
            obj = ApiManagement.objects.get(id=parent)
            res.append(obj.id)
        for s in results:
            res.append(s.id)
            results_r = ApiManagement.objects.filter(parent=s.id)
            for aa in results_r:
                res.append(aa.id)
        return res
