-- 泰额版 menu 菜单初始化脚本（平台主库 / 租户业务库通用）
-- 执行库示例：antis-foodlabeling-host（平台主库）；租户业务库按需切换
-- 禁止对 antis-foodlabeling-us 执行（美国版）
--
-- MenuType: 0=目录, 1=菜单, 2=组件/按钮
-- MenuSource: 0=Ruoyi, 1=Pure, 2=Vben5；当前固定 Ruoyi(0)
-- 根菜单 ParentId 用 '0'；子菜单 ParentId 用父菜单 Guid
--
-- ========== 固定 Guid 对照表（名称 → Id） ==========
-- 首页概览   f0010001-0001-4000-8000-000000000001
-- 平台管理   f0010002-0001-4000-8000-000000000002
-- SAAS 公司  f0010003-0001-4000-8000-000000000003
-- 标签管理   f0010010-0001-4000-8000-000000000010
-- 标签       f0010011-0001-4000-8000-000000000011
-- 标签分类   f0010012-0001-4000-8000-000000000012
-- 标签类型   f0010013-0001-4000-8000-000000000013
-- 标签模板   f0010014-0001-4000-8000-000000000014
-- 多选选项集 f0010015-0001-4000-8000-000000000015
-- 业务模块   f0010020-0001-4000-8000-000000000020
-- 培训       f0010021-0001-4000-8000-000000000021
-- 告警       f0010022-0001-4000-8000-000000000022
-- 任务       f0010023-0001-4000-8000-000000000023
-- 传感器     f0010024-0001-4000-8000-000000000024
-- 食物浪费   f0010025-0001-4000-8000-000000000025
-- 电子标签   f0010026-0001-4000-8000-000000000026
-- 管理       f0010030-0001-4000-8000-000000000030
-- 账户管理   f0010031-0001-4000-8000-000000000031
-- 系统菜单   f0010032-0001-4000-8000-000000000032
-- 菜单管理   f0010033-0001-4000-8000-000000000033
-- 设备       f0010034-0001-4000-8000-000000000034
-- 报表       f0010035-0001-4000-8000-000000000035
-- 发票       f0010036-0001-4000-8000-000000000036
-- 二维码     f0010037-0001-4000-8000-000000000037
-- 支持       f0010038-0001-4000-8000-000000000038
-- API        f0010039-0001-4000-8000-000000000039
-- ==================================================

-- 1a) 物理删除非法 Id（th-menu-*），避免留下脏数据行
DELETE FROM `rolemenu` WHERE `MenuId` LIKE 'th-menu-%';
DELETE FROM `menu` WHERE `Id` LIKE 'th-menu-%';

-- 1b) 同 PermissionCode 的其它旧行（非本脚本固定 Guid）做逻辑删除
UPDATE `menu`
SET `IsDeleted` = 1,
    `LastModificationTime` = NOW()
WHERE `IsDeleted` = 0
  AND (
    (
      `PermissionCode` IN (
        'menu.dashboard.analytics',
        'menu.platform',
        'menu.platform.tenants',
        'menu.labeling',
        'menu.labeling.labels',
        'menu.labeling.categories',
        'menu.labeling.types',
        'menu.labeling.templates',
        'menu.labeling.multiple-options',
        'menu.modules',
        'menu.modules.training',
        'menu.modules.alerts',
        'menu.modules.tasks',
        'menu.modules.sensors',
        'menu.modules.food-waste',
        'menu.modules.e-label',
        'menu.management',
        'menu.management.account',
        'menu.management.system-menu',
        'menu.management.menu',
        'menu.management.devices',
        'menu.management.reports',
        'menu.management.invoices',
        'menu.management.qr-codes',
        'menu.management.support',
        'menu.management.api'
      )
      AND `Id` NOT IN (
        'f0010001-0001-4000-8000-000000000001',
        'f0010002-0001-4000-8000-000000000002',
        'f0010003-0001-4000-8000-000000000003',
        'f0010010-0001-4000-8000-000000000010',
        'f0010011-0001-4000-8000-000000000011',
        'f0010012-0001-4000-8000-000000000012',
        'f0010013-0001-4000-8000-000000000013',
        'f0010014-0001-4000-8000-000000000014',
        'f0010015-0001-4000-8000-000000000015',
        'f0010020-0001-4000-8000-000000000020',
        'f0010021-0001-4000-8000-000000000021',
        'f0010022-0001-4000-8000-000000000022',
        'f0010023-0001-4000-8000-000000000023',
        'f0010024-0001-4000-8000-000000000024',
        'f0010025-0001-4000-8000-000000000025',
        'f0010026-0001-4000-8000-000000000026',
        'f0010030-0001-4000-8000-000000000030',
        'f0010031-0001-4000-8000-000000000031',
        'f0010032-0001-4000-8000-000000000032',
        'f0010033-0001-4000-8000-000000000033',
        'f0010034-0001-4000-8000-000000000034',
        'f0010035-0001-4000-8000-000000000035',
        'f0010036-0001-4000-8000-000000000036',
        'f0010037-0001-4000-8000-000000000037',
        'f0010038-0001-4000-8000-000000000038',
        'f0010039-0001-4000-8000-000000000039'
      )
    )
  );

