crud.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 MenuModel
  6. from .schema import MenuCreateSchema, MenuUpdateSchema
  7. class MenuCRUD(CRUDBase[MenuModel, MenuCreateSchema, MenuUpdateSchema]):
  8. """菜单模块数据层"""
  9. def __init__(self, auth: AuthSchema) -> None:
  10. """初始化菜单CRUD"""
  11. self.auth = auth
  12. super().__init__(model=MenuModel, auth=auth)
  13. async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> MenuModel | None:
  14. """
  15. 根据 id 获取菜单信息。
  16. 参数:
  17. - id (int): 菜单 ID。
  18. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  19. 返回:
  20. - MenuModel | 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[str] | None = None) -> Sequence[MenuModel]:
  27. """
  28. 获取菜单列表。
  29. 参数:
  30. - search (dict | None): 搜索条件。
  31. - order_by (list[dict] | None): 排序字段列表。
  32. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  33. 返回:
  34. - Sequence[MenuModel]: 菜单列表。
  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[str] | None = None) -> Sequence[MenuModel]:
  38. """
  39. 获取菜单树形列表。
  40. 参数:
  41. - search (dict | None): 搜索条件。
  42. - order_by (list[dict] | None): 排序字段列表。
  43. - preload (list[str] | None): 预加载关系,未提供时使用模型默认项
  44. 返回:
  45. - Sequence[MenuModel]: 菜单树形列表。
  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)