-- 空调多联机：一级菜单 + 6 子菜单 + 超级管理员授权

-- 一级目录（无 PATH 或空 PATH，仅作分组）
INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT 0, '空调多联机', '', '智精灵空调多联机管理', NULL, 0,
       IFNULL((SELECT MAX(sm.`SORT`) FROM `SYSTEM_MENU` sm WHERE sm.`PARENT_ID` = 0 AND sm.`DELETED` = 0), 0) + 1,
       0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
WHERE NOT EXISTS (
    SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`NAME` = '空调多联机' AND (x.`PATH` IS NULL OR x.`PATH` = '')
);

-- 子菜单
INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT p.`ID`, '空调内机', '/business/ywconditioner', '空调内机卡片控制', NULL, 0, 1, 0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` p WHERE p.`DELETED` = 0 AND p.`NAME` = '空调多联机' AND (p.`PATH` IS NULL OR p.`PATH` = '')
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywconditioner')
LIMIT 1;

INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT p.`ID`, '网关管理', '/business/ywconditionergateway', '网关同步与上下线记录', NULL, 0, 2, 0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` p WHERE p.`DELETED` = 0 AND p.`NAME` = '空调多联机' AND (p.`PATH` IS NULL OR p.`PATH` = '')
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywconditionergateway')
LIMIT 1;

INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT p.`ID`, '电表管理', '/business/ywconditionermeter', '电表同步与查量', NULL, 0, 3, 0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` p WHERE p.`DELETED` = 0 AND p.`NAME` = '空调多联机' AND (p.`PATH` IS NULL OR p.`PATH` = '')
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywconditionermeter')
LIMIT 1;

INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT p.`ID`, '计费系数', '/business/ywconditionerbilling', '计费系数同步', NULL, 0, 4, 0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` p WHERE p.`DELETED` = 0 AND p.`NAME` = '空调多联机' AND (p.`PATH` IS NULL OR p.`PATH` = '')
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywconditionerbilling')
LIMIT 1;

INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT p.`ID`, '设备控制记录', '/business/ywconditioneractions', '空调控制操作历史', NULL, 0, 5, 0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` p WHERE p.`DELETED` = 0 AND p.`NAME` = '空调多联机' AND (p.`PATH` IS NULL OR p.`PATH` = '')
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywconditioneractions')
LIMIT 1;

INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT p.`ID`, '用量数据报表', '/business/ywconditionerreport', '设备用量透视报表', NULL, 0, 6, 0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` p WHERE p.`DELETED` = 0 AND p.`NAME` = '空调多联机' AND (p.`PATH` IS NULL OR p.`PATH` = '')
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywconditionerreport')
LIMIT 1;

-- 超级管理员：菜单
INSERT INTO `SYSTEM_ROLE_MENU` (`ROLE_ID`, `MENU_ID`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`)
SELECT r.`ID`, menu.`ID`, CURRENT_TIMESTAMP, NULL, 1, NULL, 0
FROM `SYSTEM_ROLE` r
INNER JOIN `SYSTEM_MENU` menu ON menu.`DELETED` = 0 AND (
    (menu.`NAME` = '空调多联机' AND (menu.`PATH` IS NULL OR menu.`PATH` = ''))
    OR menu.`PATH` IN (
        '/business/ywconditioner',
        '/business/ywconditionergateway',
        '/business/ywconditionermeter',
        '/business/ywconditionerbilling',
        '/business/ywconditioneractions',
        '/business/ywconditionerreport'
    )
)
WHERE r.`DELETED` = 0 AND (r.`CODE` = 'admin' OR r.`NAME` IN ('超级管理员', '管理员'))
  AND NOT EXISTS (
    SELECT 1 FROM `SYSTEM_ROLE_MENU` rm
    WHERE rm.`ROLE_ID` = r.`ID` AND rm.`MENU_ID` = menu.`ID` AND rm.`DELETED` = 0
  );
