crud.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. from typing import Sequence
  3. from app.core.base_crud import CRUDBase
  4. from ..auth.schema import AuthSchema
  5. from .model import DeptModel
  6. from .schema import DeptCreateSchema, DeptUpdateSchema
  7. class DeptCRUD(CRUDBase[DeptModel, DeptCreateSchema, DeptUpdateSchema]):
  8. """部门模块数据层"""
  9. def __init__(self, auth: AuthSchema) -> None:
  10. """初始化部门CRUD"""
  11. self.auth = auth
  12. super().__init__(model=DeptModel, auth=auth)
  13. async def get_by_id_crud(self, id: int, preload: list | None = None) -> DeptModel | None:
  14. """
  15. 根据 id 获取部门信息。
  16. 参数:
  17. - id (int): 部门 ID。
  18. - preload (list | None): 预加载关系,未提供时使用模型默认项
  19. 返回:
  20. - DeptModel | None: 部门信息,未找到返回 None。
  21. """
  22. obj = await self.get(id=id, preload=preload)
  23. if not obj:
  24. return None
  25. return obj
  26. async def get_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[DeptModel]:
  27. """
  28. 获取部门列表。
  29. 参数:
  30. - search (dict | None): 搜索条件。
  31. - order_by (list[dict] | None): 排序字段列表。
  32. - preload (list | None): 预加载关系,未提供时使用模型默认项
  33. 返回:
  34. - Sequence[DeptModel]: 部门列表。
  35. """
  36. return await self.list(search=search, order_by=order_by, preload=preload)
  37. async def get_tree_list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[DeptModel]:
  38. """
  39. 获取部门树形列表。
  40. 参数:
  41. - search (dict | None): 搜索条件。
  42. - order_by (list[dict] | None): 排序字段列表。
  43. - preload (list | None): 预加载关系,未提供时使用模型默认项
  44. 返回:
  45. - Sequence[DeptModel]: 部门树形列表。
  46. """
  47. return await self.tree_list(search=search, order_by=order_by, children_attr='children', preload=preload)
  48. async def set_available_crud(self, ids: list[int], status: str) -> None:
  49. """
  50. 批量设置部门可用状态。
  51. 参数:
  52. - ids (list[int]): 部门 ID 列表。
  53. - status (str): 可用状态。
  54. 返回:
  55. - None
  56. """
  57. await self.set(ids=ids, status=status)
  58. async def get_name_crud(self, id: int) -> str | None:
  59. """
  60. 根据 id 获取部门名称。
  61. 参数:
  62. - id (int): 部门 ID。
  63. 返回:
  64. - str | None: 部门名称,未找到返回 None。
  65. """
  66. obj = await self.get(id=id)
  67. return obj.name if obj else None