| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # app/services/fault_service.py
- from typing import Optional, List
- from sqlalchemy.ext.asyncio import AsyncSession
- from sqlalchemy import select, func
- from app.models.fault import VasFaultCompensation
- from app.schemas.fault import VasFaultCreate
- from app.utils.pagination import paginate
- class FaultService:
- @staticmethod
- async def create(db: AsyncSession, data: VasFaultCreate) -> VasFaultCompensation:
- record = VasFaultCompensation(
- order_id=data.order_id,
- liable_user_id=data.liable_user_id,
- penalty_amount=data.penalty_amount,
- reason=data.reason,
- notes=data.notes,
- deducted_status=0,
- )
- db.add(record)
- await db.commit()
- await db.refresh(record)
- return record
- @staticmethod
- async def list_pending_by_user(db: AsyncSession, user_id: str) -> List[VasFaultCompensation]:
- stmt = select(VasFaultCompensation).where(
- VasFaultCompensation.liable_user_id == user_id,
- VasFaultCompensation.deducted_status == 0
- )
- return (await db.execute(stmt)).scalars().all()
- @staticmethod
- async def list_all(
- db: AsyncSession,
- user_id: Optional[str] = None,
- deducted_status: Optional[int] = None,
- page: int = 0,
- size: int = 20
- ):
- stmt = select(VasFaultCompensation)
- if user_id:
- stmt = stmt.where(VasFaultCompensation.liable_user_id == user_id)
- if deducted_status is not None:
- stmt = stmt.where(VasFaultCompensation.deducted_status == deducted_status)
- stmt = stmt.order_by(VasFaultCompensation.created_at.desc())
- return await paginate(db, stmt, page, size)
|