fault_service.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # app/services/fault_service.py
  2. from typing import Optional, List
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from sqlalchemy import select, func
  5. from app.models.fault import VasFaultCompensation
  6. from app.schemas.fault import VasFaultCreate
  7. from app.utils.pagination import paginate
  8. class FaultService:
  9. @staticmethod
  10. async def create(db: AsyncSession, data: VasFaultCreate) -> VasFaultCompensation:
  11. record = VasFaultCompensation(
  12. order_id=data.order_id,
  13. liable_user_id=data.liable_user_id,
  14. penalty_amount=data.penalty_amount,
  15. reason=data.reason,
  16. notes=data.notes,
  17. deducted_status=0,
  18. )
  19. db.add(record)
  20. await db.commit()
  21. await db.refresh(record)
  22. return record
  23. @staticmethod
  24. async def list_pending_by_user(db: AsyncSession, user_id: str) -> List[VasFaultCompensation]:
  25. stmt = select(VasFaultCompensation).where(
  26. VasFaultCompensation.liable_user_id == user_id,
  27. VasFaultCompensation.deducted_status == 0
  28. )
  29. return (await db.execute(stmt)).scalars().all()
  30. @staticmethod
  31. async def list_all(
  32. db: AsyncSession,
  33. user_id: Optional[str] = None,
  34. deducted_status: Optional[int] = None,
  35. page: int = 0,
  36. size: int = 20
  37. ):
  38. stmt = select(VasFaultCompensation)
  39. if user_id:
  40. stmt = stmt.where(VasFaultCompensation.liable_user_id == user_id)
  41. if deducted_status is not None:
  42. stmt = stmt.where(VasFaultCompensation.deducted_status == deducted_status)
  43. stmt = stmt.order_by(VasFaultCompensation.created_at.desc())
  44. return await paginate(db, stmt, page, size)