Files
FastapiAdmin/backend/app/api/v1/module_system/dept/service.py
T
zhangtao 6a5f8cf0dd refactor: 完成项目大规模重构与功能优化
这是一次综合性的项目迭代,包含以下核心变更:
1.  **目录与模块重构**
    - 调整工作流节点类型模块目录结构,迁移节点类型相关代码
    - 重命名platform模块为system模块,更新插件配置信息
    - 重构代码生成模块导入路径
2.  **数据库与CRUD优化**
    - 统一所有CRUD类构造函数,新增数据库会话参数
    - 修复权限过滤器数据库会话使用问题
    - 更新模板生成器的CRUD代码模板
3.  **认证与安全改进**
    - 重构JWT密钥配置,移除默认密钥强制要求环境变量
    - 重命名密码工具类,统一密码加密校验逻辑
    - 优化OAuth认证流程,修复匿名认证使用问题
4.  **前端与静态资源**
    - 重构前端挂载逻辑,增加目录存在性校验
    - 使用标准StaticFiles替换自定义前端挂载实现
5.  **工具类与依赖更新**
    - 修复导入工具的表名重复检测逻辑
    - 优化限流回调代码,移除冗余依赖
    - 更新用户、租户等模块的响应模型字段
6.  **数据与配置修正**
    - 修复系统版本数据字段命名不统一问题
    - 简化枚举类校验逻辑,移除冗余注释
    - 修复测试用例中的密码工具类导入错误
2026-07-11 13:03:28 +08:00

118 lines
4.5 KiB
Python

from sqlalchemy.ext.asyncio import AsyncSession
from app.core.base_schema import AuthSchema, BatchSetAvailable
from app.core.exceptions import CustomException
from app.utils.common_util import (
get_child_id_map,
get_child_recursion,
get_parent_id_map,
get_parent_recursion,
)
from .crud import DeptCRUD
from .schema import (
DeptCreateSchema,
DeptOutSchema,
DeptQueryParam,
DeptTreeOutSchema,
DeptUpdateSchema,
)
class DeptService:
"""部门管理服务
提供部门 CRUD、树形结构查询、级联启/禁用、租户配额检查等业务能力。
"""
def __init__(self, auth: AuthSchema, db: AsyncSession) -> None:
self.auth = auth
self.db = db
async def detail(self, id: int) -> DeptOutSchema:
dept = await DeptCRUD(self.auth, self.db).get_or_404(id=id)
dept_out = DeptOutSchema.model_validate(dept)
if dept.parent_id:
parent = await DeptCRUD(self.auth, self.db).get(id=dept.parent_id)
if parent:
dept_out.parent_name = parent.name
return dept_out
async def tree(
self,
search: DeptQueryParam | None = None,
order_by: list[dict] | None = None,
) -> list[dict]:
dept_list = await DeptCRUD(self.auth, self.db).tree_list(search=vars(search) if search else None, order_by=order_by)
dept_dict_list = [DeptTreeOutSchema.model_validate(dept).model_dump() for dept in dept_list]
return [d for d in dept_dict_list if d.get("parent_id") is None]
async def create(self, data: DeptCreateSchema) -> DeptOutSchema:
dept = await DeptCRUD(self.auth, self.db).get(name=data.name)
if dept:
raise CustomException(msg="创建失败,该数据已存在")
obj = await DeptCRUD(self.auth, self.db).get(code=data.code)
if obj:
raise CustomException(msg="创建失败,编码已存在")
# 检查租户配额
from app.api.v1.module_platform.tenant.service import TenantService
user = self.auth.user
if not user:
raise CustomException(msg="未登录")
await TenantService(self.auth, self.db).check_quota(user.tenant_id, "dept")
dept = await DeptCRUD(self.auth, self.db).create(data=data)
return DeptOutSchema.model_validate(dept)
async def update(self, id: int, data: DeptUpdateSchema) -> DeptOutSchema:
dept = await DeptCRUD(self.auth, self.db).get_or_404(id=id, msg="更新失败,该数据不存在")
exist_dept = await DeptCRUD(self.auth, self.db).get(name=data.name)
if exist_dept and exist_dept.id != id:
raise CustomException(msg="更新失败,名称已存在")
exist_code = await DeptCRUD(self.auth, self.db).get(code=data.code)
if exist_code and exist_code.id != id:
raise CustomException(msg="更新失败,编码已存在")
dept = await DeptCRUD(self.auth, self.db).update(id=id, data=data)
dept_out = DeptOutSchema.model_validate(dept)
if dept_out.parent_id:
parent = await DeptCRUD(self.auth, self.db).get(id=dept_out.parent_id)
if parent:
dept_out.parent_name = parent.name
return dept_out
async def delete(self, ids: list[int]) -> None:
if len(ids) < 1:
raise CustomException(msg="删除失败,删除对象不能为空")
# 获取所有部门列表,用于构建树形关系
all_depts = await DeptCRUD(self.auth, self.db).get_list()
# 构建子部门ID映射
child_id_map = get_child_id_map(model_list=all_depts)
for pid in ids:
if child_id_map.get(pid):
raise CustomException(msg="存在子部门,不允许删除父部门")
await DeptCRUD(self.auth, self.db).delete(ids=ids)
async def batch_set_available(self, data: BatchSetAvailable) -> None:
dept_list = await DeptCRUD(self.auth, self.db).get_list()
total_ids = []
if data.status == 0:
id_map = get_parent_id_map(model_list=dept_list)
for dept_id in data.ids:
enable_ids = get_parent_recursion(id=dept_id, id_map=id_map)
total_ids.extend(enable_ids)
else:
id_map = get_child_id_map(model_list=dept_list)
for dept_id in data.ids:
disable_ids = get_child_recursion(id=dept_id, id_map=id_map)
total_ids.extend(disable_ids)
await DeptCRUD(self.auth, self.db).set(ids=total_ids, status=data.status)