-- =====================================================================================
-- S8-DEMO-DATA-SEED-1 / STEP 3：UATAdminA Golden Exception / Watch / Detection / Timeline / Decision
--
-- 目标 scope ：tenant=838257186181189 (UATTEST_CHL) / factory=838257186320453
-- 依赖       ：STEP 2 的 5 条 Golden Order（WIP-S8-DEMO-GOLDEN-DATASET.sql，555 行）已落库
-- 设计来源   ：STEP 3 DESIGN CLOSURE，G1–G24 = 24/24 PASS
--
-- 总计 79 行：Watch 4 + Exception 14 + Detection 8 + Timeline 50 + Decision 3｜Evidence = 0
--
-- 边界铁律：
--   * 不修改 legacy 438 / 439 / 470，不修改 factory_id=1 的 14 条 junk，不修改 UATA-A01-WATCH-01
--   * 不做 whole-table copy，不 INSERT...SELECT FROM 源租户（797403760988230）
--   * 全部为新设计的 deterministic Golden 记录，固定 ID 段，禁用 MAX(id)+1
--   * 幂等：每条 INSERT 均带 NOT EXISTS(id) 守卫；exception 另有唯一键
--     uk_ado_s8_exception_tenant_factory_code (tenant_id, factory_id, exception_code) 作第二重保险
--
-- ⚠️ 运行时安全（本脚本相对 STEP 3 approved roster 的两处偏离，均为防污染所必需）：
--   D-1  watch_rule.enabled = 0
--        job_s8_watch_scheduler 当前处于运行态（每 60s）。S8WatchSchedulerService 按 `WHERE r.enabled = 1`
--        选规则；若置 1，调度器会在 60 秒内评估这 4 条规则并可能自动建单 / 写 EVALUATE_FAILED 检测日志，
--        直接破坏 Golden 集的 14/8 基数。故落库为 0（规则结构完整、可随时启用）。
--   D-2  exception.sla_deadline = NULL
--        job_s8_timeout_auto_escalation 当前处于运行态（每 5min），扫描条件为
--        `sla_deadline IS NOT NULL AND sla_deadline < now AND status IN ('ASSIGNED','IN_PROGRESS')`。
--        Golden 的历史日期（2026-08）必然过期，若写入非空 sla_deadline，5 条 OPEN 异常会被自动升级为
--        ESCALATED，破坏状态分布与 timeline 连续性。NULL 与 legacy 470 的既有取值一致。
--
-- 状态机权威：S8StatusRules + S8TaskFlowService（逐动作 IsAllowedTransition 强校验）
--   合法链 NEW → ASSIGNED → IN_PROGRESS → PENDING_VERIFICATION → CLOSED
--   对应 CREATE / CLAIM / START_PROGRESS / VERIFY_SUBMITTED / VERIFY_APPROVED
--   禁用旧 seed 的 NEW→IN_PROGRESS 与 IN_PROGRESS→CLOSED，服务层无 CLOSE 动作
--
-- TIMEOUT 语义：S8TimeoutRuleEvaluator 判定 `dueAt <= now - graceMinutes`（即 now >= dueAt + grace）
--   Golden graceMinutes = 0；CREATED detection 统一取 dueAt + 1 minute（在 > 与 >= 下均成立）
-- =====================================================================================

-- ---------------------------------------------------------------------------
-- 1) WATCH RULE × 4    id 1329909460001 ~ 1329909460004
--    params_json 严格对齐 S8TimeoutParams 的解析键（dueAtField / statusField /
--    completedStates / objectCodeField / objectIdField / graceMinutes / exceptionTypeCode）。
--    expression 的列别名对齐 evaluator 的 canonical 列（related_object_code / source_object_id /
--    due_at / status），status 用 CASE 表达「阶段是否已完成」，使
--    HIT ⇔ due_at <= now AND status NOT IN ('COMPLETED')
--        ⇔ now >= actual_start_at + planned_days AND actual_end_at IS NULL
-- ---------------------------------------------------------------------------
INSERT INTO `ado_s8_watch_rule`
  (`id`,`tenant_id`,`factory_id`,`rule_code`,`scene_code`,`data_source_id`,`watch_object_type`,`expression`,
   `severity`,`poll_interval_seconds`,`enabled`,`created_at`,`updated_at`,`rule_type`,`source_object_type`,
   `params_json`,`consecutive_failure_count`,`trigger_count_required`,`recover_count_required`,
   `stage_code`,`order_flow_code`,`rule_mechanism`)
