crud.py 8.7 KB

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