增加商家订单积分抵扣的逻辑。
This commit is contained in:
parent
3bc27ea65d
commit
9e8bb7747f
@ -33,11 +33,21 @@ async def create_order(
|
|||||||
if not product:
|
if not product:
|
||||||
return error_response(code=404, message="商品不存在")
|
return error_response(code=404, message="商品不存在")
|
||||||
|
|
||||||
|
# 计算积分抵扣
|
||||||
|
available_points = current_user.points or 0
|
||||||
|
deduct_amount = min(
|
||||||
|
float(product.max_deduct_points),
|
||||||
|
available_points
|
||||||
|
) if available_points > 0 else 0
|
||||||
|
|
||||||
|
# 计算实际支付金额
|
||||||
|
original_amount = float(product.sale_price)
|
||||||
|
pay_amount = max(0, original_amount - deduct_amount)
|
||||||
|
|
||||||
# 生成订单号和核销码
|
# 生成订单号和核销码
|
||||||
while True:
|
while True:
|
||||||
order_id = generate_order_id()
|
order_id = generate_order_id()
|
||||||
verify_code = generate_verify_code()
|
verify_code = generate_verify_code()
|
||||||
# 检查是否已存在
|
|
||||||
exists = db.query(MerchantOrderDB).filter(
|
exists = db.query(MerchantOrderDB).filter(
|
||||||
(MerchantOrderDB.order_id == order_id) |
|
(MerchantOrderDB.order_id == order_id) |
|
||||||
(MerchantOrderDB.order_verify_code == verify_code)
|
(MerchantOrderDB.order_verify_code == verify_code)
|
||||||
@ -50,10 +60,17 @@ async def create_order(
|
|||||||
order_id=order_id,
|
order_id=order_id,
|
||||||
user_id=current_user.userid,
|
user_id=current_user.userid,
|
||||||
merchant_product_id=order.merchant_product_id,
|
merchant_product_id=order.merchant_product_id,
|
||||||
order_amount=order.order_amount,
|
order_amount=original_amount,
|
||||||
status=MerchantOrderStatus.CREATED, # 创建时状态为已下单
|
deduct_amount=deduct_amount,
|
||||||
|
pay_amount=pay_amount,
|
||||||
|
status=MerchantOrderStatus.CREATED,
|
||||||
order_verify_code=verify_code
|
order_verify_code=verify_code
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 扣除用户积分
|
||||||
|
if deduct_amount > 0:
|
||||||
|
current_user.points -= deduct_amount
|
||||||
|
|
||||||
db.add(db_order)
|
db.add(db_order)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -289,4 +306,44 @@ async def get_order_detail(
|
|||||||
"merchant_phone": order.merchant_phone
|
"merchant_phone": order.merchant_phone
|
||||||
}
|
}
|
||||||
|
|
||||||
return success_response(data=order_data)
|
return success_response(data=order_data)
|
||||||
|
|
||||||
|
@router.post("/calculate-price", response_model=ResponseModel)
|
||||||
|
async def calculate_order_price(
|
||||||
|
order: MerchantOrderCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
current_user: UserDB = Depends(get_current_user)
|
||||||
|
):
|
||||||
|
"""计算订单价格
|
||||||
|
|
||||||
|
1. 获取商品信息和用户积分
|
||||||
|
2. 计算最高可抵扣金额
|
||||||
|
3. 返回最终支付金额
|
||||||
|
"""
|
||||||
|
# 查询商品信息
|
||||||
|
product = db.query(MerchantProductDB).filter(
|
||||||
|
MerchantProductDB.id == order.merchant_product_id
|
||||||
|
).first()
|
||||||
|
|
||||||
|
if not product:
|
||||||
|
return error_response(code=404, message="商品不存在")
|
||||||
|
|
||||||
|
# 获取用户积分
|
||||||
|
available_points = current_user.points or 0
|
||||||
|
|
||||||
|
# 计算最高可抵扣金额(1积分 = 1分钱)
|
||||||
|
max_deduct_amount = min(
|
||||||
|
float(product.max_deduct_points), # 商品最高可抵扣金额
|
||||||
|
available_points # 用户可用积分
|
||||||
|
) if available_points > 0 else 0
|
||||||
|
|
||||||
|
# 计算最终支付金额
|
||||||
|
final_amount = max(0, float(product.sale_price) - max_deduct_amount)
|
||||||
|
|
||||||
|
return success_response(data={
|
||||||
|
"original_price": float(product.sale_price),
|
||||||
|
"available_points": available_points,
|
||||||
|
"max_deduct_points": float(product.max_deduct_points),
|
||||||
|
"max_deduct_amount": max_deduct_amount,
|
||||||
|
"final_amount": final_amount
|
||||||
|
})
|
||||||
@ -24,6 +24,8 @@ class MerchantOrderDB(Base):
|
|||||||
user_id = Column(Integer, ForeignKey("users.userid"), nullable=False)
|
user_id = Column(Integer, ForeignKey("users.userid"), nullable=False)
|
||||||
merchant_product_id = Column(Integer, ForeignKey("merchant_products.id"), nullable=False)
|
merchant_product_id = Column(Integer, ForeignKey("merchant_products.id"), nullable=False)
|
||||||
order_amount = Column(DECIMAL(10,2), nullable=False)
|
order_amount = Column(DECIMAL(10,2), nullable=False)
|
||||||
|
deduct_amount = Column(DECIMAL(10,2), nullable=False, default=0)
|
||||||
|
pay_amount = Column(DECIMAL(10,2), nullable=False, default=0)
|
||||||
status = Column(Enum(MerchantOrderStatus), nullable=False, default=MerchantOrderStatus.CREATED)
|
status = Column(Enum(MerchantOrderStatus), nullable=False, default=MerchantOrderStatus.CREATED)
|
||||||
order_verify_code = Column(String(21), unique=True, nullable=False)
|
order_verify_code = Column(String(21), unique=True, nullable=False)
|
||||||
verify_time = Column(DateTime(timezone=True), nullable=True)
|
verify_time = Column(DateTime(timezone=True), nullable=True)
|
||||||
@ -41,6 +43,8 @@ class MerchantOrderInfo(BaseModel):
|
|||||||
user_id: int
|
user_id: int
|
||||||
merchant_product_id: int
|
merchant_product_id: int
|
||||||
order_amount: float
|
order_amount: float
|
||||||
|
deduct_amount: float
|
||||||
|
pay_amount: float
|
||||||
status: MerchantOrderStatus
|
status: MerchantOrderStatus
|
||||||
order_verify_code: str
|
order_verify_code: str
|
||||||
verify_time: Optional[datetime]
|
verify_time: Optional[datetime]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user