crud.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from app.api.v1.module_system.auth.schema import AuthSchema
  2. from app.core.base_crud import CRUDBase
  3. from app.core.exceptions import CustomException
  4. from .model import PointsModel, PointsRecordModel
  5. from .schema import PointsCreateSchema
  6. class PointsCRUD(CRUDBase[PointsModel, PointsCreateSchema, PointsCreateSchema]):
  7. """积分 CRUD 操作"""
  8. def __init__(self, auth: AuthSchema) -> None:
  9. self.auth = auth
  10. super().__init__(model=PointsModel, auth=auth)
  11. async def get_by_tenant_id(self, tenant_id: int) -> PointsModel | None:
  12. """根据租户ID查询积分"""
  13. return await self.get(tenant_id=tenant_id)
  14. async def create_or_update(self, tenant_id: int, points: float) -> PointsModel:
  15. """创建或更新积分"""
  16. points_obj = await self.get_by_tenant_id(tenant_id)
  17. if points_obj:
  18. points_obj.points = points
  19. await self.auth.db.flush()
  20. await self.auth.db.refresh(points_obj)
  21. return points_obj
  22. else:
  23. create_data = {
  24. "tenant_id": tenant_id,
  25. "points": points
  26. }
  27. return await self.create(create_data)
  28. class PointsRecordCRUD(CRUDBase[PointsRecordModel, None, None]):
  29. """积分记录 CRUD 操作"""
  30. def __init__(self, auth: AuthSchema) -> None:
  31. self.auth = auth
  32. super().__init__(model=PointsRecordModel, auth=auth)