130 lines
4.3 KiB
Python
130 lines
4.3 KiB
Python
"""
|
||
backend/app/services/voucher_check/config.py
|
||
|
||
用途:解析立账凭证检核环境变量与 ScheduleTask.job_params。
|
||
依赖:Flask current_app.config、core.errors.ValidationError。
|
||
逻辑:runner / erp_repository / 引擎启动前调用;非法配置抛 VOUCHER_CHECK.CONFIG_INVALID;
|
||
ERP 账号优先 VOUCHER_CHECK_ERP_*,否则 ERP_*,再回退 CONTRACT_PAYMENT_ERP_*。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from dataclasses import dataclass
|
||
from typing import Any
|
||
|
||
from flask import has_app_context, current_app
|
||
|
||
from app.core.errors import ValidationError
|
||
|
||
DEFAULT_JOB_PARAMS: dict[str, Any] = {
|
||
"profile_code": "sales",
|
||
"scope_code": "HXJM",
|
||
"notify_missing_enabled": True,
|
||
"notify_failure_enabled": True,
|
||
}
|
||
|
||
|
||
@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,
|
||
code="VOUCHER_CHECK.CONFIG_INVALID",
|
||
details=details or {},
|
||
)
|
||
|
||
|
||
def _as_bool(value: Any, default: bool) -> bool:
|
||
if value is None:
|
||
return default
|
||
if isinstance(value, bool):
|
||
return value
|
||
text = str(value).strip().lower()
|
||
if text in {"1", "true", "yes", "on"}:
|
||
return True
|
||
if text in {"0", "false", "no", "off"}:
|
||
return False
|
||
return default
|
||
|
||
|
||
def _erp_settings() -> tuple[str, str, str, int]:
|
||
"""
|
||
读取 ERP 只读连接;密码只来自环境/配置,不写日志。
|
||
|
||
核心逻辑:VOUCHER_CHECK_ERP_* 优先,其次通用 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 cfg.get("ERP_USERNAME")
|
||
or os.getenv("ERP_USERNAME")
|
||
or cfg.get("CONTRACT_PAYMENT_ERP_USERNAME")
|
||
or os.getenv("CONTRACT_PAYMENT_ERP_USERNAME")
|
||
or ""
|
||
).strip()
|
||
erp_password = str(
|
||
cfg.get("VOUCHER_CHECK_ERP_PASSWORD")
|
||
or os.getenv("VOUCHER_CHECK_ERP_PASSWORD")
|
||
or cfg.get("ERP_PASSWORD")
|
||
or os.getenv("ERP_PASSWORD")
|
||
or cfg.get("CONTRACT_PAYMENT_ERP_PASSWORD")
|
||
or os.getenv("CONTRACT_PAYMENT_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 cfg.get("ERP_TIMEOUT_SECONDS")
|
||
or os.getenv("ERP_TIMEOUT_SECONDS")
|
||
or 30
|
||
)
|
||
return erp_dsn, erp_username, erp_password, max(1, timeout)
|
||
|
||
|
||
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,
|
||
)
|