SELECT 1329909460001, 838257186181189, 838257186320453, 'GWR-S1-ORDER-DUE', 'S1', 5, 'ORDER',
       'SELECT o.order_code AS related_object_code, o.order_code AS source_object_id, DATE_ADD(o.release_at, INTERVAL o.target_cycle_days DAY) AS due_at, CASE WHEN o.actual_cycle_days IS NULL THEN ''RUNNING'' ELSE ''COMPLETED'' END AS status FROM ado_s8_order_flow_order o WHERE o.tenant_id=838257186181189 AND o.factory_id=838257186320453 AND o.is_deleted=0 AND o.current_order_flow_code=''FINAL_ASSEMBLY_DELIVERY''',
       'FOLLOW', 300, 0, '2026-08-28 00:00:00', NULL, 'TIMEOUT', 'ORDER',
       '{"dueAtField":"due_at","statusField":"status","completedStates":["COMPLETED"],"objectCodeField":"related_object_code","objectIdField":"source_object_id","graceMinutes":0,"exceptionTypeCode":"ORDER_DELIVERY_DELAY_WARNING"}',
       0, 1, 1, 'S1', NULL, 'RULE'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_watch_rule` x WHERE x.`id` = 1329909460001);

INSERT INTO `ado_s8_watch_rule`
  (`id`,`tenant_id`,`factory_id`,`rule_code`,`scene_code`,`data_source_id`,`watch_object_type`,`expression`,
   `severity`,`poll_interval_seconds`,`enabled`,`created_at`,`updated_at`,`rule_type`,`source_object_type`,
   `params_json`,`consecutive_failure_count`,`trigger_count_required`,`recover_count_required`,
   `stage_code`,`order_flow_code`,`rule_mechanism`)
SELECT 1329909460002, 838257186181189, 838257186320453, 'GWR-S4-PO-DELAY', 'S4', 5, 'PURCHASE_ORDER',
       'SELECT s.order_code AS related_object_code, s.order_code AS source_object_id, DATE_ADD(s.actual_start_at, INTERVAL s.planned_days DAY) AS due_at, CASE WHEN s.actual_end_at IS NULL THEN ''RUNNING'' ELSE ''COMPLETED'' END AS status FROM ado_s8_order_flow_stage s WHERE s.tenant_id=838257186181189 AND s.factory_id=838257186320453 AND s.is_deleted=0 AND s.order_flow_code=''MATERIAL_PURCHASE'' AND s.actual_start_at IS NOT NULL',
       'FOLLOW', 300, 0, '2026-08-28 00:00:00', NULL, 'TIMEOUT', 'PURCHASE_ORDER',
       '{"dueAtField":"due_at","statusField":"status","completedStates":["COMPLETED"],"objectCodeField":"related_object_code","objectIdField":"source_object_id","graceMinutes":0,"exceptionTypeCode":"PURCHASE_EXECUTION_DELAY"}',
       0, 1, 1, 'S4', 'MATERIAL_PURCHASE', 'RULE'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_watch_rule` x WHERE x.`id` = 1329909460002);

INSERT INTO `ado_s8_watch_rule`
  (`id`,`tenant_id`,`factory_id`,`rule_code`,`scene_code`,`data_source_id`,`watch_object_type`,`expression`,
   `severity`,`poll_interval_seconds`,`enabled`,`created_at`,`updated_at`,`rule_type`,`source_object_type`,
   `params_json`,`consecutive_failure_count`,`trigger_count_required`,`recover_count_required`,
   `stage_code`,`order_flow_code`,`rule_mechanism`)
SELECT 1329909460003, 838257186181189, 838257186320453, 'GWR-S6-WO-DELAY', 'S6', 5, 'WORK_ORDER',
       'SELECT s.order_code AS related_object_code, s.order_code AS source_object_id, DATE_ADD(s.actual_start_at, INTERVAL s.planned_days DAY) AS due_at, CASE WHEN s.actual_end_at IS NULL THEN ''RUNNING'' ELSE ''COMPLETED'' END AS status FROM ado_s8_order_flow_stage s WHERE s.tenant_id=838257186181189 AND s.factory_id=838257186320453 AND s.is_deleted=0 AND s.order_flow_code=''BODY_PRODUCTION'' AND s.actual_start_at IS NOT NULL',
       'SERIOUS', 300, 0, '2026-08-28 00:00:00', NULL, 'TIMEOUT', 'WORK_ORDER',
       '{"dueAtField":"due_at","statusField":"status","completedStates":["COMPLETED"],"objectCodeField":"related_object_code","objectIdField":"source_object_id","graceMinutes":0,"exceptionTypeCode":"PRODUCTION_WORK_ORDER_COMPLETION_DELAY"}',
       0, 1, 1, 'S6', 'BODY_PRODUCTION', 'RULE'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_watch_rule` x WHERE x.`id` = 1329909460003);

INSERT INTO `ado_s8_watch_rule`
  (`id`,`tenant_id`,`factory_id`,`rule_code`,`scene_code`,`data_source_id`,`watch_object_type`,`expression`,
   `severity`,`poll_interval_seconds`,`enabled`,`created_at`,`updated_at`,`rule_type`,`source_object_type`,
   `params_json`,`consecutive_failure_count`,`trigger_count_required`,`recover_count_required`,
   `stage_code`,`order_flow_code`,`rule_mechanism`)
SELECT 1329909460004, 838257186181189, 838257186320453, 'GWR-S7-SHIP-DELAY', 'S7', 5, 'SHIPMENT',
       'SELECT s.order_code AS related_object_code, s.order_code AS source_object_id, DATE_ADD(s.actual_start_at, INTERVAL s.planned_days DAY) AS due_at, CASE WHEN s.actual_end_at IS NULL THEN ''RUNNING'' ELSE ''COMPLETED'' END AS status FROM ado_s8_order_flow_stage s WHERE s.tenant_id=838257186181189 AND s.factory_id=838257186320453 AND s.is_deleted=0 AND s.order_flow_code=''FINAL_ASSEMBLY_DELIVERY'' AND s.actual_start_at IS NOT NULL',
       'SERIOUS', 300, 0, '2026-08-28 00:00:00', NULL, 'TIMEOUT', 'SHIPMENT',
       '{"dueAtField":"due_at","statusField":"status","completedStates":["COMPLETED"],"objectCodeField":"related_object_code","objectIdField":"source_object_id","graceMinutes":0,"exceptionTypeCode":"SHIPMENT_ABNORMAL"}',
       0, 1, 1, 'S7', 'FINAL_ASSEMBLY_DELIVERY', 'RULE'
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_watch_rule` x WHERE x.`id` = 1329909460004);

