更新 用户积分逻辑。

This commit is contained in:
aaron 2025-01-17 11:21:10 +08:00
parent 9cc593b305
commit 39362f2bc8
2 changed files with 14 additions and 50 deletions

View File

@ -16,7 +16,7 @@ from app.models.user import UserDB
from app.core.response import success_response, error_response, ResponseModel from app.core.response import success_response, error_response, ResponseModel
from datetime import datetime, timezone from datetime import datetime, timezone
from app.models.merchant import MerchantDB from app.models.merchant import MerchantDB
from app.models.user_point_log import UserPointLogDB, PointChangeType from app.models.point import PointRecordDB
router = APIRouter() router = APIRouter()
@ -71,16 +71,15 @@ async def create_order(
# 扣除用户积分并记录日志 # 扣除用户积分并记录日志
if deduct_amount > 0: if deduct_amount > 0:
current_user.points -= deduct_amount 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) db.add(db_order)
try: try:
@ -260,15 +259,13 @@ async def confirm_refund(
if order.deduct_amount > 0: if order.deduct_amount > 0:
# 返还用户积分 # 返还用户积分
order.user.points += float(order.deduct_amount) order.user.points += float(order.deduct_amount)
# 记录积分变动日志 point_record = PointRecordDB(
point_log = UserPointLogDB(
user_id=order.user_id, user_id=order.user_id,
change_amount=float(order.deduct_amount), # 正数表示返还 points=float(order.deduct_amount), # 正数表示返还
type=PointChangeType.CONSUME_RETURN, description=f"订单退款返还"
remark=f"订单{order_id}退款返还"
) )
db.add(point_log) db.add(point_record)
db.commit() db.commit()
return success_response( return success_response(

View File

@ -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