Skip to content

Commit

Permalink
resolve: solve some problems
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 committed Jan 20, 2025
1 parent c1c8a98 commit 0b42ba0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 38 deletions.
38 changes: 1 addition & 37 deletions src/bk-user/bkuser/apis/open_v3/views/department.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.
from typing import Any, Dict, List

from drf_yasg.utils import swagger_auto_schema
from rest_framework import generics, status
Expand All @@ -27,7 +26,6 @@
TenantDepartmentRetrieveInputSLZ,
TenantDepartmentRetrieveOutputSLZ,
)
from bkuser.apps.data_source.models import DataSourceDepartment
from bkuser.apps.tenant.models import TenantDepartment
from bkuser.biz.organization import DataSourceDepartmentHandler

Expand Down Expand Up @@ -107,40 +105,6 @@ def get(self, request, *args, **kwargs):
queryset = tenant_depts

# 处理部门信息
dept_info = self._list_dept_infos_with_parent(self.paginate_queryset(queryset))
dept_info = DataSourceDepartmentHandler.list_dept_infos_with_parent(self.paginate_queryset(queryset))

return self.get_paginated_response(TenantDepartmentListOutputSLZ(dept_info, many=True).data)

@staticmethod
def _list_dept_infos_with_parent(tenant_depts: List[TenantDepartment]) -> List[Dict[str, Any]]:
"""
获取部门信息(包含 parent_id)
"""

# 预加载部门对应的租户部门
tenant_dept_id_map = {
(data_source_dept_id, tenant_id): dept_id
for (data_source_dept_id, tenant_id, dept_id) in TenantDepartment.objects.values_list(
"data_source_department_id", "tenant_id", "id"
)
}

# 获取数据源部门 ID
data_source_dept_ids = {dept.data_source_department_id for dept in tenant_depts}

# 预加载部门对应的名称
dept_name_map = dict(
DataSourceDepartment.objects.filter(id__in=data_source_dept_ids).values_list("id", "name")
)

# 组装数据
return [
{
"id": dept.id,
"name": dept_name_map[dept.data_source_department_id],
"parent_id": tenant_dept_id_map.get(
(dept.data_source_department.department_relation.parent_id, dept.tenant_id)
),
}
for dept in tenant_depts
]
38 changes: 37 additions & 1 deletion src/bk-user/bkuser/biz/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
# to the current version of the project delivered to anyone in the future.

import datetime
from typing import List
from typing import Any, Dict, List

from django.db import transaction
from django.utils import timezone

from bkuser.apps.data_source.models import (
DataSourceDepartment,
DataSourceDepartmentRelation,
DataSourceUser,
DataSourceUserDeprecatedPasswordRecord,
LocalDataSourceIdentityInfo,
)
from bkuser.apps.tenant.models import TenantDepartment
from bkuser.common.constants import PERMANENT_TIME
from bkuser.common.hashers import make_password

Expand Down Expand Up @@ -116,3 +118,37 @@ def get_dept_ancestors(dept_id: int) -> List[int]:
return []
# 返回的祖先部门默认以降序排列,从根祖先部门 -> 父部门
return list(relation.get_ancestors().values_list("department_id", flat=True))

@staticmethod
def list_dept_infos_with_parent(tenant_depts: List[TenantDepartment]) -> List[Dict[str, Any]]:
"""
获取部门信息(包含 parent_id)
"""

# 预加载部门对应的租户部门
tenant_dept_id_map = {
(data_source_dept_id, tenant_id): dept_id
for (data_source_dept_id, tenant_id, dept_id) in TenantDepartment.objects.values_list(
"data_source_department_id", "tenant_id", "id"
)
}

# 获取数据源部门 ID
data_source_dept_ids = {dept.data_source_department_id for dept in tenant_depts}

# 预加载部门对应的名称
dept_name_map = dict(
DataSourceDepartment.objects.filter(id__in=data_source_dept_ids).values_list("id", "name")
)

# 组装数据
return [
{
"id": dept.id,
"name": dept_name_map[dept.data_source_department_id],
"parent_id": tenant_dept_id_map.get(
(dept.data_source_department.department_relation.parent_id, dept.tenant_id)
),
}
for dept in tenant_depts
]

0 comments on commit 0b42ba0

Please sign in to comment.