添加 委外批次进数据库、委外批次入飞书

This commit is contained in:
2026-04-24 19:00:07 +08:00
parent 3e1d479643
commit 65216cd4cf
11 changed files with 1809 additions and 313 deletions
+74
View File
@@ -1,5 +1,7 @@
from __future__ import annotations
import logging
import re
from typing import Any
from .feishu import FeishuApiError, FeishuClient
@@ -8,6 +10,38 @@ from .settings import AppConfig
from .specs import BRAND_SYNC_TARGETS, FIELD_SPECS, PRIMARY_FIELD_NAME, PRIMARY_FIELD_SOURCE, BrandSyncTarget
logger = logging.getLogger("inventory_sync.service")
SENSITIVE_PATTERN = re.compile(r"(?i)(password|passwd|secret|token|authorization)\s*[:=]\s*([^\s,;]+)")
LOG_FIELD_LABELS = {
"advanced_permission_enabled": "高级权限已开启",
"brand_name": "品牌",
"created_records": "新增记录数",
"deleted_records": "删除记录数",
"existing_records": "已有记录数",
"rows": "行数",
"selected_table_id": "选中表ID",
"source_rows": "源数据行数",
"table_count": "表数量",
"table_name": "表名",
"target_count": "目标数量",
}
def sanitize_value(value: Any) -> Any:
if isinstance(value, str):
return SENSITIVE_PATTERN.sub(r"\1=***", value)
return value
def log_event(level: int, event: str, **fields: Any) -> None:
safe_event = sanitize_value(event)
details = " ".join(
f"{LOG_FIELD_LABELS.get(key, key)}={sanitize_value(value)!r}" for key, value in fields.items()
)
logger.log(level, "事件=%s%s", safe_event, f" {details}" if details else "")
def build_feishu_record(row: dict[str, Any]) -> dict[str, Any]:
if PRIMARY_FIELD_SOURCE not in row:
raise KeyError(f"映射缺少主字段来源列: {PRIMARY_FIELD_SOURCE}")
@@ -30,10 +64,19 @@ def build_feishu_record(row: dict[str, Any]) -> dict[str, Any]:
def run_feishu_summary(client: FeishuClient) -> dict[str, Any]:
log_event(logging.INFO, "开始获取飞书租户访问令牌")
access_token = client.get_tenant_access_token()
log_event(logging.INFO, "飞书租户访问令牌获取完成")
base_info = client.parse_base_info(client.build_base_url(), access_token)
log_event(logging.INFO, "飞书Base信息解析完成", selected_table_id=base_info["selected_table_id"])
advanced_permission_enabled = client.enable_advanced_permission(access_token, base_info["app_token"] or "")
tables = client.list_tables(access_token, base_info["app_token"] or "")
log_event(
logging.INFO,
"飞书Base表列表读取完成",
table_count=len(tables),
advanced_permission_enabled=advanced_permission_enabled,
)
return {
"base_url": base_info["base_url"],
"app_token": base_info["app_token"],
@@ -61,6 +104,13 @@ def sync_brand_target(
target: BrandSyncTarget,
postgres_rows: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
log_event(
logging.INFO,
"开始同步目标飞书表",
brand_name=target.brand_name,
table_name=target.table_name,
source_rows=len(postgres_rows or []),
)
target_table = next(
(table for table in tables if table.get("name") == target.table_name),
None,
@@ -75,6 +125,13 @@ def sync_brand_target(
available_fields = client.ensure_target_fields(access_token, app_token, table_id)
existing_records = client.list_records(access_token, app_token, table_id)
existing_record_ids = [record.get("record_id") for record in existing_records if record.get("record_id")]
log_event(
logging.INFO,
"目标飞书表已有记录读取完成",
brand_name=target.brand_name,
table_name=target.table_name,
existing_records=len(existing_record_ids),
)
deleted_count = client.batch_delete_records(access_token, app_token, table_id, existing_record_ids) if existing_record_ids else 0
created_count = client.batch_create_records(
access_token,
@@ -82,6 +139,14 @@ def sync_brand_target(
table_id,
[build_feishu_record(row) for row in target_rows],
) if target_rows else 0
log_event(
logging.INFO,
"目标飞书表同步完成",
brand_name=target.brand_name,
table_name=target.table_name,
deleted_records=deleted_count,
created_records=created_count,
)
return {
"brand_name": target.brand_name,
@@ -97,11 +162,13 @@ def sync_brand_target(
def run_sync_brand(config: AppConfig, client: FeishuClient, postgres_result: dict[str, Any] | None = None) -> dict[str, Any]:
log_event(logging.INFO, "开始准备品牌同步", target_count=len(BRAND_SYNC_TARGETS))
access_token = client.get_tenant_access_token()
base_info = client.parse_base_info(client.build_base_url(), access_token)
app_token = base_info["app_token"] or ""
client.enable_advanced_permission(access_token, app_token)
tables = client.list_tables(access_token, app_token)
log_event(logging.INFO, "品牌同步准备完成", table_count=len(tables))
results = []
source_row_count = 0
@@ -111,6 +178,13 @@ def run_sync_brand(config: AppConfig, client: FeishuClient, postgres_result: dic
else:
postgres_payload = query_postgres(config, target=target)
postgres_rows = postgres_payload["rows"]
log_event(
logging.INFO,
"目标品牌PostgreSQL数据已准备",
brand_name=target.brand_name,
table_name=target.table_name,
rows=len(postgres_rows or []),
)
source_row_count += len(postgres_rows or [])
results.append(sync_brand_target(client, access_token, app_token, tables, target, postgres_rows))