feat(contract-payment): 采购合同付款预警
This commit is contained in:
1 parent
5aa431cc8e
commit
03e5e88e27
113 files changed
+11381
-2991
No files matched your search
@@ -0,0 +1,208 @@
|
||||
"""
|
||||
backend/app/models/contract_payment.py
|
||||
|
||||
用途:采购合同付款预警域模型,保存 OA 合同台账、附件来源、付款节点、履约事件和企微预警幂等记录。
|
||||
依赖:models.base.Base、users、file_assets。
|
||||
逻辑:同步服务写入合同、付款请款与事件,解析服务生成付款节点,预警服务按节点状态写入提醒记录并调用企业微信。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date, datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any, Optional
|
||||
|
||||
from sqlalchemy import Date, DateTime, ForeignKey, Index, JSON, LargeBinary, Numeric, String, Text, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class ContractPaymentContract(Base):
|
||||
"""OA 归档后的采购合同主记录。"""
|
||||
|
||||
__tablename__ = "contract_payment_contracts"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("contract_no", name="uq_contract_payment_contracts_contract_no"),
|
||||
Index("ix_contract_payment_contracts_purchase_order_no", "purchase_order_no"),
|
||||
Index("ix_contract_payment_contracts_status", "status"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
# 合同附件解析前可能为空;OA 表单中的流程编号不再写入此字段。
|
||||
contract_no: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
purchase_order_no: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
supplier_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
applicant_name: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
total_amount: Mapped[Optional[Decimal]] = mapped_column(Numeric(18, 2), nullable=True)
|
||||
currency: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
|
||||
effective_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
|
||||
delivery_due_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
|
||||
warranty_period_days: Mapped[Optional[int]] = mapped_column(nullable=True)
|
||||
owner_user_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True, index=True)
|
||||
oa_record_id: Mapped[Optional[str]] = mapped_column(String(128), nullable=True, index=True)
|
||||
workflow_no: Mapped[Optional[str]] = mapped_column(String(128), nullable=True, index=True)
|
||||
oa_archive_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="active", nullable=False)
|
||||
parse_status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False, index=True)
|
||||
plan_status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False, index=True)
|
||||
source_payload_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
parse_result_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
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 ContractPaymentAttachment(Base):
|
||||
"""合同附件来源记录;OA 为主来源,文档管理系统为延迟补充。"""
|
||||
|
||||
__tablename__ = "contract_payment_attachments"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("contract_id", "source", "source_key", name="uq_contract_payment_attachments_source"),
|
||||
Index("ix_contract_payment_attachments_contract", "contract_id"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
contract_id: Mapped[int] = mapped_column(ForeignKey("contract_payment_contracts.id", ondelete="CASCADE"), nullable=False)
|
||||
source: Mapped[str] = mapped_column(String(32), nullable=False) # oa | dms
|
||||
source_key: Mapped[str] = mapped_column(String(512), nullable=False)
|
||||
file_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
mime_type: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
file_asset_id: Mapped[Optional[int]] = mapped_column(ForeignKey("file_assets.id"), nullable=True)
|
||||
dms_path: Mapped[Optional[str]] = mapped_column(String(1024), nullable=True)
|
||||
created_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
metadata_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="discovered", nullable=False)
|
||||
created_record_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
|
||||
class ContractPaymentNode(Base):
|
||||
"""从合同条款编译出的付款节点。"""
|
||||
|
||||
__tablename__ = "contract_payment_nodes"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("contract_id", "node_code", name="uq_contract_payment_nodes_code"),
|
||||
Index("ix_contract_payment_nodes_due_date", "due_date", "status"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
contract_id: Mapped[int] = mapped_column(ForeignKey("contract_payment_contracts.id", ondelete="CASCADE"), nullable=False)
|
||||
node_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
node_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
amount: Mapped[Optional[Decimal]] = mapped_column(Numeric(18, 2), nullable=True)
|
||||
percentage: Mapped[Optional[Decimal]] = mapped_column(Numeric(8, 4), nullable=True)
|
||||
anchor_type: Mapped[Optional[str]] = mapped_column(String(32), nullable=True)
|
||||
anchor_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
|
||||
offset_days: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
due_date: Mapped[Optional[date]] = mapped_column(Date, nullable=True)
|
||||
reminder_days: Mapped[int] = mapped_column(default=14, nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False, index=True)
|
||||
evidence_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
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 ContractPaymentEvent(Base):
|
||||
"""OA 验收归档及可选履约事件。"""
|
||||
|
||||
__tablename__ = "contract_payment_events"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("event_type", "source_event_id", name="uq_contract_payment_events_source"),
|
||||
Index("ix_contract_payment_events_contract_type", "contract_id", "event_type"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
contract_id: Mapped[int] = mapped_column(ForeignKey("contract_payment_contracts.id", ondelete="CASCADE"), nullable=False)
|
||||
event_type: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
event_date: Mapped[date] = mapped_column(Date, nullable=False)
|
||||
source_event_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
payload_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
|
||||
class ContractPaymentVendorRequest(Base):
|
||||
"""OA 厂商预付款请款作业的标准化记录,用于判定预付款与出货款是否还需提醒。"""
|
||||
|
||||
__tablename__ = "contract_payment_vendor_requests"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("source_request_id", name="uq_contract_payment_vendor_requests_source"),
|
||||
Index("ix_contract_payment_vendor_requests_contract_stage", "contract_id", "payment_stage"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
contract_id: Mapped[int] = mapped_column(ForeignKey("contract_payment_contracts.id", ondelete="CASCADE"), nullable=False)
|
||||
source_request_id: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
request_no: Mapped[Optional[str]] = mapped_column(String(128), nullable=True)
|
||||
payment_stage: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
request_status: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
|
||||
requested_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
payload_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
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 ContractPaymentAlert(Base):
|
||||
"""付款预警发送幂等和审计记录。"""
|
||||
|
||||
__tablename__ = "contract_payment_alerts"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("node_id", "trigger_key", "recipient_user_id", name="uq_contract_payment_alerts_trigger"),
|
||||
Index("ix_contract_payment_alerts_status_scheduled", "status", "scheduled_at"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
contract_id: Mapped[int] = mapped_column(ForeignKey("contract_payment_contracts.id", ondelete="CASCADE"), nullable=False)
|
||||
node_id: Mapped[int] = mapped_column(ForeignKey("contract_payment_nodes.id", ondelete="CASCADE"), nullable=False)
|
||||
trigger_key: Mapped[str] = mapped_column(String(64), nullable=False)
|
||||
scheduled_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
sent_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
recipient_user_id: Mapped[Optional[int]] = mapped_column(ForeignKey("users.id"), nullable=True)
|
||||
alert_round: Mapped[int] = mapped_column(default=1, nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False, index=True)
|
||||
message_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
|
||||
class ContractPaymentAlertBatch(Base):
|
||||
"""按周、按收件人归并的合同付款预警发送批次。"""
|
||||
|
||||
__tablename__ = "contract_payment_alert_batches"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("week_key", "recipient_user_id", name="uq_contract_payment_alert_batches_week_recipient"),
|
||||
Index("ix_contract_payment_alert_batches_status_created", "status", "created_at"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
batch_no: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||||
week_key: Mapped[str] = mapped_column(String(16), nullable=False)
|
||||
recipient_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False)
|
||||
summary_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
excel_file_name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
|
||||
excel_file_data: Mapped[Optional[bytes]] = mapped_column(LargeBinary, nullable=True)
|
||||
excel_file_size: Mapped[Optional[int]] = mapped_column(nullable=True)
|
||||
sent_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
wecom_trace_json: Mapped[Optional[dict[str, Any]]] = mapped_column(JSON, nullable=True)
|
||||
error_message: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
|
||||
class ContractPaymentSyncState(Base):
|
||||
"""合同付款域外部来源的 OA 增量同步断点(游标)。
|
||||
|
||||
用途:记录 OA 合同 HTTP 列表与验收 SQL 同步上次成功位置,供时间窗口增量拉取。
|
||||
依赖:models.base.Base。
|
||||
逻辑:同一 state_key 只保留一行;合同列表用 `oa-contract-archive`,验收归档用 `oa-acceptance-archive`。
|
||||
成功同步后更新 last_successful_sync_at,下次以它减去 overlap 作为时间下限。
|
||||
"""
|
||||
|
||||
__tablename__ = "contract_payment_sync_states"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
state_key: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||||
last_successful_sync_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
last_archive_seen_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
last_run_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
||||
synced_contract_count: Mapped[int] = mapped_column(default=0, nullable=False)
|
||||
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)
|
||||
Reference in new issue
Block a user