from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import Mapped, mapped_column
from datetime import datetime
from zoneinfo import ZoneInfo

Base = declarative_base()
local_timezone = ZoneInfo("Asia/Shanghai")


class DbCommon(Base):
    __abstract__ = True
    id: Mapped[int] = mapped_column(primary_key=True)
    create_time: Mapped[datetime] = mapped_column(default=datetime.now(local_timezone))
    update_time: Mapped[datetime] = mapped_column(default=datetime.now(local_timezone),
                                                  onupdate=datetime.now(local_timezone))

    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()
