移除团购积分抵扣
This commit is contained in:
parent
e073a5ed72
commit
d4ea6ae946
@ -22,7 +22,7 @@ from app.core.account import AccountManager
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("", response_model=ResponseModel)
|
@router.post("", response_model=ResponseModel)
|
||||||
async def create_order(
|
async def create_merchant_order(
|
||||||
order: MerchantOrderCreate,
|
order: MerchantOrderCreate,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
current_user: UserDB = Depends(get_current_user)
|
current_user: UserDB = Depends(get_current_user)
|
||||||
@ -35,16 +35,8 @@ 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)
|
original_amount = float(product.sale_price)
|
||||||
pay_amount = max(0, original_amount - deduct_amount)
|
pay_amount = original_amount
|
||||||
|
|
||||||
# 生成订单号和核销码
|
# 生成订单号和核销码
|
||||||
while True:
|
while True:
|
||||||
@ -63,27 +55,13 @@ async def create_order(
|
|||||||
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=original_amount,
|
order_amount=original_amount,
|
||||||
deduct_amount=deduct_amount,
|
|
||||||
pay_amount=pay_amount,
|
pay_amount=pay_amount,
|
||||||
status=MerchantOrderStatus.CREATED,
|
status=MerchantOrderStatus.CREATED,
|
||||||
order_verify_code=verify_code
|
order_verify_code=verify_code
|
||||||
)
|
)
|
||||||
|
|
||||||
# 扣除用户积分并记录日志
|
|
||||||
if deduct_amount > 0:
|
|
||||||
current_user.points -= deduct_amount
|
|
||||||
|
|
||||||
# 记录积分变动日志
|
|
||||||
point_record = PointRecordDB(
|
|
||||||
user_id=current_user.userid,
|
|
||||||
points=-float(deduct_amount), # 负数表示扣减
|
|
||||||
description=f"订单消费抵扣"
|
|
||||||
)
|
|
||||||
db.add(point_record)
|
|
||||||
|
|
||||||
db.add(db_order)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
db.add(db_order)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_order)
|
db.refresh(db_order)
|
||||||
return success_response(data=MerchantOrderInfo.model_validate(db_order))
|
return success_response(data=MerchantOrderInfo.model_validate(db_order))
|
||||||
@ -276,19 +254,6 @@ async def confirm_refund(
|
|||||||
try:
|
try:
|
||||||
# 更新状态为已退款
|
# 更新状态为已退款
|
||||||
order.status = MerchantOrderStatus.REFUNDED
|
order.status = MerchantOrderStatus.REFUNDED
|
||||||
|
|
||||||
# 如果有积分抵扣,返还积分
|
|
||||||
if order.deduct_amount > 0:
|
|
||||||
# 返还用户积分
|
|
||||||
order.user.points += float(order.deduct_amount)
|
|
||||||
|
|
||||||
point_record = PointRecordDB(
|
|
||||||
user_id=order.user_id,
|
|
||||||
points=float(order.deduct_amount), # 正数表示返还
|
|
||||||
description=f"订单退款返还"
|
|
||||||
)
|
|
||||||
db.add(point_record)
|
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
return success_response(
|
return success_response(
|
||||||
message="退款确认成功",
|
message="退款确认成功",
|
||||||
@ -375,22 +340,8 @@ async def calculate_order_price(
|
|||||||
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
|
|
||||||
|
|
||||||
# 计算最高可抵扣金额(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={
|
return success_response(data={
|
||||||
"original_price": float(product.sale_price),
|
"original_price": float(product.sale_price),
|
||||||
"available_points": available_points,
|
"final_amount": product.sale_price
|
||||||
"max_deduct_points": float(product.max_deduct_points),
|
|
||||||
"max_deduct_amount": max_deduct_amount,
|
|
||||||
"final_amount": final_amount
|
|
||||||
})
|
})
|
||||||
@ -24,7 +24,6 @@ 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)
|
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)
|
||||||
@ -43,7 +42,6 @@ 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
|
pay_amount: float
|
||||||
status: MerchantOrderStatus
|
status: MerchantOrderStatus
|
||||||
order_verify_code: str
|
order_verify_code: str
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user