crud.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. from typing import Sequence
  3. from app.core.base_crud import CRUDBase
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from .model import BizMecModel
  6. from .schema import BizMecCreateSchema, BizMecUpdateSchema, BizMecOutSchema
  7. class BizMecCRUD(CRUDBase[BizMecModel, BizMecCreateSchema, BizMecUpdateSchema]):
  8. """机构信息数据层"""
  9. def __init__(self, auth: AuthSchema) -> None:
  10. """
  11. 初始化CRUD数据层
  12. 参数:
  13. - auth (AuthSchema): 认证信息模型
  14. """
  15. super().__init__(model=BizMecModel, auth=auth)
  16. async def get_by_id_mec_crud(self, id: int, preload: list | None = None) -> BizMecModel | None:
  17. """
  18. 详情
  19. 参数:
  20. - id (int): 对象ID
  21. - preload (list | None): 预加载关系,未提供时使用模型默认项
  22. 返回:
  23. - BizMecModel | None: 模型实例或None
  24. """
  25. return await self.get(id=id, preload=preload)
  26. async def get_by_id_mec_crud_for_no(self, mec_no: str, preload: list | None = None) -> BizMecModel | None:
  27. """
  28. 详情
  29. 参数:
  30. - id (int): 对象ID
  31. - preload (list | None): 预加载关系,未提供时使用模型默认项
  32. 返回:
  33. - BizMecModel | None: 模型实例或None
  34. """
  35. return await self.get(mec_no=mec_no, preload=preload)
  36. async def list_mec_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list | None = None) -> Sequence[BizMecModel]:
  37. """
  38. 列表查询
  39. 参数:
  40. - search (dict | None): 查询参数
  41. - order_by (list[dict] | None): 排序参数,未提供时使用模型默认项
  42. - preload (list | None): 预加载关系,未提供时使用模型默认项
  43. 返回:
  44. - Sequence[BizMecModel]: 模型实例序列
  45. """
  46. return await self.list(search=search, order_by=order_by, preload=preload)
  47. async def create_mec_crud(self, data: BizMecCreateSchema) -> BizMecModel | None:
  48. """
  49. 创建
  50. 参数:
  51. - data (BizMecCreateSchema): 创建模型
  52. 返回:
  53. - BizMecModel | None: 模型实例或None
  54. """
  55. return await self.create(data=data)
  56. async def update_mec_crud(self, id: int, data: BizMecUpdateSchema) -> BizMecModel | None:
  57. """
  58. 更新
  59. 参数:
  60. - id (int): 对象ID
  61. - data (BizMecUpdateSchema): 更新模型
  62. 返回:
  63. - BizMecModel | None: 模型实例或None
  64. """
  65. return await self.update(id=id, data=data)
  66. async def delete_mec_crud(self, ids: list[int]) -> None:
  67. """
  68. 批量删除
  69. 参数:
  70. - ids (list[int]): 对象ID列表
  71. 返回:
  72. - None
  73. """
  74. return await self.delete(ids=ids)
  75. async def set_available_mec_crud(self, ids: list[int], status: str) -> None:
  76. """
  77. 批量设置可用状态
  78. 参数:
  79. - ids (list[int]): 对象ID列表
  80. - status (str): 可用状态
  81. 返回:
  82. - None
  83. """
  84. return await self.set(ids=ids, status=status)
  85. async def page_mec_crud(self, offset: int, limit: int, order_by: list[dict] | None = None, search: dict | None = None, preload: list | None = None) -> dict:
  86. """
  87. 分页查询
  88. 参数:
  89. - offset (int): 偏移量
  90. - limit (int): 每页数量
  91. - order_by (list[dict] | None): 排序参数,未提供时使用模型默认项
  92. - search (dict | None): 查询参数,未提供时查询所有
  93. - preload (list | None): 预加载关系,未提供时使用模型默认项
  94. 返回:
  95. - Dict: 分页数据
  96. """
  97. order_by_list = order_by or [{'id': 'asc'}]
  98. search_dict = search or {}
  99. return await self.page(
  100. offset=offset,
  101. limit=limit,
  102. order_by=order_by_list,
  103. search=search_dict,
  104. out_schema=BizMecOutSchema,
  105. preload=preload
  106. )