feat(weibo): 区分充值资金来源并完善协作单记录
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.weibo import (
|
||||
@@ -18,7 +19,7 @@ from core.weibo import (
|
||||
normalize_weibo_cookie,
|
||||
)
|
||||
|
||||
from ..models import WeiboAccount, WeiboWorkbenchAccount
|
||||
from ..models import WeiboAccount, WeiboRechargeRecord, WeiboWorkbenchAccount
|
||||
from .weibo_image_service import embed_weibo_goods_images
|
||||
|
||||
|
||||
@@ -468,6 +469,75 @@ def create_weibo_recharge_order(
|
||||
raise
|
||||
|
||||
|
||||
def persist_weibo_recharge_order(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
order: WeiboRechargeOrder,
|
||||
*,
|
||||
created_by: int | None,
|
||||
) -> WeiboRechargeRecord:
|
||||
"""持久化 Web 充值下单结果。"""
|
||||
row = (
|
||||
db.query(WeiboRechargeRecord)
|
||||
.filter(WeiboRechargeRecord.order_id == order.order_id)
|
||||
.first()
|
||||
)
|
||||
if row is None:
|
||||
row = WeiboRechargeRecord(
|
||||
account_id=account.id,
|
||||
order_id=order.order_id,
|
||||
amount_yuan=order.amount_yuan,
|
||||
single_reward=order.single_reward,
|
||||
created_by=created_by,
|
||||
)
|
||||
db.add(row)
|
||||
row.account_id = account.id
|
||||
row.pay_id = order.pay_id
|
||||
row.amount_yuan = order.amount_yuan
|
||||
row.single_reward = order.single_reward
|
||||
row.status = "created"
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return row
|
||||
|
||||
|
||||
def persist_weibo_payment_status(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
status: WeiboPaymentStatus,
|
||||
) -> WeiboRechargeRecord | None:
|
||||
"""把充值支付查询结果写回对应记录。"""
|
||||
row = (
|
||||
db.query(WeiboRechargeRecord)
|
||||
.filter(
|
||||
WeiboRechargeRecord.account_id == account.id,
|
||||
or_(
|
||||
WeiboRechargeRecord.pay_id == status.pay_id,
|
||||
WeiboRechargeRecord.order_id == status.out_pay_id,
|
||||
),
|
||||
)
|
||||
.order_by(WeiboRechargeRecord.id.desc())
|
||||
.first()
|
||||
)
|
||||
if row is None:
|
||||
return None
|
||||
normalized = {
|
||||
"PAY_STATUS_SUCCESS": "paid",
|
||||
"PAY_STATUS_PENDING": "pending",
|
||||
"PAY_STATUS_FAILED": "failed",
|
||||
"PAY_STATUS_CLOSED": "closed",
|
||||
}.get(status.status, "unknown")
|
||||
now = _now()
|
||||
row.status = normalized
|
||||
row.sub_status = status.sub_status
|
||||
row.status_checked_at = now
|
||||
if normalized == "paid" and row.paid_at is None:
|
||||
row.paid_at = now
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return row
|
||||
|
||||
|
||||
def query_weibo_payment_status(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
|
||||
Reference in New Issue
Block a user