增加在线订单商户结算逻辑

This commit is contained in:
aaron 2025-02-06 22:55:55 +09:00
parent c6df387b1f
commit 5099de621c
2 changed files with 26 additions and 8 deletions

View File

@ -16,6 +16,7 @@ import random
import string
from app.models.merchant_order import MerchantOrderDB, MerchantOrderStatus
from app.models.merchant_pay_order import MerchantPayOrderDB, MerchantPayOrderStatus
from app.models.merchant import MerchantDB
import enum
from app.core.point_manager import PointManager
from app.core.point_manager import PointRecordType
@ -230,7 +231,7 @@ async def payment_notify(
account_manager.change_balance(
user_id=order.deliveryman_user_id,
amount=deliveryman_share,
description=f"配送订单收益",
description=f"配送订单结算",
transaction_id=order.orderid
)
@ -256,8 +257,11 @@ async def payment_notify(
order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
elif out_trade_no.startswith("P"): # 商家在线买单
order = db.query(MerchantPayOrderDB).filter(
order = db.query(MerchantPayOrderDB,MerchantDB).filter(
MerchantPayOrderDB.order_id == out_trade_no
).join(
MerchantDB,
MerchantPayOrderDB.merchant_id == MerchantDB.id
).first()
if not order:
@ -266,13 +270,23 @@ async def payment_notify(
if trade_state_desc == "支付成功":
# 添加积分
point_manager = PointManager(db)
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"买单订单奖励", order.order_id)
point_manager.add_points(order.MerchantPayOrderDB.user_id, order.MerchantPayOrderDB.gift_points, PointRecordType.CONSUME_RETURN, f"买单订单奖励", order.MerchantPayOrderDB.order_id)
# 更新订单状态
order.pay_status = True
order.status = MerchantPayOrderStatus.PAID
order.transaction_id = transaction_id
order.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
order.MerchantPayOrderDB.pay_status = True
order.MerchantPayOrderDB.status = MerchantPayOrderStatus.PAID
order.MerchantPayOrderDB.transaction_id = transaction_id
order.MerchantPayOrderDB.pay_time = datetime.fromisoformat(success_time.replace('Z', '+00:00'))
# 对商家进行分账计算
account_manager = AccountManager(db)
settlement_amount = float(order.MerchantPayOrderDB.amount) * float(order.MerchantDB.pay_share_rate) / 100
account_manager.change_balance(
user_id=order.MerchantDB.user_id,
amount=settlement_amount,
description=f"在线买单结算",
transaction_id=order.MerchantPayOrderDB.order_id
)
else:
return error_response(code=400, message="未知的订单类型")

View File

@ -19,10 +19,11 @@ class MerchantDB(Base):
latitude = Column(DECIMAL(9, 6), nullable=False) # 纬度精确到小数点后6位
phone = Column(String(20), nullable=False)
brand_image_url = Column(String(200)) # 品牌图片地址
pay_gift_points_rate = Column(DECIMAL(4,2), nullable=False, default=0.00) # 支付赠送积分比例默认0%
pay_share_rate = Column(DECIMAL(4,2), nullable=False, default=0.00) # 在线买单分润比例默认0%
create_time = Column(DateTime(timezone=True), server_default=func.now())
update_time = Column(DateTime(timezone=True), onupdate=func.now())
category_id = Column(Integer, ForeignKey("merchant_categories.id"), nullable=True)
pay_gift_points_rate = Column(DECIMAL(4,2), nullable=False, default=0.00) # 支付赠送积分比例默认0%
class MerchantCreate(BaseModel):
user_id: int
@ -33,6 +34,7 @@ class MerchantCreate(BaseModel):
latitude: float = Field(..., ge=-90, le=90, description="纬度")
phone: str = Field(..., max_length=20, pattern=r'^\d+$')
pay_gift_points_rate: Optional[float] = Field(10.00, ge=0, le=100) # 支付赠送积分比例
pay_share_rate: Optional[float] = Field(0.00, ge=0, le=100) # 在线买单分润比例
brand_image_url: Optional[str] = Field(None, max_length=200)
category_id: Optional[int] = None
@ -45,6 +47,7 @@ class MerchantUpdate(BaseModel):
latitude: Optional[float] = Field(None, ge=-90, le=90, description="纬度")
phone: Optional[str] = Field(None, max_length=20, pattern=r'^\d+$')
pay_gift_points_rate: Optional[float] = Field(None, ge=0, le=100) # 支付赠送积分比例
pay_share_rate: Optional[float] = Field(None, ge=0, le=100) # 在线买单分润比例
brand_image_url: Optional[str] = Field(None, max_length=200)
category_id: Optional[int] = None
@ -61,6 +64,7 @@ class MerchantInfo(BaseModel):
latitude: float
phone: str
pay_gift_points_rate: float
pay_share_rate: float
brand_image_url: Optional[str] = None
create_time: datetime
update_time: Optional[datetime]