Files
meijiaka-zy/python-api/app/crud/point_transaction.py
T
小鱼开发 51521fc0dd feat(payment): 微信支付 APIv2 + 积分充值 + SMS 短信 + 双 Token 认证
- 微信支付从 APIv3 降级为 APIv2(MD5/XML)
- 积分系统:充值下单、微信回调、消费冻结/结算/退款
- SMS B2M 短信验证码服务
- 双 Token 认证(Access 30min + Refresh 30days)
- SSE 单设备踢人
- 用户设备管理、积分账户模型
- Alembic 迁移脚本
2026-05-07 18:43:02 +08:00

62 lines
1.7 KiB
Python

"""
积分流水 CRUD
=============
只增不改,用于审计和对账。
"""
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.crud.base import CRUDBase
from app.models.point_transaction import PointTransaction
class PointTransactionCRUD(CRUDBase[PointTransaction]):
"""积分流水数据访问对象"""
def __init__(self) -> None:
super().__init__(PointTransaction)
async def get_by_user_id(
self,
db: AsyncSession,
*,
user_id: str,
skip: int = 0,
limit: int = 50,
) -> list[PointTransaction]:
"""根据用户 ID 获取流水记录(分页,按时间倒序)"""
result = await db.execute(
select(PointTransaction)
.where(PointTransaction.user_id == user_id)
.order_by(PointTransaction.created_at.desc())
.offset(skip)
.limit(limit)
)
return list(result.scalars().all())
async def get_by_source(
self,
db: AsyncSession,
*,
user_id: str,
source_type: str,
source_id: str,
) -> list[PointTransaction]:
"""根据消费来源查询流水(用于查询某次 AI 调用的扣费记录)"""
result = await db.execute(
select(PointTransaction)
.where(
PointTransaction.user_id == user_id,
PointTransaction.source_type == source_type,
PointTransaction.source_id == source_id,
)
.order_by(PointTransaction.created_at.desc())
)
return list(result.scalars().all())
# 导出实例
point_transaction = PointTransactionCRUD()