-- 监控中心 - 大客卡金异常（挂在 monitorCenter 下）
SET @AdminRoleId = '94e3a9bb0fce4547886972998fddba1c';
SET @LargeAmountAuditId = 'monitor-large-amount-audit-lq';

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

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
  @LargeAmountAuditId, @ActualMonitorCenterId, 2, '大客卡金异常', 'largeAmountAudit',
  'monitorCenter/large-amount-audit', 'icon-ym icon-ym-generator-money',
  2, 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 = 'largeAmountAudit' AND F_Category = 'Web' AND F_DeleteMark IS NULL
  );

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

UPDATE BASE_MODULE
SET F_ParentId = @ActualMonitorCenterId,
    F_FullName = '大客卡金异常',
    F_UrlAddress = 'monitorCenter/large-amount-audit',
    F_SortCode = 2,
    F_EnabledMark = 1,
    F_LastModifyTime = NOW()
WHERE F_Id = @ActualLargeAmountAuditId AND @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', @ActualLargeAmountAuditId, 'Role', @AdminRoleId, 1, NOW(), 'admin'
FROM (SELECT 1) AS seed
WHERE @ActualLargeAmountAuditId IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_AUTHORIZE WHERE F_ItemId = @ActualLargeAmountAuditId AND F_ObjectId = @AdminRoleId
  );

INSERT INTO BASE_AUTHORIZE (F_Id, F_ItemType, F_ItemId, F_ObjectType, F_ObjectId, F_SortCode, F_CreatorTime, F_CreatorUserId)
SELECT REPLACE(UUID(), '-', ''), 'module', @ActualLargeAmountAuditId, '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 @ActualLargeAmountAuditId IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM BASE_AUTHORIZE x WHERE x.F_ItemId = @ActualLargeAmountAuditId AND x.F_ObjectId = a.F_ObjectId
  )
GROUP BY a.F_ObjectId;
