1
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
"""银行账户管理与交易查询后台路由。"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.dependencies import get_admin_user, get_db
|
||||
from app.models.bank_account import BankAccount
|
||||
from app.models.bank_transaction import BankTransaction
|
||||
from app.models.user import User
|
||||
from app.services.bank.service import query_transactions_with_log
|
||||
from app.utils.id_gen import generate_id
|
||||
@@ -155,19 +157,60 @@ async def list_transactions(
|
||||
admin: User = Depends(get_admin_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
"""查询银行交易流水(带接口请求记录到文件日志)。"""
|
||||
"""查询本地已同步的银行交易流水。"""
|
||||
result = await db.execute(select(BankAccount).where(BankAccount.id == account_id))
|
||||
account = result.scalar_one_or_none()
|
||||
if account is None:
|
||||
raise HTTPException(status_code=404, detail="银行账户不存在")
|
||||
|
||||
data = await query_transactions_with_log(
|
||||
admin.id,
|
||||
acct_no=account.account_no,
|
||||
start_date=start_date,
|
||||
end_date=end_date,
|
||||
dc_flag=dc_flag,
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
# 构建查询条件
|
||||
conditions = [BankTransaction.account_id == account_id]
|
||||
if start_date:
|
||||
conditions.append(BankTransaction.transaction_time >= start_date)
|
||||
if end_date:
|
||||
conditions.append(BankTransaction.transaction_time < end_date + " 23:59:59")
|
||||
if dc_flag is not None:
|
||||
direction = "DR" if dc_flag == 0 else "CR"
|
||||
conditions.append(BankTransaction.balance_direction == direction)
|
||||
|
||||
# 查总数
|
||||
count_result = await db.execute(
|
||||
select(sa.func.count()).where(*conditions)
|
||||
)
|
||||
return data
|
||||
total = count_result.scalar() or 0
|
||||
|
||||
# 查分页数据
|
||||
offset = (page - 1) * page_size
|
||||
data_result = await db.execute(
|
||||
select(BankTransaction)
|
||||
.where(*conditions)
|
||||
.order_by(BankTransaction.transaction_time.desc())
|
||||
.offset(offset)
|
||||
.limit(page_size)
|
||||
)
|
||||
transactions = data_result.scalars().all()
|
||||
|
||||
return {
|
||||
"items": [
|
||||
{
|
||||
"id": t.id,
|
||||
"account_id": t.account_id,
|
||||
"account_no": t.account_no,
|
||||
"transaction_no": t.transaction_no,
|
||||
"transaction_time": t.transaction_time.isoformat() if t.transaction_time else None,
|
||||
"transaction_amount": t.transaction_amount,
|
||||
"balance_direction": t.balance_direction,
|
||||
"balance_after": t.balance_after,
|
||||
"counterparty_name": t.counterparty_name,
|
||||
"counterparty_account": t.counterparty_account,
|
||||
"counterparty_bank": t.counterparty_bank,
|
||||
"remark": t.remark,
|
||||
"digest_code": t.digest_code,
|
||||
"sync_batch": t.sync_batch,
|
||||
"is_synced": t.is_synced,
|
||||
"created_at": t.created_at.isoformat() if t.created_at else None,
|
||||
}
|
||||
for t in transactions
|
||||
],
|
||||
"total": total,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user