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:
Neo
2026-05-04 02:30:19 +08:00
parent 2010034840
commit caf179a5da
130 changed files with 14543 additions and 2717 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
-- =============================================================================
-- etl_feiqiu / appRLS 视图层)
-- 生成日期2026-04-12
-- 生成日期2026-05-02
-- 来源:测试库(通过脚本自动导出)
-- =============================================================================
@@ -36,7 +36,8 @@ SELECT id,
unique_customers,
unique_tables,
created_at
FROM dws.dws_assistant_daily_detail d;
FROM dws.dws_assistant_daily_detail d
WHERE (stat_date <= app.business_date_now());
;
CREATE OR REPLACE VIEW app.v_cfg_area_category AS
@@ -61,7 +62,8 @@ SELECT price_id,
description,
created_at,
updated_at
FROM dws.cfg_assistant_level_price;
FROM dws.cfg_assistant_level_price
WHERE ((effective_from <= app.business_date_now()) AND (effective_to >= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_cfg_bonus_rules AS
@@ -112,7 +114,8 @@ SELECT tier_id,
description,
created_at,
updated_at
FROM dws.cfg_performance_tier;
FROM dws.cfg_performance_tier
WHERE ((effective_from <= app.business_date_now()) AND (effective_to >= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dim_assistant AS
@@ -301,7 +304,7 @@ SELECT assistant_service_id,
is_delete,
real_service_money
FROM dwd.dwd_assistant_service_log
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (COALESCE((create_time)::date, '0001-01-01'::date) <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dwd_recharge_order AS
@@ -330,7 +333,7 @@ SELECT recharge_order_id,
real_electricity_money,
electricity_adjust_money
FROM dwd.dwd_recharge_order
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (COALESCE((pay_time)::date, '0001-01-01'::date) <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dwd_settlement_head AS
@@ -372,7 +375,7 @@ SELECT order_settle_id,
pl_coupon_sale_amount,
mervou_sales_amount
FROM dwd.dwd_settlement_head
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (COALESCE((create_time)::date, '0001-01-01'::date) <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dwd_store_goods_sale AS
@@ -402,7 +405,7 @@ SELECT store_goods_sale_id,
coupon_share_money,
discount_price
FROM dwd.dwd_store_goods_sale
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (COALESCE((create_time)::date, '0001-01-01'::date) <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dwd_table_fee_log AS
@@ -436,7 +439,7 @@ SELECT table_fee_log_id,
activity_discount_amount,
real_service_money
FROM dwd.dwd_table_fee_log
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (COALESCE((create_time)::date, '0001-01-01'::date) <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_assistant_customer_stats AS
@@ -518,7 +521,7 @@ SELECT id,
is_exempt,
per_hour_contribution
FROM dws.dws_assistant_daily_detail
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_assistant_finance_analysis AS
@@ -543,7 +546,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_assistant_finance_analysis
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_assistant_monthly_summary AS
@@ -583,7 +586,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_assistant_monthly_summary
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_month <= (date_trunc('month'::text, (app.business_date_now())::timestamp with time zone))::date));
;
CREATE OR REPLACE VIEW app.v_dws_assistant_order_contribution AS
@@ -687,7 +690,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_assistant_salary_calc
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (salary_month <= (date_trunc('month'::text, (app.business_date_now())::timestamp with time zone))::date));
;
CREATE OR REPLACE VIEW app.v_dws_coach_area_hours AS
@@ -746,9 +749,10 @@ SELECT id,
renewal_cash,
order_count,
created_at,
updated_at
updated_at,
member_order_count
FROM dws.dws_finance_area_daily
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_finance_board_cache AS
@@ -822,7 +826,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_finance_daily_summary
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_finance_discount_detail AS
@@ -839,7 +843,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_finance_discount_detail
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_finance_expense_summary AS
@@ -860,7 +864,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_finance_expense_summary
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (expense_month <= (date_trunc('month'::text, (app.business_date_now())::timestamp with time zone))::date));
;
CREATE OR REPLACE VIEW app.v_dws_finance_income_structure AS
@@ -878,7 +882,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_finance_income_structure
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_finance_recharge_summary AS
@@ -912,7 +916,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_finance_recharge_summary
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_member_assistant_intimacy AS
@@ -1033,7 +1037,7 @@ SELECT DISTINCT ON (member_id) id,
recharge_amount_90d,
avg_ticket_amount
FROM dws.dws_member_consumption_summary
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint)
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (stat_date <= app.business_date_now()))
ORDER BY member_id, stat_date DESC;
;
@@ -1162,7 +1166,7 @@ SELECT id,
created_at,
updated_at
FROM dws.dws_member_visit_detail
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint);
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (visit_date <= app.business_date_now()));
;
CREATE OR REPLACE VIEW app.v_dws_member_winback_index AS
@@ -1204,7 +1208,7 @@ SELECT DISTINCT ON (member_id) winback_id,
ideal_next_visit_date,
stat_date
FROM dws.dws_member_winback_index
WHERE (site_id = (current_setting('app.current_site_id'::text))::bigint)
WHERE ((site_id = (current_setting('app.current_site_id'::text))::bigint) AND (COALESCE((last_visit_time)::date, '0001-01-01'::date) <= app.business_date_now()))
ORDER BY member_id, stat_date DESC;
;

View File

@@ -0,0 +1,89 @@
-- 20260420_ai_trigger_jobs_and_app2_prewarm.sql
-- 注册 AI 事件触发器 + App2 财务洞察 cron 预热任务
--
-- 背景Phase 0.1~0.3 AI 模块收尾。dispatcher 已注册 5 个 handler 到
-- trigger_scheduler._JOB_REGISTRY通过 main.py lifespan 调用),
-- 但 biz.trigger_jobs 缺对应数据行fire_event 查不到事件绑定而失效。
--
-- 本脚本插入:
-- 4 条 event 类型记录ai_consumption_settled / ai_note_created /
-- ai_task_assigned / ai_dws_completed
-- 1 条 cron 类型记录ai_dws_prewarm (每日 08:30 给所有 site 触发 App2 × 8 维度)
--
-- 幂等job_name UNIQUE 约束 + ON CONFLICT DO NOTHING。
--
-- 验证 SQL执行后
-- 1. SELECT count(*) FROM biz.trigger_jobs WHERE job_type LIKE 'ai_%';
-- 应为 5
-- 2. SELECT job_name, trigger_condition, trigger_config FROM biz.trigger_jobs
-- WHERE trigger_condition = 'event' AND trigger_config->>'event_name' LIKE 'ai_%';
-- 应为 4 条event_name 与 job_name 一致
-- 3. SELECT job_name, trigger_config->>'cron_expression' FROM biz.trigger_jobs
-- WHERE job_type = 'ai_dws_prewarm';
-- 应为 "30 8 * * *"
--
-- 回滚:
-- DELETE FROM biz.trigger_jobs WHERE job_name IN (
-- 'ai_consumption_settled', 'ai_note_created',
-- 'ai_task_assigned', 'ai_dws_completed', 'ai_dws_prewarm'
-- );
BEGIN;
-- ── 4 条 event 类型触发器 ──────────────────────────────
INSERT INTO biz.trigger_jobs
(job_type, job_name, trigger_condition, trigger_config, status, description)
VALUES
(
'ai_consumption_settled',
'ai_consumption_settled',
'event',
'{"event_name": "ai_consumption_settled"}'::jsonb,
'enabled',
'AI 消费事件链App3 → App8 → App7+ App4 → App5 含助教)'
),
(
'ai_note_created',
'ai_note_created',
'event',
'{"event_name": "ai_note_created"}'::jsonb,
'enabled',
'AI 备注事件链App6 → App8'
),
(
'ai_task_assigned',
'ai_task_assigned',
'event',
'{"event_name": "ai_task_assigned"}'::jsonb,
'enabled',
'AI 任务分配事件链App4 → App5'
),
(
'ai_dws_completed',
'ai_dws_completed',
'event',
'{"event_name": "ai_dws_completed"}'::jsonb,
'enabled',
'AI DWS 完成事件App2 财务洞察 × 8 时间维度预生成'
)
ON CONFLICT (job_name) DO NOTHING;
-- ── 1 条 cron 类型触发器 ───────────────────────────────
-- 每日 08:30 对所有 active 门店逐个触发 ai_dws_completed 事件,
-- 作为 etl-completed 端点之外的兜底机制。
INSERT INTO biz.trigger_jobs
(job_type, job_name, trigger_condition, trigger_config, status, description)
VALUES
(
'ai_dws_prewarm',
'ai_dws_prewarm_0830',
'cron',
'{"cron_expression": "30 8 * * *"}'::jsonb,
'enabled',
'App2 财务洞察每日预热08:30 对所有门店触发 ai_dws_completed × 8 维度'
)
ON CONFLICT (job_name) DO NOTHING;
COMMIT;

