-- =============================================
-- 管理驾驶舱与统计分析模块 初始化脚本
-- 包含：「管理驾驶舱」页面及按钮权限（查询/导出）
-- 说明：顶级目录「统计分析」(id=602700) 已存在，本脚本仅在其下挂载页面。
-- 适用库：mom-thzy（种子业务主库，含生产/质检/库存/出库/追溯全部业务表）
-- 注意：若目标库缺少种子业务表，需先执行以下脚本：
--   mes_pro_batch_bag_code_init.sql      （生产批次/袋码/托盘）
--   mes_seed_quality_init.sql            （种子质检）
--   mes_trace_consumer_scan_event.sql    （消费者扫码事件）
--   mes_trace_anti_cross_region_init.sql （窜货预警）
--   mes_pro_scan_inbound_init.sql        （生产线扫码入库）
-- MySQL 8.4 适用
-- =============================================

SET NAMES utf8mb4;

-- ----------------------------
-- 1. 页面：管理驾驶舱（挂到「统计分析」目录 602700 下）
-- ----------------------------
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 602701, '管理驾驶舱', '', 2, 1, 602700, 'management-cockpit', 'ep:monitor', 'management-cockpit/index', 'ManagementCockpit',
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE (id = 602701 OR (parent_id = 602700 AND path = 'management-cockpit' AND deleted = 0)) AND deleted = 0
)
  AND EXISTS (SELECT 1 FROM system_menu WHERE id = 602700 AND deleted = 0);

-- ----------------------------
-- 2. 按钮权限：查询 / 导出
-- ----------------------------
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 602702, '驾驶舱查询', 'mes:dashboard:query', 3, 1, 602701, '', '', '', '',
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE (id = 602702 OR (parent_id = 602701 AND permission = 'mes:dashboard:query' AND deleted = 0)) AND deleted = 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 602703, '驾驶舱导出', 'mes:dashboard:export', 3, 2, 602701, '', '', '', '',
       0, b'1', b'1', b'1', '1', NOW(), '1', NOW(), b'0'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM system_menu
    WHERE (id = 602703 OR (parent_id = 602701 AND permission = 'mes:dashboard:export' AND deleted = 0)) AND deleted = 0
);
