-- 员工花名册：管理端动态菜单（与「用户管理」同级，挂在「组织机构」下）
--
-- 说明：父级与授权来源均从「用户管理」行自动解析，无需再手工替换占位符。
-- 若曾执行过带 REPLACE_WITH_* 的旧脚本，请再执行：员工花名册菜单修复-父级与授权.sql

SET @ModuleId = 'employee-roster-lq';

SELECT F_ParentId INTO @ParentId
FROM BASE_MODULE
WHERE F_FullName = '用户管理' AND F_Type = 2 AND F_DeleteMark IS NULL
LIMIT 1;

SELECT F_Id INTO @SourceModuleId
FROM BASE_MODULE
WHERE F_FullName = '用户管理' AND F_Type = 2 AND F_DeleteMark IS NULL
LIMIT 1;

DELETE FROM BASE_MODULE WHERE F_Id = @ModuleId;
DELETE FROM BASE_AUTHORIZE WHERE F_ItemId = @ModuleId;

INSERT INTO BASE_MODULE (
  F_Id, F_ParentId, F_Type, F_FullName, F_EnCode, F_UrlAddress, F_Icon,
  F_SortCode, F_EnabledMark, F_Category, F_DeleteMark, F_LinkTarget,
  F_PropertyJson, F_IsButtonAuthorize, F_IsColumnAuthorize, F_IsDataAuthorize,
  F_IsFormAuthorize, F_CreatorTime
)
VALUES (
  @ModuleId, @ParentId, 2, '员工花名册', 'employeeRoster', 'permission/employee-roster', 'icon-ym icon-ym-addressBook',
  50, 1, 'Web', NULL, '_self', '{"moduleId":"","iconBackgroundColor":"","isTree":0}',
  1, 1, 1, 1, NOW()
);

INSERT INTO BASE_AUTHORIZE (
  F_Id, F_ItemType, F_ItemId, F_ObjectType, F_ObjectId, F_SortCode, F_CreatorTime, F_CreatorUserId
)
SELECT REPLACE(UUID(), '-', ''), 'module', @ModuleId, 'Role', t.F_ObjectId, 1, NOW(), 'admin'
FROM (
  SELECT DISTINCT F_ObjectId
  FROM BASE_AUTHORIZE
  WHERE F_ItemId = @SourceModuleId AND F_ItemType = 'module' AND F_ObjectType = 'Role'
) t;

-- 若此前 F_UrlAddress 带 /index，可改为：
-- UPDATE BASE_MODULE SET F_UrlAddress = 'permission/employee-roster' WHERE F_Id = @ModuleId;
