crud.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. from collections.abc import Sequence
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.api.v1.module_system.dict.model import DictDataModel, DictTypeModel
  4. from app.api.v1.module_system.dict.schema import (
  5. DictDataCreateSchema,
  6. DictDataUpdateSchema,
  7. DictTypeCreateSchema,
  8. DictTypeUpdateSchema,
  9. )
  10. from app.core.base_crud import CRUDBase
  11. class DictTypeCRUD(CRUDBase[DictTypeModel, DictTypeCreateSchema, DictTypeUpdateSchema]):
  12. """数据字典类型数据层"""
  13. def __init__(self, auth: AuthSchema) -> None:
  14. """
  15. 初始化数据字典类型数据层。
  16. 参数:
  17. - auth (AuthSchema): 认证信息模型(含 DB 会话等上下文)。
  18. 返回:
  19. - None
  20. """
  21. self.auth = auth
  22. super().__init__(model=DictTypeModel, auth=auth)
  23. async def get_obj_by_id_crud(
  24. self, id: int, preload: list | None = None
  25. ) -> DictTypeModel | None:
  26. """
  27. 获取数据字典类型详情
  28. 参数:
  29. - id (int): 数据字典类型ID
  30. - preload (list | None): 预加载关系,未提供时使用模型默认项
  31. 返回:
  32. - DictTypeModel | None: 数据字典类型模型,如果不存在则为None
  33. """
  34. # 添加默认预加载字典数据关系
  35. if preload is None:
  36. preload = []
  37. return await self.get(id=id, preload=preload)
  38. async def get_obj_list_crud(
  39. self,
  40. search: dict | None = None,
  41. order_by: list[dict] | None = None,
  42. preload: list | None = None,
  43. ) -> Sequence[DictTypeModel]:
  44. """
  45. 获取数据字典类型列表
  46. 参数:
  47. - search (dict | None): 查询参数,默认值为None
  48. - order_by (list[dict] | None): 排序参数,默认值为None
  49. - preload (list | None): 预加载关系,未提供时使用模型默认项
  50. 返回:
  51. - Sequence[DictTypeModel]: 数据字典类型模型序列
  52. """
  53. # 添加默认预加载字典数据关系
  54. if preload is None:
  55. preload = []
  56. return await self.list(search=search, order_by=order_by, preload=preload)
  57. async def create_obj_crud(self, data: DictTypeCreateSchema) -> DictTypeModel | None:
  58. """
  59. 创建数据字典类型
  60. 参数:
  61. - data (DictTypeCreateSchema): 数据字典类型创建模型
  62. 返回:
  63. - DictTypeModel | None: 创建的数据字典类型模型,如果创建失败则为None
  64. """
  65. return await self.create(data=data)
  66. async def update_obj_crud(self, id: int, data: DictTypeUpdateSchema) -> DictTypeModel | None:
  67. """
  68. 更新数据字典类型
  69. 参数:
  70. - id (int): 数据字典类型ID
  71. - data (DictTypeUpdateSchema): 数据字典类型更新模型
  72. 返回:
  73. - DictTypeModel | None: 更新的数据字典类型模型,如果更新失败则为None
  74. """
  75. return await self.update(id=id, data=data)
  76. async def delete_obj_crud(self, ids: list[int]) -> None:
  77. """
  78. 删除数据字典类型
  79. 参数:
  80. - ids (list[int]): 数据字典类型ID列表
  81. 返回:
  82. - None
  83. """
  84. return await self.delete(ids=ids)
  85. async def set_obj_available_crud(self, ids: list[int], status: str) -> None:
  86. """
  87. 设置数据字典类型的可用状态
  88. 参数:
  89. - ids (list[int]): 数据字典类型ID列表
  90. - status (str): 可用状态,0表示正常,1表示停用
  91. 返回:
  92. - None
  93. """
  94. return await self.set(ids=ids, status=status)
  95. async def batch_delete_obj_crud(self, ids: list[int]) -> int:
  96. """
  97. 批量删除数据字典类型
  98. 参数:
  99. - ids (list[int]): 数据字典类型ID列表
  100. 返回:
  101. - int: 删除的记录数量
  102. """
  103. await self.delete(ids=ids)
  104. return len(ids)
  105. class DictDataCRUD(CRUDBase[DictDataModel, DictDataCreateSchema, DictDataUpdateSchema]):
  106. """数据字典数据层"""
  107. def __init__(self, auth: AuthSchema) -> None:
  108. """
  109. 初始化数据字典项数据层。
  110. 参数:
  111. - auth (AuthSchema): 认证信息模型(含 DB 会话等上下文)。
  112. 返回:
  113. - None
  114. """
  115. self.auth = auth
  116. super().__init__(model=DictDataModel, auth=auth)
  117. async def get_obj_by_id_crud(
  118. self, id: int, preload: list | None = None
  119. ) -> DictDataModel | None:
  120. """
  121. 获取数据字典数据详情
  122. 参数:
  123. - id (int): 数据字典数据ID
  124. - preload (list | None): 预加载关系,未提供时使用模型默认项
  125. 返回:
  126. - DictDataModel | None: 数据字典数据模型,如果不存在则为None
  127. """
  128. # 添加默认预加载字典类型关系
  129. if preload is None:
  130. preload = []
  131. return await self.get(id=id, preload=preload)
  132. async def get_obj_list_crud(
  133. self,
  134. search: dict | None = None,
  135. order_by: list[dict] | None = None,
  136. preload: list | None = None,
  137. ) -> Sequence[DictDataModel]:
  138. """
  139. 获取数据字典数据列表
  140. 参数:
  141. - search (dict | None): 查询参数,默认值为None
  142. - order_by (list[dict] | None): 排序参数,默认值为None
  143. - preload (list | None): 预加载关系,未提供时使用模型默认项
  144. 返回:
  145. - Sequence[DictDataModel]: 数据字典数据模型序列
  146. """
  147. # 添加默认预加载字典类型关系
  148. if preload is None:
  149. preload = []
  150. return await self.list(search=search, order_by=order_by, preload=preload)
  151. async def create_obj_crud(self, data: DictDataCreateSchema) -> DictDataModel | None:
  152. """
  153. 创建数据字典数据
  154. 参数:
  155. - data (DictDataCreateSchema): 数据字典数据创建模型
  156. 返回:
  157. - DictDataModel | None: 创建的数据字典数据模型,如果创建失败则为None
  158. """
  159. return await self.create(data=data)
  160. async def update_obj_crud(self, id: int, data: DictDataUpdateSchema) -> DictDataModel | None:
  161. """
  162. 更新数据字典数据
  163. 参数:
  164. - id (int): 数据字典数据ID
  165. - data (DictDataUpdateSchema): 数据字典数据更新模型
  166. 返回:
  167. - DictDataModel | None: 更新的数据字典数据模型,如果更新失败则为None
  168. """
  169. return await self.update(id=id, data=data)
  170. async def delete_obj_crud(self, ids: list[int]) -> None:
  171. """
  172. 删除数据字典数据
  173. 参数:
  174. - ids (list[int]): 数据字典数据ID列表
  175. 返回:
  176. - None
  177. """
  178. return await self.delete(ids=ids)
  179. async def set_obj_available_crud(self, ids: list[int], status: str) -> None:
  180. """
  181. 设置数据字典数据的可用状态
  182. 参数:
  183. - ids (list[int]): 数据字典数据ID列表
  184. - status (str): 可用状态,0表示正常,1表示停用
  185. 返回:
  186. - None
  187. """
  188. return await self.set(ids=ids, status=status)
  189. async def batch_delete_obj_crud(self, ids: list[int], exclude_system: bool = True) -> int:
  190. """
  191. 批量删除数据字典数据
  192. 参数:
  193. - ids (list[int]): 数据字典数据ID列表
  194. - exclude_system (bool): 是否排除系统默认数据,默认为True
  195. 返回:
  196. - int: 删除的记录数量
  197. """
  198. # 如果需要排除系统默认数据,可以在这里添加过滤逻辑
  199. # 假设系统默认数据在remark字段中包含"系统默认"字符串
  200. if exclude_system:
  201. # 获取非系统默认数据的ID
  202. system_data_filter = {
  203. "id__in": ids,
  204. "remark__contains": "系统默认",
  205. }
  206. system_data = await self.list(search=system_data_filter)
  207. system_ids = [item.id for item in system_data]
  208. # 从待删除ID列表中排除系统默认数据
  209. ids = [id for id in ids if id not in system_ids]
  210. if ids:
  211. await self.delete(ids=ids)
  212. return len(ids)
  213. async def get_obj_list_by_dict_type_crud(
  214. self, dict_type: str, status: str | None = "0"
  215. ) -> Sequence[DictDataModel]:
  216. """
  217. 根据字典类型获取字典数据列表
  218. 参数:
  219. - dict_type (str): 字典类型
  220. - status (str | None): 状态过滤,None表示不过滤
  221. 返回:
  222. - Sequence[DictDataModel]: 数据字典数据模型序列
  223. """
  224. search = {"dict_type": dict_type}
  225. if status is not None:
  226. search["status"] = status
  227. order_by = [{"id": "asc"}]
  228. return await self.list(search=search, order_by=order_by)