-- 从截图识别生成：密炼物料导入 SQL（目标表：mes_mixer_material）
-- 说明：
-- 1) feed_manage_status=1(投管), use_status=1(使用), shelf_life_days=365
-- 2) 物料大类/小类按分类字典映射到 sys_category.id
--    物料大类：名称「原辅材料」，类型编码 code = XSLMES_MATERIAL_RAW_AUX（sys_category.code）
--    物料小类：名称「炭黑」，且 pid 指向大类（若已维护父子关系）
-- 3) tenant_id / sys_org_code 未知，先置空
-- 4) material_name / material_desc / alias_name 中如包含 TODO_*，请在执行前按业务名称修正
-- 5) 为避免重复，先按 material_code 删除后再插入

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 大类：优先按类型编码匹配，否则按名称「原辅材料」
SET @major_category_id := (
  SELECT sc.id
  FROM sys_category sc
  WHERE sc.code = 'XSLMES_MATERIAL_RAW_AUX' OR sc.name = '原辅材料'
  ORDER BY
    (CASE WHEN sc.code = 'XSLMES_MATERIAL_RAW_AUX' THEN 0 WHEN sc.name = '原辅材料' THEN 1 ELSE 2 END),
    sc.create_time DESC
  LIMIT 1
);

-- 小类：名称「炭黑」，优先挂在大类下
SET @minor_category_id := (
  SELECT sc.id
  FROM sys_category sc
  WHERE sc.name = '炭黑'
    AND (
      @major_category_id IS NULL
      OR sc.pid = @major_category_id
      OR sc.pid IS NULL
      OR sc.pid = '0'
    )
  ORDER BY (CASE WHEN sc.pid = @major_category_id THEN 0 ELSE 1 END), sc.create_time DESC
  LIMIT 1
);

DELETE FROM mes_mixer_material
WHERE material_code IN (
  '110110001','110110002','110110003','110110004','110110005','110110006','110110007','110110008',
  '110110009','110110010','110110011','110110012','110110013','110110014','110110015','110110016',
  '110110017','110110018','110110019','110110020','110110021','110110022','110110023','110110024',
  '110110025','110110026','110110027','110110028'
);

INSERT INTO mes_mixer_material (
  id,
  material_code,
  material_name,
  erp_code,
  major_category_id,
  minor_category_id,
  material_desc,
  alias_name,
  feed_manage_status,
  use_status,
  specific_gravity,
  shelf_life_days,
  min_bake_minutes,
  total_safety_stock_kg,
  qualified_safety_stock_kg,
  remark,
  tenant_id,
  sys_org_code,
  create_by,
  create_time,
  del_flag
)
SELECT
  REPLACE(UUID(), '-', '') AS id,
  t.material_code,
  t.material_name,
  t.erp_code,
  @major_category_id AS major_category_id,
  @minor_category_id AS minor_category_id,
  t.material_desc,
  t.alias_name,
  1 AS feed_manage_status,
  1 AS use_status,
  t.specific_gravity,
  365 AS shelf_life_days,
  t.min_bake_minutes,
  NULL AS total_safety_stock_kg,
  NULL AS qualified_safety_stock_kg,
  '图片识别导入' AS remark,
  NULL AS tenant_id,
  NULL AS sys_org_code,
  'image_import' AS create_by,
  NOW() AS create_time,
  0 AS del_flag
