41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Enum
|
|
from sqlalchemy.sql import func
|
|
from pydantic import BaseModel, Field
|
|
from typing import Optional
|
|
from datetime import datetime
|
|
from .database import Base
|
|
import enum
|
|
|
|
class PointRecordType(str, enum.Enum):
|
|
CONSUME_DEDUCT = "CONSUME_DEDUCT" # 消费抵扣
|
|
CONSUME_RETURN = "CONSUME_RETURN" # 消费返还
|
|
SYSTEM_SEND = "SYSTEM_SEND" # 系统发放
|
|
|
|
class PointRecordDB(Base):
|
|
__tablename__ = "point_records"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
user_id = Column(Integer, ForeignKey("users.userid"), 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)
|
|
create_time = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
class PointRecordCreate(BaseModel):
|
|
user_id: int = Field(..., description="用户ID")
|
|
points: float = Field(..., description="积分变动值,可为正或负")
|
|
type: PointRecordType = Field(..., description="积分变动类型")
|
|
description: str = Field(..., min_length=1, max_length=200)
|
|
|
|
class PointRecordInfo(BaseModel):
|
|
id: int
|
|
user_id: int
|
|
points: int
|
|
type: PointRecordType
|
|
order_id: Optional[str]
|
|
description: str
|
|
create_time: datetime
|
|
|
|
class Config:
|
|
from_attributes = True |