更新 用户积分逻辑。
This commit is contained in:
parent
9cc593b305
commit
39362f2bc8
@ -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()
|
||||||
|
|
||||||
@ -73,13 +73,12 @@ async def create_order(
|
|||||||
current_user.points -= deduct_amount
|
current_user.points -= deduct_amount
|
||||||
|
|
||||||
# 记录积分变动日志
|
# 记录积分变动日志
|
||||||
point_log = UserPointLogDB(
|
point_record = PointRecordDB(
|
||||||
user_id=current_user.userid,
|
user_id=current_user.userid,
|
||||||
change_amount=-deduct_amount, # 负数表示扣减
|
points=-float(deduct_amount), # 负数表示扣减
|
||||||
type=PointChangeType.CONSUME_DEDUCT,
|
description=f"订单消费抵扣"
|
||||||
remark=f"订单{order_id}消费抵扣"
|
|
||||||
)
|
)
|
||||||
db.add(point_log)
|
db.add(point_record)
|
||||||
|
|
||||||
db.add(db_order)
|
db.add(db_order)
|
||||||
|
|
||||||
@ -261,14 +260,12 @@ async def confirm_refund(
|
|||||||
# 返还用户积分
|
# 返还用户积分
|
||||||
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(
|
||||||
|
|||||||
@ -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
|
|
||||||
Loading…
Reference in New Issue
Block a user