-- MES 经销商管理菜单初始化
-- 经销商复用 CRM 客户主数据，不新增业务表和后端权限。
-- 执行前请确认当前数据库为项目实际业务库。

SET @dealer_menu_id = (SELECT id FROM system_menu
                        WHERE parent_id = 5780 AND path = 'dealer' AND deleted = b'0'
                        ORDER BY id LIMIT 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 6740, '经销商管理', '', 2, 11, 5780, 'dealer', 'ep:user', 'crm/customer/index', 'MesWmDealer',
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE @dealer_menu_id IS NULL
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6740);

SET @dealer_menu_id = COALESCE(@dealer_menu_id, (SELECT id FROM system_menu WHERE id = 6740 AND deleted = b'0'));

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 6741, '查询经销商', 'crm:customer:query', 3, 1, @dealer_menu_id, '', '', '', NULL,
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE EXISTS (SELECT 1 FROM system_menu WHERE id = @dealer_menu_id AND deleted = b'0')
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6741);

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 6742, '创建经销商', 'crm:customer:create', 3, 2, @dealer_menu_id, '', '', '', NULL,
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE EXISTS (SELECT 1 FROM system_menu WHERE id = @dealer_menu_id AND deleted = b'0')
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6742);

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 6743, '更新经销商', 'crm:customer:update', 3, 3, @dealer_menu_id, '', '', '', NULL,
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE EXISTS (SELECT 1 FROM system_menu WHERE id = @dealer_menu_id AND deleted = b'0')
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6743);

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 6744, '删除经销商', 'crm:customer:delete', 3, 4, @dealer_menu_id, '', '', '', NULL,
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE EXISTS (SELECT 1 FROM system_menu WHERE id = @dealer_menu_id AND deleted = b'0')
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6744);

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 6745, '导出经销商', 'crm:customer:export', 3, 5, @dealer_menu_id, '', '', '', NULL,
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE EXISTS (SELECT 1 FROM system_menu WHERE id = @dealer_menu_id AND deleted = b'0')
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6745);

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 6746, '导入经销商', 'crm:customer:import', 3, 6, @dealer_menu_id, '', '', '', NULL,
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
WHERE EXISTS (SELECT 1 FROM system_menu WHERE id = @dealer_menu_id AND deleted = b'0')
  AND NOT EXISTS (SELECT 1 FROM system_menu WHERE id = 6746);

SET @dealer_menu_id = NULL;