View File

@@ -0,0 +1,42 @@
-- 20260421_app2_prewarm_cron_reschedule.sql
-- App2 财务洞察 cron 预热时间从 08:30 调整为 10:00 + 扩展为 72 组合覆盖
--
-- 背景:用户需求 2026-04-21
-- 1) 每日 10:00 为所有门店生成 board-finance 所有筛选组合下的 AI 洞察
-- 2) 筛选组合 = 8 时间维度 × 9 区域 = 72 组合/门店
-- 3) 前端根据当前筛选条件读缓存target_id=time__area
--
-- 变更:
-- 1. job_name rename: ai_dws_prewarm_0830 → ai_dws_prewarm_1000
-- 2. cron_expression: "30 8 * * *" → "0 10 * * *"
-- 3. description 更新为"72 组合"说明
--
-- 注dispatcher._handle_dws_completed 已改为遍历 72 组合,无需额外的 handler 注册。
--
-- 验证 SQL执行后
-- 1. SELECT job_name FROM biz.trigger_jobs
-- WHERE job_type = 'ai_dws_prewarm';
-- 应为 'ai_dws_prewarm_1000'
-- 2. SELECT trigger_config->>'cron_expression' FROM biz.trigger_jobs
-- WHERE job_type = 'ai_dws_prewarm';
-- 应为 '0 10 * * *'
-- 3. SELECT count(*) FROM biz.trigger_jobs WHERE job_type LIKE 'ai_%';
-- 应为 5不变
--
-- 回滚:
-- UPDATE biz.trigger_jobs
-- SET job_name = 'ai_dws_prewarm_0830',
-- trigger_config = '{"cron_expression": "30 8 * * *"}'::jsonb,
-- description = 'App2 财务洞察每日预热08:30 对所有门店触发 ai_dws_completed × 8 维度'
-- WHERE job_type = 'ai_dws_prewarm';
BEGIN;
UPDATE biz.trigger_jobs
SET
job_name = 'ai_dws_prewarm_1000',
trigger_config = '{"cron_expression": "0 10 * * *"}'::jsonb,
description = 'App2 财务洞察每日预热10:00 对所有门店触发 ai_dws_completed × 72 组合8 时间 × 9 区域)'
WHERE job_type = 'ai_dws_prewarm';
COMMIT;

