-- =============================================================================
-- 监控中心 + 重复充值审计菜单
-- 说明：
--   1. 新建一级菜单「监控中心」及子菜单「重复充值审计」
--   2. 页面路径：views/monitorCenter/duplicate-recharge-audit/index.vue
--   3. 授权给系统管理员角色；其他角色需在菜单权限中另行勾选
--   4. 脚本可重复执行
-- =============================================================================

SET @AdminRoleId = '94e3a9bb0fce4547886972998fddba1c';
SET @MonitorCenterId = 'monitor-center-root-lq';
SET @DuplicateAuditId = 'monitor-duplicate-recharge-audit-lq';

-- 一级：监控中心
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, F_CreatorUserId
)
SELECT
  @MonitorCenterId, '-1', 1, '监控中心', 'monitorCenter',
  '', 'icon-ym icon-ym-sysMonitor',
  6, 1, 'Web', NULL, '_self',
  '{"moduleId":"","iconBackgroundColor":"","isTree":0}',
  0, 0, 0, 0, NOW(), 'admin'
FROM (SELECT 1) AS seed
WHERE NOT EXISTS (
  SELECT 1 FROM BASE_MODULE
  WHERE F_EnCode = 'monitorCenter' AND F_Category = 'Web' AND F_DeleteMark IS NULL
);

SELECT F_Id INTO @ActualMonitorCenterId
FROM BASE_MODULE
WHERE F_EnCode = 'monitorCenter' AND F_Category = 'Web' AND F_DeleteMark IS NULL
LIMIT 1;

UPDATE BASE_MODULE
SET F_ParentId = '-1',
    F_Type = 1,
    F_FullName = '监控中心',
    F_UrlAddress = '',
    F_Icon = 'icon-ym icon-ym-sysMonitor',
    F_SortCode = 6,
    F_EnabledMark = 1,
    F_LastModifyTime = NOW(),
    F_LastModifyUserId = 'admin'
WHERE F_Id = @ActualMonitorCenterId;

-- 二级：重复充值审计
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, F_CreatorUserId
)
SELECT
  @DuplicateAuditId, @ActualMonitorCenterId, 2, '重复充值审计', 'duplicateRechargeAudit',
  'monitorCenter/duplicate-recharge-audit', 'icon-ym icon-ym-generator-warning',
  1, 1, 'Web', NULL, '_self',
  '{"moduleId":"","iconBackgroundColor":"","isTree":0}',
  1, 0, 1, 0, NOW(), 'admin'
FROM (SELECT 1) AS seed
WHERE @ActualMonitorCenterId IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_MODULE
    WHERE F_EnCode = 'duplicateRechargeAudit' AND F_Category = 'Web' AND F_DeleteMark IS NULL
  );

SELECT F_Id INTO @ActualDuplicateAuditId
FROM BASE_MODULE
WHERE F_EnCode = 'duplicateRechargeAudit' AND F_Category = 'Web' AND F_DeleteMark IS NULL
LIMIT 1;

UPDATE BASE_MODULE
SET F_ParentId = @ActualMonitorCenterId,
    F_Type = 2,
    F_FullName = '重复充值审计',
    F_UrlAddress = 'monitorCenter/duplicate-recharge-audit',
    F_Icon = 'icon-ym icon-ym-generator-warning',
    F_SortCode = 1,
    F_EnabledMark = 1,
    F_LastModifyTime = NOW(),
    F_LastModifyUserId = 'admin'
WHERE F_Id = @ActualDuplicateAuditId
  AND @ActualMonitorCenterId IS NOT NULL;

-- 授权：系统管理员
DELETE FROM BASE_AUTHORIZE
WHERE F_ItemType = 'module'
  AND F_ObjectType = 'Role'
  AND F_ObjectId = @AdminRoleId
  AND F_ItemId IN (@ActualMonitorCenterId, @ActualDuplicateAuditId);

INSERT INTO BASE_AUTHORIZE (
  F_Id, F_ItemType, F_ItemId, F_ObjectType, F_ObjectId, F_SortCode, F_CreatorTime, F_CreatorUserId
)
SELECT REPLACE(UUID(), '-', ''), 'module', @ActualMonitorCenterId, 'Role', @AdminRoleId, 1, NOW(), 'admin'
FROM (SELECT 1) AS seed
WHERE @ActualMonitorCenterId IS NOT NULL;

INSERT INTO BASE_AUTHORIZE (
  F_Id, F_ItemType, F_ItemId, F_ObjectType, F_ObjectId, F_SortCode, F_CreatorTime, F_CreatorUserId
)
SELECT REPLACE(UUID(), '-', ''), 'module', @ActualDuplicateAuditId, 'Role', @AdminRoleId, 1, NOW(), 'admin'
FROM (SELECT 1) AS seed
WHERE @ActualDuplicateAuditId IS NOT NULL;

-- 同步给已拥有「事业部战略决策」菜单的角色（便于事业部运营直接看到）
INSERT INTO BASE_AUTHORIZE (
  F_Id, F_ItemType, F_ItemId, F_ObjectType, F_ObjectId, F_SortCode, F_CreatorTime, F_CreatorUserId
)
SELECT REPLACE(UUID(), '-', ''), 'module', @ActualMonitorCenterId, 'Role', a.F_ObjectId, 1, NOW(), 'admin'
FROM BASE_AUTHORIZE a
INNER JOIN BASE_MODULE m ON a.F_ItemId = m.F_Id
WHERE a.F_ItemType = 'module'
  AND a.F_ObjectType = 'Role'
  AND m.F_EnCode = 'sybzljc'
  AND m.F_DeleteMark IS NULL
  AND @ActualMonitorCenterId IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_AUTHORIZE x
    WHERE x.F_ItemId = @ActualMonitorCenterId
      AND x.F_ObjectType = 'Role'
      AND x.F_ObjectId = a.F_ObjectId
  )
GROUP BY a.F_ObjectId;

INSERT INTO BASE_AUTHORIZE (
  F_Id, F_ItemType, F_ItemId, F_ObjectType, F_ObjectId, F_SortCode, F_CreatorTime, F_CreatorUserId
)
SELECT REPLACE(UUID(), '-', ''), 'module', @ActualDuplicateAuditId, 'Role', a.F_ObjectId, 1, NOW(), 'admin'
FROM BASE_AUTHORIZE a
INNER JOIN BASE_MODULE m ON a.F_ItemId = m.F_Id
WHERE a.F_ItemType = 'module'
  AND a.F_ObjectType = 'Role'
  AND m.F_EnCode = 'sybzljc'
  AND m.F_DeleteMark IS NULL
  AND @ActualDuplicateAuditId IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_AUTHORIZE x
    WHERE x.F_ItemId = @ActualDuplicateAuditId
      AND x.F_ObjectType = 'Role'
      AND x.F_ObjectId = a.F_ObjectId
  )
GROUP BY a.F_ObjectId;
