-- 1.0.355：清理 S1 平台租户与 tenant-as-factory 历史污染。
UPDATE `mdp_stg_so`
SET `factory_id`=NULL
WHERE `tenant_id` > 0 AND `factory_id`=`tenant_id`;

UPDATE `mdp_std_so`
SET `factory_id`=1
WHERE `tenant_id` > 0 AND `factory_id`=`tenant_id`;

DELETE FROM `mdp_stg_so` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `mdp_std_so` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `dwd_ship_trans` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `dwd_requirement_examine_detail` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `ado_smart_ops_kpi_atomic_day` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `mdp_sync_log` WHERE `tenant_id` IN (0,1,1300000000001);
DELETE FROM `mdp_transform_run_log` WHERE `tenant_id` IN (0,1,1300000000001);

UPDATE `ado_module_dashboard_rebuild_job`
SET `status`='FAILED',
    `current_stage`='FAILED',
    `failed_stage`=COALESCE(`failed_stage`, 'SCOPE_VALIDATION'),
    `error_message`='租户/工厂作用域无效，已由 1.0.355 终止',
    `finished_at`=COALESCE(`finished_at`, NOW(3)),
    `update_time`=NOW(3)
WHERE `status` IN ('QUEUED','RUNNING')
  AND (`tenant_id` IN (0,1,1300000000001) OR `factory_id`=`tenant_id`);
