完善牧安云哨-后端

This commit is contained in:
BBIT-Kai
2025-12-29 16:30:36 +08:00
parent cd7aa35960
commit b9b8d30ebf
23 changed files with 1074 additions and 41 deletions
+46 -2
View File
@@ -942,11 +942,55 @@ def get_dept_ids_by_user_id(user_id: UUID) -> list:
return dept_ids
def get_dept_id_by_user_id(user_id: UUID) -> list:
# 第一步:通过 user_id 查找其所属的 dept_id
def get_dept_id_by_user_id(user_id: str) -> str:
# 通过 user_id 查找其所属的 dept_id
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT dept_id FROM users WHERE id = %s", (user_id,))
dept_id = cursor.fetchone()
dept_id = dept_id[0]
return str(dept_id)
def get_dept_id_by_iot_user_name(user_id: UUID) -> str:
# 通过 iot_user_id 查找其所属的 dept_id
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT dept_id FROM iot_users WHERE name = %s", (user_id,))
dept_id = cursor.fetchone()
dept_id = dept_id[0]
return dept_id
from typing import List
def get_dept_ids_by_dept_id(dept_id: str) -> List[str]:
"""
获取当前部门 ID 以及其所有父部门 ID(递归向上)
返回顺序:从当前部门一直到最顶层父部门
"""
with pg_pool.getConn() as conn:
with conn.cursor() as cursor:
cursor.execute(
"""
WITH RECURSIVE dept_tree AS (
-- 起点:当前部门
SELECT id, parent_id
FROM sys_dept
WHERE id = %s
UNION ALL
-- 向上递归找父部门
SELECT d.id, d.parent_id
FROM sys_dept d
INNER JOIN dept_tree dt ON d.id = dt.parent_id
)
SELECT id FROM dept_tree;
""",
(dept_id,),
)
rows = cursor.fetchall()
return [str(row[0]) for row in rows]