-- ---------------------------------------------------------------------------
-- 2) GOLDEN EXCEPTION × 14    id 1329909430001 ~ 1329909430014
--    priority：SERIOUS → (80.00,'P1')；FOLLOW → (0.00,'P3')（沿用库内既有取值先例）
--    sla_deadline 一律 NULL（见文件头 D-2）
-- ---------------------------------------------------------------------------
INSERT INTO `ado_s8_exception`
 (`id`,`tenant_id`,`factory_id`,`exception_code`,`title`,`description`,`scene_code`,`source_type`,`status`,`severity`,
  `priority_score`,`priority_level`,`occurrence_dept_id`,`responsible_dept_id`,`assignee_id`,`sla_deadline`,`timeout_flag`,
  `created_at`,`updated_at`,`closed_at`,`is_deleted`,`related_object_code`,`module_code`,`exception_type_code`,
  `source_rule_code`,`source_object_type`,`source_object_id`,`dedup_key`,`last_detected_at`,
  `consecutive_hit_count`,`consecutive_miss_count`,`stage_code`,`order_flow_code`,`rule_mechanism`)
SELECT * FROM (
  SELECT 1329909430001 AS a,838257186181189 AS b,838257186320453 AS c,'GEX-001-PD' AS d,'订单 SO-2026-001 产品设计延期 2 天' AS e,'高压接线盒图纸评审反复，产品设计阶段计划 3 天、实际 5 天，节点偏差 +2 天。' AS f,'S1' AS g,'MANUAL' AS h,'CLOSED' AS i,'SERIOUS' AS j,80.00 AS k,'P1' AS l,250 AS m,250 AS n,3210 AS o,NULL AS p,0 AS q,'2026-07-28 09:00:00' AS r,'2026-07-30 09:00:00' AS s,'2026-07-30 09:00:00' AS t,0 AS u,'SO-2026-001' AS v,'S1' AS w,'PRODUCT_DESIGN_DELAY' AS x,NULL AS y,NULL AS z,NULL AS aa,'GEX-001-PD' AS ab,NULL AS ac,0 AS ad,0 AS ae,'S1' AS af,'PRODUCT_DESIGN' AS ag,'MANUAL' AS ah
  UNION ALL SELECT 1329909430002,838257186181189,838257186320453,'GEX-002-MP','订单 SO-2026-002 材料采购超期 2 天','材料采购阶段计划 14 天、实际 16 天，到期未完成触发监控规则；到货后超期解除。','S4','AUTO_WATCH','CLOSED','FOLLOW',0.00,'P3',245,245,3183,NULL,0,'2026-08-11 09:01:00','2026-08-13 10:00:00','2026-08-13 10:00:00',0,'SO-2026-002','S4','PURCHASE_EXECUTION_DELAY','GWR-S4-PO-DELAY','PURCHASE_ORDER','SO-2026-002','T838257186181189:F838257186320453:RGWR-S4-PO-DELAY:PURCHASE_ORDER:SO-2026-002','2026-08-13 09:00:00',1,1,'S4','MATERIAL_PURCHASE','RULE'
  UNION ALL SELECT 1329909430003,838257186181189,838257186320453,'GEX-002-ETA','订单 SO-2026-002 供应商未回复交期','供应商迟迟未回复交期确认，是材料采购阶段超期的上游成因。','S3','MANUAL','CLOSED','FOLLOW',0.00,'P3',245,245,3183,NULL,0,'2026-08-05 09:00:00','2026-08-10 09:00:00','2026-08-10 09:00:00',0,'SO-2026-002','S3','SUPPLIER_ETA_ISSUE',NULL,NULL,NULL,'GEX-002-ETA',NULL,0,0,'S3','MATERIAL_PURCHASE','MANUAL'
  UNION ALL SELECT 1329909430004,838257186181189,838257186320453,'GEX-012-MP','订单 SO-2026-012 采购到货交期异常','材料采购到期未到货，收货环节暴露；经切换备选供应商后到货，超期解除。','S4','AUTO_WATCH','CLOSED','SERIOUS',80.00,'P1',251,245,3183,NULL,0,'2026-08-11 09:01:00','2026-08-13 10:30:00','2026-08-13 10:30:00',0,'SO-2026-012','S4','PURCHASE_DELIVERY_ABNORMAL','GWR-S4-PO-DELAY','PURCHASE_ORDER','SO-2026-012','T838257186181189:F838257186320453:RGWR-S4-PO-DELAY:PURCHASE_ORDER:SO-2026-012','2026-08-13 09:00:00',1,1,'S4','MATERIAL_PURCHASE','RULE'
  UNION ALL SELECT 1329909430005,838257186181189,838257186320453,'GEX-012-BP','订单 SO-2026-012 本体生产延期预警','上游累计偏差 +2 天，本体生产开工即预警；经压缩排产实际 4 天完成，累计偏差回正。','S6','MANUAL','CLOSED','FOLLOW',0.00,'P3',249,249,3185,NULL,0,'2026-08-13 11:00:00','2026-08-17 09:00:00','2026-08-17 09:00:00',0,'SO-2026-012','S6','BODY_PRODUCTION_DELAY_WARNING',NULL,NULL,NULL,'GEX-012-BP',NULL,0,0,'S6','BODY_PRODUCTION','MANUAL'
  UNION ALL SELECT 1329909430006,838257186181189,838257186320453,'GEX-019-WO','订单 SO-2026-019 生产工单完工延期','本体生产 08-11 开工、计划 6 天应于 08-17 完工，至今未完工，监控规则持续命中。','S6','AUTO_WATCH','IN_PROGRESS','SERIOUS',80.00,'P1',249,249,3185,NULL,0,'2026-08-17 09:01:00','2026-08-24 09:01:00',NULL,0,'SO-2026-019','S6','PRODUCTION_WORK_ORDER_COMPLETION_DELAY','GWR-S6-WO-DELAY','WORK_ORDER','SO-2026-019','T838257186181189:F838257186320453:RGWR-S6-WO-DELAY:WORK_ORDER:SO-2026-019','2026-08-24 09:01:00',2,0,'S6','BODY_PRODUCTION','RULE'
  UNION ALL SELECT 1329909430007,838257186181189,838257186320453,'GEX-019-BPW','订单 SO-2026-019 本体生产延期预警','完工前 2 天发出延期预警；08-17 到期未完工后升级为正式工单完工延期，本预警闭环。','S6','MANUAL','CLOSED','FOLLOW',0.00,'P3',249,249,3185,NULL,0,'2026-08-15 14:00:00','2026-08-17 08:00:00','2026-08-17 08:00:00',0,'SO-2026-019','S6','BODY_PRODUCTION_DELAY_WARNING',NULL,NULL,NULL,'GEX-019-BPW',NULL,0,0,'S6','BODY_PRODUCTION','MANUAL'
  UNION ALL SELECT 1329909430008,838257186181189,838257186320453,'GEX-019-KIT','订单 SO-2026-019 物料齐套延期预警','齐套缺料导致本体生产线停滞，问题在产线暴露，备料责任在仓储，已认领待处理。','S5','MANUAL','ASSIGNED','FOLLOW',0.00,'P3',249,251,3179,NULL,0,'2026-08-19 10:00:00','2026-08-19 11:00:00',NULL,0,'SO-2026-019','S5','MATERIAL_KITTING_DELAY_WARNING',NULL,NULL,NULL,'GEX-019-KIT',NULL,0,0,'S5','BODY_PRODUCTION','MANUAL'
  UNION ALL SELECT 1329909430009,838257186181189,838257186320453,'GEX-019-EQ','订单 SO-2026-019 关键设备停机故障','本体生产线关键设备故障停机，问题在产线暴露，维修责任在工程，处理中。','S2','MANUAL','IN_PROGRESS','SERIOUS',80.00,'P1',249,247,3172,NULL,0,'2026-08-20 08:00:00','2026-08-20 10:00:00',NULL,0,'SO-2026-019','S2','EQUIP_FAULT',NULL,NULL,NULL,'GEX-019-EQ',NULL,0,0,'S2','BODY_PRODUCTION','MANUAL'
  UNION ALL SELECT 1329909430010,838257186181189,838257186320453,'GEX-019-QC','订单 SO-2026-019 制造协同质量异常','停机复产后首件出现批量质量异常，在产线发现，判定与处置责任在质量，尚未认领。','S2','MANUAL','NEW','SERIOUS',80.00,'P1',249,248,NULL,NULL,0,'2026-08-26 11:00:00',NULL,NULL,0,'SO-2026-019','S2','MFG_QUALITY_ABNORMAL',NULL,NULL,NULL,'GEX-019-QC',NULL,0,0,'S2','BODY_PRODUCTION','MANUAL'
  UNION ALL SELECT 1329909430011,838257186181189,838257186320453,'GEX-020-FAW','订单 SO-2026-020 总装发货延期预警','总装发货阶段开工即发出发货风险预警；08-20 到期未完成后升级为出货异常，本预警闭环。','S7','MANUAL','CLOSED','FOLLOW',0.00,'P3',251,251,3179,NULL,0,'2026-08-18 09:00:00','2026-08-20 08:00:00','2026-08-20 08:00:00',0,'SO-2026-020','S7','DELIVERY_DELAY_WARNING',NULL,NULL,NULL,'GEX-020-FAW',NULL,0,0,'S7','FINAL_ASSEMBLY_DELIVERY','MANUAL'
  UNION ALL SELECT 1329909430012,838257186181189,838257186320453,'GEX-020-SHIP','订单 SO-2026-020 出货异常','总装发货 08-17 开工、计划 3 天应于 08-20 完成，至今未完成，出货受阻。','S7','AUTO_WATCH','IN_PROGRESS','SERIOUS',80.00,'P1',251,251,3179,NULL,0,'2026-08-20 09:01:00','2026-08-20 14:00:00',NULL,0,'SO-2026-020','S7','SHIPMENT_ABNORMAL','GWR-S7-SHIP-DELAY','SHIPMENT','SO-2026-020','T838257186181189:F838257186320453:RGWR-S7-SHIP-DELAY:SHIPMENT:SO-2026-020','2026-08-20 09:01:00',1,0,'S7','FINAL_ASSEMBLY_DELIVERY','RULE'
  UNION ALL SELECT 1329909430013,838257186181189,838257186320453,'GEX-020-PEND','订单 SO-2026-020 成品待发异常','成品已下线但迟迟未发运，仓储已认领待处理。','S7','MANUAL','ASSIGNED','FOLLOW',0.00,'P3',251,251,3179,NULL,0,'2026-08-24 14:00:00','2026-08-24 15:00:00',NULL,0,'SO-2026-020','S7','FINISHED_GOODS_PENDING_SHIPMENT',NULL,NULL,NULL,'GEX-020-PEND',NULL,0,0,'S7','FINAL_ASSEMBLY_DELIVERY','MANUAL'
  UNION ALL SELECT 1329909430014,838257186181189,838257186320453,'GEX-020-DUE','订单 SO-2026-020 交付逾期预警','订单 07-20 下达、目标周期 31 天应于 08-20 交付，现已逾期且仍在总装发货阶段。','S1','AUTO_WATCH','NEW','FOLLOW',0.00,'P3',251,246,NULL,NULL,0,'2026-08-20 09:01:00',NULL,NULL,0,'SO-2026-020','S1','ORDER_DELIVERY_DELAY_WARNING','GWR-S1-ORDER-DUE','ORDER','SO-2026-020','T838257186181189:F838257186320453:RGWR-S1-ORDER-DUE:ORDER:SO-2026-020','2026-08-20 09:01:00',1,0,'S1',NULL,'RULE'
) g
WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_exception` x WHERE x.`id` = g.a)
  AND NOT EXISTS (SELECT 1 FROM `ado_s8_exception` y
                  WHERE y.`tenant_id` = g.b AND y.`factory_id` = g.c AND y.`exception_code` = g.d);

-- ---------------------------------------------------------------------------
-- 3) DETECTION LOG × 8    id 1329909450001 ~ 1329909450008
--    lineage：detection_log.exception_id → exception.id；rule_id/rule_code → watch_rule
--    dedup_key 沿用源租户实测格式 T{tenant}:F{factory}:R{ruleCode}:{sourceObjectType}:{relatedObjectCode}
--    CREATED 时间 = due_time + 1 minute；RECOVERED 时间 = 阶段 actual_end_at
-- ---------------------------------------------------------------------------
INSERT INTO `ado_s8_detection_log`
 (`id`,`tenant_id`,`factory_id`,`created_at`,`updated_at`,`is_deleted`,`rule_id`,`rule_code`,`rule_type`,`scene_code`,
  `source_object_type`,`source_object_id`,`related_object_code`,`dedup_key`,`detect_result`,`exception_id`,`detected_at`,
  `trigger_source`,`remark`)
SELECT * FROM (
  SELECT 1329909450001 AS a,838257186181189 AS b,838257186320453 AS c,'2026-08-11 09:01:00' AS d,NULL AS e,0 AS f,1329909460002 AS g,'GWR-S4-PO-DELAY' AS h,'TIMEOUT' AS i,'S4' AS j,'PURCHASE_ORDER' AS k,'SO-2026-002' AS l,'SO-2026-002' AS m,'T838257186181189:F838257186320453:RGWR-S4-PO-DELAY:PURCHASE_ORDER:SO-2026-002' AS n,'CREATED' AS o,1329909430002 AS p,'2026-08-11 09:01:00' AS q,'SCHEDULER' AS r,'材料采购到期未完成（due 2026-08-11 09:00）' AS s
  UNION ALL SELECT 1329909450002,838257186181189,838257186320453,'2026-08-13 09:00:00',NULL,0,1329909460002,'GWR-S4-PO-DELAY','TIMEOUT','S4','PURCHASE_ORDER','SO-2026-002','SO-2026-002','T838257186181189:F838257186320453:RGWR-S4-PO-DELAY:PURCHASE_ORDER:SO-2026-002','RECOVERED',1329909430002,'2026-08-13 09:00:00','SCHEDULER','采购阶段完成落库，超期条件解除'
  UNION ALL SELECT 1329909450003,838257186181189,838257186320453,'2026-08-11 09:01:00',NULL,0,1329909460002,'GWR-S4-PO-DELAY','TIMEOUT','S4','PURCHASE_ORDER','SO-2026-012','SO-2026-012','T838257186181189:F838257186320453:RGWR-S4-PO-DELAY:PURCHASE_ORDER:SO-2026-012','CREATED',1329909430004,'2026-08-11 09:01:00','SCHEDULER','材料采购到期未完成（due 2026-08-11 09:00）'
  UNION ALL SELECT 1329909450004,838257186181189,838257186320453,'2026-08-13 09:00:00',NULL,0,1329909460002,'GWR-S4-PO-DELAY','TIMEOUT','S4','PURCHASE_ORDER','SO-2026-012','SO-2026-012','T838257186181189:F838257186320453:RGWR-S4-PO-DELAY:PURCHASE_ORDER:SO-2026-012','RECOVERED',1329909430004,'2026-08-13 09:00:00','SCHEDULER','切换供应商后到货，超期条件解除'
  UNION ALL SELECT 1329909450005,838257186181189,838257186320453,'2026-08-17 09:01:00',NULL,0,1329909460003,'GWR-S6-WO-DELAY','TIMEOUT','S6','WORK_ORDER','SO-2026-019','SO-2026-019','T838257186181189:F838257186320453:RGWR-S6-WO-DELAY:WORK_ORDER:SO-2026-019','CREATED',1329909430006,'2026-08-17 09:01:00','SCHEDULER','本体生产到期未完工（due 2026-08-17 09:00）'
  UNION ALL SELECT 1329909450006,838257186181189,838257186320453,'2026-08-24 09:01:00',NULL,0,1329909460003,'GWR-S6-WO-DELAY','TIMEOUT','S6','WORK_ORDER','SO-2026-019','SO-2026-019','T838257186181189:F838257186320453:RGWR-S6-WO-DELAY:WORK_ORDER:SO-2026-019','REFRESHED',1329909430006,'2026-08-24 09:01:00','SCHEDULER','仍未完工，命中刷新'
  UNION ALL SELECT 1329909450007,838257186181189,838257186320453,'2026-08-20 09:01:00',NULL,0,1329909460004,'GWR-S7-SHIP-DELAY','TIMEOUT','S7','SHIPMENT','SO-2026-020','SO-2026-020','T838257186181189:F838257186320453:RGWR-S7-SHIP-DELAY:SHIPMENT:SO-2026-020','CREATED',1329909430012,'2026-08-20 09:01:00','SCHEDULER','总装发货到期未完成（due 2026-08-20 09:00）'
  UNION ALL SELECT 1329909450008,838257186181189,838257186320453,'2026-08-20 09:01:00',NULL,0,1329909460001,'GWR-S1-ORDER-DUE','TIMEOUT','S1','ORDER','SO-2026-020','SO-2026-020','T838257186181189:F838257186320453:RGWR-S1-ORDER-DUE:ORDER:SO-2026-020','CREATED',1329909430014,'2026-08-20 09:01:00','SCHEDULER','订单目标交付到期未交付（due 2026-08-20 09:00）'
) d
WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_detection_log` x WHERE x.`id` = d.a);

