crud.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from typing import Any
  2. from app.api.v1.module_system.auth.schema import AuthSchema
  3. from app.core.base_crud import CRUDBase
  4. from app.core.exceptions import CustomException
  5. from .model import AlipayNotifyLogModel
  6. class AlipayNotifyLogCRUD(CRUDBase[AlipayNotifyLogModel, Any, Any]):
  7. """支付宝通知日志 CRUD 操作"""
  8. def __init__(self, auth: AuthSchema) -> None:
  9. self.auth = auth
  10. super().__init__(model=AlipayNotifyLogModel, auth=auth)
  11. async def get_by_notify_id(
  12. self, notify_id: str
  13. ) -> AlipayNotifyLogModel | None:
  14. return await self.get(notify_id=notify_id)
  15. async def update_by_notify_id(
  16. self, notify_id: str, data: dict
  17. ) -> AlipayNotifyLogModel | None:
  18. obj = await self.get(notify_id=notify_id, preload=[])
  19. if not obj:
  20. raise CustomException(msg="通知日志不存在")
  21. if self.auth.user and hasattr(obj, "updated_id"):
  22. setattr(obj, "updated_id", self.auth.user.id)
  23. for key, value in data.items():
  24. if hasattr(obj, key):
  25. setattr(obj, key, value)
  26. await self.auth.db.flush()
  27. await self.auth.db.refresh(obj)
  28. return obj