-- 2) 插入 / 更新 26 条食品 SaaS 菜单（合法 Guid）
INSERT INTO `menu`
(
  `Id`,
  `IsDeleted`,
  `CreationTime`,
  `CreatorId`,
  `LastModifierId`,
  `LastModificationTime`,
  `OrderNum`,
  `State`,
  `MenuName`,
  `RouterName`,
  `MenuType`,
  `PermissionCode`,
  `ParentId`,
  `MenuIcon`,
  `Router`,
  `IsLink`,
  `IsCache`,
  `IsShow`,
  `Remark`,
  `Component`,
  `MenuSource`,
  `Query`,
  `ConcurrencyStamp`
)
VALUES
('f0010001-0001-4000-8000-000000000001', 0, NOW(), NULL, NULL, NULL, -1, 1, '首页概览', 'FoodLabelingDashboard', 1, 'menu.dashboard.analytics', '0', 'lucide:layout-dashboard', '/analytics', 0, 0, 1, NULL, '/food-labeling/dashboard/index', 0, NULL, UUID()),

('f0010002-0001-4000-8000-000000000002', 0, NOW(), NULL, NULL, NULL, 5, 1, '平台管理', 'FoodLabelingPlatform', 0, 'menu.platform', '0', 'lucide:cloud-cog', '/platform', 0, 0, 1, NULL, NULL, 0, NULL, UUID()),
('f0010003-0001-4000-8000-000000000003', 0, NOW(), NULL, NULL, NULL, 6, 1, 'SAAS 公司', 'FoodLabelingPlatformTenants', 1, 'menu.platform.tenants', 'f0010002-0001-4000-8000-000000000002', 'lucide:building', '/platform/tenants', 0, 0, 1, NULL, '/food-labeling/platform/tenants/index', 0, NULL, UUID()),

('f0010010-0001-4000-8000-000000000010', 0, NOW(), NULL, NULL, NULL, 10, 1, '标签管理', 'FoodLabelingLabeling', 0, 'menu.labeling', '0', 'lucide:tags', '/labeling', 0, 0, 1, NULL, NULL, 0, NULL, UUID()),
('f0010011-0001-4000-8000-000000000011', 0, NOW(), NULL, NULL, NULL, 11, 1, '标签', 'FoodLabelingLabels', 1, 'menu.labeling.labels', 'f0010010-0001-4000-8000-000000000010', 'lucide:tag', '/labels', 0, 0, 1, NULL, '/food-labeling/labeling/labels/index', 0, NULL, UUID()),
('f0010012-0001-4000-8000-000000000012', 0, NOW(), NULL, NULL, NULL, 12, 1, '标签分类', 'FoodLabelingLabelCategories', 1, 'menu.labeling.categories', 'f0010010-0001-4000-8000-000000000010', 'lucide:folder-tree', '/label-categories', 0, 0, 1, NULL, '/food-labeling/labeling/label-categories/index', 0, NULL, UUID()),
('f0010013-0001-4000-8000-000000000013', 0, NOW(), NULL, NULL, NULL, 13, 1, '标签类型', 'FoodLabelingLabelTypes', 1, 'menu.labeling.types', 'f0010010-0001-4000-8000-000000000010', 'lucide:layers', '/label-types', 0, 0, 1, NULL, '/food-labeling/labeling/label-types/index', 0, NULL, UUID()),
('f0010014-0001-4000-8000-000000000014', 0, NOW(), NULL, NULL, NULL, 14, 1, '标签模板', 'FoodLabelingLabelTemplates', 1, 'menu.labeling.templates', 'f0010010-0001-4000-8000-000000000010', 'lucide:layout-template', '/label-templates', 0, 0, 1, NULL, '/food-labeling/labeling/label-templates/index', 0, NULL, UUID()),
('f0010015-0001-4000-8000-000000000015', 0, NOW(), NULL, NULL, NULL, 15, 1, '多选选项集', 'FoodLabelingMultipleOptions', 1, 'menu.labeling.multiple-options', 'f0010010-0001-4000-8000-000000000010', 'lucide:list-checks', '/multiple-options', 0, 0, 1, NULL, '/food-labeling/labeling/multiple-options/index', 0, NULL, UUID()),

