crud.py 6.2 KB

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