File size: 1,635 Bytes
32ddb29
 
bf34e9d
32ddb29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf34e9d
 
 
 
 
32ddb29
 
 
 
 
 
 
 
 
bf34e9d
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from datetime import datetime

from sqlalchemy import Boolean, DateTime, Index, Integer, String, func
from sqlalchemy.orm import Mapped, mapped_column

from app.database import Base


class Category(Base):
    __tablename__ = "category"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    name: Mapped[str] = mapped_column(String(30), nullable=False, comment="分类名称")
    parent_id: Mapped[int | None] = mapped_column(Integer, comment="父分类ID")
    sort_order: Mapped[int] = mapped_column(Integer, default=0, comment="排序")
    is_active: Mapped[bool] = mapped_column(Boolean, default=True, comment="是否启用")
    created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now())

    __table_args__ = (
        # 分类树查询:活跃 + 排序
        Index("ix_category_active_sort", "is_active", "sort_order"),
    )


class CategoryMapping(Base):
    __tablename__ = "category_mapping"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    category_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="统一分类ID")
    source_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="资源站ID")
    source_type_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="源站分类ID")
    source_type_name: Mapped[str] = mapped_column(String(30), nullable=False, comment="源站分类名称")

    __table_args__ = (
        # 采集器分类映射查找:source_id + source_type_id 定位分类
        Index("ix_category_mapping_source_type", "source_id", "source_type_id"),
    )