108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
from datetime import date, datetime
|
||
from typing import Optional
|
||
|
||
from sqlalchemy import Column
|
||
from sqlmodel import Date, SQLModel, Field, INT, DateTime
|
||
from pydantic import field_validator
|
||
|
||
class LotteryDLT(SQLModel, table=True):
|
||
"""大乐透彩票数据表"""
|
||
__tablename__ = "lottery_dlt"
|
||
|
||
id: int | None = Field(default=None, primary_key=True, index=True)
|
||
|
||
issue: str = Field(
|
||
unique=True,
|
||
nullable=False,
|
||
max_length=10,
|
||
sa_column_kwargs={"comment": "彩票期号,唯一"}
|
||
)
|
||
"""彩票期号"""
|
||
|
||
game_name: str = Field(
|
||
max_length=50,
|
||
nullable=False,
|
||
sa_column_kwargs={"comment": "彩票种类名称"}
|
||
)
|
||
"""彩票种类名称"""
|
||
|
||
draw_date: date = Field(
|
||
sa_column = Column(Date, nullable=False, comment="开奖日期")
|
||
)
|
||
"""开奖日期"""
|
||
|
||
draw_time: datetime = Field(
|
||
sa_column=Column(
|
||
DateTime(timezone=True),
|
||
nullable=False,
|
||
comment="开奖时间"
|
||
)
|
||
)
|
||
"""开奖时间"""
|
||
|
||
# 前区排序后的5个号码
|
||
front_sort_1: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码1"))
|
||
"""前区排序后的号码1"""
|
||
front_sort_2: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码2"))
|
||
"""前区排序后的号码2"""
|
||
front_sort_3: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码3"))
|
||
"""前区排序后的号码3"""
|
||
front_sort_4: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码4"))
|
||
"""前区排序后的号码4"""
|
||
front_sort_5: Optional[int] = Field(sa_column=Column(INT, comment="前区排序后的号码5"))
|
||
"""前区排序后的号码5"""
|
||
|
||
# 后区排序后的2个号码
|
||
back_sort_1: Optional[int] = Field(sa_column=Column(INT, comment="后区排序后的号码1"))
|
||
"""后区排序后的号码1"""
|
||
back_sort_2: Optional[int] = Field(sa_column=Column(INT, comment="后区排序后的号码2"))
|
||
"""后区排序后的号码2"""
|
||
|
||
# 前区5个号码
|
||
front_1: int = Field(sa_column=Column(INT, comment="前区号码1"))
|
||
"""前区号码1"""
|
||
front_2: int = Field(sa_column=Column(INT, comment="前区号码2"))
|
||
"""前区号码2"""
|
||
front_3: int = Field(sa_column=Column(INT, comment="前区号码3"))
|
||
"""前区号码3"""
|
||
front_4: int = Field(sa_column=Column(INT, comment="前区号码4"))
|
||
"""前区号码4"""
|
||
front_5: int = Field(sa_column=Column(INT, comment="前区号码5"))
|
||
"""前区号码5"""
|
||
|
||
# 后区2个号码
|
||
back_1: int = Field(sa_column=Column(INT, comment="后区号码1"))
|
||
"""后区号码1"""
|
||
back_2: int = Field(sa_column=Column(INT, comment="后区号码2"))
|
||
"""后区号码2"""
|
||
|
||
# 元数据字段
|
||
created_at: datetime = Field(
|
||
default_factory=datetime.now,
|
||
sa_column_kwargs={"comment": "创建时间"}
|
||
)
|
||
"""创建时间"""
|
||
|
||
updated_at: datetime = Field(
|
||
default_factory=datetime.now,
|
||
sa_column_kwargs={"onupdate": datetime.now, "comment": "更新时间"}
|
||
)
|
||
"""更新时间"""
|
||
|
||
# Pydantic 校验器:确保号码合法性
|
||
@field_validator(
|
||
'front_1', 'front_2', 'front_3', 'front_4', 'front_5',
|
||
'front_sort_1', 'front_sort_2', 'front_sort_3', 'front_sort_4', 'front_sort_5'
|
||
)
|
||
@classmethod
|
||
def validate_front(cls, v):
|
||
if not 1 <= v <= 35:
|
||
raise ValueError(f'前区号码必须在1-35之间,当前值: {v}')
|
||
return v
|
||
|
||
@field_validator('back_1', 'back_2', 'back_sort_1', 'back_sort_2')
|
||
@classmethod
|
||
def validate_back(cls, v):
|
||
if not 1 <= v <= 12:
|
||
raise ValueError(f'后区号码必须在1-12之间,当前值: {v}')
|
||
return v |