添加 羽心堂只筛选指定仓库

This commit is contained in:
2026-04-23 16:40:45 +08:00
parent 110b8a960c
commit 3e1d479643
5 changed files with 103 additions and 42 deletions
+19 -4
View File
@@ -7,10 +7,26 @@ import subprocess
from typing import Any
from .settings import AppConfig
from .sql import POSTGRES_QUERY
from .specs import BrandSyncTarget
from .sql import DEFAULT_SYNC_TARGET, POSTGRES_QUERY, build_postgres_query
def query_postgres(config: AppConfig) -> dict[str, Any]:
def query_postgres(
config: AppConfig,
brand_name: str | None = None,
warehouse_keys: tuple[int | str, ...] | None = None,
target: BrandSyncTarget | None = None,
) -> dict[str, Any]:
active_brand_name = brand_name or (target.brand_name if target else DEFAULT_SYNC_TARGET.brand_name)
active_warehouse_keys = warehouse_keys or (target.warehouse_keys if target else DEFAULT_SYNC_TARGET.warehouse_keys)
query = (
POSTGRES_QUERY
if brand_name is None and warehouse_keys is None and target is None
else build_postgres_query(
brand_name=active_brand_name,
warehouse_keys=active_warehouse_keys,
)
)
env = os.environ.copy()
env["PGPASSWORD"] = config.analytics_db_password
command = [
@@ -28,7 +44,7 @@ def query_postgres(config: AppConfig) -> dict[str, Any]:
"-c",
"SET TIME ZONE 'Asia/Shanghai';",
"-c",
f"COPY ({POSTGRES_QUERY}) TO STDOUT WITH CSV HEADER",
f"COPY ({query}) TO STDOUT WITH CSV HEADER",
]
try:
@@ -48,4 +64,3 @@ def query_postgres(config: AppConfig) -> dict[str, Any]:
)
rows = list(csv.DictReader(io.StringIO(cleaned_output)))
return {"row_count": len(rows), "rows": rows}
+18 -14
View File
@@ -58,8 +58,8 @@ def sync_brand_target(
access_token: str,
app_token: str,
tables: list[dict[str, Any]],
postgres_rows: list[dict[str, Any]],
target: BrandSyncTarget,
postgres_rows: list[dict[str, Any]] | None = None,
) -> dict[str, Any]:
target_table = next(
(table for table in tables if table.get("name") == target.table_name),
@@ -71,7 +71,7 @@ def sync_brand_target(
if not table_id:
raise FeishuApiError(f"目标飞书表缺少 table_id: {target.table_name}")
filtered_rows = [row for row in (postgres_rows or []) if row.get("品牌名称") == target.brand_name]
target_rows = postgres_rows or []
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")]
@@ -80,38 +80,42 @@ def sync_brand_target(
access_token,
app_token,
table_id,
[build_feishu_record(row) for row in filtered_rows],
) if filtered_rows else 0
[build_feishu_record(row) for row in target_rows],
) if target_rows else 0
return {
"brand_name": target.brand_name,
"warehouse_keys": target.warehouse_keys,
"target_table_name": target.table_name,
"target_table_id": table_id,
"available_fields": available_fields,
"source_row_count": len(postgres_rows or []),
"filtered_row_count": len(filtered_rows),
"source_row_count": len(target_rows),
"filtered_row_count": len(target_rows),
"deleted_record_count": deleted_count,
"created_record_count": created_count,
}
def run_sync_brand(config: AppConfig, client: FeishuClient, postgres_result: dict[str, Any] | None = None) -> dict[str, Any]:
postgres_payload = postgres_result or query_postgres(config)
postgres_rows = postgres_payload["rows"]
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)
results = [
sync_brand_target(client, access_token, app_token, tables, postgres_rows, target)
for target in BRAND_SYNC_TARGETS
]
results = []
source_row_count = 0
for index, target in enumerate(BRAND_SYNC_TARGETS):
if postgres_result is not None and index == 0:
postgres_payload = postgres_result
else:
postgres_payload = query_postgres(config, target=target)
postgres_rows = postgres_payload["rows"]
source_row_count += len(postgres_rows or [])
results.append(sync_brand_target(client, access_token, app_token, tables, target, postgres_rows))
return {
"target_count": len(BRAND_SYNC_TARGETS),
"source_row_count": len(postgres_rows or []),
"source_row_count": source_row_count,
"targets": results,
}
+2 -1
View File
@@ -12,6 +12,7 @@ PRIMARY_FIELD_SOURCE = "统一规格编码"
class BrandSyncTarget:
brand_name: str
table_name: str
warehouse_keys: tuple[int, ...]
@dataclass(frozen=True)
@@ -24,7 +25,7 @@ class FieldSpec:
BRAND_SYNC_TARGETS = [
BrandSyncTarget(brand_name="羽心堂", table_name="羽心堂"),
BrandSyncTarget(brand_name="羽心堂", table_name="羽心堂", warehouse_keys=(2, 3, 7, 13)),
]
+41 -7
View File
@@ -1,4 +1,31 @@
POSTGRES_QUERY = """
from __future__ import annotations
from collections.abc import Sequence
from .specs import BRAND_SYNC_TARGETS
DEFAULT_SYNC_TARGET = BRAND_SYNC_TARGETS[0]
def _quote_sql_literal(value: str) -> str:
return "'" + value.replace("'", "''") + "'"
def _format_warehouse_keys(warehouse_keys: Sequence[int | str]) -> str:
cleaned_keys = tuple(int(key) for key in warehouse_keys)
if not cleaned_keys:
raise ValueError("warehouse_keys 不能为空")
return ", ".join(str(key) for key in cleaned_keys)
def build_postgres_query(
brand_name: str = DEFAULT_SYNC_TARGET.brand_name,
warehouse_keys: Sequence[int | str] = DEFAULT_SYNC_TARGET.warehouse_keys,
) -> str:
brand_filter = _quote_sql_literal(brand_name)
warehouse_key_filter = _format_warehouse_keys(warehouse_keys)
return f"""
WITH inventory_latest AS (
SELECT
t."统一规格编码",
@@ -26,29 +53,32 @@ inventory_summary AS (
"统一规格编码",
SUM("物理总库存") AS "总库存"
FROM inventory_latest
WHERE "仓库主键" IN ({warehouse_key_filter})
GROUP BY "统一规格编码"
),
shop_listing_latest AS (
shop_stock_latest AS (
SELECT
t."统一规格编码",
t."店铺主键",
t."商品挂单量"
t."可发库存"
FROM (
SELECT
f."统一规格编码",
f."店铺主键",
f."快照时间",
f."商品挂单量",
f."可发库存",
f."状态",
ROW_NUMBER() OVER (
PARTITION BY f."统一规格编码", f."店铺主键"
ORDER BY f."快照时间" DESC
) AS rn
FROM analytics_layer.fact_shop_listing_snapshot f
FROM analytics_layer.fact_shop_stock_snapshot f
WHERE f."数据来源系统" = '旺店通店铺'
AND NULLIF(TRIM(f."统一规格编码"), '') IS NOT NULL
) t
WHERE t.rn = 1
AND t."状态" <> '已失效'
),
sales_summary AS (
@@ -109,12 +139,12 @@ SELECT
pd."条形码",
pd."零售价",
ds."店铺名称",
COALESCE(sl."商品挂单量", 0) AS "店铺挂单量",
COALESCE(sl."可发库存", 0) AS "店铺挂单量",
COALESCE(inv."总库存", 0) AS "总库存",
COALESCE(ss."本月销售数量", 0) AS "本月销售数量",
COALESCE(ss."上月销售数量", 0) AS "上月销售数量",
COALESCE(ss."上上月销售数量", 0) AS "上上月销售数量"
FROM shop_listing_latest sl
FROM shop_stock_latest sl
LEFT JOIN inventory_summary inv
ON sl."统一规格编码" = inv."统一规格编码"
LEFT JOIN analytics_layer.dim_shop ds
@@ -124,5 +154,9 @@ LEFT JOIN sales_summary ss
AND sl."店铺主键" = ss."店铺主键"
LEFT JOIN product_dim pd
ON sl."统一规格编码" = pd."统一规格编码"
WHERE pd."品牌名称" = {brand_filter}
ORDER BY sl."统一规格编码", sl."店铺主键"
"""
POSTGRES_QUERY = build_postgres_query()