-- ============================================================
-- 物料分类体系初始化脚本
-- 1. 插入标准分类数据（三级层级）
-- 2. 批量更新现有物料的分类关联
-- ============================================================

SET NAMES utf8mb4;

-- ============================================================
-- 第一部分：插入标准分类数据
-- ============================================================

-- 一级分类
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
VALUES
(1, '电气控制', 0, '0', 1, '0', 'admin', NOW()),
(1, '传感器',   0, '0', 2, '0', 'admin', NOW()),
(1, '机械传动', 0, '0', 3, '0', 'admin', NOW()),
(1, '气动液压', 0, '0', 4, '0', 'admin', NOW()),
(1, '辅材耗材', 0, '0', 5, '0', 'admin', NOW());

-- 获取一级分类ID并插入二级分类
-- 电气控制 -> PLC控制器, 变频器, 低压电器, 人机界面
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT 'PLC控制器' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='电气控制' AND parent_id=0
  UNION ALL
  SELECT '变频器', category_id, 2 FROM biz_material_category WHERE category_name='电气控制' AND parent_id=0
  UNION ALL
  SELECT '低压电器', category_id, 3 FROM biz_material_category WHERE category_name='电气控制' AND parent_id=0
  UNION ALL
  SELECT '人机界面', category_id, 4 FROM biz_material_category WHERE category_name='电气控制' AND parent_id=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;

-- 传感器 -> 接近开关, 光电传感器, 温度传感器
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT '接近开关' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='传感器' AND parent_id=0
  UNION ALL
  SELECT '光电传感器', category_id, 2 FROM biz_material_category WHERE category_name='传感器' AND parent_id=0
  UNION ALL
  SELECT '温度传感器', category_id, 3 FROM biz_material_category WHERE category_name='传感器' AND parent_id=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;

-- 机械传动 -> 电机, 减速机, 联轴器
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT '电机' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='机械传动' AND parent_id=0
  UNION ALL
  SELECT '减速机', category_id, 2 FROM biz_material_category WHERE category_name='机械传动' AND parent_id=0
  UNION ALL
  SELECT '联轴器', category_id, 3 FROM biz_material_category WHERE category_name='机械传动' AND parent_id=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;

-- 气动液压 -> 气缸, 阀岛, 管接头
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT '气缸' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='气动液压' AND parent_id=0
  UNION ALL
  SELECT '阀岛', category_id, 2 FROM biz_material_category WHERE category_name='气动液压' AND parent_id=0
  UNION ALL
  SELECT '管接头', category_id, 3 FROM biz_material_category WHERE category_name='气动液压' AND parent_id=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;

-- 辅材耗材 -> 线缆, 紧固件, 标签标识
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT '线缆' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='辅材耗材' AND parent_id=0
  UNION ALL
  SELECT '紧固件', category_id, 2 FROM biz_material_category WHERE category_name='辅材耗材' AND parent_id=0
  UNION ALL
  SELECT '标签标识', category_id, 3 FROM biz_material_category WHERE category_name='辅材耗材' AND parent_id=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;

-- 三级分类：PLC控制器 -> 西门子系列, 三菱系列, 欧姆龙系列
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT '西门子系列' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='PLC控制器' AND parent_id!=0
  UNION ALL
  SELECT '三菱系列', category_id, 2 FROM biz_material_category WHERE category_name='PLC控制器' AND parent_id!=0
  UNION ALL
  SELECT '欧姆龙系列', category_id, 3 FROM biz_material_category WHERE category_name='PLC控制器' AND parent_id!=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;

-- 三级分类：低压电器 -> 断路器, 接触器, 继电器
INSERT INTO biz_material_category (tenant_id, category_name, parent_id, ancestors, sort, status, create_by, create_time)
SELECT 1, t.sub_name, t.pid, CONCAT(p.ancestors, ',', p.category_id), t.sort, '0', 'admin', NOW()
FROM (
  SELECT '断路器' AS sub_name, category_id AS pid, 1 AS sort FROM biz_material_category WHERE category_name='低压电器' AND parent_id!=0
  UNION ALL
  SELECT '接触器', category_id, 2 FROM biz_material_category WHERE category_name='低压电器' AND parent_id!=0
  UNION ALL
  SELECT '继电器', category_id, 3 FROM biz_material_category WHERE category_name='低压电器' AND parent_id!=0
) t
JOIN biz_material_category p ON t.pid = p.category_id;


