Files
FastapiAdmin/backend/app/api/v1/module_system/dept/service.py
T
zhangtao cf88ab8897 refactor: 整合仪表盘功能到监控模块,清理冗余代码
- 移除原监控仪表盘独立模块,将相关功能合并到在线监控模块
- 重构租户配置字段名,统一使用logo_url和name替代tenant_logo/tenant_name
- 优化搜索工具函数,移除重复导入
- 调整参数配置模型字段长度限制,移除config_value的max_length约束
- 清理冗余的常量定义和导入语句
- 修复批量状态设置接口的redis依赖注入
- 增强OAuth登录安全性,添加租户默认归属和state一次性消费
- 优化资源目录缓存逻辑,减少重复计算
- 新增API Token模块基础框架
- 完善用户token版本管理,支持主动失效JWT
- 调整AI模型配置缓存过期时间
- 修复菜单类型字段索引,提升查询性能
- 简化前端刷新token调用逻辑
- 新增滑块验证完成接口和忘记密码验证码校验
- 调整系统配置默认值,添加操作日志保留天数和接口白名单配置
- 限制Mock支付回调仅在开发环境可用
- 重构websocket认证方式,支持更安全的subprotocol传参
2026-07-13 01:14:20 +08:00

118 lines
4.5 KiB
Python

from sqlalchemy.ext.asyncio import AsyncSession
from app.api.v1.module_platform.tenant.service import TenantService
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,
search_to_dict,
)
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=search_to_dict(search), 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="创建失败,编码已存在")
# 检查租户配额
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 not ids:
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)