crud.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # -*- coding: utf-8 -*-
  2. from typing import Sequence, Any
  3. from datetime import datetime
  4. from app.core.base_crud import CRUDBase
  5. from app.api.v1.module_system.auth.schema import AuthSchema
  6. from .model import UserModel
  7. from .schema import UserCreateSchema, UserForgetPasswordSchema, UserUpdateSchema
  8. from ..role.crud import RoleCRUD
  9. from ..position.crud import PositionCRUD
  10. class UserCRUD(CRUDBase[UserModel, UserCreateSchema, UserUpdateSchema]):
  11. """用户模块数据层"""
  12. def __init__(self, auth: AuthSchema) -> None:
  13. """
  14. 初始化用户CRUD
  15. 参数:
  16. - auth (AuthSchema): 认证信息模型
  17. """
  18. self.auth = auth
  19. super().__init__(model=UserModel, auth=auth)
  20. async def get_by_id_crud(self, id: int, preload: list[str | Any] | None = None) -> UserModel | None:
  21. """
  22. 根据id获取用户信息
  23. 参数:
  24. - id (int): 用户ID
  25. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  26. 返回:
  27. - UserModel | None: 用户信息,如果不存在则为None
  28. """
  29. return await self.get(
  30. preload=preload,
  31. id=id,
  32. )
  33. async def get_by_username_crud(self, username: str, preload: list[str | Any] | None = None) -> UserModel | None:
  34. """
  35. 根据用户名获取用户信息
  36. 参数:
  37. - username (str): 用户名
  38. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  39. 返回:
  40. - UserModel | None: 用户信息,如果不存在则为None
  41. """
  42. return await self.get(
  43. preload=preload,
  44. username=username,
  45. )
  46. async def get_by_mobile_crud(self, mobile: str, preload: list[str | Any] | None = None) -> UserModel | None:
  47. """
  48. 根据手机号获取用户信息
  49. 参数:
  50. - mobile (str): 手机号
  51. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  52. 返回:
  53. - UserModel | None: 用户信息,如果不存在则为None
  54. """
  55. return await self.get(
  56. preload=preload,
  57. mobile=mobile,
  58. )
  59. async def get_list_crud(self, search: dict | None = None, order_by: list[dict[str, str]] | None = None, preload: list[str | Any] | None = None) -> Sequence[UserModel]:
  60. """
  61. 获取用户列表
  62. 参数:
  63. - search (dict | None): 查询参数对象。
  64. - order_by (list[dict[str, str]] | None): 排序参数列表。
  65. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  66. 返回:
  67. - Sequence[UserModel]: 用户列表
  68. """
  69. return await self.list(
  70. search=search,
  71. order_by=order_by,
  72. preload=preload,
  73. )
  74. async def update_last_login_crud(self, id: int) -> UserModel | None:
  75. """
  76. 更新用户最后登录时间
  77. 参数:
  78. - id (int): 用户ID
  79. 返回:
  80. - UserModel | None: 更新后的用户信息
  81. """
  82. return await self.update(id=id, data={"last_login": datetime.now()})
  83. async def set_available_crud(self, ids: list[int], status: str) -> None:
  84. """
  85. 批量设置用户可用状态
  86. 参数:
  87. - ids (list[int]): 用户ID列表
  88. - status (bool): 可用状态
  89. 返回:
  90. - None:
  91. """
  92. await self.set(ids=ids, status=status)
  93. async def set_user_roles_crud(self, user_ids: list[int], role_ids: list[int]) -> None:
  94. """
  95. 批量设置用户角色
  96. 参数:
  97. - user_ids (list[int]): 用户ID列表
  98. - role_ids (list[int]): 角色ID列表
  99. 返回:
  100. - None:
  101. """
  102. user_objs = await self.list(search={"id": ("in", user_ids)})
  103. if role_ids:
  104. role_objs = await RoleCRUD(self.auth).get_list_crud(search={"id": ("in", role_ids)})
  105. else:
  106. role_objs = []
  107. for obj in user_objs:
  108. relationship = obj.roles
  109. relationship.clear()
  110. relationship.extend(role_objs)
  111. await self.auth.db.flush()
  112. async def set_user_positions_crud(self, user_ids: list[int], position_ids: list[int]) -> None:
  113. """
  114. 批量设置用户岗位
  115. 参数:
  116. - user_ids (list[int]): 用户ID列表
  117. - position_ids (list[int]): 岗位ID列表
  118. 返回:
  119. - None:
  120. """
  121. user_objs = await self.list(search={"id": ("in", user_ids)})
  122. if position_ids:
  123. position_objs = await PositionCRUD(self.auth).get_list_crud(search={"id": ("in", position_ids)})
  124. else:
  125. position_objs = []
  126. for obj in user_objs:
  127. relationship = obj.positions
  128. relationship.clear()
  129. relationship.extend(position_objs)
  130. await self.auth.db.flush()
  131. async def change_password_crud(self, id: int, password_hash: str) -> UserModel:
  132. """
  133. 修改用户密码
  134. 参数:
  135. - id (int): 用户ID
  136. - password_hash (str): 密码哈希值
  137. 返回:
  138. - UserModel: 更新后的用户信息
  139. """
  140. return await self.update(id=id, data=UserUpdateSchema(password=password_hash))
  141. async def forget_password_crud(self, id: int, password_hash: str) -> UserModel:
  142. """
  143. 重置密码
  144. 参数:
  145. - id (int): 用户ID
  146. - password_hash (str): 密码哈希值
  147. 返回:
  148. - UserModel: 更新后的用户信息
  149. """
  150. return await self.update(id=id, data=UserUpdateSchema(password=password_hash))
  151. async def register_user_crud(self, data: UserForgetPasswordSchema) -> UserModel:
  152. """
  153. 用户注册
  154. 参数:
  155. - data (UserForgetPasswordSchema): 用户注册信息
  156. 返回:
  157. - UserModel: 注册成功的用户信息
  158. """
  159. return await self.create(data=UserCreateSchema(**data.model_dump()))