crud.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from collections.abc import Sequence
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.api.v1.module_system.dept.crud import DeptCRUD
  4. from app.api.v1.module_system.menu.crud import MenuCRUD
  5. from app.core.base_crud import CRUDBase
  6. from .model import RoleModel
  7. from .schema import RoleCreateSchema, RoleUpdateSchema
  8. class RoleCRUD(CRUDBase[RoleModel, RoleCreateSchema, RoleUpdateSchema]):
  9. """角色模块数据层"""
  10. def __init__(self, auth: AuthSchema) -> None:
  11. """
  12. 初始化角色数据层。
  13. 参数:
  14. - auth (AuthSchema): 认证信息模型(含 DB 会话等上下文)。
  15. 返回:
  16. - None
  17. """
  18. self.auth = auth
  19. super().__init__(model=RoleModel, auth=auth)
  20. async def get_by_id_crud(self, id: int, preload: list | None = None) -> RoleModel | None:
  21. """
  22. 根据id获取角色信息
  23. 参数:
  24. - id (int): 角色ID
  25. - preload (list | None): 预加载选项
  26. 返回:
  27. - RoleModel | None: 角色模型对象
  28. """
  29. return await self.get(id=id, preload=preload)
  30. async def get_list_crud(
  31. self,
  32. search: dict | None = None,
  33. order_by: list | None = None,
  34. preload: list | None = None,
  35. ) -> Sequence[RoleModel]:
  36. """
  37. 获取角色列表
  38. 参数:
  39. - search (dict | None): 查询参数
  40. - order_by (list | None): 排序参数
  41. - preload (list | None): 预加载选项
  42. 返回:
  43. - Sequence[RoleModel]: 角色模型对象列表
  44. """
  45. return await self.list(search=search, order_by=order_by, preload=preload)
  46. async def set_role_menus_crud(self, role_ids: list[int], menu_ids: list[int]) -> None:
  47. """
  48. 设置角色的菜单权限
  49. 参数:
  50. - role_ids (list[int]): 角色ID列表
  51. - menu_ids (list[int]): 菜单ID列表
  52. 返回:
  53. - None
  54. """
  55. roles = await self.list(search={"id": ("in", role_ids)})
  56. # 前端未勾选时 menu_ids 可能为空:此时应清空关联,而不是触发全量查询
  57. menus = (
  58. []
  59. if not menu_ids
  60. else await MenuCRUD(self.auth).get_list_crud(search={"id": ("in", menu_ids)})
  61. )
  62. for obj in roles:
  63. relationship = obj.menus
  64. relationship.clear()
  65. relationship.extend(menus)
  66. await self.auth.db.flush()
  67. async def set_role_data_scope_crud(self, role_ids: list[int], data_scope: int) -> None:
  68. """
  69. 设置角色的数据范围
  70. 参数:
  71. - role_ids (list[int]): 角色ID列表
  72. - data_scope (int): 数据范围
  73. 返回:
  74. - None
  75. """
  76. await self.set(ids=role_ids, data_scope=data_scope)
  77. async def set_role_depts_crud(self, role_ids: list[int], dept_ids: list[int]) -> None:
  78. """
  79. 设置角色的部门权限
  80. 参数:
  81. - role_ids (list[int]): 角色ID列表
  82. - dept_ids (list[int]): 部门ID列表
  83. 返回:
  84. - None
  85. """
  86. roles = await self.list(search={"id": ("in", role_ids)})
  87. # 前端未勾选时 dept_ids 可能为空:此时应清空关联,而不是触发全量查询
  88. depts = (
  89. []
  90. if not dept_ids
  91. else await DeptCRUD(self.auth).get_list_crud(search={"id": ("in", dept_ids)})
  92. )
  93. for obj in roles:
  94. relationship = obj.depts
  95. relationship.clear()
  96. relationship.extend(depts)
  97. await self.auth.db.flush()
  98. async def set_available_crud(self, ids: list[int], status: str) -> None:
  99. """
  100. 设置角色的可用状态
  101. 参数:
  102. - ids (list[int]): 角色ID列表
  103. - status (str): 可用状态
  104. 返回:
  105. - None
  106. """
  107. await self.set(ids=ids, status=status)