-- BIT 样品仓菜单补充 v2
-- 说明：
-- 1. 2026-06-22-bit-sample-wms.sql 已作为种子执行后，不再修改原文件。
-- 2. 本文件仅补充 BIT 样品仓复用的产品物料分类入口，方便 BIT 仓库管理员维护产品/物料/备件分类。
-- 3. 贴码上架只在 PDA 端操作，Web 不新增贴码上架页面。

SET @menu_seed := GREATEST((SELECT IFNULL(MAX(id), 0) FROM system_menu), 900000);

SET @erp_menu_id := (
  SELECT id FROM system_menu
  WHERE path = '/erp' AND parent_id = 0 AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_wms_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @erp_menu_id AND path = 'bitwms' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @bit_product_category_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'product-category' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT @menu_seed, '产品物料分类', 'erp:product-category:query', 2, 25, @bit_wms_id, 'product-category', 'fa:certificate', 'erp/product/category/index', 'ErpBitWmsProductCategory', 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_wms_id IS NOT NULL AND @bit_product_category_menu_id IS NULL;

SET @bit_product_category_menu_id := (
  SELECT id FROM system_menu
  WHERE parent_id = @bit_wms_id AND path = 'product-category' AND deleted = b'0'
  ORDER BY id LIMIT 1
);

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT @menu_seed, '分类查询', 'erp:product-category:query', 3, 10, @bit_product_category_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_product_category_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE parent_id = @bit_product_category_menu_id AND permission = 'erp:product-category:query' AND deleted = b'0'
  );

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT @menu_seed, '分类创建', 'erp:product-category:create', 3, 20, @bit_product_category_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_product_category_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE parent_id = @bit_product_category_menu_id AND permission = 'erp:product-category:create' AND deleted = b'0'
  );

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT @menu_seed, '分类更新', 'erp:product-category:update', 3, 30, @bit_product_category_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_product_category_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE parent_id = @bit_product_category_menu_id AND permission = 'erp:product-category:update' AND deleted = b'0'
  );

SET @menu_seed := @menu_seed + 1;
INSERT INTO system_menu
  (id, name, permission, type, sort, parent_id, path, icon, component, component_name, status, visible, keep_alive, always_show, creator, create_time, updater, update_time, deleted)
SELECT @menu_seed, '分类删除', 'erp:product-category:delete', 3, 40, @bit_product_category_menu_id, '', '', '', NULL, 0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @bit_product_category_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE parent_id = @bit_product_category_menu_id AND permission = 'erp:product-category:delete' AND deleted = b'0'
  );
