-- XSLMES 原材料车间剩余量菜单与权限
-- 说明：
-- 1) 若已存在 /xslmes 父级菜单，则复用该父级；
-- 2) 若不存在，则自动创建 XSLMES管理 一级菜单；
-- 3) 默认授权 admin 角色（role_id=f6817f48af4fb3af11b9e8bf182f618b）。
SET NAMES utf8mb4;

SET @xsl_parent_id = (
  SELECT id
  FROM sys_permission
  WHERE url = '/xslmes' AND menu_type = 0
  ORDER BY create_time ASC
  LIMIT 1
);
SET @xsl_parent_id = IFNULL(@xsl_parent_id, '1860000000000099001');

-- 一级菜单：XSLMES管理（不存在时创建，存在时更新为目录菜单格式）
INSERT INTO sys_permission(
  id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no,
  is_route, is_leaf, hidden, status, del_flag, always_show, keep_alive, internal_or_external, create_by, create_time
)
VALUES (
  @xsl_parent_id, NULL, 'XSLMES管理', '/xslmes', 'layouts/RouteView', 'XslMesRoot', 0, NULL, '1', 80,
  0, 0, 0, '1', 0, 1, 0, 0, 'admin', NOW()
)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  url = VALUES(url),
  component = VALUES(component),
  component_name = VALUES(component_name),
  menu_type = VALUES(menu_type),
  perms = VALUES(perms),
  perms_type = VALUES(perms_type),
  sort_no = VALUES(sort_no),
  is_route = VALUES(is_route),
  is_leaf = VALUES(is_leaf),
  hidden = VALUES(hidden),
  status = VALUES(status),
  del_flag = VALUES(del_flag),
  always_show = VALUES(always_show),
  keep_alive = VALUES(keep_alive),
  internal_or_external = VALUES(internal_or_external);

-- 二级菜单：原材料车间剩余量
INSERT INTO sys_permission(
  id, parent_id, name, url, component, component_name, menu_type, perms, perms_type, sort_no,
  is_route, is_leaf, hidden, status, del_flag, keep_alive, internal_or_external, create_by, create_time
)
VALUES (
  '1860000000000099011', @xsl_parent_id, '原材料车间剩余量',
  '/xslmes/mesXslRawMaterialWorkshopRemainList',
  'xslmes/mesXslRawMaterialWorkshopRemain/MesXslRawMaterialWorkshopRemainList',
  'MesXslRawMaterialWorkshopRemainList', 1, NULL, '1', 21,
  1, 1, 0, '1', 0, 1, 0, 'admin', NOW()
)
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id),
  name = VALUES(name),
  url = VALUES(url),
  component = VALUES(component),
  component_name = VALUES(component_name),
  menu_type = VALUES(menu_type),
  perms = VALUES(perms),
  perms_type = VALUES(perms_type),
  sort_no = VALUES(sort_no),
  is_route = VALUES(is_route),
  is_leaf = VALUES(is_leaf),
  hidden = VALUES(hidden),
  status = VALUES(status),
  del_flag = VALUES(del_flag),
  keep_alive = VALUES(keep_alive),
  internal_or_external = VALUES(internal_or_external);

-- 按钮权限
INSERT INTO sys_permission(id, parent_id, name, menu_type, perms, perms_type, status, del_flag, create_by, create_time) VALUES
('1860000000000099012', '1860000000000099011', '编辑', 2, 'xslmes:mes_xsl_raw_material_workshop_remain:edit', '1', '1', 0, 'admin', NOW()),
('1860000000000099013', '1860000000000099011', '导出', 2, 'xslmes:mes_xsl_raw_material_workshop_remain:exportXls', '1', '1', 0, 'admin', NOW())
ON DUPLICATE KEY UPDATE
  parent_id = VALUES(parent_id),
  name = VALUES(name),
  menu_type = VALUES(menu_type),
  perms = VALUES(perms),
  perms_type = VALUES(perms_type),
  status = VALUES(status),
  del_flag = VALUES(del_flag);

-- admin 角色授权
INSERT INTO sys_role_permission(id, role_id, permission_id, operate_date, operate_ip)
SELECT REPLACE(UUID(), '-', ''), 'f6817f48af4fb3af11b9e8bf182f618b', p.id, NOW(), '127.0.0.1'
FROM sys_permission p
WHERE p.id IN ('1860000000000099011', '1860000000000099012', '1860000000000099013')
  AND NOT EXISTS (
    SELECT 1
    FROM sys_role_permission rp
    WHERE rp.role_id = 'f6817f48af4fb3af11b9e8bf182f618b'
      AND rp.permission_id = p.id
  );

