crud.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from collections.abc import Sequence
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.base_crud import CRUDBase
  4. from .model import MenuModel
  5. from .schema import MenuCreateSchema, MenuUpdateSchema
  6. class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
  7. """菜单模块数据层"""
  8. def __init__(self, auth: AuthSchema) -> None:
  9. """
  10. 初始化菜单数据层。
  11. 参数:
  12. - auth (AuthSchema): 认证信息模型(含 DB 会话等上下文)。
  13. 返回:
  14. - None
  15. """
  16. self.auth = auth
  17. super().__init__(model=MenuModel, auth=auth)
  18. async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> MenuModel | None:
  19. """
  20. 根据 id 获取菜单信息。
  21. 参数:
  22. - id (int): 菜单 ID。
  23. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  24. 返回:
  25. - MenuModel | None: 菜单信息,未找到返回 None。
  26. """
  27. obj = await self.get(id=id, preload=preload)
  28. if not obj:
  29. return None
  30. return obj
  31. async def get_list_crud(
  32. self,
  33. search: dict | None = None,
  34. order_by: list[dict] | None = None,
  35. preload: list[str] | None = None,
  36. ) -> Sequence[MenuModel]:
  37. """
  38. 获取菜单列表。
  39. 参数:
  40. - search (dict | None): 搜索条件。
  41. - order_by (list[dict] | None): 排序字段列表。
  42. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  43. 返回:
  44. - Sequence[MenuModel]: 菜单列表。
  45. """
  46. return await self.list(search=search, order_by=order_by, preload=preload)
  47. async def get_tree_list_crud(
  48. self,
  49. search: dict | None = None,
  50. order_by: list[dict] | None = None,
  51. preload: list[str] | None = None,
  52. ) -> Sequence[MenuModel]:
  53. """
  54. 获取菜单树形列表。
  55. 参数:
  56. - search (dict | None): 搜索条件。
  57. - order_by (list[dict] | None): 排序字段列表。
  58. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  59. 返回:
  60. - Sequence[MenuModel]: 菜单树形列表。
  61. """
  62. return await self.tree_list(
  63. search=search,
  64. order_by=order_by,
  65. children_attr="children",
  66. preload=preload,
  67. )
  68. async def set_available_crud(self, ids: list[int], status: str) -> None:
  69. """
  70. 批量设置菜单可用状态。
  71. 参数:
  72. - ids (list[int]): 菜单 ID 列表。
  73. - status (str): 可用状态。
  74. 返回:
  75. - None
  76. """
  77. await self.set(ids=ids, status=status)