""" backend/app/models/voucher_check.py 用途:立账凭证检核 ORM(Profile / 批次 / 主体 / 单文件结果 / 企微通知)。 依赖:models.base.Base、schedule_runs。 逻辑:引擎写入 profiles/runs/subjects/file_results/notifications;工作台与查询服务只读这些表。 """ from __future__ import annotations from datetime import date, datetime from typing import Optional from sqlalchemy import ( BigInteger, Date, DateTime, ForeignKey, Index, JSON, String, Text, UniqueConstraint, ) from sqlalchemy.orm import Mapped, mapped_column from app.models.base import Base class VoucherCheckProfile(Base): """已发布 Profile 版本;批次绑定不可变快照,运行时不以本表热更新覆盖。""" __tablename__ = "voucher_check_profiles" __table_args__ = ( UniqueConstraint("profile_code", "version", name="uq_voucher_check_profiles_code_version"), Index("ix_voucher_check_profiles_status", "status"), ) id: Mapped[int] = mapped_column(BigInteger, primary_key=True) profile_code: Mapped[str] = mapped_column(String(32), nullable=False) version: Mapped[str] = mapped_column(String(32), nullable=False) definition_json: Mapped[dict] = mapped_column(JSON, nullable=False) status: Mapped[str] = mapped_column(String(24), nullable=False, default="published") created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) updated_at: Mapped[datetime] = mapped_column( DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False, ) class VoucherCheckRun(Base): """一次检核批次;列表统计取本表快照,不在查询时重算单文件结果。""" __tablename__ = "voucher_check_runs" __table_args__ = ( Index("ix_voucher_check_runs_period_profile", "period_month", "profile_code"), Index("ix_voucher_check_runs_status", "status"), Index("ix_voucher_check_runs_finished_at", "finished_at"), ) id: Mapped[int] = mapped_column(BigInteger, primary_key=True) schedule_run_id: Mapped[Optional[int]] = mapped_column( BigInteger, ForeignKey("schedule_runs.id", ondelete="SET NULL"), nullable=True, index=True, ) run_no: Mapped[str] = mapped_column(String(64), nullable=False) profile_code: Mapped[str] = mapped_column(String(32), nullable=False) profile_version: Mapped[str] = mapped_column(String(32), nullable=False) profile_snapshot_json: Mapped[dict] = mapped_column(JSON, nullable=False) period_month: Mapped[date] = mapped_column(Date, nullable=False) mode: Mapped[str] = mapped_column(String(24), nullable=False, default="incremental") status: Mapped[str] = mapped_column(String(24), nullable=False, default="running") subject_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) archived_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) skipped_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) missing_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) missing_invoice_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) technical_error_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) staged_file_count: Mapped[int] = mapped_column(BigInteger, nullable=False, default=0) stats_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) started_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) class VoucherCheckSubject(Base): """批次内一张销售 invoice 主体。""" __tablename__ = "voucher_check_subjects" __table_args__ = ( UniqueConstraint("run_id", "invoice_no", name="uq_voucher_check_subjects_run_invoice"), Index("ix_voucher_check_subjects_invoice_no", "invoice_no"), ) id: Mapped[int] = mapped_column(BigInteger, primary_key=True) run_id: Mapped[int] = mapped_column( BigInteger, ForeignKey("voucher_check_runs.id", ondelete="CASCADE"), nullable=False, index=True, ) invoice_no: Mapped[str] = mapped_column(String(128), nullable=False) invoice_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True) source_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) attributes_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) class VoucherCheckFileResult(Base): """invoice × 文件规则 的单文件结果,是缺件统计与 Excel 的真相源。""" __tablename__ = "voucher_check_file_results" __table_args__ = ( UniqueConstraint( "run_id", "invoice_no", "rule_code", name="uq_voucher_check_file_results_run_invoice_rule", ), Index("ix_voucher_check_file_results_status", "run_id", "status"), Index("ix_voucher_check_file_results_invoice", "invoice_no"), ) id: Mapped[int] = mapped_column(BigInteger, primary_key=True) run_id: Mapped[int] = mapped_column( BigInteger, ForeignKey("voucher_check_runs.id", ondelete="CASCADE"), nullable=False, index=True, ) subject_id: Mapped[Optional[int]] = mapped_column( BigInteger, ForeignKey("voucher_check_subjects.id", ondelete="SET NULL"), nullable=True, ) invoice_no: Mapped[str] = mapped_column(String(128), nullable=False) rule_code: Mapped[str] = mapped_column(String(64), nullable=False) rule_name: Mapped[str] = mapped_column(String(128), nullable=False) source_type: Mapped[str] = mapped_column(String(32), nullable=False) expected_name: Mapped[Optional[str]] = mapped_column(String(512), nullable=True) matched_name: Mapped[Optional[str]] = mapped_column(String(512), nullable=True) source_folder_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) source_item_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) archive_folder_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) archive_item_id: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) source_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True) archive_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True) status: Mapped[str] = mapped_column(String(24), nullable=False) missing_reason: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) error_code: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True) source_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True) checked_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False) class VoucherCheckNotification(Base): """一个检核批次对应一条企微汇总通知审计。""" __tablename__ = "voucher_check_notifications" __table_args__ = (Index("ix_voucher_check_notifications_status", "send_status"),) id: Mapped[int] = mapped_column(BigInteger, primary_key=True) run_id: Mapped[int] = mapped_column( BigInteger, ForeignKey("voucher_check_runs.id", ondelete="CASCADE"), nullable=False, unique=True, ) excel_filename: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) recipient_user_ids_json: Mapped[Optional[list]] = mapped_column(JSON, nullable=True) send_status: Mapped[str] = mapped_column(String(32), nullable=False) skip_reason: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) summary_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True) sent_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True) created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)