84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
"""银行账户前端查询接口。"""
|
|
|
|
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
|
|
]
|
|
}
|