('f0010020-0001-4000-8000-000000000020', 0, NOW(), NULL, NULL, NULL, 15, 1, '业务模块', 'FoodLabelingModules', 0, 'menu.modules', '0', 'lucide:boxes', '/modules', 0, 0, 1, NULL, NULL, 0, NULL, UUID()),
('f0010021-0001-4000-8000-000000000021', 0, NOW(), NULL, NULL, NULL, 16, 1, '培训', 'FoodLabelingTraining', 1, 'menu.modules.training', 'f0010020-0001-4000-8000-000000000020', 'lucide:graduation-cap', '/training', 0, 0, 1, NULL, '/food-labeling/modules/training/index', 0, NULL, UUID()),
('f0010022-0001-4000-8000-000000000022', 0, NOW(), NULL, NULL, NULL, 17, 1, '告警', 'FoodLabelingAlerts', 1, 'menu.modules.alerts', 'f0010020-0001-4000-8000-000000000020', 'lucide:bell', '/alerts', 0, 0, 1, NULL, '/food-labeling/modules/alerts/index', 0, NULL, UUID()),
('f0010023-0001-4000-8000-000000000023', 0, NOW(), NULL, NULL, NULL, 18, 1, '任务', 'FoodLabelingTasks', 1, 'menu.modules.tasks', 'f0010020-0001-4000-8000-000000000020', 'lucide:list-todo', '/tasks', 0, 0, 1, NULL, '/food-labeling/modules/tasks/index', 0, NULL, UUID()),
('f0010024-0001-4000-8000-000000000024', 0, NOW(), NULL, NULL, NULL, 19, 1, '传感器', 'FoodLabelingSensors', 1, 'menu.modules.sensors', 'f0010020-0001-4000-8000-000000000020', 'lucide:activity', '/sensors', 0, 0, 1, NULL, '/food-labeling/modules/sensors/index', 0, NULL, UUID()),
('f0010025-0001-4000-8000-000000000025', 0, NOW(), NULL, NULL, NULL, 20, 1, '食物浪费', 'FoodLabelingFoodWaste', 1, 'menu.modules.food-waste', 'f0010020-0001-4000-8000-000000000020', 'lucide:apple', '/food-waste', 0, 0, 1, NULL, '/food-labeling/modules/food-waste/index', 0, NULL, UUID()),
('f0010026-0001-4000-8000-000000000026', 0, NOW(), NULL, NULL, NULL, 21, 1, '电子标签', 'FoodLabelingELabelModule', 1, 'menu.modules.e-label', 'f0010020-0001-4000-8000-000000000020', 'lucide:file-digit', '/e-label-module', 0, 0, 1, NULL, '/food-labeling/modules/e-label/index', 0, NULL, UUID()),