-- ---------------------------------------------------------------------------
-- 4) TIMELINE × 50    id 1329909440001 ~ 1329909440050
--    唯一合法范式（S8StatusRules）：
--      NEW         CREATE
--      ASSIGNED    CREATE → CLAIM
--      IN_PROGRESS CREATE → CLAIM → START_PROGRESS
--      CLOSED      CREATE → CLAIM → START_PROGRESS → VERIFY_SUBMITTED → VERIFY_APPROVED
--    严格 event[i].to_status == event[i+1].from_status；不使用旧 CLOSE action
-- ---------------------------------------------------------------------------
INSERT INTO `ado_s8_exception_timeline`
 (`id`,`exception_id`,`action_code`,`action_label`,`from_status`,`to_status`,`operator_id`,`operator_name`,`action_remark`,`created_at`)
SELECT * FROM (
  -- GEX-001-PD (CLOSED)
  SELECT 1329909440001 AS a,1329909430001 AS b,'CREATE' AS c,'创建' AS d,NULL AS e,'NEW' AS f,NULL AS g,'系统' AS h,'产品设计阶段偏差 +2 天，创建异常' AS i,'2026-07-28 09:00:00' AS j
  UNION ALL SELECT 1329909440002,1329909430001,'CLAIM','认领','NEW','ASSIGNED',3210,'研发工程师01','研发认领',                    '2026-07-28 10:00:00'
  UNION ALL SELECT 1329909440003,1329909430001,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3210,'研发工程师01','加派评审资源',        '2026-07-28 14:00:00'
  UNION ALL SELECT 1329909440004,1329909430001,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3210,'研发工程师01','图纸定版，提交复检','2026-07-30 08:00:00'
  UNION ALL SELECT 1329909440005,1329909430001,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3210,'研发工程师01','复检通过，闭环',      '2026-07-30 09:00:00'
  -- GEX-002-MP (CLOSED, AUTO_WATCH)
  UNION ALL SELECT 1329909440006,1329909430002,'CREATE','派生',NULL,'NEW',NULL,'系统（监控规则）','GWR-S4-PO-DELAY 命中，自动建单','2026-08-11 09:01:00'
  UNION ALL SELECT 1329909440007,1329909430002,'CLAIM','认领','NEW','ASSIGNED',3183,'梁军','采购认领',                          '2026-08-11 10:00:00'
  UNION ALL SELECT 1329909440008,1329909430002,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3183,'梁军','催交并跟踪到货',            '2026-08-11 14:00:00'
  UNION ALL SELECT 1329909440009,1329909430002,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3183,'梁军','到货完成，提交复检','2026-08-13 09:10:00'
  UNION ALL SELECT 1329909440010,1329909430002,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3183,'梁军','复检通过，闭环',      '2026-08-13 10:00:00'
  -- GEX-002-ETA (CLOSED)
  UNION ALL SELECT 1329909440011,1329909430003,'CREATE','创建',NULL,'NEW',3183,'梁军','供应商未回复交期，人工提报','2026-08-05 09:00:00'
  UNION ALL SELECT 1329909440012,1329909430003,'CLAIM','认领','NEW','ASSIGNED',3183,'梁军','采购认领',              '2026-08-05 10:00:00'
  UNION ALL SELECT 1329909440013,1329909430003,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3183,'梁军','升级联系供应商商务', '2026-08-05 14:00:00'
  UNION ALL SELECT 1329909440014,1329909430003,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3183,'梁军','供应商已确认交期','2026-08-10 08:00:00'
  UNION ALL SELECT 1329909440015,1329909430003,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3183,'梁军','复检通过，闭环','2026-08-10 09:00:00'
  -- GEX-012-MP (CLOSED, AUTO_WATCH)
  UNION ALL SELECT 1329909440016,1329909430004,'CREATE','派生',NULL,'NEW',NULL,'系统（监控规则）','GWR-S4-PO-DELAY 命中，自动建单','2026-08-11 09:01:00'
  UNION ALL SELECT 1329909440017,1329909430004,'CLAIM','认领','NEW','ASSIGNED',3183,'梁军','采购认领',                          '2026-08-11 10:30:00'
  UNION ALL SELECT 1329909440018,1329909430004,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3183,'梁军','评估备选供应商',            '2026-08-11 14:30:00'
  UNION ALL SELECT 1329909440019,1329909430004,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3183,'梁军','备选供应商到货，提交复检','2026-08-13 09:20:00'
  UNION ALL SELECT 1329909440020,1329909430004,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3183,'梁军','复检通过，闭环',      '2026-08-13 10:30:00'
  -- GEX-012-BP (CLOSED)
  UNION ALL SELECT 1329909440021,1329909430005,'CREATE','创建',NULL,'NEW',NULL,'系统','累计偏差 +2 天，本体生产开工预警','2026-08-13 11:00:00'
  UNION ALL SELECT 1329909440022,1329909430005,'CLAIM','认领','NEW','ASSIGNED',3185,'路群','生产认领',                    '2026-08-13 12:00:00'
  UNION ALL SELECT 1329909440023,1329909430005,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3185,'路群','压缩排产追赶工期',      '2026-08-13 14:00:00'
  UNION ALL SELECT 1329909440024,1329909430005,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3185,'路群','本体生产提前完成','2026-08-17 08:00:00'
  UNION ALL SELECT 1329909440025,1329909430005,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3185,'路群','复检通过，闭环',  '2026-08-17 09:00:00'
  -- GEX-019-WO (IN_PROGRESS, AUTO_WATCH)
  UNION ALL SELECT 1329909440026,1329909430006,'CREATE','派生',NULL,'NEW',NULL,'系统（监控规则）','GWR-S6-WO-DELAY 命中，自动建单','2026-08-17 09:01:00'
  UNION ALL SELECT 1329909440027,1329909430006,'CLAIM','认领','NEW','ASSIGNED',3185,'路群','生产认领',                          '2026-08-17 10:00:00'
  UNION ALL SELECT 1329909440028,1329909430006,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3185,'路群','排查停滞原因并调整排产',      '2026-08-17 14:00:00'
  -- GEX-019-BPW (CLOSED)
  UNION ALL SELECT 1329909440029,1329909430007,'CREATE','创建',NULL,'NEW',NULL,'系统','本体生产完工前延期预警','2026-08-15 14:00:00'
  UNION ALL SELECT 1329909440030,1329909430007,'CLAIM','认领','NEW','ASSIGNED',3185,'路群','生产认领',            '2026-08-15 15:00:00'
  UNION ALL SELECT 1329909440031,1329909430007,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3185,'路群','评估工期风险',    '2026-08-15 16:00:00'
  UNION ALL SELECT 1329909440032,1329909430007,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3185,'路群','已升级为正式工单延期','2026-08-17 07:00:00'
  UNION ALL SELECT 1329909440033,1329909430007,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3185,'路群','预警闭环','2026-08-17 08:00:00'
  -- GEX-019-KIT (ASSIGNED)
  UNION ALL SELECT 1329909440034,1329909430008,'CREATE','创建',NULL,'NEW',NULL,'系统','齐套缺料导致产线停滞','2026-08-19 10:00:00'
  UNION ALL SELECT 1329909440035,1329909430008,'CLAIM','认领','NEW','ASSIGNED',3179,'章立坤','仓储认领',        '2026-08-19 11:00:00'
  -- GEX-019-EQ (IN_PROGRESS)
  UNION ALL SELECT 1329909440036,1329909430009,'CREATE','创建',NULL,'NEW',NULL,'系统','关键设备停机','2026-08-20 08:00:00'
  UNION ALL SELECT 1329909440037,1329909430009,'CLAIM','认领','NEW','ASSIGNED',3172,'WB8异常A','工程认领',  '2026-08-20 09:00:00'
  UNION ALL SELECT 1329909440038,1329909430009,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3172,'WB8异常A','现场抢修','2026-08-20 10:00:00'
  -- GEX-019-QC (NEW)
  UNION ALL SELECT 1329909440039,1329909430010,'CREATE','创建',NULL,'NEW',NULL,'系统','复产首件批量质量异常','2026-08-26 11:00:00'
  -- GEX-020-FAW (CLOSED)
  UNION ALL SELECT 1329909440040,1329909430011,'CREATE','创建',NULL,'NEW',NULL,'系统','总装开工发货风险预警','2026-08-18 09:00:00'
  UNION ALL SELECT 1329909440041,1329909430011,'CLAIM','认领','NEW','ASSIGNED',3179,'章立坤','仓储认领',      '2026-08-18 10:00:00'
  UNION ALL SELECT 1329909440042,1329909430011,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3179,'章立坤','协调总装与发运资源','2026-08-18 14:00:00'
  UNION ALL SELECT 1329909440043,1329909430011,'VERIFY_SUBMITTED','提交复检','IN_PROGRESS','PENDING_VERIFICATION',3179,'章立坤','已升级为出货异常','2026-08-20 07:00:00'
  UNION ALL SELECT 1329909440044,1329909430011,'VERIFY_APPROVED','检验通过','PENDING_VERIFICATION','CLOSED',3179,'章立坤','预警闭环','2026-08-20 08:00:00'
  -- GEX-020-SHIP (IN_PROGRESS, AUTO_WATCH)
  UNION ALL SELECT 1329909440045,1329909430012,'CREATE','派生',NULL,'NEW',NULL,'系统（监控规则）','GWR-S7-SHIP-DELAY 命中，自动建单','2026-08-20 09:01:00'
  UNION ALL SELECT 1329909440046,1329909430012,'CLAIM','认领','NEW','ASSIGNED',3179,'章立坤','仓储认领',                        '2026-08-20 10:00:00'
  UNION ALL SELECT 1329909440047,1329909430012,'START_PROGRESS','开始处理','ASSIGNED','IN_PROGRESS',3179,'章立坤','协调分批发运',              '2026-08-20 14:00:00'
  -- GEX-020-PEND (ASSIGNED)
  UNION ALL SELECT 1329909440048,1329909430013,'CREATE','创建',NULL,'NEW',NULL,'系统','成品下线后待发运','2026-08-24 14:00:00'
  UNION ALL SELECT 1329909440049,1329909430013,'CLAIM','认领','NEW','ASSIGNED',3179,'章立坤','仓储认领',    '2026-08-24 15:00:00'
  -- GEX-020-DUE (NEW, AUTO_WATCH)
  UNION ALL SELECT 1329909440050,1329909430014,'CREATE','派生',NULL,'NEW',NULL,'系统（监控规则）','GWR-S1-ORDER-DUE 命中，自动建单','2026-08-20 09:01:00'
) t
WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_exception_timeline` x WHERE x.`id` = t.a);

-- ---------------------------------------------------------------------------
-- 5) DECISION RECORD × 3    id 1329909470001 ~ 1329909470003
--
--    ⚠️ decision_type 的三个取值 SUPPLIER_SWITCH / SCHEDULE_ADJUST / SPLIT_SHIPMENT
--       定性为 GOLDEN_SEED_LOCAL_VOCABULARY：
--         * 不是平台 enum
--         * 不是 SysDictType / SysDictData 字典项
--         * 不是 Config Dictionary 配置项
--         * 业务代码不得依赖这些值做逻辑分支
--       依据：ado_s8_decision_record.decision_type 为无受控字典的自由 varchar(64)，
--             S8DecisionService 无取值校验，全库此前 0 行、无既有枚举可复用。
-- ---------------------------------------------------------------------------
INSERT INTO `ado_s8_decision_record`
 (`id`,`exception_id`,`decision_type`,`decision_maker_id`,`decision_maker_name`,`decision_basis`,`decision_result`,`decision_remark`,`decision_time`,`is_deleted`)
SELECT * FROM (
  SELECT 1329909470001 AS a,1329909430004 AS b,'SUPPLIER_SWITCH' AS c,3183 AS d,'梁军' AS e,'材料采购 2026-08-11 09:00 到期未到货，检测记录 GDL-03（GWR-S4-PO-DELAY）' AS f,'切换至备选供应商 B，剩余批次加急空运' AS g,'GOLDEN_SEED_LOCAL_VOCABULARY：decision_type 非平台 enum / 非字典项，代码不得据此分支' AS h,'2026-08-11 15:00:00' AS i,0 AS j
  UNION ALL SELECT 1329909470002,1329909430006,'SCHEDULE_ADJUST',3185,'路群','本体生产 2026-08-17 09:00 到期未完工，检测记录 GDL-05（GWR-S6-WO-DELAY）','19# 工位任务转移至 22#，本体生产顺延 1 天，优先保交付','GOLDEN_SEED_LOCAL_VOCABULARY：decision_type 非平台 enum / 非字典项，代码不得据此分支','2026-08-19 09:00:00',0
  UNION ALL SELECT 1329909470003,1329909430012,'SPLIT_SHIPMENT',3179,'章立坤','总装发货 2026-08-20 09:00 到期未完成，检测记录 GDL-07（GWR-S7-SHIP-DELAY）','急单优先发运，与客户确认分批交付','GOLDEN_SEED_LOCAL_VOCABULARY：decision_type 非平台 enum / 非字典项，代码不得据此分支','2026-08-22 10:00:00',0
) d
WHERE NOT EXISTS (SELECT 1 FROM `ado_s8_decision_record` x WHERE x.`id` = d.a);

-- =====================================================================================
-- Evidence：本设计 = 0 行。无真实、可访问、安全的文件引用，拒绝 seed 假 URL / PDF / 图片 / 路径。
-- =====================================================================================
