-- 隔离模拟验收记录
-- 07_simulate_monthly_review.sql 批量生成的验收记录特征：
--   process_evidence = '已按行动要求执行，提交过程证据'
--   task_monthly_review 中的 revenue_stable / customer_stable 恒为 true
--   store_task_log 中 operator = '区域经理'（模拟验收）

-- Step 1: 给 store_task 加 is_simulated 标记列
ALTER TABLE analytics.store_task
  ADD COLUMN IF NOT EXISTS is_simulated boolean DEFAULT false;

-- Step 2: 标记模拟验收的任务
UPDATE analytics.store_task
SET is_simulated = true
WHERE process_evidence = '已按行动要求执行，提交过程证据'
  AND status = '已验收';

-- Step 3: 给 task_monthly_review 加标记列
ALTER TABLE analytics.task_monthly_review
  ADD COLUMN IF NOT EXISTS is_simulated boolean DEFAULT false;

-- Step 4: 标记模拟的月度验收记录
UPDATE analytics.task_monthly_review r
SET is_simulated = true
FROM analytics.store_task t
WHERE r.task_id = t.task_id
  AND t.is_simulated = true;

-- Step 5: 给 store_task_log 加标记列
ALTER TABLE analytics.store_task_log
  ADD COLUMN IF NOT EXISTS is_simulated boolean DEFAULT false;

-- Step 6: 标记模拟的操作日志
UPDATE analytics.store_task_log
SET is_simulated = true
WHERE operator = '区域经理'
  AND action = '月度验收'
  AND comment IN ('达标', '改善中', '未改善');

-- Step 7: 给 best_practice 加标记列
ALTER TABLE analytics.best_practice
  ADD COLUMN IF NOT EXISTS is_simulated boolean DEFAULT false;

-- Step 8: 标记模拟的经验库记录
UPDATE analytics.best_practice bp
SET is_simulated = true
FROM analytics.store_task t
WHERE bp.source_store_code = t.store_code
  AND t.is_simulated = true
  AND bp.status = '待推广';