View File

@@ -0,0 +1,117 @@
-- 2026-05-01
-- 业务运行上下文与沙箱隔离。
BEGIN;
CREATE TABLE IF NOT EXISTS biz.site_runtime_context (
site_id bigint PRIMARY KEY,
mode character varying(20) NOT NULL DEFAULT 'live',
sandbox_date date,
sandbox_instance_id character varying(64),
ai_mode character varying(20) NOT NULL DEFAULT 'live',
status character varying(20) NOT NULL DEFAULT 'active',
reason text,
updated_by bigint,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT site_runtime_context_site_id_fkey
FOREIGN KEY (site_id) REFERENCES biz.sites(site_id),
CONSTRAINT site_runtime_context_mode_check
CHECK (mode IN ('live', 'sandbox')),
CONSTRAINT site_runtime_context_ai_mode_check
CHECK (ai_mode IN ('live')),
CONSTRAINT site_runtime_context_sandbox_check
CHECK (
(mode = 'live' AND sandbox_date IS NULL AND sandbox_instance_id IS NULL)
OR
(mode = 'sandbox' AND sandbox_date IS NOT NULL AND sandbox_instance_id IS NOT NULL)
)
);
COMMENT ON TABLE biz.site_runtime_context IS '门店业务运行上下文live 使用真实日期sandbox 使用指定业务日期并按实例隔离写入。';
COMMENT ON COLUMN biz.site_runtime_context.mode IS '运行模式live / sandbox。';
COMMENT ON COLUMN biz.site_runtime_context.sandbox_date IS 'sandbox 模式下系统假设的业务日期。';
COMMENT ON COLUMN biz.site_runtime_context.sandbox_instance_id IS 'sandbox 模式写入隔离实例 ID。';
COMMENT ON COLUMN biz.site_runtime_context.ai_mode IS 'AI 调用模式;当前固定 live沙箱也真实调用 DashScope。';
ALTER TABLE biz.coach_tasks
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.coach_task_transfer_log
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.recall_events
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.coach_task_history
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.ai_cache
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.ai_run_logs
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
ALTER TABLE biz.ai_trigger_jobs
ADD COLUMN IF NOT EXISTS runtime_mode character varying(20) NOT NULL DEFAULT 'live',
ADD COLUMN IF NOT EXISTS sandbox_instance_id character varying(64) NOT NULL DEFAULT 'live';
UPDATE biz.coach_tasks SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.coach_task_transfer_log SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.recall_events SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.coach_task_history SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.ai_cache SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.ai_run_logs SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
UPDATE biz.ai_trigger_jobs SET runtime_mode = 'live', sandbox_instance_id = 'live' WHERE sandbox_instance_id IS NULL;
DROP INDEX IF EXISTS biz.idx_coach_tasks_site_assistant_member_type;
CREATE UNIQUE INDEX IF NOT EXISTS idx_coach_tasks_runtime_unique_active
ON biz.coach_tasks (site_id, assistant_id, member_id, task_type, runtime_mode, sandbox_instance_id)
WHERE status = 'active';
DROP INDEX IF EXISTS biz.idx_recall_events_site_assistant_member_day;
CREATE UNIQUE INDEX IF NOT EXISTS idx_recall_events_runtime_site_assistant_member_day
ON biz.recall_events (
site_id,
assistant_id,
member_id,
runtime_mode,
sandbox_instance_id,
(date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai'))
);
CREATE INDEX IF NOT EXISTS idx_coach_tasks_runtime_assistant_status
ON biz.coach_tasks (site_id, runtime_mode, sandbox_instance_id, assistant_id, status);
CREATE INDEX IF NOT EXISTS idx_ai_cache_runtime_lookup
ON biz.ai_cache (cache_type, site_id, runtime_mode, sandbox_instance_id, target_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_ai_trigger_jobs_runtime_site
ON biz.ai_trigger_jobs (site_id, runtime_mode, sandbox_instance_id, event_type, status);
COMMIT;
-- 回滚参考:
-- BEGIN;
-- DROP INDEX IF EXISTS biz.idx_ai_trigger_jobs_runtime_site;
-- DROP INDEX IF EXISTS biz.idx_ai_cache_runtime_lookup;
-- DROP INDEX IF EXISTS biz.idx_coach_tasks_runtime_assistant_status;
-- DROP INDEX IF EXISTS biz.idx_recall_events_runtime_site_assistant_member_day;
-- CREATE UNIQUE INDEX idx_recall_events_site_assistant_member_day ON biz.recall_events USING btree (site_id, assistant_id, member_id, (date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai')));
-- DROP INDEX IF EXISTS biz.idx_coach_tasks_runtime_unique_active;
-- CREATE UNIQUE INDEX idx_coach_tasks_site_assistant_member_type ON biz.coach_tasks USING btree (site_id, assistant_id, member_id, task_type) WHERE status = 'active';
-- ALTER TABLE biz.ai_trigger_jobs DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.ai_run_logs DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.ai_cache DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.coach_task_history DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.recall_events DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.coach_task_transfer_log DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- ALTER TABLE biz.coach_tasks DROP COLUMN IF EXISTS sandbox_instance_id, DROP COLUMN IF EXISTS runtime_mode;
-- DROP TABLE IF EXISTS biz.site_runtime_context;
-- COMMIT;

View File

@@ -1,6 +1,6 @@
-- =============================================================================
-- zqyy_app / biz核心业务表任务/备注/触发器))
-- 生成日期2026-04-06
-- 生成日期2026-05-02
-- 来源:测试库(通过脚本自动导出)
-- =============================================================================
@@ -16,11 +16,11 @@ CREATE SEQUENCE IF NOT EXISTS biz.cfg_task_generator_params_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.coach_task_history_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.coach_task_transfer_log_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.coach_tasks_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.recall_events_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.connectors_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS biz.dws_assistant_task_monthly_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.excel_upload_log_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.notes_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.recall_events_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.salary_adjustments_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS biz.site_code_history_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS biz.sites_id_seq AS integer;
@@ -41,7 +41,9 @@ CREATE TABLE biz.ai_cache (
triggered_by character varying(100),
created_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone,
status character varying(20) DEFAULT 'valid'::character varying
status character varying(20) DEFAULT 'valid'::character varying,
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.ai_conversations (
@@ -86,7 +88,9 @@ CREATE TABLE biz.ai_run_logs (
session_id character varying(100),
created_at timestamp with time zone DEFAULT now() NOT NULL,
finished_at timestamp with time zone,
alert_status character varying(20) DEFAULT NULL::character varying
alert_status character varying(20) DEFAULT NULL::character varying,
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.ai_trigger_jobs (
@@ -102,7 +106,9 @@ CREATE TABLE biz.ai_trigger_jobs (
started_at timestamp with time zone,
finished_at timestamp with time zone,
error_message text,
created_at timestamp with time zone DEFAULT now() NOT NULL
created_at timestamp with time zone DEFAULT now() NOT NULL,
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.cfg_task_generator_params (
@@ -124,7 +130,9 @@ CREATE TABLE biz.coach_task_history (
old_task_type character varying(50),
new_task_type character varying(50),
detail jsonb,
created_at timestamp with time zone DEFAULT now()
created_at timestamp with time zone DEFAULT now(),
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.coach_task_transfer_log (
@@ -138,7 +146,9 @@ CREATE TABLE biz.coach_task_transfer_log (
transfer_reason text,
guard_checks jsonb,
transfer_score numeric,
created_at timestamp with time zone DEFAULT now() NOT NULL
created_at timestamp with time zone DEFAULT now() NOT NULL,
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.coach_tasks (
@@ -154,24 +164,15 @@ CREATE TABLE biz.coach_tasks (
abandon_reason text,
completed_at timestamp with time zone,
completed_task_type character varying(50),
completion_type character varying(10),
parent_task_id bigint,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
transfer_count integer DEFAULT 0 NOT NULL,
transferred_from bigint,
transferred_at timestamp with time zone
);
CREATE TABLE biz.recall_events (
id bigint DEFAULT nextval('biz.recall_events_id_seq'::regclass) NOT NULL,
site_id bigint NOT NULL,
assistant_id bigint NOT NULL,
member_id bigint NOT NULL,
pay_time timestamp with time zone NOT NULL,
task_id bigint,
task_type character varying(50),
created_at timestamp with time zone DEFAULT now()
transferred_at timestamp with time zone,
completion_type character varying(10),
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.connectors (
@@ -232,6 +233,19 @@ CREATE TABLE biz.notes (
score smallint
);
CREATE TABLE biz.recall_events (
id bigint DEFAULT nextval('biz.recall_events_id_seq'::regclass) NOT NULL,
site_id bigint NOT NULL,
assistant_id bigint NOT NULL,
member_id bigint NOT NULL,
pay_time timestamp with time zone NOT NULL,
task_id bigint,
task_type character varying(50),
created_at timestamp with time zone DEFAULT now(),
runtime_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_instance_id character varying(64) DEFAULT 'live'::character varying NOT NULL
);
CREATE TABLE biz.salary_adjustments (
id bigint DEFAULT nextval('biz.salary_adjustments_id_seq'::regclass) NOT NULL,
site_id bigint NOT NULL,
@@ -256,6 +270,19 @@ CREATE TABLE biz.site_code_history (
retired_at timestamp with time zone
);
CREATE TABLE biz.site_runtime_context (
site_id bigint NOT NULL,
mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
sandbox_date date,
sandbox_instance_id character varying(64),
ai_mode character varying(20) DEFAULT 'live'::character varying NOT NULL,
status character varying(20) DEFAULT 'active'::character varying NOT NULL,
reason text,
updated_by bigint,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE biz.sites (
id integer DEFAULT nextval('biz.sites_id_seq'::regclass) NOT NULL,
tenant_id integer NOT NULL,
@@ -354,12 +381,14 @@ ALTER TABLE biz.dws_assistant_task_monthly ADD CONSTRAINT dws_assistant_task_mon
ALTER TABLE biz.excel_upload_log ADD CONSTRAINT excel_upload_log_pkey PRIMARY KEY (id);
ALTER TABLE biz.notes ADD CONSTRAINT notes_task_id_fkey FOREIGN KEY (task_id) REFERENCES biz.coach_tasks(id);
ALTER TABLE biz.notes ADD CONSTRAINT notes_pkey PRIMARY KEY (id);
ALTER TABLE biz.recall_events ADD CONSTRAINT recall_events_pkey PRIMARY KEY (id);
ALTER TABLE biz.recall_events ADD CONSTRAINT recall_events_task_id_fkey FOREIGN KEY (task_id) REFERENCES biz.coach_tasks(id);
ALTER TABLE biz.recall_events ADD CONSTRAINT recall_events_pkey PRIMARY KEY (id);
ALTER TABLE biz.salary_adjustments ADD CONSTRAINT salary_adjustments_upload_batch_id_fkey FOREIGN KEY (upload_batch_id) REFERENCES biz.excel_upload_log(id);
ALTER TABLE biz.salary_adjustments ADD CONSTRAINT salary_adjustments_pkey PRIMARY KEY (id);
ALTER TABLE biz.site_code_history ADD CONSTRAINT site_code_history_pkey PRIMARY KEY (id);
ALTER TABLE biz.site_code_history ADD CONSTRAINT site_code_history_site_code_key UNIQUE (site_code);
ALTER TABLE biz.site_runtime_context ADD CONSTRAINT site_runtime_context_site_id_fkey FOREIGN KEY (site_id) REFERENCES biz.sites(site_id);
ALTER TABLE biz.site_runtime_context ADD CONSTRAINT site_runtime_context_pkey PRIMARY KEY (site_id);
ALTER TABLE biz.sites ADD CONSTRAINT sites_tenant_id_fkey FOREIGN KEY (tenant_id) REFERENCES biz.tenants(id);
ALTER TABLE biz.sites ADD CONSTRAINT sites_pkey PRIMARY KEY (id);
ALTER TABLE biz.sites ADD CONSTRAINT sites_site_code_key UNIQUE (site_code);
@@ -379,6 +408,7 @@ ALTER TABLE biz.trigger_jobs ADD CONSTRAINT trigger_jobs_job_name_key UNIQUE (jo
-- 索引
CREATE INDEX idx_ai_cache_cleanup ON biz.ai_cache USING btree (cache_type, site_id, target_id, created_at);
CREATE INDEX idx_ai_cache_lookup ON biz.ai_cache USING btree (cache_type, site_id, target_id, created_at DESC);
CREATE INDEX idx_ai_cache_runtime_lookup ON biz.ai_cache USING btree (cache_type, site_id, runtime_mode, sandbox_instance_id, target_id, created_at DESC);
CREATE INDEX idx_ai_conv_app_site ON biz.ai_conversations USING btree (app_id, site_id, created_at DESC);
CREATE INDEX idx_ai_conv_context ON biz.ai_conversations USING btree (user_id, site_id, context_type, context_id, last_message_at DESC NULLS LAST) WHERE (context_type IS NOT NULL);
CREATE INDEX idx_ai_conv_last_msg ON biz.ai_conversations USING btree (user_id, site_id, last_message_at DESC NULLS LAST);
@@ -390,18 +420,20 @@ CREATE INDEX idx_ai_run_logs_created_brin ON biz.ai_run_logs USING brin (created
CREATE INDEX idx_ai_run_logs_site_app ON biz.ai_run_logs USING btree (site_id, app_type);
CREATE INDEX idx_ai_run_logs_status ON biz.ai_run_logs USING btree (status);
CREATE INDEX idx_ai_trigger_jobs_dedup ON biz.ai_trigger_jobs USING btree (event_type, member_id, site_id, created_at) WHERE ((status)::text <> 'skipped_duplicate'::text);
CREATE INDEX idx_ai_trigger_jobs_runtime_site ON biz.ai_trigger_jobs USING btree (site_id, runtime_mode, sandbox_instance_id, event_type, status);
CREATE INDEX idx_ai_trigger_jobs_site ON biz.ai_trigger_jobs USING btree (site_id, event_type);
CREATE INDEX idx_ai_trigger_jobs_status ON biz.ai_trigger_jobs USING btree (status);
CREATE INDEX idx_transfer_log_member ON biz.coach_task_transfer_log USING btree (member_id, created_at DESC);
CREATE INDEX idx_transfer_log_site_created ON biz.coach_task_transfer_log USING btree (site_id, created_at DESC);
CREATE INDEX idx_coach_tasks_assistant_status ON biz.coach_tasks USING btree (site_id, assistant_id, status);
CREATE UNIQUE INDEX idx_coach_tasks_site_assistant_member_type ON biz.coach_tasks USING btree (site_id, assistant_id, member_id, task_type) WHERE ((status)::text = 'active'::text);
CREATE INDEX idx_coach_tasks_runtime_assistant_status ON biz.coach_tasks USING btree (site_id, runtime_mode, sandbox_instance_id, assistant_id, status);
CREATE UNIQUE INDEX idx_coach_tasks_runtime_unique_active ON biz.coach_tasks USING btree (site_id, assistant_id, member_id, task_type, runtime_mode, sandbox_instance_id) WHERE ((status)::text = 'active'::text);
CREATE INDEX idx_task_monthly_assistant ON biz.dws_assistant_task_monthly USING btree (assistant_id, stat_month DESC);
CREATE INDEX idx_task_monthly_site_month ON biz.dws_assistant_task_monthly USING btree (site_id, stat_month DESC);
CREATE INDEX idx_excel_log_site ON biz.excel_upload_log USING btree (site_id, created_at DESC);
CREATE INDEX idx_notes_target ON biz.notes USING btree (site_id, target_type, target_id);
CREATE INDEX idx_salary_adj_assistant_month ON biz.salary_adjustments USING btree (assistant_id, salary_month);
CREATE UNIQUE INDEX idx_recall_events_site_assistant_member_day ON biz.recall_events USING btree (site_id, assistant_id, member_id, (date_trunc('day', pay_time AT TIME ZONE 'Asia/Shanghai')));
CREATE INDEX idx_recall_events_assistant_pay ON biz.recall_events USING btree (site_id, assistant_id, pay_time);
CREATE UNIQUE INDEX idx_recall_events_runtime_site_assistant_member_day ON biz.recall_events USING btree (site_id, assistant_id, member_id, runtime_mode, sandbox_instance_id, date_trunc('day'::text, (pay_time AT TIME ZONE 'Asia/Shanghai'::text)));
CREATE INDEX idx_salary_adj_assistant_month ON biz.salary_adjustments USING btree (assistant_id, salary_month);
CREATE INDEX idx_salary_adj_site_month ON biz.salary_adjustments USING btree (site_id, salary_month);