Files
FastapiAdmin/backend/app/api/v1/module_system/dept/service.py
T
zhangtao 0ce31936aa chore: 清理冗余代码与配置,优化项目结构
1.  删除无用文件与废弃代码:移除locale枚举、element-plus插件、sse路由、api token模块等
2.  简化类型导入与依赖:移除大量未使用的类型导入,统一echarts导入方式
3.  优化配置与样式:调整gitignore、样式引入顺序,新增列表动画样式
4.  修复接口与模型:修正接口返回类型、查询参数配置,更新部门模型字段
5.  优化性能与体验:添加图片懒加载,优化加载逻辑与表格渲染
6.  调整环境配置:新增并更新开发/生产环境配置文件
2026-07-23 20:49:52 +08:00

106 lines
3.9 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,
search_to_dict,
traversal_to_tree,
)
from .crud import DeptCRUD
from .schema import (
DeptCreateSchema,
DeptOutSchema,
DeptQueryParam,
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).get_list(search=search_to_dict(search), order_by=order_by)
dept_dict_list = [DeptOutSchema.model_validate(dept).model_dump() for dept in dept_list]
return traversal_to_tree(dept_dict_list)
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="创建失败,编码已存在")
dept = await DeptCRUD(self.auth, self.db).create(data=data)
return await self.detail(id=dept.id)
async def update(self, id: int, data: DeptUpdateSchema) -> DeptOutSchema:
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="更新失败,编码已存在")
await DeptCRUD(self.auth, self.db).update(id=id, data=data)
return await self.detail(id=id)
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)