-- 员工档案审核（扫码入职）：管理端动态菜单
-- 与「用户管理」同级，挂在「组织机构」下；授权复制自用户管理角色。
-- 前端页面：permission/employee-profile-submission -> src/views/permission/employee-profile-submission/index.vue
-- 静态路由兜底：/permission/employee-profile-submission（见 router/modules/base.js）

SET @ModuleId = 'employee-profile-submission-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;

INSERT IGNORE 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, '员工档案审核', 'employeeProfileSubmission', 'permission/employee-profile-submission', 'icon-ym icon-ym-qrcode',
  55, 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 LEFT(SHA2(CONCAT(@ModuleId, ':', t.F_ObjectId), 256), 50),
       '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
WHERE NOT EXISTS (
  SELECT 1
  FROM BASE_AUTHORIZE a
  WHERE a.F_ItemId = @ModuleId
    AND a.F_ItemType = 'module'
    AND a.F_ObjectType = 'Role'
    AND a.F_ObjectId = t.F_ObjectId
);
