From 39362f2bc8fd168434f9033ed58dcce9f3f671e2 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Fri, 17 Jan 2025 11:21:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=A7=AF=E5=88=86=E9=80=BB=E8=BE=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/merchant_order.py | 31 ++++++++++++--------------- app/models/user_point_log.py | 33 ----------------------------- 2 files changed, 14 insertions(+), 50 deletions(-) delete mode 100644 app/models/user_point_log.py diff --git a/app/api/endpoints/merchant_order.py b/app/api/endpoints/merchant_order.py index 97b8095..d493e07 100644 --- a/app/api/endpoints/merchant_order.py +++ b/app/api/endpoints/merchant_order.py @@ -16,7 +16,7 @@ from app.models.user import UserDB from app.core.response import success_response, error_response, ResponseModel from datetime import datetime, timezone from app.models.merchant import MerchantDB -from app.models.user_point_log import UserPointLogDB, PointChangeType +from app.models.point import PointRecordDB router = APIRouter() @@ -71,16 +71,15 @@ async def create_order( # 扣除用户积分并记录日志 if deduct_amount > 0: current_user.points -= deduct_amount - - # 记录积分变动日志 - point_log = UserPointLogDB( - user_id=current_user.userid, - change_amount=-deduct_amount, # 负数表示扣减 - type=PointChangeType.CONSUME_DEDUCT, - remark=f"订单{order_id}消费抵扣" - ) - db.add(point_log) + # 记录积分变动日志 + point_record = PointRecordDB( + user_id=current_user.userid, + points=-float(deduct_amount), # 负数表示扣减 + description=f"订单消费抵扣" + ) + db.add(point_record) + db.add(db_order) try: @@ -260,15 +259,13 @@ async def confirm_refund( if order.deduct_amount > 0: # 返还用户积分 order.user.points += float(order.deduct_amount) - - # 记录积分变动日志 - point_log = UserPointLogDB( + + point_record = PointRecordDB( user_id=order.user_id, - change_amount=float(order.deduct_amount), # 正数表示返还 - type=PointChangeType.CONSUME_RETURN, - remark=f"订单{order_id}退款返还" + points=float(order.deduct_amount), # 正数表示返还 + description=f"订单退款返还" ) - db.add(point_log) + db.add(point_record) db.commit() return success_response( diff --git a/app/models/user_point_log.py b/app/models/user_point_log.py deleted file mode 100644 index c8b7f67..0000000 --- a/app/models/user_point_log.py +++ /dev/null @@ -1,33 +0,0 @@ -from sqlalchemy import Column, Integer, DateTime, ForeignKey, Enum, String -from sqlalchemy.sql import func -from .database import Base -import enum -from pydantic import BaseModel -from datetime import datetime -from typing import Optional - -class PointChangeType(str, enum.Enum): - CONSUME_RETURN = "CONSUME_RETURN" # 消费返还 - CONSUME_DEDUCT = "CONSUME_DEDUCT" # 消费抵扣 - SYSTEM_SEND = "SYSTEM_SEND" # 系统发放 - -class UserPointLogDB(Base): - __tablename__ = "user_point_logs" - - id = Column(Integer, primary_key=True, autoincrement=True) - user_id = Column(Integer, ForeignKey("users.userid"), nullable=False) - change_amount = Column(Integer, nullable=False) # 变动数量,正数为增加,负数为减少 - type = Column(Enum(PointChangeType), nullable=False) - remark = Column(String(200)) # 备注说明 - create_time = Column(DateTime(timezone=True), server_default=func.now()) - -class UserPointLogInfo(BaseModel): - id: int - user_id: int - change_amount: int - type: PointChangeType - remark: Optional[str] - create_time: datetime - - class Config: - from_attributes = True \ No newline at end of file