diff --git a/video-gen-api/app/api/v1/__init__.py b/video-gen-api/app/api/v1/__init__.py index f47470d4..1df58b3b 100644 --- a/video-gen-api/app/api/v1/__init__.py +++ b/video-gen-api/app/api/v1/__init__.py @@ -1,6 +1,7 @@ from fastapi import APIRouter from app.api.v1.auth import router as auth_router +from app.api.v1.bank import router as bank_router from app.api.v1.projects import router as projects_router from app.api.v1.generation import router as generation_router from app.api.v1.credits import router as credits_router @@ -41,6 +42,7 @@ from app.api.v1.invoice_headers import router as invoice_headers_router api_router = APIRouter() api_router.include_router(auth_router) +api_router.include_router(bank_router) api_router.include_router(projects_router) api_router.include_router(generation_router) api_router.include_router(credits_router) diff --git a/video-gen-api/app/api/v1/bank.py b/video-gen-api/app/api/v1/bank.py new file mode 100644 index 00000000..8e9ab343 --- /dev/null +++ b/video-gen-api/app/api/v1/bank.py @@ -0,0 +1,83 @@ +"""银行账户前端查询接口。""" + +from fastapi import APIRouter, Depends +from sqlalchemy import select +from sqlalchemy.ext.asyncio import AsyncSession + +from app.dependencies import get_current_user, get_db +from app.models.bank_account import BankAccount +from app.models.user import User + +router = APIRouter(prefix="/bank", tags=["bank"]) + + +@router.get("/default-account") +async def get_default_account( + current_user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), +): + """获取默认银行账户信息(前端展示用)。 + + 优先返回标记为默认的启用账户;如果没有默认账户,返回第一个启用账户。 + """ + # 先查默认账户 + result = await db.execute( + select(BankAccount).where( + BankAccount.is_active.is_(True), + BankAccount.is_default.is_(True), + ) + ) + account = result.scalar_one_or_none() + + # 没有默认账户则取第一个启用账户 + if account is None: + result = await db.execute( + select(BankAccount).where( + BankAccount.is_active.is_(True), + ).order_by(BankAccount.created_at.asc()) + ) + account = result.scalar_one_or_none() + + if account is None: + return { + "has_account": False, + "account": None, + } + + return { + "has_account": True, + "account": { + "id": account.id, + "accountName": account.account_name, + "bankName": account.bank_name, + "accountNo": account.account_no, + "description": account.description, + }, + } + + +@router.get("/accounts") +async def list_active_accounts( + current_user: User = Depends(get_current_user), + db: AsyncSession = Depends(get_db), +): + """获取所有启用的银行账户列表(前端展示用)。""" + result = await db.execute( + select(BankAccount).where( + BankAccount.is_active.is_(True), + ).order_by(BankAccount.is_default.desc(), BankAccount.created_at.asc()) + ) + accounts = result.scalars().all() + return { + "items": [ + { + "id": a.id, + "accountName": a.account_name, + "bankName": a.bank_name, + "accountNo": a.account_no, + "isDefault": a.is_default, + "description": a.description, + } + for a in accounts + ] + }