1
This commit is contained in:
@@ -0,0 +1,27 @@
|
|||||||
|
"""银行流水表加 created_at 字段
|
||||||
|
|
||||||
|
Revision ID: 20260814_bank_tx_ca
|
||||||
|
Revises: 20260814_bank_tx
|
||||||
|
Create Date: 2026-08-14 16:00:00.000000
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
# revision identifiers, use by Alembic.
|
||||||
|
revision = '20260814_bank_tx_ca'
|
||||||
|
down_revision = 'e7f2527691bb'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.add_column(
|
||||||
|
'bank_transactions',
|
||||||
|
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
|
||||||
|
)
|
||||||
|
op.create_index('ix_bank_transactions_created_at', 'bank_transactions', ['created_at'])
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_index('ix_bank_transactions_created_at', table_name='bank_transactions')
|
||||||
|
op.drop_column('bank_transactions', 'created_at')
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
"""银行账户管理与交易查询后台路由。"""
|
"""银行账户管理与交易查询后台路由。"""
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
||||||
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
from app.dependencies import get_admin_user, get_db
|
from app.dependencies import get_admin_user, get_db
|
||||||
from app.models.bank_account import BankAccount
|
from app.models.bank_account import BankAccount
|
||||||
|
from app.models.bank_transaction import BankTransaction
|
||||||
from app.models.user import User
|
from app.models.user import User
|
||||||
from app.services.bank.service import query_transactions_with_log
|
from app.services.bank.service import query_transactions_with_log
|
||||||
from app.utils.id_gen import generate_id
|
from app.utils.id_gen import generate_id
|
||||||
@@ -155,19 +157,60 @@ async def list_transactions(
|
|||||||
admin: User = Depends(get_admin_user),
|
admin: User = Depends(get_admin_user),
|
||||||
db: AsyncSession = Depends(get_db),
|
db: AsyncSession = Depends(get_db),
|
||||||
):
|
):
|
||||||
"""查询银行交易流水(带接口请求记录到文件日志)。"""
|
"""查询本地已同步的银行交易流水。"""
|
||||||
result = await db.execute(select(BankAccount).where(BankAccount.id == account_id))
|
result = await db.execute(select(BankAccount).where(BankAccount.id == account_id))
|
||||||
account = result.scalar_one_or_none()
|
account = result.scalar_one_or_none()
|
||||||
if account is None:
|
if account is None:
|
||||||
raise HTTPException(status_code=404, detail="银行账户不存在")
|
raise HTTPException(status_code=404, detail="银行账户不存在")
|
||||||
|
|
||||||
data = await query_transactions_with_log(
|
# 构建查询条件
|
||||||
admin.id,
|
conditions = [BankTransaction.account_id == account_id]
|
||||||
acct_no=account.account_no,
|
if start_date:
|
||||||
start_date=start_date,
|
conditions.append(BankTransaction.transaction_time >= start_date)
|
||||||
end_date=end_date,
|
if end_date:
|
||||||
dc_flag=dc_flag,
|
conditions.append(BankTransaction.transaction_time < end_date + " 23:59:59")
|
||||||
page=page,
|
if dc_flag is not None:
|
||||||
page_size=page_size,
|
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,
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
"""银行交易流水模型。"""
|
"""银行交易流水模型。"""
|
||||||
|
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from sqlalchemy import Boolean, DateTime, Index, String, Text
|
from sqlalchemy import Boolean, DateTime, Index, String, Text
|
||||||
from sqlalchemy.orm import Mapped, mapped_column
|
from sqlalchemy.orm import Mapped, mapped_column
|
||||||
|
|
||||||
@@ -15,6 +17,14 @@ class BankTransaction(Base):
|
|||||||
account_id: Mapped[str] = mapped_column(String(32), index=True, nullable=False, comment="银行账户ID")
|
account_id: Mapped[str] = mapped_column(String(32), index=True, nullable=False, comment="银行账户ID")
|
||||||
account_no: Mapped[str] = mapped_column(String(64), index=True, nullable=False, comment="银行账号")
|
account_no: Mapped[str] = mapped_column(String(64), index=True, nullable=False, comment="银行账号")
|
||||||
|
|
||||||
|
# 记录创建时间
|
||||||
|
created_at: Mapped[datetime] = mapped_column(
|
||||||
|
DateTime(timezone=True),
|
||||||
|
default=lambda: datetime.now(timezone.utc),
|
||||||
|
nullable=False,
|
||||||
|
comment="记录创建时间",
|
||||||
|
)
|
||||||
|
|
||||||
# 交易信息
|
# 交易信息
|
||||||
transaction_no: Mapped[str | None] = mapped_column(String(128), unique=True, nullable=True, comment="交易流水号(唯一)")
|
transaction_no: Mapped[str | None] = mapped_column(String(128), unique=True, nullable=True, comment="交易流水号(唯一)")
|
||||||
transaction_time: Mapped[DateTime | None] = mapped_column(DateTime(timezone=True), index=True, nullable=True, comment="交易时间")
|
transaction_time: Mapped[DateTime | None] = mapped_column(DateTime(timezone=True), index=True, nullable=True, comment="交易时间")
|
||||||
|
|||||||
Reference in New Issue
Block a user