from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, DateTime, Table, MetaData,Text
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker

Base = declarative_base()
metadata = MetaData()

from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
from sqlalchemy.orm import relationship


class KnowledgeFile(Base):
    __tablename__ = 'knowledge_file'

    id = Column(Integer, primary_key=True)
    file_identifier = Column(String(50), unique=True, nullable=False, comment='文件唯一标识符')
    file_path = Column(Text, nullable=False, comment='文件存储路径')
    file_name = Column(String(255), nullable=False, comment='文件名字')
    is_deleted = Column(Integer, default=0, comment='是否删除')

    # 一个文件通过知识库的base_code来关联
    knowledge_base_code = Column(String(50), ForeignKey('knowledge_base.base_code'), nullable=False)
    knowledge_base = relationship("KnowledgeBase", back_populates="files")


class KnowledgeBase(Base):
    __tablename__ = 'knowledge_base'

    # 知识库表主键
    reportId = Column(String(255), primary_key=True)
    # 报告名称
    reportName = Column(String(400), comment='报告名称')
    # 主题词
    subject = Column(String(100), comment='主题词')
    # 事件类型 // 事件类型，字典项值（例如，自然灾害）
    eventType = Column(String(10), comment='事件类型')
    # 发布日期
    publishDate = Column(DateTime, comment='发布日期')
    # 来源单位
    publishingUnit = Column(String(255), comment='来源单位')
    # 摘要
    summary = Column(String(100), comment='摘要')
    # 知识类型notificationType
    notificationType = Column(String(100), comment='知识类型')

    # 知识库的基础编码
    base_code = Column(String(50), unique=True, comment='知识库的基础编码')

    # 关联文件
    files = relationship("KnowledgeFile", order_by=KnowledgeFile.id, back_populates="knowledge_base")
