-- 信息维护菜单移除导出权限并统一菜单顺序
-- 可重复执行

DELETE rm
FROM sys_role_menu rm
JOIN sys_menu m ON m.menu_id = rm.menu_id
WHERE m.perms IN (
    'ccdi:baseStaff:export',
    'ccdi:staffFmyRelation:export',
    'ccdi:staffEnterpriseRelation:export',
    'ccdi:staffRecruitment:export',
    'ccdi:staffTransfer:export',
    'ccdi:custFmyRelation:export',
    'ccdi:custEnterpriseRelation:export',
    'ccdi:purchaseTransaction:export',
    'ccdi:accountInfo:export',
    'ccdi:intermediary:export',
    'dpc:intermediary:export'
);

DELETE FROM sys_menu
WHERE perms IN (
    'ccdi:baseStaff:export',
    'ccdi:staffFmyRelation:export',
    'ccdi:staffEnterpriseRelation:export',
    'ccdi:staffRecruitment:export',
    'ccdi:staffTransfer:export',
    'ccdi:custFmyRelation:export',
    'ccdi:custEnterpriseRelation:export',
    'ccdi:purchaseTransaction:export',
    'ccdi:accountInfo:export',
    'ccdi:intermediary:export',
    'dpc:intermediary:export'
);

SET @parent_menu_id = (
    SELECT menu_id
    FROM sys_menu
    WHERE menu_name = '信息维护'
      AND parent_id = 0
    LIMIT 1
);

UPDATE sys_menu
SET order_num = CASE
    WHEN path = 'baseStaff'
      OR perms = 'ccdi:baseStaff:list'
      OR menu_name = '员工信息维护' THEN 1
    WHEN path = 'staffRecruitment'
      OR perms = 'ccdi:staffRecruitment:list'
      OR menu_name = '招聘信息维护'
      OR menu_name = '员工招聘信息' THEN 2
    WHEN path = 'staffTransfer'
      OR perms = 'ccdi:staffTransfer:list'
      OR menu_name = '员工调动记录'
      OR menu_name = '员工调动信息' THEN 3
    WHEN path = 'staffFmyRelation'
      OR perms = 'ccdi:staffFmyRelation:list'
      OR menu_name = '员工亲属关系维护' THEN 4
    WHEN path = 'staffEnterpriseRelation'
      OR perms = 'ccdi:staffEnterpriseRelation:list'
      OR menu_name = '员工实体关系维护'
      OR menu_name = '员工实体关系' THEN 5
    WHEN path = 'creditInfo'
      OR perms = 'ccdi:creditInfo:list'
      OR menu_name = '征信维护' THEN 6
    WHEN path = 'enterpriseBaseInfo'
      OR perms = 'ccdi:enterpriseBaseInfo:list'
      OR menu_name = '实体库管理' THEN 7
    WHEN path = 'intermediary'
      OR perms IN ('ccdi:intermediary:list', 'dpc:intermediary:list')
      OR menu_name = '中介库管理'
      OR menu_name = '中介黑名单'
      OR menu_name = '中介管理' THEN 8
    WHEN path = 'accountInfo'
      OR perms = 'ccdi:accountInfo:list'
      OR menu_name = '账户库管理' THEN 9
    WHEN path = 'custFmyRelation'
      OR perms = 'ccdi:custFmyRelation:list'
      OR menu_name = '信贷客户家庭关系' THEN 10
    WHEN path = 'custEnterpriseRelation'
      OR perms = 'ccdi:custEnterpriseRelation:list'
      OR menu_name = '信贷客户实体关联' THEN 11
    WHEN path = 'purchaseTransaction'
      OR perms = 'ccdi:purchaseTransaction:list'
      OR menu_name = '采购交易管理' THEN 12
    ELSE order_num
END
WHERE @parent_menu_id IS NOT NULL
  AND parent_id = @parent_menu_id;
