-- =====================================================
-- MDM 主数据迁移 - 第一阶段：基础数据迁移
-- 执行顺序：01 -> 02 -> 03 -> 04 -> 05 -> 06
-- 适用场景：新项目初始化，无历史业务数据
-- =====================================================

-- =====================================================
-- 00. 创建默认单位（确保 WMS 商品单位不为空）
-- =====================================================

-- 创建一个默认单位，用于 WMS 商品单位无法匹配的情况
INSERT INTO mdm_unit_measure (id, code, name, primary_flag, primary_id, change_rate, status, remark, creator, create_time, updater, update_time, deleted)
VALUES (1, 'PCS', '个', TRUE, NULL, 1, 0, '默认单位', '1', NOW(), '1', NOW(), 0)
ON DUPLICATE KEY UPDATE name = VALUES(name);

-- =====================================================
-- 01. 迁移分类数据
-- =====================================================

-- 1.1 迁移 ERP 产品分类到 MDM 物料分类
-- ERP 分类默认关联产品类型（itemType=2）
INSERT INTO mdm_item_category (id, parent_id, code, name, item_type, sort, status, creator, create_time, updater, update_time, deleted)
SELECT
    id,
    COALESCE(parent_id, 0) AS parent_id,
    code,
    name,
    2 AS item_type,  -- 产品类型
    sort,
    status,
    '1' AS creator,
    create_time,
    '1' AS updater,
    update_time,
    0 AS deleted
FROM erp_product_category
WHERE deleted = 0
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    code = VALUES(code);

-- 1.2 迁移 MES 物料分类到 MDM 物料分类
-- MES 分类根据 itemOrProduct 字段判断类型
INSERT INTO mdm_item_category (id, parent_id, code, name, item_type, sort, status, remark, creator, create_time, updater, update_time, deleted)
SELECT
    id + 10000 AS id,  -- MES 分类ID偏移10000避免冲突
    COALESCE(parent_id, 0) +
        CASE WHEN parent_id IS NOT NULL AND parent_id > 0 THEN 10000 ELSE 0 END AS parent_id,
    code,
    name,
    CASE item_or_product
        WHEN 'ITEM' THEN 1    -- 物料
        WHEN 'PRODUCT' THEN 2 -- 产品
        ELSE NULL             -- 全部
    END AS item_type,
    sort,
    status,
    remark,
    '1' AS creator,
    create_time,
    '1' AS updater,
    update_time,
    0 AS deleted
FROM mes_md_item_type
WHERE deleted = 0
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    code = VALUES(code);

-- 1.3 迁移 WMS 商品分类到 MDM 物料分类
INSERT INTO mdm_item_category (id, parent_id, code, name, item_type, sort, status, creator, create_time, updater, update_time, deleted)
SELECT
    id + 20000 AS id,  -- WMS 分类ID偏移20000避免冲突
    COALESCE(parent_id, 0) +
        CASE WHEN parent_id IS NOT NULL AND parent_id > 0 THEN 20000 ELSE 0 END AS parent_id,
    code,
    name,
    1 AS item_type,  -- 默认物料类型
    sort,
    status,
    '1' AS creator,
    create_time,
    '1' AS updater,
    update_time,
    0 AS deleted
FROM wms_item_category
WHERE deleted = 0
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    code = VALUES(code);

-- =====================================================
-- 02. 迁移计量单位数据
-- =====================================================

-- 2.1 迁移 ERP 计量单位到 MDM（ID 从 100 开始）
INSERT INTO mdm_unit_measure (id, code, name, primary_flag, primary_id, change_rate, status, remark, creator, create_time, updater, update_time, deleted)
SELECT
    id + 100 AS id,  -- ERP 单位ID偏移100避免与默认单位冲突
    name AS code,  -- ERP 单位没有编码，使用名称作为编码
    name,
    TRUE AS primary_flag,  -- 默认为主单位
    NULL AS primary_id,
    1 AS change_rate,
    status,
    NULL AS remark,
    '1' AS creator,
    create_time,
    '1' AS updater,
    update_time,
    0 AS deleted
FROM erp_product_unit
WHERE deleted = 0
ON DUPLICATE KEY UPDATE
    name = VALUES(name);

-- 2.2 迁移 MES 计量单位到 MDM（ID 从 1000 开始）
INSERT INTO mdm_unit_measure (id, code, name, primary_flag, primary_id, change_rate, status, remark, creator, create_time, updater, update_time, deleted)
SELECT
    id + 1000 AS id,  -- MES 单位ID偏移1000避免冲突
    code,
    name,
    COALESCE(primary_flag, FALSE) AS primary_flag,
    CASE WHEN primary_id IS NOT NULL THEN primary_id + 1000 ELSE NULL END AS primary_id,
    change_rate,
    status,
    remark,
    '1' AS creator,
    create_time,
    '1' AS updater,
    update_time,
    0 AS deleted
FROM mes_md_unit_measure
WHERE deleted = 0
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    code = VALUES(code);

-- =====================================================
-- 03. 迁移品牌数据
-- =====================================================

-- 3.1 迁移 WMS 品牌到 MDM
INSERT INTO mdm_brand (id, code, name, status, remark, creator, create_time, updater, update_time, deleted)
SELECT
    id,
    code,
    name,
    0 AS status,  -- 默认启用
    NULL AS remark,
    '1' AS creator,
    create_time,
    '1' AS updater,
    update_time,
    0 AS deleted
FROM wms_item_brand
WHERE deleted = 0
ON DUPLICATE KEY UPDATE
    name = VALUES(name),
    code = VALUES(code);

-- =====================================================
-- 04. 数据验证
-- =====================================================

-- 验证分类数据迁移
SELECT 'ERP分类迁移' AS source,
       (SELECT COUNT(*) FROM erp_product_category WHERE deleted = 0) AS original_count,
       (SELECT COUNT(*) FROM mdm_item_category WHERE id < 10000 AND deleted = 0) AS migrated_count;

SELECT 'MES分类迁移' AS source,
       (SELECT COUNT(*) FROM mes_md_item_type WHERE deleted = 0) AS original_count,
       (SELECT COUNT(*) FROM mdm_item_category WHERE id >= 10000 AND id < 20000 AND deleted = 0) AS migrated_count;

SELECT 'WMS分类迁移' AS source,
       (SELECT COUNT(*) FROM wms_item_category WHERE deleted = 0) AS original_count,
       (SELECT COUNT(*) FROM mdm_item_category WHERE id >= 20000 AND deleted = 0) AS migrated_count;

-- 验证单位数据迁移
SELECT 'ERP单位迁移' AS source,
       (SELECT COUNT(*) FROM erp_product_unit WHERE deleted = 0) AS original_count,
       (SELECT COUNT(*) FROM mdm_unit_measure WHERE id >= 100 AND id < 1000 AND deleted = 0) AS migrated_count;

SELECT 'MES单位迁移' AS source,
       (SELECT COUNT(*) FROM mes_md_unit_measure WHERE deleted = 0) AS original_count,
       (SELECT COUNT(*) FROM mdm_unit_measure WHERE id >= 1000 AND deleted = 0) AS migrated_count;

-- 验证品牌数据迁移
SELECT 'WMS品牌迁移' AS source,
       (SELECT COUNT(*) FROM wms_item_brand WHERE deleted = 0) AS original_count,
       (SELECT COUNT(*) FROM mdm_brand WHERE deleted = 0) AS migrated_count;