ZHIWEI666 commited on
Commit
1953fc1
·
verified ·
1 Parent(s): 3c40bd1

Upload router_wallet.py

Browse files
Files changed (1) hide show
  1. router_wallet.py +16 -6
router_wallet.py CHANGED
@@ -929,13 +929,11 @@ async def refund_purchase(request: Request, account: str, item_id: str, db: Sess
929
  seller_wallet = db.query(Wallet).filter(Wallet.account == seller_account).with_for_update().first()
930
 
931
  if seller_wallet:
 
 
 
932
  seller_wallet.balance -= refund_amount # 卖家扣回
933
  # 不修改 earn_balance(保持"累计销售收益只增不减"语义)
934
-
935
- # P1 退款负数保护
936
- if seller_wallet.balance < 0:
937
- db.rollback()
938
- raise HTTPException(status_code=400, detail="卖家余额不足,无法处理退款")
939
 
940
  if buyer_wallet:
941
  buyer_wallet.balance += refund_amount # 买家退款
@@ -1071,7 +1069,19 @@ async def complete_withdrawal(
1071
  # 解冻余额(使用 net_amount 字段,确保解冻金额与冻结时一致)
1072
  wallet = db.query(Wallet).filter(Wallet.account == tx.account).first()
1073
  if wallet:
1074
- net_amount = tx.net_amount if tx.net_amount is not None else abs(tx.amount) # 优先使用 net_amount,兼容旧数据
 
 
 
 
 
 
 
 
 
 
 
 
1075
  wallet.frozen_balance = max(0, wallet.frozen_balance - net_amount)
1076
 
1077
  db.commit()
 
929
  seller_wallet = db.query(Wallet).filter(Wallet.account == seller_account).with_for_update().first()
930
 
931
  if seller_wallet:
932
+ # 先检查余额是否充足,再扣款
933
+ if seller_wallet.balance < refund_amount:
934
+ raise HTTPException(status_code=400, detail="卖家余额不足,无法处理退款")
935
  seller_wallet.balance -= refund_amount # 卖家扣回
936
  # 不修改 earn_balance(保持"累计销售收益只增不减"语义)
 
 
 
 
 
937
 
938
  if buyer_wallet:
939
  buyer_wallet.balance += refund_amount # 买家退款
 
1069
  # 解冻余额(使用 net_amount 字段,确保解冻金额与冻结时一致)
1070
  wallet = db.query(Wallet).filter(Wallet.account == tx.account).first()
1071
  if wallet:
1072
+ if tx.net_amount is not None:
1073
+ net_amount = tx.net_amount
1074
+ else:
1075
+ # 历史数据兼容:查找关联手续费交易
1076
+ fee_tx = db.query(Transaction).filter(
1077
+ Transaction.account == tx.account,
1078
+ Transaction.tx_type == "WITHDRAW_FEE",
1079
+ Transaction.created_at >= tx.created_at - datetime.timedelta(seconds=5),
1080
+ Transaction.created_at <= tx.created_at + datetime.timedelta(seconds=5)
1081
+ ).first()
1082
+ fee_amount = abs(fee_tx.amount) if fee_tx else 0
1083
+ net_amount = abs(tx.amount) - fee_amount
1084
+ logger.warning(f"WITHDRAW net_amount为NULL,已计算恢复: tx_id={tx.tx_id}, net={net_amount}, fee={fee_amount}")
1085
  wallet.frozen_balance = max(0, wallet.frozen_balance - net_amount)
1086
 
1087
  db.commit()