Merge branch 'dev' into runny827

feat(training): 新增精益培训视频与周任务管理

# Conflicts:
#	backend/app/models/__init__.py
This commit is contained in:
runny committed 2026-09-08 18:30:09 +08:00
1 parent 85d27e9ffa
commit 91b7233872
812 files changed
+73763 -18908

No files matched your search

+124 -58
View File
@@ -1,9 +1,9 @@
"""
backend/app/models/voucher_check.py
用途:立账凭证检核单票最新状态与批次明细 ORM。
用途:立账凭证检核 ORM(Profile / 批次 / 主体 / 单文件结果 / 企微通知)。
依赖:models.base.Base、schedule_runs。
逻辑:runner/persistence 写入;query_service 与工作台 API 读取;增量跳过依赖 invoice_states。
逻辑:引擎写入 profiles/runs/subjects/file_results/notifications;工作台与查询服务只读这些表。
"""
from __future__ import annotations
@@ -13,7 +13,6 @@ from typing import Optional
from sqlalchemy import (
BigInteger,
Boolean,
Date,
DateTime,
ForeignKey,
@@ -28,41 +27,20 @@ from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
class VoucherCheckInvoiceState(Base):
"""账套+月份+发票号的最新材料齐全状态(增量跳过真相源)。"""
class VoucherCheckProfile(Base):
"""已发布 Profile 版本;批次绑定不可变快照,运行时不以本表热更新覆盖。"""
__tablename__ = "voucher_check_invoice_states"
__tablename__ = "voucher_check_profiles"
__table_args__ = (
UniqueConstraint(
"scope_code",
"period_month",
"invoice_no",
name="uq_voucher_check_invoice_states_scope_period_invoice",
),
Index("ix_voucher_check_invoice_states_period_status", "period_month", "status"),
Index("ix_voucher_check_invoice_states_invoice_no", "invoice_no"),
Index("ix_voucher_check_invoice_states_notified_on", "last_missing_notified_on"),
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)
scope_code: Mapped[str] = mapped_column(String(32), nullable=False)
period_month: Mapped[date] = mapped_column(Date, nullable=False)
invoice_no: Mapped[str] = mapped_column(String(128), nullable=False)
invoice_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
status: Mapped[str] = mapped_column(String(24), nullable=False)
folder_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
folder_exists: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
material_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
matched_files_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
missing_materials_json: Mapped[Optional[list]] = mapped_column(JSON, nullable=True)
last_actual_run_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("schedule_runs.id", ondelete="SET NULL"),
nullable=True,
)
last_checked_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
last_seen_erp_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
last_missing_notified_on: Mapped[Optional[date]] = mapped_column(Date, nullable=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,
@@ -72,43 +50,131 @@ class VoucherCheckInvoiceState(Base):
)
class VoucherCheckRunItem(Base):
"""单次 ScheduleRun 的发票检核快照(含跳过项,供审计与 Excel)。"""
class VoucherCheckRun(Base):
"""一次检核批次;列表统计取本表快照,不在查询时重算单文件结果。"""
__tablename__ = "voucher_check_run_items"
__tablename__ = "voucher_check_runs"
__table_args__ = (
UniqueConstraint(
"schedule_run_id",
"invoice_no",
name="uq_voucher_check_run_items_run_invoice",
),
Index("ix_voucher_check_run_items_run_result", "schedule_run_id", "result"),
Index("ix_voucher_check_run_items_period_result", "period_month", "result"),
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[int] = mapped_column(
schedule_run_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("schedule_runs.id", ondelete="CASCADE"),
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_state_id: Mapped[Optional[int]] = mapped_column(
BigInteger,
ForeignKey("voucher_check_invoice_states.id", ondelete="SET NULL"),
nullable=True,
)
scope_code: Mapped[str] = mapped_column(String(32), nullable=False)
period_month: Mapped[date] = mapped_column(Date, nullable=False)
invoice_no: Mapped[str] = mapped_column(String(128), nullable=False)
invoice_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
result: Mapped[str] = mapped_column(String(24), nullable=False)
folder_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
folder_exists: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
material_fingerprint: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
matched_files_json: Mapped[Optional[dict]] = mapped_column(JSON, nullable=True)
missing_materials_json: Mapped[Optional[list]] = mapped_column(JSON, 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)