model.py 1.07 KB
from __future__ import annotations

from sqlalchemy import BigInteger, Column, String, DateTime, UniqueConstraint, Index, text

from app.infrastructure.database import Base


class CustomerGroupPhone(Base):
    __tablename__ = "customer_group_phones"

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    customer_name = Column(String(255), nullable=False)
    group_id = Column(String(255), nullable=False)
    group_status = Column(String(50), nullable=False, default="active")
    phone = Column(String(20), nullable=True)
    last_pushed_at = Column(DateTime, nullable=True)
    create_time = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"), nullable=False)
    update_time = Column(DateTime, server_default=text("CURRENT_TIMESTAMP"), onupdate=text("CURRENT_TIMESTAMP"), nullable=False)

    __table_args__ = (
        # 以 group_id 作为唯一键,它是群的稳定标识。
        UniqueConstraint("group_id", name="uk_group_id"),
        Index("idx_phone", "phone"),
        Index("idx_push_candidates", "group_status", "phone", "last_pushed_at"),
    )