-- 日常用电管理：智能电表、充值记录、电表参数设置（需在 dev 库执行）
-- 父菜单按「日常用电」匹配，若名称不同请改 LIKE 条件

-- 智能电表
INSERT INTO `SYSTEM_MENU` (`PARENT_ID`, `NAME`, `PATH`, `REMARK`, `ICON`, `DISABLED`, `SORT`, `FIXED`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`, `PARAMS`)
SELECT m.`ID`, '智能电表', '/business/ywelectrical', '智能电表管理', NULL, 0,
       IFNULL((SELECT MAX(sm.`SORT`) FROM `SYSTEM_MENU` sm WHERE sm.`PARENT_ID` = m.`ID` AND sm.`DELETED` = 0), 0) + 1,
       0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` m
WHERE m.`DELETED` = 0 AND m.`NAME` LIKE '%日常用电%'
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywelectrical')
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 m.`ID`, '充值记录', '/business/ywelectricalcharge', '电表充值记录', NULL, 0,
       IFNULL((SELECT MAX(sm.`SORT`) FROM `SYSTEM_MENU` sm WHERE sm.`PARENT_ID` = m.`ID` AND sm.`DELETED` = 0), 0) + 1,
       0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` m
WHERE m.`DELETED` = 0 AND m.`NAME` LIKE '%日常用电%'
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywelectricalcharge')
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 m.`ID`, '电表参数设置', '/business/ywelectricalparam', '电表参数档案', NULL, 0,
       IFNULL((SELECT MAX(sm.`SORT`) FROM `SYSTEM_MENU` sm WHERE sm.`PARENT_ID` = m.`ID` AND sm.`DELETED` = 0), 0) + 1,
       0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` m
WHERE m.`DELETED` = 0 AND m.`NAME` LIKE '%日常用电%'
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywelectricalparam')
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 m.`ID`, '抄表记录', '/business/ywelectricaldata', '电表抄表记录', NULL, 0,
       IFNULL((SELECT MAX(sm.`SORT`) FROM `SYSTEM_MENU` sm WHERE sm.`PARENT_ID` = m.`ID` AND sm.`DELETED` = 0), 0) + 1,
       0, CURRENT_TIMESTAMP, NULL, 1, NULL, 0, NULL
FROM `SYSTEM_MENU` m
WHERE m.`DELETED` = 0 AND m.`NAME` LIKE '%日常用电%'
  AND NOT EXISTS (SELECT 1 FROM `SYSTEM_MENU` x WHERE x.`DELETED` = 0 AND x.`PATH` = '/business/ywelectricaldata')
LIMIT 1;

-- admin 角色：菜单
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.`PATH` IN ('/business/ywelectrical', '/business/ywelectricalcharge', '/business/ywelectricalparam', '/business/ywelectricaldata') AND menu.`DELETED` = 0
WHERE r.`DELETED` = 0 AND (r.`CODE` = 'admin' OR r.`NAME` = '管理员')
  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
  );

-- admin 角色：智能电表按钮权限
INSERT INTO `SYSTEM_ROLE_PERMISSION` (`ROLE_ID`, `PERMISSION_ID`, `CREATE_TIME`, `UPDATE_TIME`, `CREATE_USER`, `UPDATE_USER`, `DELETED`)
SELECT r.`ID`, p.`ID`, CURRENT_TIMESTAMP, NULL, 1, NULL, 0
FROM `SYSTEM_ROLE` r
INNER JOIN `SYSTEM_PERMISSION` p ON p.`CODE` IN (
    'business:ywelectrical:query',
    'business:ywelectrical:update',
    'business:ywelectrical:device',
    'business:ywelectrical:exportExcel',
    'business:ywelectricalcharge:query',
    'business:ywelectricalcharge:exportExcel',
    'business:ywelectricalparam:query',
    'business:ywelectricalparam:create',
    'business:ywelectricalparam:update',
    'business:ywelectricalparam:delete',
    'business:ywelectricalparam:exportExcel',
    'business:ywelectricaldata:query',
    'business:ywelectricaldata:exportExcel',
    'business:ywelectricaldata:sync'
) AND p.`DELETED` = 0
WHERE r.`DELETED` = 0 AND (r.`CODE` = 'admin' OR r.`NAME` = '管理员')
  AND NOT EXISTS (
    SELECT 1 FROM `SYSTEM_ROLE_PERMISSION` rp
    WHERE rp.`ROLE_ID` = r.`ID` AND rp.`PERMISSION_ID` = p.`ID` AND rp.`DELETED` = 0
  );
