This commit is contained in:
sophie committed 2026-09-07 11:13:33 +08:00
1 parent 78576f5745
commit 63b997a7e4
68 files changed
+3680 -1413

No files matched your search

+73 -27
View File
@@ -3,7 +3,7 @@ backend/app/services/voucher_check/config.py
用途:解析立账凭证检核环境变量与 ScheduleTask.job_params,校验目录模板与文件名规则。
依赖:Flask current_app.config、core.errors.ValidationError。
逻辑:runner / share_scanner / erp_repository 启动前调用;非法配置抛 VOUCHER_CHECK.CONFIG_INVALID。
逻辑:runner / share_scanner / erp_repository / 新引擎启动前调用;非法配置抛 VOUCHER_CHECK.CONFIG_INVALID。
"""
from __future__ import annotations
@@ -38,13 +38,14 @@ DEFAULT_REQUIRED_FILES: list[dict[str, Any]] = [
]
DEFAULT_JOB_PARAMS: dict[str, Any] = {
"profile_code": "sales",
"scope_code": "HXJM",
"directory_template": "{root}/{YYYY}/{MM}/{invoice_no}",
"required_files": DEFAULT_REQUIRED_FILES,
"filename_case_sensitive": True,
"notify_missing_enabled": True,
"notify_failure_enabled": True,
"same_day_dedup_enabled": True,
"same_day_dedup_enabled": False,
}
@@ -78,6 +79,20 @@ class VoucherCheckConfig:
erp_timeout_seconds: int = 30
@dataclass(frozen=True)
class VoucherCheckEngineConfig:
"""销售一期引擎运行配置:ERP + 通知开关,不再依赖共享盘。"""
scope_code: str
profile_code: str
notify_missing_enabled: bool
notify_failure_enabled: bool
erp_dsn: str
erp_username: str
erp_password: str
erp_timeout_seconds: int = 30
def _config_invalid(message: str, *, details: dict[str, Any] | None = None) -> ValidationError:
return ValidationError(
message,
@@ -168,6 +183,36 @@ def validate_directory_template(template: str) -> str:
return text
def _erp_settings() -> tuple[str, str, str, int]:
"""读取 ERP 只读连接;密码只来自环境/配置,不写日志。"""
cfg = current_app.config if has_app_context() else {}
erp_dsn = str(
cfg.get("VOUCHER_CHECK_ERP_DSN")
or os.getenv("VOUCHER_CHECK_ERP_DSN")
or cfg.get("ERP_DB_DSN")
or os.getenv("ERP_DB_DSN")
or ""
).strip()
erp_username = str(
cfg.get("VOUCHER_CHECK_ERP_USERNAME")
or os.getenv("VOUCHER_CHECK_ERP_USERNAME")
or ""
).strip()
erp_password = str(
cfg.get("VOUCHER_CHECK_ERP_PASSWORD")
or os.getenv("VOUCHER_CHECK_ERP_PASSWORD")
or ""
)
if not erp_dsn or not erp_username:
raise _config_invalid("未配置 VOUCHER_CHECK_ERP_DSN / VOUCHER_CHECK_ERP_USERNAME")
timeout = int(
cfg.get("VOUCHER_CHECK_ERP_TIMEOUT_SECONDS")
or os.getenv("VOUCHER_CHECK_ERP_TIMEOUT_SECONDS")
or 30
)
return erp_dsn, erp_username, erp_password, max(1, timeout)
def load_voucher_check_config(job_params: dict[str, Any] | None = None) -> VoucherCheckConfig:
"""
合并 env 与 job_params,返回校验后的检核配置。
@@ -207,31 +252,7 @@ def load_voucher_check_config(job_params: dict[str, Any] | None = None) -> Vouch
if not local_root and not share_unc:
raise _config_invalid("未配置 VOUCHER_CHECK_LOCAL_ROOT 或 VOUCHER_CHECK_SHARE_UNC")
erp_dsn = str(
cfg.get("VOUCHER_CHECK_ERP_DSN")
or os.getenv("VOUCHER_CHECK_ERP_DSN")
or cfg.get("ERP_DB_DSN")
or os.getenv("ERP_DB_DSN")
or ""
).strip()
erp_username = str(
cfg.get("VOUCHER_CHECK_ERP_USERNAME")
or os.getenv("VOUCHER_CHECK_ERP_USERNAME")
or ""
).strip()
erp_password = str(
cfg.get("VOUCHER_CHECK_ERP_PASSWORD")
or os.getenv("VOUCHER_CHECK_ERP_PASSWORD")
or ""
)
if not erp_dsn or not erp_username:
raise _config_invalid("未配置 VOUCHER_CHECK_ERP_DSN / VOUCHER_CHECK_ERP_USERNAME")
timeout = int(
cfg.get("VOUCHER_CHECK_ERP_TIMEOUT_SECONDS")
or os.getenv("VOUCHER_CHECK_ERP_TIMEOUT_SECONDS")
or 30
)
erp_dsn, erp_username, erp_password, timeout = _erp_settings()
return VoucherCheckConfig(
scope_code=scope_code,
@@ -251,6 +272,31 @@ def load_voucher_check_config(job_params: dict[str, Any] | None = None) -> Vouch
)
def load_engine_config(job_params: dict[str, Any] | None = None) -> VoucherCheckEngineConfig:
"""
加载销售一期引擎配置。
核心逻辑:不要求共享盘;DMS 根目录由 SalesDmsClient.ensure_configured 在 runner 中校验。
"""
params = dict(DEFAULT_JOB_PARAMS)
if isinstance(job_params, dict):
params.update({k: v for k, v in job_params.items() if v is not None})
scope_code = str(params.get("scope_code") or "HXJM").strip().upper() or "HXJM"
if scope_code != "HXJM":
raise _config_invalid("一期仅支持 scope_code=HXJM", details={"scope_code": scope_code})
profile_code = str(params.get("profile_code") or "sales").strip() or "sales"
erp_dsn, erp_username, erp_password, timeout = _erp_settings()
return VoucherCheckEngineConfig(
scope_code=scope_code,
profile_code=profile_code,
notify_missing_enabled=_as_bool(params.get("notify_missing_enabled"), True),
notify_failure_enabled=_as_bool(params.get("notify_failure_enabled"), True),
erp_dsn=erp_dsn,
erp_username=erp_username,
erp_password=erp_password,
erp_timeout_seconds=timeout,
)
def resolve_share_root(config: VoucherCheckConfig) -> str:
"""
解析实际扫描根路径。