feat: 2026-04-15~05-02 累积变更基线 — AI 重构 + Runtime Context + DWS 修复
涵盖(每条对应已存的审计记录): - AI 模块拆分:apps/backend/app/ai/apps -> prompts/(8 个 APP + app2a 派生) audit: 2026-04-20__ai-module-complete.md - admin-web AI 管理套件:AIDashboard / AIOperations / AIRunLogs / AITriggers / TriggerManager audit: 2026-04-21__admin-web-ai-management-suite.md - App2 财务洞察 prompt v3 -> v5.1 + 小程序 AI 接入(chat / board-finance) audit: 2026-04-22__app2_prompt_v5_1_and_miniprogram_ai_insight.md - App2 prewarm 全过滤器 + AI 触发器 cron reschedule audit: 2026-04-21__app2-finance-prewarm-all-filters.md migration: 20260420_ai_trigger_jobs_and_app2_prewarm.sql / 20260421_app2_prewarm_cron_reschedule.sql - AppType 联合类型对齐 + adminAiAppTypes.test.ts audit: 2026-04-30__admin_web_ai_app_type_alignment.md - DashScope tokens_used 提取修复 audit: 2026-04-30__backend_dashscope_tokens_used_extraction.md - App3 线索完整详情 prompt audit: 2026-05-01__backend_app3_full_detail_prompt.md - Runtime Context 沙箱(5-1~5-2 主线): - 后端 schema/service + admin_runtime_context / xcx_runtime_clock 两个 router - admin-web RuntimeContext.tsx + miniprogram runtime-clock.ts - migration: 20260501__runtime_context_sandbox.sql - tools/db/verify_admin_web_sandbox.py + verify_sandbox_end_to_end.py - database/changes: 7 份 sandbox_* 验证报告 - 飞球 DWS 修复:finance_area_daily 区域汇总 + task_engine 调整 + RLS 视图业务日上界(migration 20260502 + scripts/ops/gen_rls_business_date_migration.py) 合规: - .gitignore 启用 tmp/ 排除 - 不入仓:apps/etl/connectors/feiqiu/.env(API_TOKEN secret,本地修改保留) 待验证清单: - docs/audit/changes/2026-05-04__cumulative_baseline_pending_verification.md 每个主题的功能完整性 / 上线验证几乎都未收口,按优先级 P0~P3 逐一处理
This commit is contained in:
@@ -22,6 +22,13 @@ import json
|
||||
import logging
|
||||
from datetime import timedelta
|
||||
|
||||
from app.services.runtime_context import (
|
||||
LIVE_INSTANCE_ID,
|
||||
MODE_LIVE,
|
||||
MODE_SANDBOX,
|
||||
get_runtime_context,
|
||||
task_runtime_filter,
|
||||
)
|
||||
from app.trace.decorators import trace_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -141,6 +148,10 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
resolved = 0
|
||||
|
||||
from app.services.fdw_queries import _fdw_context
|
||||
runtime_ctx = get_runtime_context(site_id, conn=conn)
|
||||
runtime_now = runtime_ctx.business_now
|
||||
runtime_mode = MODE_SANDBOX if runtime_ctx.is_sandbox else MODE_LIVE
|
||||
sandbox_instance_id = runtime_ctx.sandbox_instance_id if runtime_ctx.is_sandbox else LIVE_INSTANCE_ID
|
||||
|
||||
# ── 1. 获取本门店所有 MAIN 关系对 ──
|
||||
with _fdw_context(conn, site_id) as cur:
|
||||
@@ -173,13 +184,14 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
ON sl.order_settle_id = sh.order_settle_id
|
||||
AND sl.is_delete = 0
|
||||
WHERE sh.member_id = ANY(%s)
|
||||
AND sh.pay_time <= %s
|
||||
AND (
|
||||
sh.settle_type = 1
|
||||
OR (sh.settle_type = 3 AND sl.order_assistant_type = 2)
|
||||
)
|
||||
GROUP BY sl.site_assistant_id, sh.member_id
|
||||
""",
|
||||
(member_ids,),
|
||||
(member_ids, runtime_now),
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
settlement_map[(row[0], row[1])] = row[2]
|
||||
@@ -190,6 +202,7 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
SELECT sh.member_id, MAX(sh.pay_time) AS latest_pay_time
|
||||
FROM app.v_dwd_settlement_head sh
|
||||
WHERE sh.member_id = ANY(%s)
|
||||
AND sh.pay_time <= %s
|
||||
AND (
|
||||
sh.settle_type = 1
|
||||
OR (sh.settle_type = 3 AND EXISTS (
|
||||
@@ -201,7 +214,7 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
)
|
||||
GROUP BY sh.member_id
|
||||
""",
|
||||
(member_ids,),
|
||||
(member_ids, runtime_now),
|
||||
)
|
||||
member_visited_map = {}
|
||||
for row in cur.fetchall():
|
||||
@@ -209,16 +222,18 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
|
||||
# ── 3. 获取本门店所有 active 的召回/回访任务(用于匹配) ──
|
||||
active_tasks_map: dict[tuple[int, int], list] = {} # (assistant_id, member_id) → [(id, task_type, created_at)]
|
||||
runtime_clause, runtime_params = task_runtime_filter(site_id, conn=conn)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT id, assistant_id, member_id, task_type, created_at
|
||||
FROM biz.coach_tasks
|
||||
WHERE site_id = %s
|
||||
{runtime_clause}
|
||||
AND status = 'active'
|
||||
AND task_type IN ('high_priority_recall', 'priority_recall', 'follow_up_visit')
|
||||
""",
|
||||
(site_id,),
|
||||
[site_id, *runtime_params],
|
||||
)
|
||||
for row in cur.fetchall():
|
||||
key = (row[1], row[2])
|
||||
@@ -238,7 +253,7 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
try:
|
||||
result = _process_pair(
|
||||
conn, site_id, assistant_id, member_id,
|
||||
latest_pay, active_tasks,
|
||||
latest_pay, active_tasks, runtime_now, runtime_mode, sandbox_instance_id,
|
||||
)
|
||||
completed += result["completed"]
|
||||
events += result["events"]
|
||||
@@ -257,25 +272,26 @@ def _process_site(conn, site_id: int) -> dict:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute("BEGIN")
|
||||
cur.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT id, assistant_id, task_type, created_at
|
||||
FROM biz.coach_tasks
|
||||
WHERE site_id = %s AND member_id = %s
|
||||
{runtime_clause}
|
||||
AND status = 'active'
|
||||
AND task_type IN ('high_priority_recall', 'priority_recall')
|
||||
AND created_at < %s
|
||||
""",
|
||||
(site_id, member_id, pay_time),
|
||||
[site_id, member_id, *runtime_params, pay_time],
|
||||
)
|
||||
remaining = cur.fetchall()
|
||||
for task_id, aid, task_type, _ in remaining:
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE biz.coach_tasks
|
||||
SET status = 'resolved', updated_at = NOW()
|
||||
SET status = 'resolved', updated_at = %s
|
||||
WHERE id = %s AND status = 'active'
|
||||
""",
|
||||
(task_id,),
|
||||
(runtime_now, task_id),
|
||||
)
|
||||
_insert_history(
|
||||
cur, task_id,
|
||||
@@ -308,6 +324,9 @@ def _process_pair(
|
||||
member_id: int,
|
||||
latest_pay_time,
|
||||
active_tasks: list[dict],
|
||||
runtime_now,
|
||||
runtime_mode: str,
|
||||
sandbox_instance_id: str,
|
||||
) -> dict:
|
||||
"""
|
||||
处理单个 MAIN 关系对的召回检测。
|
||||
@@ -339,14 +358,16 @@ def _process_pair(
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO biz.recall_events
|
||||
(site_id, assistant_id, member_id, pay_time, task_id, task_type)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (site_id, assistant_id, member_id, (date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai')))
|
||||
(site_id, assistant_id, member_id, pay_time, task_id, task_type,
|
||||
created_at, runtime_mode, sandbox_instance_id)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (site_id, assistant_id, member_id, runtime_mode, sandbox_instance_id,
|
||||
(date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai')))
|
||||
DO NOTHING
|
||||
RETURNING id
|
||||
""",
|
||||
(site_id, assistant_id, member_id, latest_pay_time,
|
||||
event_task_id, event_task_type),
|
||||
event_task_id, event_task_type, runtime_now, runtime_mode, sandbox_instance_id),
|
||||
)
|
||||
inserted = cur.fetchone()
|
||||
if inserted is None:
|
||||
@@ -367,10 +388,10 @@ def _process_pair(
|
||||
completed_at = %s,
|
||||
completed_task_type = %s,
|
||||
completion_type = 'auto',
|
||||
updated_at = NOW()
|
||||
updated_at = %s
|
||||
WHERE id = %s AND status = 'active'
|
||||
""",
|
||||
(latest_pay_time, task["task_type"], task["id"]),
|
||||
(latest_pay_time, task["task_type"], runtime_now, task["id"]),
|
||||
)
|
||||
_insert_history(
|
||||
cur,
|
||||
@@ -393,18 +414,19 @@ def _process_pair(
|
||||
SELECT id FROM biz.coach_tasks
|
||||
WHERE site_id = %s AND assistant_id = %s AND member_id = %s
|
||||
AND task_type = 'follow_up_visit' AND status = 'active'
|
||||
AND runtime_mode = %s AND sandbox_instance_id = %s
|
||||
""",
|
||||
(site_id, assistant_id, member_id),
|
||||
(site_id, assistant_id, member_id, runtime_mode, sandbox_instance_id),
|
||||
)
|
||||
old_follow_ups = cur.fetchall()
|
||||
for (old_id,) in old_follow_ups:
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE biz.coach_tasks
|
||||
SET status = 'inactive', updated_at = NOW()
|
||||
SET status = 'inactive', updated_at = %s
|
||||
WHERE id = %s
|
||||
""",
|
||||
(old_id,),
|
||||
(runtime_now, old_id),
|
||||
)
|
||||
_insert_history(
|
||||
cur, old_id,
|
||||
@@ -423,11 +445,14 @@ def _process_pair(
|
||||
"""
|
||||
INSERT INTO biz.coach_tasks
|
||||
(site_id, assistant_id, member_id, task_type, status,
|
||||
expires_at, created_at, updated_at)
|
||||
VALUES (%s, %s, %s, 'follow_up_visit', 'active', %s, NOW(), NOW())
|
||||
expires_at, created_at, updated_at, runtime_mode, sandbox_instance_id)
|
||||
VALUES (%s, %s, %s, 'follow_up_visit', 'active', %s, %s, %s, %s, %s)
|
||||
RETURNING id
|
||||
""",
|
||||
(site_id, assistant_id, member_id, expires_at),
|
||||
(
|
||||
site_id, assistant_id, member_id, expires_at, runtime_now,
|
||||
runtime_now, runtime_mode, sandbox_instance_id,
|
||||
),
|
||||
)
|
||||
new_follow_up_id = cur.fetchone()[0]
|
||||
_insert_history(
|
||||
|
||||
Reference in New Issue
Block a user