-- 以爬取清单（剔除后 115 个款号）为准，
-- 删除 spu.code 不在清单内的 SPU 及其对应 SKU。
-- 执行顺序：先删子表 sku，再删主表 spu。

-- 1) 删除非清单款号对应的 SKU
DELETE FROM sku
WHERE spu_id IN (
    SELECT id FROM spu
    WHERE UPPER(TRIM(code)) NOT IN ('5000B', 'AUTM001', 'AUTM002', 'AUTM003', 'BRTF004', 'BRTF007', 'BRTM001', 'BRTM002', 'C1717', 'CAHM002', 'CATM001', 'CATM006', 'CATM007', 'DEHM003', 'DETM001', 'DETM002', 'DF009', 'DF010', 'DG001', 'DG004', 'DG012', 'DG013', 'DG014', 'DG015', 'DG100', 'DG101', 'DG102', 'DG110', 'DG120', 'DG150', 'DG301', 'DG501', 'DG502', 'DG503', 'DG504', 'DG505', 'DG601', 'ESHM003', 'ESHM004', 'ESTK005', 'ESTM002', 'GBHK006', 'GBHK007', 'GBHM002', 'GBHM003', 'GBTF004', 'GBTK005', 'GBTK008', 'GBTK012', 'GBTM001', 'GBTM010', 'GBTM011', 'GBTM013', 'HM01', 'ITFM005', 'ITTK002', 'ITTM001', 'JPHM009', 'JPHM010', 'JPPX012', 'JPPX013', 'JPTK015', 'JPTM001', 'JPTM002', 'JPTM004', 'JPTM006', 'JPTM007', 'JPTM008', 'JSA004', 'JSA006', 'JSA008', 'JSA009', 'JSA011', 'JSB001', 'JSB002', 'JSB003', 'JSE008', 'JSS001', 'KRHM002', 'KRHM003', 'KRTM001', 'KRTM004', 'MESXT001', 'METB001', 'METN001', 'METP001', 'PET001', 'PLHF029', 'PLHM002', 'PLHM003', 'PLHM009', 'PLHM010', 'PLHM012', 'PLHM013', 'PLJM030', 'PLTF004', 'PLTF026', 'PLTF027', 'PLTF028', 'PLTK005', 'PLTK015', 'PLTK016', 'PLTM001', 'PLTM006', 'PLTM008', 'PLTM024', 'PLTM031', 'SAHM003', 'SATK004', 'SATU001', 'TBB001', 'VS001', 'VS002', 'VS003', 'VS004')
);

-- 2) 删除非清单款号的 SPU
DELETE FROM spu
WHERE UPPER(TRIM(code)) NOT IN ('5000B', 'AUTM001', 'AUTM002', 'AUTM003', 'BRTF004', 'BRTF007', 'BRTM001', 'BRTM002', 'C1717', 'CAHM002', 'CATM001', 'CATM006', 'CATM007', 'DEHM003', 'DETM001', 'DETM002', 'DF009', 'DF010', 'DG001', 'DG004', 'DG012', 'DG013', 'DG014', 'DG015', 'DG100', 'DG101', 'DG102', 'DG110', 'DG120', 'DG150', 'DG301', 'DG501', 'DG502', 'DG503', 'DG504', 'DG505', 'DG601', 'ESHM003', 'ESHM004', 'ESTK005', 'ESTM002', 'GBHK006', 'GBHK007', 'GBHM002', 'GBHM003', 'GBTF004', 'GBTK005', 'GBTK008', 'GBTK012', 'GBTM001', 'GBTM010', 'GBTM011', 'GBTM013', 'HM01', 'ITFM005', 'ITTK002', 'ITTM001', 'JPHM009', 'JPHM010', 'JPPX012', 'JPPX013', 'JPTK015', 'JPTM001', 'JPTM002', 'JPTM004', 'JPTM006', 'JPTM007', 'JPTM008', 'JSA004', 'JSA006', 'JSA008', 'JSA009', 'JSA011', 'JSB001', 'JSB002', 'JSB003', 'JSE008', 'JSS001', 'KRHM002', 'KRHM003', 'KRTM001', 'KRTM004', 'MESXT001', 'METB001', 'METN001', 'METP001', 'PET001', 'PLHF029', 'PLHM002', 'PLHM003', 'PLHM009', 'PLHM010', 'PLHM012', 'PLHM013', 'PLJM030', 'PLTF004', 'PLTF026', 'PLTF027', 'PLTF028', 'PLTK005', 'PLTK015', 'PLTK016', 'PLTM001', 'PLTM006', 'PLTM008', 'PLTM024', 'PLTM031', 'SAHM003', 'SATK004', 'SATU001', 'TBB001', 'VS001', 'VS002', 'VS003', 'VS004');

-- 校验：应返回 0
SELECT COUNT(*) AS orphan_sku FROM sku
WHERE spu_id NOT IN (SELECT id FROM spu);
