SET FOREIGN_KEY_CHECKS = 0;

-- ==============================================
-- 镀锌板块数据清理脚本
-- 涉及仓库ID：
--   1988150263284953089 - 镀锌原料库
--   1988150323162836993 - 镀锌成品库
--   1988150487185289217 - 镀锌纵剪分条原料库
--   1988150380649967617 - 镀锌分条成品库
-- ==============================================

-- 定义镀锌仓库ID集合
SET @zinc_warehouse_ids = '1988150263284953089,1988150323162836993,1988150487185289217,1988150380649967617';

-- ==============================================
-- 0. 先获取所有镀锌相关的实际库区ID（包括子级）
-- ==============================================

CREATE TEMPORARY TABLE temp_zinc_actual_warehouse_ids AS
WITH RECURSIVE zinc_warehouses AS (
    SELECT actual_warehouse_id 
    FROM `wms_actual_warehouse` 
    WHERE warehouse_id IN (1988150263284953089, 1988150323162836993, 1988150487185289217, 1988150380649967617)
    UNION ALL
    SELECT w.actual_warehouse_id 
    FROM `wms_actual_warehouse` w
    INNER JOIN zinc_warehouses zw ON w.parent_id = zw.actual_warehouse_id
)
SELECT actual_warehouse_id FROM zinc_warehouses;

-- ==============================================
-- 1. 删除钢卷相关数据
-- ==============================================

-- 钢卷仓库操作记录
DELETE FROM `wms_coil_warehouse_operation_` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷异常记录
DELETE FROM `wms_coil_abnormal` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷缓存记录
DELETE FROM `wms_coil_cache` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷待处理动作
DELETE FROM `wms_coil_pending_action` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷包装明细
DELETE FROM `wms_coil_packing_detail` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷包装记录
DELETE FROM `wms_coil_packing_record` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷质量复判
DELETE FROM `wms_coil_quality_rejudge` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 钢卷统计汇总
DELETE FROM `wms_coil_statistics_summary` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- ==============================================
-- 2. 删除出入库相关数据
-- ==============================================

-- 先获取涉及镀锌仓库的出入库单ID
CREATE TEMPORARY TABLE temp_zinc_io_ids AS
SELECT DISTINCT stock_io_id 
FROM `wms_stock_io_detail` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids)
   OR from_warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 删除出入库明细
DELETE FROM `wms_stock_io_detail` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids)
   OR from_warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 删除出入库单
DELETE FROM `wms_stock_io` 
WHERE stock_io_id IN (SELECT stock_io_id FROM temp_zinc_io_ids);

-- ==============================================
-- 3. 删除库存相关数据
-- ==============================================

-- 删除库存记录
DELETE FROM `wms_stock` 
WHERE warehouse_id IN (1988150263284953089, 1988150323162836993, 1988150487185289217, 1988150380649967617);

-- 删除钢卷物料记录
DELETE FROM `wms_material_coil` 
WHERE actual_warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- 删除库存流水记录
DELETE FROM `wms_stock_log` 
WHERE warehouse_id IN (1988150263284953089, 1988150323162836993, 1988150487185289217, 1988150380649967617);

-- ==============================================
-- 4. 删除仓库/库区/库位记录
-- ==============================================

DELETE FROM `wms_warehouse` 
WHERE warehouse_id IN (1988150263284953089, 1988150323162836993, 1988150487185289217, 1988150380649967617);

-- 删除实际库区/库位记录（包括所有子级）
DELETE FROM `wms_actual_warehouse` 
WHERE actual_warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- ==============================================
-- 5. 删除发货计划相关数据
-- ==============================================

DELETE FROM `wms_delivery_plan_coil_operate` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- ==============================================
-- 6. 删除质量检查任务相关数据
-- ==============================================

DELETE FROM `wms_check_task_item` 
WHERE check_task_id IN (
    SELECT check_task_id FROM `wms_check_task` 
    WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids)
);

DELETE FROM `wms_check_task` 
WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids);

-- ==============================================
-- 7. 删除检查任务与钢卷关联数据
-- ==============================================

DELETE FROM `wms_check_task_coil_relation` 
WHERE check_task_id IN (
    SELECT check_task_id FROM `wms_check_task` 
    WHERE warehouse_id IN (SELECT actual_warehouse_id FROM temp_zinc_actual_warehouse_ids)
);

-- ==============================================
-- 清理临时表
-- ==============================================

DROP TEMPORARY TABLE IF EXISTS temp_zinc_io_ids;
DROP TEMPORARY TABLE IF EXISTS temp_zinc_actual_warehouse_ids;

SET FOREIGN_KEY_CHECKS = 1;

SELECT '镀锌板块数据清理完成！' AS result;