feat(invoice check): 立账凭证检核优化部分代码
This commit is contained in:
1 parent
21d41acf50
commit
d123d9cdf6
45 files changed
+498
-2508
No files matched your search
@@ -3,7 +3,7 @@ backend/app/services/voucher_check/schedule_seed.py
|
||||
|
||||
用途:确保 accounting-voucher-check 默认周批 ScheduleTask 存在(周二 06:00 Asia/Shanghai)。
|
||||
依赖:ScheduleTask/User、timezone_util、voucher_check.config.DEFAULT_JOB_PARAMS。
|
||||
逻辑:迁移种子 + create_app / User after_commit 幂等补建;已存在的日批默认任务会改成周二。
|
||||
逻辑:迁移种子 + create_app / User after_commit 幂等补建;已存在的日批改成周二,并剥离 schema 外的旧 job_params。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -14,6 +14,7 @@ from typing import Any
|
||||
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.orm.attributes import flag_modified
|
||||
|
||||
from app import db
|
||||
from app.models import ScheduleTask, User
|
||||
@@ -31,13 +32,33 @@ _HOOKS_REGISTERED = False
|
||||
_SESSION_INFO_KEY = "_seed_voucher_check_schedule"
|
||||
|
||||
|
||||
def _normalize_existing_job_params(raw: Any) -> tuple[dict[str, Any], bool]:
|
||||
"""
|
||||
把已有排程的 job_params 收成当前 schema。
|
||||
核心逻辑:保留 DEFAULT_JOB_PARAMS 与 _advanced;丢掉共享盘时代的未知字段。
|
||||
"""
|
||||
from app.services.schedule.job_params import ADVANCED_PARAMS_KEY
|
||||
|
||||
current = dict(raw) if isinstance(raw, dict) else {}
|
||||
cleaned: dict[str, Any] = {}
|
||||
for key, default in DEFAULT_JOB_PARAMS.items():
|
||||
value = current.get(key)
|
||||
cleaned[key] = default if value is None else value
|
||||
advanced = current.get(ADVANCED_PARAMS_KEY)
|
||||
if isinstance(advanced, dict):
|
||||
cleaned[ADVANCED_PARAMS_KEY] = advanced
|
||||
return cleaned, current != cleaned
|
||||
|
||||
|
||||
def ensure_default_voucher_check_schedule(
|
||||
*,
|
||||
commit: bool = True,
|
||||
session: Any | None = None,
|
||||
) -> ScheduleTask | None:
|
||||
"""
|
||||
幂等确保默认立账凭证检核日批排程存在。
|
||||
幂等确保默认立账凭证检核周批排程存在。
|
||||
核心逻辑:已存在任务会把日批 cron 改成周二,并剥离 schema 外的旧 job_params,
|
||||
避免 worker 执行前校验 unknown params 整批失败。
|
||||
|
||||
Args:
|
||||
commit: 是否立即提交。
|
||||
@@ -56,6 +77,7 @@ def ensure_default_voucher_check_schedule(
|
||||
.first()
|
||||
)
|
||||
if existing is not None:
|
||||
changed = False
|
||||
if existing.cron_expr == "0 6 * * *":
|
||||
existing.cron_expr = DEFAULT_VOUCHER_CHECK_CRON
|
||||
if existing.name == "立账凭证检核(日批)":
|
||||
@@ -64,6 +86,17 @@ def ensure_default_voucher_check_schedule(
|
||||
DEFAULT_VOUCHER_CHECK_CRON,
|
||||
timezone_name="Asia/Shanghai",
|
||||
) or existing.next_run_at
|
||||
changed = True
|
||||
cleaned_params, params_changed = _normalize_existing_job_params(existing.job_params)
|
||||
if params_changed:
|
||||
existing.job_params = cleaned_params
|
||||
flag_modified(existing, "job_params")
|
||||
changed = True
|
||||
_LOGGER.info(
|
||||
"stripped legacy accounting-voucher-check job_params id=%s",
|
||||
existing.id,
|
||||
)
|
||||
if changed:
|
||||
existing.updated_at = datetime.utcnow()
|
||||
if commit:
|
||||
sess.commit()
|
||||
|
||||
Reference in new issue
Block a user