-- ============================================================
-- 第二部分：批量更新现有物料的分类关联
-- 根据物料名称关键词自动匹配分类
-- ============================================================

-- PLC相关 -> PLC控制器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='PLC控制器' AND parent_id!=0 LIMIT 1
) WHERE (material_name LIKE '%PLC%' OR material_name LIKE '%可编程%') AND (category_id = 0 OR category_id IS NULL);

-- 变频器相关
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='变频器' AND parent_id!=0 LIMIT 1
) WHERE (material_name LIKE '%变频%' OR material_name LIKE '%VFD%' OR material_name LIKE '%inverter%')
  AND (category_id = 0 OR category_id IS NULL);

-- 断路器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='断路器' LIMIT 1
) WHERE (material_name LIKE '%断路%' OR material_name LIKE '%空开%')
  AND (category_id = 0 OR category_id IS NULL);

-- 接触器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='接触器' LIMIT 1
) WHERE material_name LIKE '%接触%' AND (category_id = 0 OR category_id IS NULL);

-- 继电器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='继电器' LIMIT 1
) WHERE material_name LIKE '%继电%' AND (category_id = 0 OR category_id IS NULL);

-- 接近开关
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='接近开关' LIMIT 1
) WHERE material_name LIKE '%接近开关%' AND (category_id = 0 OR category_id IS NULL);

-- 光电传感器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='光电传感器' LIMIT 1
) WHERE (material_name LIKE '%光电%' OR material_name LIKE '%光电传感%')
  AND (category_id = 0 OR category_id IS NULL);

-- 温度传感器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='温度传感器' LIMIT 1
) WHERE (material_name LIKE '%温度传感%' OR material_name LIKE '%热电偶%' OR material_name LIKE '%热电阻%')
  AND (category_id = 0 OR category_id IS NULL);

-- 电机
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='电机' LIMIT 1
) WHERE (material_name LIKE '%电机%' OR material_name LIKE '%马达%')
  AND (category_id = 0 OR category_id IS NULL);

-- 减速机
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='减速机' LIMIT 1
) WHERE material_name LIKE '%减速%' AND (category_id = 0 OR category_id IS NULL);

-- 联轴器
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='联轴器' LIMIT 1
) WHERE material_name LIKE '%联轴%' AND (category_id = 0 OR category_id IS NULL);

-- 气缸
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='气缸' LIMIT 1
) WHERE material_name LIKE '%气缸%' AND (category_id = 0 OR category_id IS NULL);

-- 阀岛/电磁阀
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='阀岛' LIMIT 1
) WHERE (material_name LIKE '%阀岛%' OR material_name LIKE '%电磁阀%')
  AND (category_id = 0 OR category_id IS NULL);

-- 线缆
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='线缆' LIMIT 1
) WHERE (material_name LIKE '%线缆%' OR material_name LIKE '%电缆%' OR material_name LIKE '%导线%')
  AND (category_id = 0 OR category_id IS NULL);

-- 人机界面/触摸屏
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='人机界面' LIMIT 1
) WHERE (material_name LIKE '%触摸屏%' OR material_name LIKE '%HMI%' OR material_name LIKE '%人机%')
  AND (category_id = 0 OR category_id IS NULL);

-- 未匹配的物料归入"辅材耗材"
UPDATE biz_material SET category_id = (
  SELECT category_id FROM biz_material_category WHERE category_name='辅材耗材' AND parent_id=0 LIMIT 1
) WHERE (category_id = 0 OR category_id IS NULL);

-- ============================================================
-- 验证结果
-- ============================================================
SELECT '=== 分类数据统计 ===' AS info;
SELECT COUNT(*) AS total_categories FROM biz_material_category;

SELECT '=== 各分类物料数量 ===' AS info;
SELECT
  c.category_name,
  COUNT(m.material_id) AS material_count
FROM biz_material_category c
LEFT JOIN biz_material m ON c.category_id = m.category_id
GROUP BY c.category_id, c.category_name
ORDER BY c.sort;

SELECT '=== 未分类物料数量 ===' AS info;
SELECT COUNT(*) AS uncategorized FROM biz_material WHERE category_id = 0 OR category_id IS NULL;
