""" 栏目管理模型 """ from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, func from sqlalchemy.orm import relationship from app.database import Base class ColumnModel(Base): """CMS 栏目(两级架构:顶级栏目 + 二级子栏目)""" __tablename__ = "columns" id = Column(Integer, primary_key=True, index=True) name = Column(String(50), nullable=False, comment="栏目名称") slug = Column(String(50), unique=True, nullable=False, comment="URL标识") description = Column(String(200), comment="栏目描述") parent_id = Column(Integer, ForeignKey("columns.id"), nullable=True, comment="父栏目ID") sort_order = Column(Integer, default=0, comment="排序权重") is_enabled = Column(Boolean, default=True, comment="是否前端可见") display_mode = Column(String(20), default="list", comment="展示模式: list|card") layout_style = Column(String(20), default="left", comment="布局样式: left|right|top") icon_name = Column(String(50), nullable=True, comment="图标名称(lucide图标)") created_at = Column(DateTime, server_default=func.now(), comment="创建时间") updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment="更新时间") # 自关联关系 parent = relationship("ColumnModel", remote_side=[id], back_populates="children") children = relationship("ColumnModel", back_populates="parent", cascade="all, delete-orphan") def to_dict(self, include_content_count: bool = False): """转换为字典""" data = { "id": self.id, "name": self.name, "slug": self.slug, "description": self.description, "parent_id": self.parent_id, "sort_order": self.sort_order, "is_enabled": self.is_enabled, "display_mode": self.display_mode, "layout_style": self.layout_style, "icon_name": self.icon_name, "created_at": self.created_at.isoformat() if self.created_at else None, "updated_at": self.updated_at.isoformat() if self.updated_at else None, } if include_content_count and hasattr(self, "content_count"): data["content_count"] = self.content_count return data