FROM (
  -- material_code(9位), material_name, erp_code, material_desc, alias_name, specific_gravity, min_bake_minutes
  SELECT '110110001' AS material_code, '炭黑-N220'      AS material_name, '10201' AS erp_code, '炭黑-N220'        AS material_desc, '炭黑-N220'      AS alias_name, 1.82 AS specific_gravity, NULL AS min_bake_minutes
  UNION ALL SELECT '110110002', '炭黑-N330',       '10203', '炭黑-N330',         '炭黑-N330',       1.82, NULL
  UNION ALL SELECT '110110003', '炭黑-N660',       '10205', '炭黑-N660',         '炭黑-N660',       1.82, NULL
  UNION ALL SELECT '110110004', '裂解炭黑',         '10206', '裂解炭黑',           '裂解炭黑',         1.95, NULL
  UNION ALL SELECT '110110005', '陶土1',           '10254', '填料-陶土1',         '填料-陶土1',       2.60, NULL
  UNION ALL SELECT '110110006', '碳酸钙',           '10253', '填料-碳酸钙',         '填料-碳酸钙',       2.70, NULL
  UNION ALL SELECT '110110007', '硫酸钡',           '10252', '填料-硫酸钡',         '填料-硫酸钡',       4.20, NULL
  UNION ALL SELECT '110110008', '二氧化钛中阳',      '10251', '二氧化钛中阳',        '二氧化钛中阳',     2.20, NULL
  UNION ALL SELECT '110110009', '炭黑-N234',       '10202', '炭黑-N234',         '炭黑-N234',       1.85, NULL
  UNION ALL SELECT '110110010', '炭黑 N339',       '10204', '炭黑 N339',         '炭黑 N339',       1.85, NULL
  UNION ALL SELECT '110110011', '云母粉',           '10255', '云母粉',             '云母粉',           2.00, NULL
  UNION ALL SELECT '110110012', '硬脂酸2',          '10543', '硬脂酸2',            '硬脂酸2',          0.85, 0
  UNION ALL SELECT '110110013', 'FI-N339',        '10204', 'FI-N339',          'FI-N339',         1.85, 4
  UNION ALL SELECT '110110014', 'IL N330',        '10203', 'IL N330',          'IL N330',         1.85, 4
  UNION ALL SELECT '110110015', '卡博特N234',       '11006', '卡博特N234',          '卡博特N234',       1.10, 0
  UNION ALL SELECT '110110016', '卡博特234',        '11006', '卡博特234',           '卡博特234',        1.80, 8
  UNION ALL SELECT '110110017', '头条B-N220',      '10201', '头条B-N220',         '头条B-N220',      1.82, 0
  UNION ALL SELECT '110110018', '集采B-N330',      '10203', '集采B-N330',         '集采B-N330',      1.82, 24
  UNION ALL SELECT '110110019', '集采B-N660',      '10205', '集采B-N660',         '集采B-N660',      1.82, 24
  UNION ALL SELECT '110110020', '集采B-裂解炭黑',    '10206', '集采B-裂解炭黑',       '集采B-裂解炭黑',    1.82, 24
  UNION ALL SELECT '110110021', '集采B-碳酸钙',     '10253', '集采B-碳酸钙',        '集采B-碳酸钙',     1.82, 24
  UNION ALL SELECT '110110022', '集采B-陶土',       '10254', '集采B-陶土',         '集采B-陶土',       1.82, 24
  UNION ALL SELECT '110110023', '集采B-N234',      '10204', '集采B-N234',         '集采B-N234',      1.82, 24
  UNION ALL SELECT '110110024', '集采B-N339',      '10204', '集采B-N339',         '集采B-N339',      1.82, 24
  UNION ALL SELECT '110110025', '氧化锌2',          '10256', '氧化锌2',            '氧化锌2',          5.55, 0
  UNION ALL SELECT '110110026', '炭黑-N550',       '10209', '炭黑-N550',         '炭黑-N550',       1.82, 0
  UNION ALL SELECT '110110027', 'AH',             '10606', 'AH',               'AH',             1.18, 0
  UNION ALL SELECT '110110028', '旺格-N220',       '10201', '旺格-N220',         '旺格-N220',       1.82, 0
) t;

SET FOREIGN_KEY_CHECKS = 1;

-- 执行后可核对：
-- SELECT @major_category_id AS major_category_id, @minor_category_id AS minor_category_id;
-- SELECT material_code, material_name, erp_code, major_category_id, minor_category_id, specific_gravity, min_bake_minutes
-- FROM mes_mixer_material
-- WHERE material_code BETWEEN '110110001' AND '110110028'
-- ORDER BY material_code;
