From 3e90da49103c65b9ea32d2903dc964908af0a60e Mon Sep 17 00:00:00 2001 From: aaron <> Date: Thu, 13 Feb 2025 16:24:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=A7=AF=E5=88=86=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=B1=BB=E5=9E=8B=E4=B8=BA=E6=95=B4=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/merchant_order.py | 4 ++-- app/api/endpoints/merchant_pay_order.py | 12 ++++++------ app/api/endpoints/order.py | 16 ++++++++-------- app/api/endpoints/point.py | 2 +- app/models/merchant_order.py | 4 ++-- app/models/merchant_pay_order.py | 4 ++-- app/models/order.py | 3 +-- app/models/point.py | 2 +- app/models/user.py | 2 +- 9 files changed, 24 insertions(+), 25 deletions(-) diff --git a/app/api/endpoints/merchant_order.py b/app/api/endpoints/merchant_order.py index 1879921..8706339 100644 --- a/app/api/endpoints/merchant_order.py +++ b/app/api/endpoints/merchant_order.py @@ -75,7 +75,7 @@ async def create_merchant_order( merchant_product_id=order.merchant_product_id, order_amount=pay_amount, pay_amount=pay_amount, - gift_points=float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO, + gift_points=int(float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO), status=MerchantOrderStatus.CREATED, order_verify_code=verify_code ) @@ -360,7 +360,7 @@ async def calculate_order_price( return error_response(code=404, message="商品不存在") return success_response(data={ - "gift_points": float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO, + "gift_points": int(float(product.sale_price) * (float(product.gift_points_rate) / 100) * settings.POINT_RATIO), "amount": product.sale_price }) diff --git a/app/api/endpoints/merchant_pay_order.py b/app/api/endpoints/merchant_pay_order.py index f233197..427cfae 100644 --- a/app/api/endpoints/merchant_pay_order.py +++ b/app/api/endpoints/merchant_pay_order.py @@ -37,10 +37,10 @@ async def calculate_gift_points( return error_response(code=404, message="商家不存在") # 计算赠送积分 - gift_points = request.amount * (float(merchant.pay_gift_points_rate) / 100) * settings.POINT_RATIO + gift_points = int(request.amount * (float(merchant.pay_gift_points_rate) / 100) * settings.POINT_RATIO) return success_response(data={ - "gift_points": float(gift_points) + "gift_points": gift_points }) @router.post("", response_model=ResponseModel) @@ -66,7 +66,7 @@ async def create_pay_order( merchant_id=order.merchant_id, user_id=current_user.userid, amount=order.amount, - gift_points=order.amount * (float(merchant.pay_gift_points_rate) / 100) * settings.POINT_RATIO + gift_points=int(order.amount * (float(merchant.pay_gift_points_rate) / 100) * settings.POINT_RATIO) ) try: @@ -93,7 +93,7 @@ async def create_pay_order( merchant_id=order.merchant_id, user_id=order.user_id, amount=float(order.amount), - gift_points=float(order.gift_points), + gift_points=order.gift_points, status=order.status, create_time=order.create_time, update_time=order.update_time, @@ -144,7 +144,7 @@ async def list_pay_orders( merchant_id=order.MerchantPayOrderDB.merchant_id, user_id=order.MerchantPayOrderDB.user_id, amount=float(order.MerchantPayOrderDB.amount), - gift_points=float(order.MerchantPayOrderDB.gift_points), + gift_points=order.MerchantPayOrderDB.gift_points, status=order.MerchantPayOrderDB.status, create_time=order.MerchantPayOrderDB.create_time, update_time=order.MerchantPayOrderDB.update_time, @@ -208,7 +208,7 @@ async def admin_list_pay_orders( merchant_id=order.merchant_id, user_id=order.user_id, amount=float(order.amount), - gift_points=float(order.gift_points), + gift_points=order.gift_points, status=order.status, create_time=order.create_time, update_time=order.update_time, diff --git a/app/api/endpoints/order.py b/app/api/endpoints/order.py index 75754bb..8fc8cf8 100644 --- a/app/api/endpoints/order.py +++ b/app/api/endpoints/order.py @@ -90,14 +90,14 @@ def calculate_price(price_request: OrderPriceCalculateRequest,user: UserDB,db: S remaining_amount -= coupon_discount # 3. 如果没有优惠券,且用户有积分,则使用积分抵扣 elif user.points > 0: - # 计算最大可抵扣金额(1元=10积分) - max_points_discount = float(user.points) / settings.POINT_RATIO + # 计算最大可抵扣金额 + max_points_discount = user.points // settings.POINT_RATIO # 使用整除 points_discount_amount = min(remaining_amount, max_points_discount) - result.price_info.points_discount_amount = points_discount_amount - result.price_info.points_used = float(points_discount_amount * settings.POINT_RATIO) - result.used_points = float(points_discount_amount * settings.POINT_RATIO) # 记录使用的积分数 - remaining_amount -= points_discount_amount + if points_discount_amount > 0: + result.price_info.points_discount_amount = points_discount_amount + result.used_points = int(points_discount_amount * settings.POINT_RATIO) + remaining_amount -= points_discount_amount # 计算最终金额 result.price_info.final_amount = remaining_amount @@ -467,7 +467,7 @@ async def cancel_order( # 如果使用了积分,返还积分 if order.point_discount_amount > 0: # 返还积分 - return_points = float(order.point_discount_amount) * settings.POINT_RATIO + return_points = int(order.point_discount_amount * settings.POINT_RATIO) current_user.points += return_points # 记录积分变动 @@ -620,7 +620,7 @@ async def deliveryman_cancel_order( # 如果使用了积分,返还积分 if order.point_discount_amount > 0: # 返还积分 - return_points = float(order.point_discount_amount) * settings.POINT_RATIO + return_points = int(order.point_discount_amount * settings.POINT_RATIO) order_user = db.query(UserDB).filter( UserDB.userid == order.userid diff --git a/app/api/endpoints/point.py b/app/api/endpoints/point.py index f181154..2e4d5cb 100644 --- a/app/api/endpoints/point.py +++ b/app/api/endpoints/point.py @@ -68,6 +68,6 @@ async def get_point_records( return success_response(data={ "total": total, # 总记录数 - "total_points": float(total_points), # 当前总积分 + "total_points": total_points, # 当前总积分 "items": [PointRecordInfo.model_validate(r) for r in records] # 积分记录列表 }) \ No newline at end of file diff --git a/app/models/merchant_order.py b/app/models/merchant_order.py index c155ecd..c7e0f0b 100644 --- a/app/models/merchant_order.py +++ b/app/models/merchant_order.py @@ -26,7 +26,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) # 赠送的积分 + gift_points = Column(Integer, 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) @@ -47,7 +47,7 @@ class MerchantOrderInfo(BaseModel): merchant_product_id: int order_amount: float pay_amount: float - gift_points: float + gift_points: int status: MerchantOrderStatus order_verify_code: str verify_time: Optional[datetime] diff --git a/app/models/merchant_pay_order.py b/app/models/merchant_pay_order.py index 450783c..5d78bbe 100644 --- a/app/models/merchant_pay_order.py +++ b/app/models/merchant_pay_order.py @@ -21,7 +21,7 @@ class MerchantPayOrderDB(Base): merchant_id = Column(Integer, ForeignKey("merchants.id"), nullable=False) user_id = Column(Integer, ForeignKey("users.userid"), nullable=False) amount = Column(DECIMAL(10,2), nullable=False) - gift_points = Column(DECIMAL(10,1), nullable=False, default=0) + gift_points = Column(Integer, nullable=False, default=0) status = Column(Enum(MerchantPayOrderStatus), nullable=False, default=MerchantPayOrderStatus.UNPAID) pay_status = Column(Boolean, nullable=False, default=False) create_time = Column(DateTime(timezone=True), server_default=func.now()) @@ -39,7 +39,7 @@ class MerchantPayOrderInfo(BaseModel): merchant_id: int user_id: int amount: float - gift_points: float + gift_points: int status: MerchantPayOrderStatus create_time: datetime update_time: Optional[datetime] diff --git a/app/models/order.py b/app/models/order.py index b85c25d..a723702 100644 --- a/app/models/order.py +++ b/app/models/order.py @@ -124,7 +124,6 @@ class OrderPriceInfo(BaseModel): original_amount: float coupon_discount_amount: float = 0 points_discount_amount: float = 0 - points_used: Optional[float] = None coupon_id: Optional[int] = None final_amount: float @@ -140,5 +139,5 @@ class OrderPriceResult(BaseModel): """订单价格计算结果""" price_info: OrderPriceInfo used_coupon_id: Optional[int] = None # 使用的优惠券ID - used_points: Optional[float] = None # 使用的积分数 + used_points: Optional[int] = None # 使用的积分数 price_detail_text: Optional[str] = None # 价格详情文本 \ No newline at end of file diff --git a/app/models/point.py b/app/models/point.py index 82661eb..e3abd96 100644 --- a/app/models/point.py +++ b/app/models/point.py @@ -16,7 +16,7 @@ class PointRecordDB(Base): id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey("users.userid"), nullable=False) - points = Column(Integer, nullable=False) # 正数表示增加,负数表示减少 + points = Column(Integer, nullable=False) # 积分变动数量 type = Column(Enum(PointRecordType), nullable=False) order_id = Column(String(32), nullable=True, index=True) # 添加索引但不设置外键 description = Column(String(200), nullable=False) diff --git a/app/models/user.py b/app/models/user.py index be1e426..fd2d900 100644 --- a/app/models/user.py +++ b/app/models/user.py @@ -33,7 +33,7 @@ class UserDB(Base): password = Column(String(128), nullable=True) # 加密后的密码 avatar = Column(String(200), nullable=True) # 头像URL地址 gender = Column(Enum(Gender), nullable=False, default=Gender.UNKNOWN) - points = Column(DECIMAL(10,1), nullable=False, default=0.0) + points = Column(Integer, default=0) # 积分 roles = Column(JSON, default=lambda: [UserRole.USER]) # 存储角色列表 create_time = Column(DateTime(timezone=True), server_default=func.now()) update_time = Column(DateTime(timezone=True), onupdate=func.now())