('f0010030-0001-4000-8000-000000000030', 0, NOW(), NULL, NULL, NULL, 20, 1, '管理', 'FoodLabelingManagement', 0, 'menu.management', '0', 'lucide:building-2', '/management', 0, 0, 1, NULL, NULL, 0, NULL, UUID()),
('f0010031-0001-4000-8000-000000000031', 0, NOW(), NULL, NULL, NULL, 21, 1, '账户管理', 'FoodLabelingAccountManagement', 1, 'menu.management.account', 'f0010030-0001-4000-8000-000000000030', 'lucide:users', '/account-management', 0, 0, 1, NULL, '/food-labeling/management/account-management/index', 0, NULL, UUID()),
('f0010032-0001-4000-8000-000000000032', 0, NOW(), NULL, NULL, NULL, 22, 1, '系统菜单', 'FoodLabelingSystemMenu', 1, 'menu.management.system-menu', 'f0010030-0001-4000-8000-000000000030', 'lucide:menu-square', '/system-menu', 0, 0, 1, NULL, '/food-labeling/management/system-menu/index', 0, NULL, UUID()),
('f0010033-0001-4000-8000-000000000033', 0, NOW(), NULL, NULL, NULL, 23, 1, '菜单管理', 'FoodLabelingMenuManagement', 1, 'menu.management.menu', 'f0010030-0001-4000-8000-000000000030', 'lucide:utensils', '/menu-management', 0, 0, 1, NULL, '/food-labeling/management/menu-management/index', 0, NULL, UUID()),
('f0010034-0001-4000-8000-000000000034', 0, NOW(), NULL, NULL, NULL, 24, 1, '设备', 'FoodLabelingDevices', 1, 'menu.management.devices', 'f0010030-0001-4000-8000-000000000030', 'lucide:smartphone', '/devices', 0, 0, 1, NULL, '/food-labeling/modules/devices/index', 0, NULL, UUID()),
('f0010035-0001-4000-8000-000000000035', 0, NOW(), NULL, NULL, NULL, 25, 1, '报表', 'FoodLabelingReports', 1, 'menu.management.reports', 'f0010030-0001-4000-8000-000000000030', 'lucide:file-bar-chart', '/reports', 0, 0, 1, NULL, '/food-labeling/management/reports/index', 0, NULL, UUID()),
('f0010036-0001-4000-8000-000000000036', 0, NOW(), NULL, NULL, NULL, 26, 1, '发票', 'FoodLabelingInvoices', 1, 'menu.management.invoices', 'f0010030-0001-4000-8000-000000000030', 'lucide:receipt', '/invoices', 0, 0, 1, NULL, '/food-labeling/modules/invoices/index', 0, NULL, UUID()),
('f0010037-0001-4000-8000-000000000037', 0, NOW(), NULL, NULL, NULL, 27, 1, '二维码', 'FoodLabelingQrCodes', 1, 'menu.management.qr-codes', 'f0010030-0001-4000-8000-000000000030', 'lucide:qr-code', '/qr-codes', 0, 0, 1, NULL, '/food-labeling/modules/qr-codes/index', 0, NULL, UUID()),
('f0010038-0001-4000-8000-000000000038', 0, NOW(), NULL, NULL, NULL, 28, 1, '支持', 'FoodLabelingSupport', 1, 'menu.management.support', 'f0010030-0001-4000-8000-000000000030', 'lucide:life-buoy', '/support', 0, 0, 1, NULL, '/food-labeling/management/support/index', 0, NULL, UUID()),
('f0010039-0001-4000-8000-000000000039', 0, NOW(), NULL, NULL, NULL, 29, 1, 'API', 'FoodLabelingApi', 1, 'menu.management.api', 'f0010030-0001-4000-8000-000000000030', 'lucide:code-2', '/api-settings', 0, 0, 1, NULL, '/food-labeling/modules/api/index', 0, NULL, UUID())
ON DUPLICATE KEY UPDATE
  `IsDeleted` = VALUES(`IsDeleted`),
  `LastModificationTime` = NOW(),
  `OrderNum` = VALUES(`OrderNum`),
  `State` = VALUES(`State`),
  `MenuName` = VALUES(`MenuName`),
  `RouterName` = VALUES(`RouterName`),
  `MenuType` = VALUES(`MenuType`),
  `PermissionCode` = VALUES(`PermissionCode`),
  `ParentId` = VALUES(`ParentId`),
  `MenuIcon` = VALUES(`MenuIcon`),
  `Router` = VALUES(`Router`),
  `IsLink` = VALUES(`IsLink`),
  `IsCache` = VALUES(`IsCache`),
  `IsShow` = VALUES(`IsShow`),
  `Remark` = VALUES(`Remark`),
  `Component` = VALUES(`Component`),
  `MenuSource` = VALUES(`MenuSource`),
  `Query` = VALUES(`Query`),
  `ConcurrencyStamp` = UUID();

-- 导入后可执行下面语句检查：
-- SELECT Id, ParentId, MenuName, RouterName, Router, MenuType, MenuSource, OrderNum, IsShow, State
-- FROM `menu`
-- WHERE IsDeleted = 0
-- ORDER BY OrderNum ASC, MenuName ASC;
