优化提现相关接口。

This commit is contained in:
aaron 2025-02-09 00:18:52 +09:00
parent 84b6c50aeb
commit e6ab6a7558
2 changed files with 30 additions and 16 deletions

View File

@ -10,9 +10,19 @@ from app.core.response import success_response, error_response, ResponseModel
from decimal import Decimal
from typing import List, Optional, Dict
from datetime import datetime
from pydantic import BaseModel, Field
from app.core.account import AccountManager
router = APIRouter()
class WithdrawApproveRequest(BaseModel):
"""提现审核请求"""
transaction_id: str = Field(..., max_length=64) # 银行交易流水号
class WithdrawRejectRequest(BaseModel):
"""提现驳回请求"""
remark: str = Field(..., max_length=200) # 驳回原因
@router.post("", response_model=ResponseModel)
async def create_withdraw(
withdraw: WithdrawCreate,
@ -62,49 +72,50 @@ async def create_withdraw(
@router.post("/{withdraw_id}/approve", response_model=ResponseModel)
async def approve_withdraw(
withdraw_id: int,
request: WithdrawApproveRequest,
db: Session = Depends(get_db),
admin: UserDB = Depends(get_admin_user)
):
"""通过提现申请"""
"""审核通过提现申请(管理员)"""
withdraw = db.query(WithdrawDB).filter(
WithdrawDB.id == withdraw_id,
WithdrawDB.status == WithdrawStatus.PENDING
).first()
if not withdraw:
return error_response(code=404, message="提现申请不存在或已处理")
return error_response(code=404, message="提现申请不存在或状态不正确")
try:
# 更新提现状态
withdraw.status = WithdrawStatus.APPROVED
withdraw.transaction_id = request.transaction_id # 保存交易流水号
# 更新账户锁定余额
# 返还锁定余额
account = db.query(UserAccountDB).filter(
UserAccountDB.user_id == withdraw.user_id
).first()
account.lock_balance -= withdraw.amount
# 添加账户明细记录
detail = AccountDetailDB(
# 使用lock_balance 创建 AccountDetailDB 记录
account_detail = AccountDetailDB(
user_id=withdraw.user_id,
amount=float(withdraw.amount),
type=AccountDetailType.EXPENSE,
amount=withdraw.amount,
balance=account.balance,
description=f"提现到银行卡(尾号{withdraw.bank_card.card_number[-4:]}"
)
db.add(detail)
db.add(account_detail)
db.commit()
return success_response(message="提现申请已通过")
return success_response(data=WithdrawInfo.model_validate(withdraw))
except Exception as e:
db.rollback()
return error_response(code=500, message=f"处理失败: {str(e)}")
return error_response(code=500, message=f"审核失败: {str(e)}")
@router.post("/{withdraw_id}/reject", response_model=ResponseModel)
async def reject_withdraw(
withdraw_id: int,
remark: str,
request: WithdrawRejectRequest,
db: Session = Depends(get_db),
admin: UserDB = Depends(get_admin_user)
):
@ -120,7 +131,7 @@ async def reject_withdraw(
try:
# 更新提现状态
withdraw.status = WithdrawStatus.REJECTED
withdraw.remark = remark
withdraw.remark = request.remark
# 返还锁定余额
account = db.query(UserAccountDB).filter(
@ -204,7 +215,7 @@ async def get_all_withdraws(
withdraw_info_dict.update({
"bank_card_number": w.bank_card.card_number, # 银行卡号
"bank_name": w.bank_card.bank_name, # 银行名称
"name": w.bank_card.name # 持卡人姓名
"name": w.bank_card.name, # 持卡人姓名
})
withdraw_list.append(withdraw_info_dict)

View File

@ -20,6 +20,7 @@ class WithdrawDB(Base):
bank_card_id = Column(Integer, ForeignKey("user_bank_cards.id"), nullable=False)
amount = Column(DECIMAL(10, 2), nullable=False)
status = Column(Enum(WithdrawStatus), nullable=False, default=WithdrawStatus.PENDING)
transaction_id = Column(String(64)) # 银行交易流水号
remark = Column(String(200)) # 备注(驳回原因等)
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
@ -36,8 +37,10 @@ class WithdrawInfo(BaseModel):
bank_card_id: int
amount: float
status: WithdrawStatus
transaction_id: Optional[str] = None # 银行交易流水号
remark: Optional[str]
create_time: datetime
update_time: Optional[datetime]
class Config:
from_attributes = True