crud.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # -*- coding: utf-8 -*-
  2. from typing import Sequence, Any
  3. from app.core.base_crud import CRUDBase
  4. from app.api.v1.module_system.auth.schema import AuthSchema
  5. from .model import JobModel, JobLogModel
  6. from .schema import JobCreateSchema,JobUpdateSchema,JobLogCreateSchema,JobLogUpdateSchema
  7. class JobCRUD(CRUDBase[JobModel, JobCreateSchema, JobUpdateSchema]):
  8. """定时任务数据层"""
  9. def __init__(self, auth: AuthSchema) -> None:
  10. """
  11. 初始化定时任务CRUD
  12. 参数:
  13. - auth (AuthSchema): 认证信息模型
  14. """
  15. self.auth = auth
  16. super().__init__(model=JobModel, auth=auth)
  17. async def get_obj_by_id_crud(self, id: int, preload: list[str | Any] | None = None) -> JobModel | None:
  18. """
  19. 获取定时任务详情
  20. 参数:
  21. - id (int): 定时任务ID
  22. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  23. 返回:
  24. - JobModel | None: 定时任务模型,如果不存在则为None
  25. """
  26. return await self.get(id=id, preload=preload)
  27. async def get_obj_list_crud(self, search: dict | None = None, order_by: list[dict[str, str]] | None = None, preload: list[str | Any] | None = None) -> Sequence[JobModel]:
  28. """
  29. 获取定时任务列表
  30. 参数:
  31. - search (dict | None): 查询参数字典
  32. - order_by (list[dict[str, str]] | None): 排序参数列表
  33. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  34. 返回:
  35. - Sequence[JobModel]: 定时任务模型序列
  36. """
  37. return await self.list(search=search, order_by=order_by, preload=preload)
  38. async def create_obj_crud(self, data: JobCreateSchema) -> JobModel | None:
  39. """
  40. 创建定时任务
  41. 参数:
  42. - data (JobCreateSchema): 创建定时任务模型
  43. 返回:
  44. - JobModel | None: 创建的定时任务模型,如果创建失败则为None
  45. """
  46. return await self.create(data=data)
  47. async def update_obj_crud(self, id: int, data: JobUpdateSchema) -> JobModel | None:
  48. """
  49. 更新定时任务
  50. 参数:
  51. - id (int): 定时任务ID
  52. - data (JobUpdateSchema): 更新定时任务模型
  53. 返回:
  54. - JobModel | None: 更新后的定时任务模型,如果更新失败则为None
  55. """
  56. return await self.update(id=id, data=data)
  57. async def delete_obj_crud(self, ids: list[int]) -> None:
  58. """
  59. 删除定时任务
  60. 参数:
  61. - ids (list[int]): 定时任务ID列表
  62. """
  63. return await self.delete(ids=ids)
  64. async def set_obj_field_crud(self, ids: list[int], **kwargs) -> None:
  65. """
  66. 设置定时任务的可用状态
  67. 参数:
  68. - ids (list[int]): 定时任务ID列表
  69. - kwargs: 其他要设置的字段,例如 available=True 或 available=False
  70. """
  71. return await self.set(ids=ids, **kwargs)
  72. async def clear_obj_crud(self) -> None:
  73. """
  74. 清除定时任务日志
  75. 注意:
  76. - 此操作会删除所有定时任务日志,请谨慎操作
  77. """
  78. return await self.clear()
  79. class JobLogCRUD(CRUDBase[JobLogModel, JobLogCreateSchema, JobLogUpdateSchema]):
  80. """定时任务日志数据层"""
  81. def __init__(self, auth: AuthSchema) -> None:
  82. """
  83. 初始化定时任务日志CRUD
  84. 参数:
  85. - auth (AuthSchema): 认证信息模型
  86. """
  87. self.auth = auth
  88. super().__init__(model=JobLogModel, auth=auth)
  89. async def get_obj_log_by_id_crud(self, id: int, preload: list[str | Any] | None = None) -> JobLogModel | None:
  90. """
  91. 获取定时任务日志详情
  92. 参数:
  93. - id (int): 定时任务日志ID
  94. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  95. 返回:
  96. - JobLogModel | None: 定时任务日志模型,如果不存在则为None
  97. """
  98. return await self.get(id=id, preload=preload)
  99. async def get_obj_log_list_crud(self, search: dict | None = None, order_by: list[dict[str, str]] | None = None, preload: list[str | Any] | None = None) -> Sequence[JobLogModel]:
  100. """
  101. 获取定时任务日志列表
  102. 参数:
  103. - search (dict | None): 查询参数字典
  104. - order_by (list[dict[str, str]] | None): 排序参数列表
  105. - preload (list[str | Any] | None): 预加载关系,未提供时使用模型默认项
  106. 返回:
  107. - Sequence[JobLogModel]: 定时任务日志模型序列
  108. """
  109. return await self.list(search=search, order_by=order_by, preload=preload)
  110. async def delete_obj_log_crud(self, ids: list[int]) -> None:
  111. """
  112. 删除定时任务日志
  113. 参数:
  114. - ids (list[int]): 定时任务日志ID列表
  115. """
  116. return await self.delete(ids=ids)
  117. async def clear_obj_log_crud(self) -> None:
  118. """
  119. 清除定时任务日志
  120. 注意:
  121. - 此操作会删除所有定时任务日志,请谨慎操作
  122. """
  123. return await self.clear()