feat(weibo): 区分充值资金来源并完善协作单记录

This commit is contained in:
yml
2026-11-13 21:07:50 +08:00
parent f0abca3d58
commit 76b344eb83
11 changed files with 740 additions and 10 deletions
@@ -0,0 +1,187 @@
"""持久化微博 Web 充值记录并区分订单资金来源。"""
from __future__ import annotations
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "20261113_0082"
down_revision: str | None = "20261113_0081"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
ORDERS = "weibo_external_orders"
CHARGES = "weibo_recharge_records"
def upgrade() -> None:
bind = op.get_bind()
inspector = sa.inspect(bind)
if inspector.has_table(ORDERS):
columns = {column["name"] for column in inspector.get_columns(ORDERS)}
if "funding_source" not in columns:
op.add_column(
ORDERS,
sa.Column(
"funding_source",
sa.String(length=32),
nullable=False,
server_default="",
),
)
op.create_index(
"ix_weibo_external_orders_funding_source",
ORDERS,
["funding_source"],
)
if "funding_source_detail" not in columns:
op.add_column(
ORDERS,
sa.Column(
"funding_source_detail",
sa.String(length=512),
nullable=False,
server_default="",
),
)
if "funding_recharge_record_id" not in columns:
op.add_column(
ORDERS,
sa.Column("funding_recharge_record_id", sa.Integer(), nullable=True),
)
op.create_index(
"ix_weibo_external_orders_funding_recharge_record_id",
ORDERS,
["funding_recharge_record_id"],
)
if not inspector.has_table(CHARGES):
op.create_table(
CHARGES,
sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
sa.Column(
"account_id",
sa.Integer(),
sa.ForeignKey("weibo_accounts.id", ondelete="CASCADE"),
nullable=False,
),
sa.Column("order_id", sa.String(length=64), nullable=False),
sa.Column(
"pay_id",
sa.String(length=128),
nullable=False,
server_default="",
),
sa.Column("amount_yuan", sa.Integer(), nullable=False),
sa.Column("single_reward", sa.Integer(), nullable=False),
sa.Column(
"status",
sa.String(length=24),
nullable=False,
server_default="created",
),
sa.Column(
"sub_status",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column("paid_at", sa.DateTime(), nullable=True),
sa.Column("status_checked_at", sa.DateTime(), nullable=True),
sa.Column(
"created_by",
sa.Integer(),
sa.ForeignKey("users.id", ondelete="SET NULL"),
nullable=True,
),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.func.now(),
),
sa.Column(
"updated_at",
sa.DateTime(),
nullable=False,
server_default=sa.func.now(),
),
sa.UniqueConstraint("order_id", name="uq_weibo_recharge_records_order_id"),
)
op.create_index(
"ix_weibo_recharge_records_account_id",
CHARGES,
["account_id"],
)
op.create_index(
"ix_weibo_recharge_records_order_id",
CHARGES,
["order_id"],
)
op.create_index(
"ix_weibo_recharge_records_pay_id",
CHARGES,
["pay_id"],
)
op.create_index(
"ix_weibo_recharge_records_status",
CHARGES,
["status"],
)
op.create_index(
"ix_weibo_recharge_records_created_at",
CHARGES,
["created_at"],
)
if inspector.has_table(ORDERS):
has_audit = inspector.has_table("audit_logs")
audit_exists = (
"""
EXISTS (
SELECT 1
FROM audit_logs a
WHERE a.action = 'weibo:recharge:create_order'
AND JSON_EXTRACT(a.detail, '$.account_id') = o.account_id
AND a.created_at >= o.created_at
AND a.created_at <= o.recharged_at
)
"""
if has_audit
else "FALSE"
)
bind.execute(
sa.text(
f"""
UPDATE {ORDERS} o
SET funding_source = CASE
WHEN {audit_exists} THEN 'legacy_web_recharge'
ELSE 'account_balance'
END,
funding_source_detail = CASE
WHEN {audit_exists}
THEN '历史回填:核账前存在 Web 充值下单审计,但历史支付结果未持久化'
ELSE '历史回填:核账前未发现该账号的 Web 充值下单记录'
END
WHERE o.recharged_at IS NOT NULL
AND o.funding_source = ''
"""
)
)
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
if inspector.has_table(CHARGES):
op.drop_table(CHARGES)
if inspector.has_table(ORDERS):
columns = {column["name"] for column in inspector.get_columns(ORDERS)}
if "funding_recharge_record_id" in columns:
op.drop_column(ORDERS, "funding_recharge_record_id")
if "funding_source_detail" in columns:
op.drop_column(ORDERS, "funding_source_detail")
if "funding_source" in columns:
op.drop_column(ORDERS, "funding_source")