535d97a70c
17-table PostgreSQL schema with full Chinese column comments, FastAPI project structure (admin/common/modules), DSL rule files, and schema migration scripts.
45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
"""LeAudit 域文档镜像模型 —— leaudit_documents 表。
|
|
|
|
通过 biz_document_id 关联业务 documents 表,不复制业务字段。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy import BigInteger, String, ForeignKey
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from fastapi_common.fastapi_common_web.models import BaseModel
|
|
|
|
|
|
class LeauditDocument(BaseModel):
|
|
"""LeAudit 文档镜像表。"""
|
|
|
|
__tablename__ = "leaudit_documents"
|
|
|
|
Id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
|
|
bizDocumentId: Mapped[int] = mapped_column(BigInteger, unique=True, comment="关联业务 documents.id")
|
|
typeId: Mapped[int | None] = mapped_column(BigInteger, comment="文档类型ID")
|
|
processingStatus: Mapped[str | None] = mapped_column(String(64), default="waiting", comment="waiting/processing/completed/failed")
|
|
currentRunId: Mapped[int | None] = mapped_column(BigInteger, comment="最新有效 run id")
|
|
|
|
@classmethod
|
|
async def get_by_biz_id(cls, session: AsyncSession, bizDocumentId: int) -> "LeauditDocument | None":
|
|
"""按业务文档 ID 查询。"""
|
|
from sqlalchemy import select
|
|
return await session.scalar(select(cls).where(cls.bizDocumentId == bizDocumentId))
|
|
|
|
@classmethod
|
|
async def upsert_by_biz_id(cls, session: AsyncSession, bizDocumentId: int, **fields) -> "LeauditDocument":
|
|
"""按业务文档 ID 创建或更新。"""
|
|
from sqlalchemy import select
|
|
doc = await session.scalar(select(cls).where(cls.bizDocumentId == bizDocumentId))
|
|
if doc is None:
|
|
doc = cls(bizDocumentId=bizDocumentId, **fields)
|
|
session.add(doc)
|
|
else:
|
|
for k, v in fields.items():
|
|
setattr(doc, k, v)
|
|
await session.flush()
|
|
return doc
|