mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-27 14:52:56 +00:00
- 将 ApplicationQueryParams 改为 ApplicationQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(demo): 重命名查询参数类以统一命名风格 - 将 DemoQueryParams 改为 DemoQueryParam - 同步更新相关导入和函数参数类型注解 - 修改 PaginationQueryParams 为 PaginationQueryParam refactor(gencode): 优化代码生成模块的模型和服务层结构 - 统一模型名称后缀为 Schema,调整相关引用 - 规范 Pydantic schema 的命名和定义 - 删除无用的 Python DAO 模板文件 - 调整导入路径,统一使用 app 目录下的模块路径 - 改进服务层方法签名,添加返回类型注解 - 使用自定义异常 CustomException 替代旧异常 - 统一成功响应格式为 SuccessResponse - 优化代码生成服务中的数据库操作 DAO 调用参数传递 - 优化代码生成业务表和字段模型的字段定义,添加注释和默认值 - 优化生成代码路径处理逻辑和异常信息提示 - 整合分页查询参数定义,统一分页模型 - 修正多个服务方法的参数类型和返回类型 - 删除无用的导入和多余注释,提升代码整洁度
137 lines
4.8 KiB
Python
137 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from typing import List, Dict
|
|
|
|
from app.core.base_schema import BatchSetAvailable
|
|
from app.core.exceptions import CustomException
|
|
from app.utils.common_util import (
|
|
get_parent_id_map,
|
|
get_parent_recursion,
|
|
get_child_id_map,
|
|
get_child_recursion,
|
|
traversal_to_tree
|
|
)
|
|
from ..auth.schema import AuthSchema
|
|
from .crud import DeptCRUD
|
|
from .param import DeptQueryParam
|
|
from .schema import (
|
|
DeptCreateSchema,
|
|
DeptUpdateSchema,
|
|
DeptOutSchema
|
|
)
|
|
|
|
|
|
class DeptService:
|
|
"""
|
|
部门管理模块服务层
|
|
"""
|
|
|
|
@classmethod
|
|
async def get_dept_detail_service(cls, auth: AuthSchema, id: int) -> Dict:
|
|
"""
|
|
获取部门详情service
|
|
|
|
:param auth: 认证对象
|
|
:param id: 部门ID
|
|
:return: 部门详情对象
|
|
"""
|
|
dept = await DeptCRUD(auth).get_by_id_crud(id=id)
|
|
return DeptOutSchema.model_validate(dept).model_dump()
|
|
|
|
@classmethod
|
|
async def get_dept_tree_service(cls, auth: AuthSchema, search: DeptQueryParam, order_by: List[Dict] = None) -> List[Dict]:
|
|
"""
|
|
获取部门树形列表service
|
|
|
|
:param auth: 认证对象
|
|
:param search: 查询参数对象
|
|
:param order_by: 排序参数
|
|
:return: 部门树形列表对象
|
|
"""
|
|
if order_by:
|
|
order_by = eval(order_by)
|
|
else:
|
|
order_by = [{"order": "asc"}]
|
|
# 使用树形结构查询,预加载children关系
|
|
dept_list = await DeptCRUD(auth).get_tree_list_crud(search=search.__dict__, order_by=order_by)
|
|
# 转换为字典列表
|
|
dept_dict_list = [DeptOutSchema.model_validate(dept).model_dump() for dept in dept_list]
|
|
# 使用traversal_to_tree构建树形结构
|
|
return traversal_to_tree(dept_dict_list)
|
|
|
|
@classmethod
|
|
async def create_dept_service(cls, auth: AuthSchema, data: DeptCreateSchema) -> Dict:
|
|
"""
|
|
创建部门service
|
|
|
|
:param auth: 认证对象
|
|
:param data: 部门创建对象
|
|
:return: 新创建的部门对象
|
|
"""
|
|
dept = await DeptCRUD(auth).get(name=data.name)
|
|
if dept:
|
|
raise CustomException(msg='创建失败,该部门已存在')
|
|
dept = await DeptCRUD(auth).create(data=data)
|
|
return DeptOutSchema.model_validate(dept).model_dump()
|
|
|
|
@classmethod
|
|
async def update_dept_service(cls, auth: AuthSchema, id:int, data: DeptUpdateSchema) -> Dict:
|
|
"""
|
|
更新部门service
|
|
|
|
:param auth: 认证对象
|
|
:param data: 部门更新对象
|
|
:return: 更新后的部门对象
|
|
"""
|
|
dept = await DeptCRUD(auth).get_by_id_crud(id=id)
|
|
if not dept:
|
|
raise CustomException(msg='更新失败,该部门不存在')
|
|
exist_dept = await DeptCRUD(auth).get(name=data.name)
|
|
if exist_dept and exist_dept.id != id:
|
|
raise CustomException(msg='更新失败,部门名称重复')
|
|
dept = await DeptCRUD(auth).update(id=id, data=data)
|
|
if data.status:
|
|
await cls.batch_set_available_service(auth=auth, data=BatchSetAvailable(ids=[id], status=True))
|
|
else:
|
|
await cls.batch_set_available_service(auth=auth, data=BatchSetAvailable(ids=[id], status=False))
|
|
return DeptOutSchema.model_validate(dept).model_dump()
|
|
|
|
@classmethod
|
|
async def delete_dept_service(cls, auth: AuthSchema, ids: list[int]) -> None:
|
|
"""
|
|
删除部门service
|
|
|
|
:param auth: 认证对象
|
|
:param id: 部门ID
|
|
"""
|
|
if len(ids) < 1:
|
|
raise CustomException(msg='删除失败,删除对象不能为空')
|
|
for id in ids:
|
|
dept = await DeptCRUD(auth).get_by_id_crud(id=id)
|
|
if not dept:
|
|
raise CustomException(msg='删除失败,该部门不存在')
|
|
await DeptCRUD(auth).delete(ids=ids)
|
|
|
|
@classmethod
|
|
async def batch_set_available_service(cls, auth: AuthSchema, data: BatchSetAvailable) -> None:
|
|
"""
|
|
批量设置部门可用状态service
|
|
|
|
:param auth: 认证对象
|
|
:param data: 批量设置可用状态对象
|
|
"""
|
|
dept_list = await DeptCRUD(auth).get_list_crud()
|
|
total_ids = []
|
|
|
|
if data.status:
|
|
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(auth).set_available_crud(ids=total_ids, status=data.status) |