feat(核算)!: 添加 T+ 正式核算与采集托管

补齐源快照、资料预演、正式导入及工资核算的依赖链路。
新增版本化输入、逐段 FIFO、生成锁和结构化当月调整,
并提供默认关闭的周期采集、有限重试及 Windows 托管。

原始客户资料、私有配置及首页改动不纳入本次提交。

BREAKING CHANGE: 升级前需应用新增迁移,并通过
PAYROLL_MAKER_USER_ID 绑定实际工资制作人,否则业务只读。
旧单批次核算默认关闭;历史兼容需显式启用
PAYROLL_ALLOW_LEGACY_CALCULATION。生产启用和真实金额待验收。
This commit is contained in:
liuwenxin committed 2026-11-02 09:42:53 +08:00
1 parent 1b074576fe
commit 108bf87225
154 files changed
+9515 -645

No files matched your search

+18
View File
@@ -17,6 +17,7 @@ from app.models.customer import Customer, OwnershipChangeLog
from app.models.login_log import LoginLog
from app.models.operation_log import OperationLog
from app.models.param import ParamChangeBatch, ParamDefinition, ParamValue
from app.models.payroll import PayrollAllocation, PayrollInput, PayrollMonth
from app.models.performance_ledger import PerformanceLedger
from app.models.personnel import Personnel, PersonnelPlan, PersonnelRegion
from app.models.product import Product
@@ -26,12 +27,20 @@ from app.models.role import Permission, Role, RolePermission
from app.models.settlement import AdjustmentOrder, MonthlySettlement, SettlementItem
from app.models.shipment import Shipment, ShipmentItem
from app.models.system_setting import SystemSetting
from app.models.tplus_automation import TPlusAutomationState
from app.models.tplus_sync import (
TPlusSyncBatch,
TPlusSyncError,
TPlusSyncRecord,
TPlusSyncWatermark,
)
from app.models.tplus_workbench import (
TPlusFact,
TPlusSourceHead,
TPlusSourceLink,
TPlusWorkLine,
TPlusWorkRun,
)
from app.models.user import User, UserRole
__all__ = [
@@ -52,6 +61,9 @@ __all__ = [
"ParamChangeBatch",
"ParamDefinition",
"ParamValue",
"PayrollAllocation",
"PayrollInput",
"PayrollMonth",
"PerformanceLedger",
"Permission",
"Personnel",
@@ -66,10 +78,16 @@ __all__ = [
"Shipment",
"ShipmentItem",
"SystemSetting",
"TPlusAutomationState",
"TPlusFact",
"TPlusSourceHead",
"TPlusSourceLink",
"TPlusSyncBatch",
"TPlusSyncError",
"TPlusSyncRecord",
"TPlusSyncWatermark",
"TPlusWorkLine",
"TPlusWorkRun",
"TimestampMixin",
"User",
"UserRole",
+66
View File
@@ -0,0 +1,66 @@
"""正式工资输入、分配过程与月度生成锁,和隔离预演分别留存。"""
from datetime import datetime
from decimal import Decimal
from sqlalchemy import JSON, DateTime, Index, Integer, Numeric, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import BIGINT_UNSIGNED, Base, TimestampMixin, mysql_table_args
class PayrollMonth(Base, TimestampMixin):
"""先锁月份行再修改方案或生成工资;生成标记不随审批退回清除。"""
__tablename__ = "payroll_months"
__table_args__ = mysql_table_args("工资月份控制表(首次生成即锁定分配,审批不解锁)")
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
month: Mapped[str] = mapped_column(String(7), unique=True, comment="工资月份")
maker_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="实际工资制作人账号ID")
maker_name: Mapped[str] = mapped_column(String(64), comment="制作人用户名快照")
generated_at: Mapped[datetime | None] = mapped_column(
DateTime, comment="首次成功生成时间(不可解锁)"
)
class PayrollInput(Base, TimestampMixin):
"""正式核算输入的版本:来源、成本、归属及期初均有依据,旧版本不覆盖。"""
__tablename__ = "payroll_inputs"
__table_args__ = mysql_table_args(
"正式工资输入版本表",
UniqueConstraint("business_id", "version", name="uk_payroll_inputs_business_version"),
Index("idx_payroll_inputs_source_group", "source_database", "group_key"),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
business_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="正式业务ID")
version: Mapped[int] = mapped_column(Integer, comment="输入版本")
source_database: Mapped[str] = mapped_column(String(128), comment="源账套或人工来源")
group_key: Mapped[str] = mapped_column(String(128), comment="独立业务平账组")
fingerprint: Mapped[str] = mapped_column(String(64), comment="来源及事实指纹")
data_json: Mapped[dict] = mapped_column(JSON, comment="经核实的源数据及人工事实快照")
requested_by: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="录入人账号ID")
class PayrollAllocation(Base, TimestampMixin):
"""一次正式计算的逐段过程;版本不可变,生效性由所属台账控制。"""
__tablename__ = "payroll_allocations"
__table_args__ = mysql_table_args(
"正式FIFO分配明细表",
UniqueConstraint("ledger_id", "line_no", name="uk_payroll_allocations_ledger_line"),
Index("idx_payroll_allocations_business_id", "business_id"),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
ledger_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="所属正式台账ID")
business_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="正式业务ID")
line_no: Mapped[int] = mapped_column(Integer, comment="本台账稳定明细序号")
input_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="所用输入版本ID")
amount: Mapped[Decimal] = mapped_column(Numeric(14, 2), comment="分配回款金额")
data_json: Mapped[dict] = mapped_column(JSON, comment="单据、成本、归属、参数和逐段计算依据")
+5
View File
@@ -55,6 +55,7 @@ class PerformanceLedger(Base, TimestampMixin):
"receipt_id",
"calc_version",
"personnel_id",
"allocation_key",
name="uk_performance_ledgers_receipt_id_calc_version_personnel",
),
Index("idx_performance_ledgers_month_active", "settlement_month", "is_active"),
@@ -65,6 +66,7 @@ class PerformanceLedger(Base, TimestampMixin):
"calc_type",
),
Index("idx_performance_ledgers_personnel_id", "personnel_id"),
Index("idx_performance_ledgers_receipt_id", "receipt_id"),
Index("idx_performance_ledgers_business_id", "business_id"),
)
@@ -82,6 +84,9 @@ class PerformanceLedger(Base, TimestampMixin):
server_default="1",
comment="核算版本(重算+1,旧行 is_active=0)",
)
allocation_key: Mapped[str] = mapped_column(
String(64), nullable=False, server_default="''", comment="正式FIFO事件键;历史单笔核算为空串",
)
is_active: Mapped[int] = mapped_column(
TINYINT_TYPE,
nullable=False,
+7
View File
@@ -79,6 +79,10 @@ class MonthlySettlement(Base, TimestampMixin):
)
locked_by: Mapped[str | None] = mapped_column(String(64), comment="锁定操作人(冗余固化)")
@property
def approval_required(self) -> bool:
return bool((self.snapshot_json or {}).get("approval_required", True))
class SettlementItem(Base):
"""月结明细表:按人按核算类型固化金额(锁定后只读;重生成=草稿态删旧插新)。"""
@@ -154,6 +158,9 @@ class AdjustmentOrder(Base, TimestampMixin):
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
adjustment_type: Mapped[str] = mapped_column(String(32), nullable=False, server_default="'GENERAL'", comment="调整类型:GENERAL普通 ALLOCATION分配差额")
source_ledger_id: Mapped[int | None] = mapped_column(BIGINT_UNSIGNED, comment="分配差额对应的原台账ID")
attribution_json: Mapped[dict | None] = mapped_column(JSON, comment="原台账业务与分管归属快照")
order_no: Mapped[str] = mapped_column(
String(32),
nullable=False,
+26
View File
@@ -0,0 +1,26 @@
"""自动采集的周期、重试和进程心跳;不承载业务核算状态。"""
from datetime import datetime
from sqlalchemy import DateTime, Integer, String
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import BIGINT_UNSIGNED, Base, TimestampMixin, mysql_table_args
class TPlusAutomationState(Base, TimestampMixin):
__tablename__ = "tplus_automation_states"
__table_args__ = mysql_table_args("T+自动采集周期与进程状态(不自动导入正式业务)")
id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID")
source_key: Mapped[str] = mapped_column(String(64), unique=True, comment="源连接身份摘要(不含凭据)")
source_database: Mapped[str] = mapped_column(String(128), comment="源账套")
scheduled_period: Mapped[str | None] = mapped_column(String(16), comment="最近已排队ISO周")
root_batch_no: Mapped[str | None] = mapped_column(String(32), comment="本周期原始采集批次")
current_batch_no: Mapped[str | None] = mapped_column(String(32), comment="当前原始或重试批次")
retry_attempts: Mapped[int] = mapped_column(Integer, server_default="0", comment="本周期自动重试次数")
next_retry_at: Mapped[datetime | None] = mapped_column(DateTime, comment="下一次自动重试时间")
last_success_at: Mapped[datetime | None] = mapped_column(DateTime, comment="最近成功采集时间")
maintenance_at: Mapped[datetime | None] = mapped_column(DateTime, comment="最近周期检查时间")
heartbeat_at: Mapped[datetime | None] = mapped_column(DateTime, comment="独立进程心跳时间")
worker_owner: Mapped[str | None] = mapped_column(String(36), comment="当前持锁进程身份")
worker_phase: Mapped[str] = mapped_column(String(16), server_default="STOPPED", comment="进程状态:IDLE/RUNNING/STOPPED")
+2
View File
@@ -63,6 +63,8 @@ class TPlusSyncBatch(Base, TimestampMixin):
requested_by: Mapped[int | None] = mapped_column(
BIGINT_UNSIGNED, comment="请求用户ID,CLI任务为空,无物理外键"
)
trigger_type: Mapped[str] = mapped_column(String(16), server_default="'MANUAL'", comment="触发来源:MANUAL/SCHEDULED/AUTO_RETRY")
automatic_attempt: Mapped[int] = mapped_column(Integer, server_default="0", comment="自动重试序号(0为原始任务)")
audit_result_json: Mapped[dict | None] = mapped_column(
JSON, comment="关联核验结果,只含系统字典和汇总"
)
+134
View File
@@ -0,0 +1,134 @@
"""T+ 基础导入与试算的隔离工作区,任何表均不关联正式工资台账。"""
from datetime import datetime
from sqlalchemy import JSON, DateTime, Index, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import BIGINT_UNSIGNED, Base, TimestampMixin, mysql_table_args
class TPlusSourceHead(Base):
"""一次完整读取实际观察到的源版本;解决 A→B→A 去重后的旧版本误选。"""
__tablename__ = "tplus_source_heads"
__table_args__ = mysql_table_args(
"T+ 源当前快照指针(按成功全量批次读取,删除不自动回写业务表)",
UniqueConstraint(
"source_database",
"entity_type",
"source_id",
name="uk_tplus_source_heads_source_entity_id",
),
Index("idx_tplus_source_heads_batch_entity", "batch_no", "entity_type"),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
source_database: Mapped[str] = mapped_column(String(128), comment="源账套")
entity_type: Mapped[str] = mapped_column(String(32), comment="实体类型")
source_id: Mapped[str] = mapped_column(String(64), comment="源内部ID")
record_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="原始快照ID")
batch_no: Mapped[str] = mapped_column(String(32), comment="最后观察批次")
class TPlusFact(Base, TimestampMixin):
"""补充资料追加版本,旧版本永久保留,乐观锁防止静默覆盖。"""
__tablename__ = "tplus_facts"
__table_args__ = mysql_table_args(
"T+ 人工补充事实版本表",
UniqueConstraint(
"source_database",
"kind",
"fact_key",
"version",
name="uk_tplus_facts_source_kind_key_version",
),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
source_database: Mapped[str] = mapped_column(String(128), comment="源账套")
kind: Mapped[str] = mapped_column(String(32), comment="事实类别")
fact_key: Mapped[str] = mapped_column(String(128), comment="事实业务键")
version: Mapped[int] = mapped_column(Integer, comment="版本号")
data_json: Mapped[dict] = mapped_column(JSON, comment="经校验的事实内容")
evidence: Mapped[str] = mapped_column(String(500), comment="依据和修改原因")
requested_by: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="操作者ID")
class TPlusSourceLink(Base, TimestampMixin):
"""源稳定身份与业务目标的唯一关联;不按名称猜测合并。"""
__tablename__ = "tplus_source_links"
__table_args__ = mysql_table_args(
"T+ 基础数据源目标关联",
UniqueConstraint(
"source_database",
"entity_type",
"source_id",
name="uk_tplus_source_links_source_entity_id",
),
UniqueConstraint("entity_type", "target_id", name="uk_tplus_source_links_entity_target"),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
source_database: Mapped[str] = mapped_column(String(128), comment="源账套")
entity_type: Mapped[str] = mapped_column(String(32), comment="实体类型")
source_id: Mapped[str] = mapped_column(String(64), comment="源内部ID")
target_id: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="业务目标ID")
class TPlusWorkRun(Base, TimestampMixin):
"""后台导入、预览和试算运行记录,固定输入,保留终态。"""
__tablename__ = "tplus_work_runs"
__table_args__ = mysql_table_args(
"T+ 导入与预演运行表",
UniqueConstraint("run_no", name="uk_tplus_work_runs_run_no"),
UniqueConstraint("request_key", name="uk_tplus_work_runs_request_key"),
Index("idx_tplus_work_runs_status_id", "status", "id"),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
run_no: Mapped[str] = mapped_column(String(32), comment="运行编号")
request_key: Mapped[str] = mapped_column(String(36), comment="请求幂等键")
kind: Mapped[str] = mapped_column(
String(32), comment="MASTER_PREVIEW/MASTER_APPLY/CALC_PREVIEW"
)
status: Mapped[str] = mapped_column(String(16), default="QUEUED", comment="运行状态")
source_database: Mapped[str] = mapped_column(String(128), comment="固定源账套")
requested_by: Mapped[int] = mapped_column(BIGINT_UNSIGNED, comment="操作者ID")
input_json: Mapped[dict] = mapped_column(JSON, comment="请求输入")
snapshot_json: Mapped[dict | None] = mapped_column(JSON, comment="规则和补充资料的固定快照")
snapshot_hash: Mapped[str | None] = mapped_column(String(64), comment="输入指纹")
summary_json: Mapped[dict | None] = mapped_column(JSON, comment="结果汇总")
error_code: Mapped[str | None] = mapped_column(String(64), comment="固定错误编码")
started_at: Mapped[datetime | None] = mapped_column(DateTime, comment="开始时间")
finished_at: Mapped[datetime | None] = mapped_column(DateTime, comment="结束时间")
class TPlusWorkLine(Base):
"""固定输入及结果逐行留存,仅经授权的工作区接口可访问。"""
__tablename__ = "tplus_work_lines"
__table_args__ = mysql_table_args(
"T+ 导入预览与核算预演明细",
UniqueConstraint("run_no", "line_no", name="uk_tplus_work_lines_run_line"),
)
id: Mapped[int] = mapped_column(
BIGINT_UNSIGNED, primary_key=True, autoincrement=True, comment="主键ID"
)
run_no: Mapped[str] = mapped_column(String(32), comment="运行编号")
line_no: Mapped[int] = mapped_column(Integer, comment="稳定行序号")
entity_type: Mapped[str] = mapped_column(String(32), comment="实体或分组类型")
source_id: Mapped[str] = mapped_column(String(128), comment="源ID或平账组")
action: Mapped[str] = mapped_column(String(32), comment="新增/更新/跳过/待补/冲突/分配")
reason: Mapped[str | None] = mapped_column(String(128), comment="固定原因编码")
data_json: Mapped[dict] = mapped_column(JSON, comment="最小必要的业务明细")
WORKBENCH_MODELS = (TPlusSourceHead, TPlusFact, TPlusSourceLink, TPlusWorkRun, TPlusWorkLine)