修复 bug
This commit is contained in:
parent
31d1083968
commit
b4fda2abc1
@ -32,6 +32,7 @@ async def account_summary(
|
||||
UserAccountDB.user_id == current_user.userid
|
||||
).first()
|
||||
balance = account.balance if account else Decimal('0')
|
||||
lock_balance = account.lock_balance if account else Decimal('0')
|
||||
|
||||
# 查询总收入记录数
|
||||
total = db.query(func.count(AccountDetailDB.id)).filter(
|
||||
@ -75,6 +76,7 @@ async def account_summary(
|
||||
|
||||
return success_response(data={
|
||||
"balance": float(balance), # 账户余额
|
||||
"lock_balance" : float(lock_balance), #锁定余额
|
||||
"total": total, # 总收入记录数
|
||||
"today_total": today_total, # 今日收入记录数
|
||||
"yesterday_total": yesterday_total, # 昨日收入记录数
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional
|
||||
from typing import Optional, List
|
||||
from app.models.merchant_pay_order import (
|
||||
MerchantPayOrderDB,
|
||||
MerchantPayOrderCreate,
|
||||
@ -10,7 +10,7 @@ from app.models.merchant_pay_order import (
|
||||
)
|
||||
from app.models.merchant import MerchantDB
|
||||
from app.models.database import get_db
|
||||
from app.api.deps import get_current_user
|
||||
from app.api.deps import get_current_user, get_admin_user
|
||||
from app.models.user import UserDB
|
||||
from app.core.response import success_response, error_response, ResponseModel
|
||||
from datetime import datetime, timezone
|
||||
@ -39,8 +39,7 @@ async def create_pay_order(
|
||||
order_id=order_id,
|
||||
merchant_id=order.merchant_id,
|
||||
user_id=current_user.userid,
|
||||
amount=order.amount,
|
||||
gift_points=order.gift_points
|
||||
amount=order.amount
|
||||
)
|
||||
|
||||
try:
|
||||
@ -132,6 +131,69 @@ async def list_pay_orders(
|
||||
) for order in orders
|
||||
]
|
||||
|
||||
return success_response(data={
|
||||
"total": total,
|
||||
"items": order_list
|
||||
})
|
||||
|
||||
@router.get("/admin/list", response_model=ResponseModel)
|
||||
async def admin_list_pay_orders(
|
||||
status: Optional[MerchantPayOrderStatus] = None,
|
||||
user_id: Optional[int] = None,
|
||||
order_id: Optional[str] = None,
|
||||
skip: int = 0,
|
||||
limit: int = 20,
|
||||
db: Session = Depends(get_db),
|
||||
admin_user: UserDB = Depends(get_admin_user)
|
||||
):
|
||||
"""获取支付订单列表(管理员接口)"""
|
||||
# 构建基础查询
|
||||
query = db.query(MerchantPayOrderDB).join(
|
||||
MerchantDB,
|
||||
MerchantPayOrderDB.merchant_id == MerchantDB.id
|
||||
)
|
||||
|
||||
# 添加筛选条件
|
||||
if status:
|
||||
query = query.filter(MerchantPayOrderDB.status == status)
|
||||
if user_id:
|
||||
query = query.filter(MerchantPayOrderDB.user_id == user_id)
|
||||
if order_id:
|
||||
query = query.filter(MerchantPayOrderDB.order_id == order_id)
|
||||
|
||||
# 获取总数
|
||||
total = query.count()
|
||||
|
||||
# 获取分页数据
|
||||
orders = query.order_by(
|
||||
MerchantPayOrderDB.create_time.desc()
|
||||
).offset(skip).limit(limit).all()
|
||||
|
||||
# 获取商家名称映射
|
||||
merchant_ids = [order.merchant_id for order in orders]
|
||||
merchant_names = dict(
|
||||
db.query(MerchantDB.id, MerchantDB.name).filter(
|
||||
MerchantDB.id.in_(merchant_ids)
|
||||
).all()
|
||||
)
|
||||
|
||||
# 处理返回数据
|
||||
order_list = [
|
||||
MerchantPayOrderInfo(
|
||||
id=order.id,
|
||||
order_id=order.order_id,
|
||||
merchant_id=order.merchant_id,
|
||||
user_id=order.user_id,
|
||||
amount=float(order.amount),
|
||||
gift_points=float(order.gift_points),
|
||||
status=order.status,
|
||||
create_time=order.create_time,
|
||||
update_time=order.update_time,
|
||||
pay_time=order.pay_time,
|
||||
merchant_name=merchant_names.get(order.merchant_id)
|
||||
) for order in orders
|
||||
]
|
||||
|
||||
return success_response(data={
|
||||
"total": total,
|
||||
"items": order_list
|
||||
|
||||
@ -231,17 +231,18 @@ async def payment_notify(
|
||||
return error_response(code=404, message="订单不存在")
|
||||
|
||||
if trade_state == "SUCCESS":
|
||||
# 添加积分
|
||||
points = order.pay_amount * 10
|
||||
point_manager = PointManager(db)
|
||||
point_manager.add_points(order.user_id, points, f"订单完成,获得{settings.POINT_ALIAS} 奖励")
|
||||
|
||||
# 更新订单状态
|
||||
order.pay_status = True
|
||||
order.status = MerchantOrderStatus.UNVERIFIED
|
||||
order.transaction_id = transaction_id
|
||||
order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
|
||||
|
||||
# 添加积分
|
||||
points = order.pay_amount * 10
|
||||
point_manager = PointManager(db)
|
||||
point_manager.add_points(order.userid, points, f"订单完成,获得{settings.POINT_ALIAS} 奖励")
|
||||
|
||||
order.gift_points = points
|
||||
|
||||
elif out_trade_no.startswith("P"): # 商家在线买单
|
||||
order = db.query(MerchantPayOrderDB).filter(
|
||||
MerchantPayOrderDB.order_id == out_trade_no
|
||||
@ -251,16 +252,17 @@ async def payment_notify(
|
||||
return error_response(code=404, message="订单不存在")
|
||||
|
||||
if trade_state == "SUCCESS":
|
||||
# 添加积分
|
||||
points = order.amount * 10
|
||||
point_manager = PointManager(db)
|
||||
point_manager.add_points(order.user_id, points, f"订单完成,获得{settings.POINT_ALIAS} 奖励")
|
||||
|
||||
# 更新订单状态
|
||||
order.pay_status = True
|
||||
order.status = MerchantPayOrderStatus.PAID
|
||||
order.transaction_id = transaction_id
|
||||
order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
|
||||
|
||||
# 添加积分
|
||||
points = order.amount * 10
|
||||
point_manager = PointManager(db)
|
||||
point_manager.add_points(order.userid, points, f"订单完成,获得{settings.POINT_ALIAS} 奖励")
|
||||
order.gift_points = points
|
||||
|
||||
else:
|
||||
return error_response(code=400, message="未知的订单类型")
|
||||
|
||||
@ -78,7 +78,7 @@ async def approve_withdraw(
|
||||
# 更新提现状态
|
||||
withdraw.status = WithdrawStatus.APPROVED
|
||||
|
||||
# 更新账户余额
|
||||
# 更新账户锁定余额
|
||||
account = db.query(UserAccountDB).filter(
|
||||
UserAccountDB.user_id == withdraw.user_id
|
||||
).first()
|
||||
@ -88,7 +88,7 @@ async def approve_withdraw(
|
||||
# 添加账户明细记录
|
||||
detail = AccountDetailDB(
|
||||
user_id=withdraw.user_id,
|
||||
type=AccountDetailType.WITHDRAW,
|
||||
type=AccountDetailType.EXPENSE,
|
||||
amount=withdraw.amount,
|
||||
balance=account.balance,
|
||||
description=f"提现到银行卡(尾号{withdraw.bank_card.card_number[-4:]})"
|
||||
|
||||
@ -25,6 +25,7 @@ class MerchantOrderDB(Base):
|
||||
merchant_product_id = Column(Integer, ForeignKey("merchant_products.id"), nullable=False)
|
||||
order_amount = Column(DECIMAL(10,2), nullable=False)
|
||||
pay_amount = Column(DECIMAL(10,2), nullable=False, default=0)
|
||||
gift_points = Column(DECIMAL(10,1), nullable=False, default=0) # 赠送的积分
|
||||
status = Column(Enum(MerchantOrderStatus), nullable=False, default=MerchantOrderStatus.CREATED)
|
||||
pay_status = Column(Boolean, nullable=False, default=False)
|
||||
order_verify_code = Column(String(21), unique=True, nullable=False)
|
||||
@ -46,6 +47,7 @@ class MerchantOrderInfo(BaseModel):
|
||||
merchant_product_id: int
|
||||
order_amount: float
|
||||
pay_amount: float
|
||||
gift_points: float
|
||||
status: MerchantOrderStatus
|
||||
order_verify_code: str
|
||||
verify_time: Optional[datetime]
|
||||
|
||||
@ -32,7 +32,6 @@ class MerchantPayOrderDB(Base):
|
||||
class MerchantPayOrderCreate(BaseModel):
|
||||
merchant_id: int
|
||||
amount: float = Field(..., gt=0)
|
||||
gift_points: float = Field(0, ge=0)
|
||||
|
||||
class MerchantPayOrderInfo(BaseModel):
|
||||
id: int
|
||||
|
||||
Loading…
Reference in New Issue
Block a user