-- 会员耗卡（lqXhHyhk）作废 / 删除 按钮权限
-- 菜单 F_Id：733911069059187973（会员耗卡）
-- 删除按钮 btn_remove 已存在：F_Id = 754918927502411013
-- 本脚本新增作废按钮 btn_cancel，并示例为角色授权

-- ========== 1. 新增「作废」按钮（若尚未配置） ==========
INSERT INTO BASE_MODULEBUTTON (
  F_Id,
  F_ParentId,
  F_FullName,
  F_EnCode,
  F_Icon,
  F_UrlAddress,
  F_PropertyJson,
  F_Description,
  F_SortCode,
  F_EnabledMark,
  F_CreatorTime,
  F_CreatorUserId,
  F_ModuleId
)
SELECT
  '834700000000000001',
  '-1',
  '作废',
  'btn_cancel',
  '',
  NULL,
  NULL,
  '会员耗卡列表/详情作废操作',
  1,
  1,
  NOW(),
  'admin',
  '733911069059187973'
FROM (SELECT 1) AS _seed
WHERE NOT EXISTS (
  SELECT 1 FROM BASE_MODULEBUTTON
  WHERE F_ModuleId = '733911069059187973' AND F_EnCode = 'btn_cancel' AND F_DeleteMark IS NULL
);

-- ========== 2. 为角色授权（按需执行，替换 @ROLE_ID） ==========
-- 查询角色：SELECT F_Id, F_FullName, F_EnCode FROM BASE_ROLE;
-- 系统管理员示例：94e3a9bb0fce4547886972998fddba1c

SET @ROLE_ID = '94e3a9bb0fce4547886972998fddba1c';

-- 作废按钮授权（btn_cancel）
INSERT INTO BASE_AUTHORIZE (
  F_Id,
  F_ItemType,
  F_ItemId,
  F_ObjectType,
  F_ObjectId,
  F_SortCode,
  F_CreatorTime,
  F_CreatorUserId
)
SELECT
  '834700000000000011',
  'button',
  mb.F_Id,
  'Role',
  @ROLE_ID,
  1,
  NOW(),
  'admin'
FROM BASE_MODULEBUTTON mb
WHERE mb.F_ModuleId = '733911069059187973'
  AND mb.F_EnCode = 'btn_cancel'
  AND mb.F_DeleteMark IS NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_AUTHORIZE a
    WHERE a.F_ItemType = 'button'
      AND a.F_ItemId = mb.F_Id
      AND a.F_ObjectType = 'Role'
      AND a.F_ObjectId = @ROLE_ID
  );

-- 删除按钮授权（btn_remove，按钮已存在）
INSERT INTO BASE_AUTHORIZE (
  F_Id,
  F_ItemType,
  F_ItemId,
  F_ObjectType,
  F_ObjectId,
  F_SortCode,
  F_CreatorTime,
  F_CreatorUserId
)
SELECT
  '834700000000000012',
  'button',
  mb.F_Id,
  'Role',
  @ROLE_ID,
  2,
  NOW(),
  'admin'
FROM BASE_MODULEBUTTON mb
WHERE mb.F_ModuleId = '733911069059187973'
  AND mb.F_EnCode = 'btn_remove'
  AND mb.F_DeleteMark IS NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_AUTHORIZE a
    WHERE a.F_ItemType = 'button'
      AND a.F_ItemId = mb.F_Id
      AND a.F_ObjectType = 'Role'
      AND a.F_ObjectId = @ROLE_ID
  );

-- ========== 3. 校验 ==========
-- SELECT F_FullName, F_EnCode, F_Id FROM BASE_MODULEBUTTON
-- WHERE F_ModuleId = '733911069059187973' AND F_DeleteMark IS NULL
-- ORDER BY F_SortCode;
