-- 1.0.342 P-022 WP-3：备份并清理派生层 tenant_id IN (0, 1300000000001) 残渣。
-- 只清 dwd_*/mdp_std_* 指定表；不动业务表；不动 ado_s8_exception tenant_id=1。
SET NAMES utf8mb4;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_material_readiness_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_material_readiness') > 0,
  'CREATE TABLE `dwd_material_readiness_bak_20260814` AS SELECT * FROM `dwd_material_readiness` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_material_shortage_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_material_shortage') > 0,
  'CREATE TABLE `dwd_material_shortage_bak_20260814` AS SELECT * FROM `dwd_material_shortage` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_s4_purchase_execution_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_s4_purchase_execution') > 0,
  'CREATE TABLE `dwd_s4_purchase_execution_bak_20260814` AS SELECT * FROM `dwd_s4_purchase_execution` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_supplier_risk_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dwd_supplier_risk') > 0,
  'CREATE TABLE `dwd_supplier_risk_bak_20260814` AS SELECT * FROM `dwd_supplier_risk` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_purchase_order_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_purchase_order') > 0,
  'CREATE TABLE `mdp_std_purchase_order_bak_20260814` AS SELECT * FROM `mdp_std_purchase_order` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_purchase_receipt_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_purchase_receipt') > 0,
  'CREATE TABLE `mdp_std_purchase_receipt_bak_20260814` AS SELECT * FROM `mdp_std_purchase_receipt` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_production_issue_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_production_issue') > 0,
  'CREATE TABLE `mdp_std_production_issue_bak_20260814` AS SELECT * FROM `mdp_std_production_issue` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @sql := IF(
  (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_s6_report_bak_20260814') = 0
  AND (SELECT COUNT(*) FROM information_schema.TABLES
    WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='mdp_std_s6_report') > 0,
  'CREATE TABLE `mdp_std_s6_report_bak_20260814` AS SELECT * FROM `mdp_std_s6_report` WHERE tenant_id IN (0, 1300000000001)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

DELETE FROM `dwd_material_readiness` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `dwd_material_shortage` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `dwd_s4_purchase_execution` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `dwd_supplier_risk` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `mdp_std_purchase_order` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `mdp_std_purchase_receipt` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `mdp_std_production_issue` WHERE tenant_id IN (0, 1300000000001);
DELETE FROM `mdp_std_s6_report` WHERE tenant_id IN (0, 1300000000001);
