-- ============================================================
-- 监督抽检 / 平台客诉 - 菜单权限 SQL(幂等)
-- ============================================================
-- 菜单路径: 风控平台 -> 监督抽检/平台客诉
-- 权限标识:
--   risk:supervision:inspection:list / add / edit / remove / export
--   risk:supervision:case:list / process
--   risk:supervision:complaint:list / add / process
-- ============================================================

SET NAMES utf8mb4;

-- ----------------------------------------------------------
-- 1. 父菜单(插入到 风控平台 菜单下)
-- ----------------------------------------------------------
INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '监督抽检/平台客诉', (
        SELECT menu_id FROM sys_menu WHERE menu_name = '风控平台' AND menu_type = 'M' LIMIT 1
    ), 12, 'supervision', 'risk/supervision/index', 1, 0, 'C', '0', '0', 'risk:supervision:list', 'monitor', 1, NOW(), 1, NOW(), '监督抽检与平台客诉目录'
WHERE NOT EXISTS (
    SELECT 1 FROM sys_menu WHERE menu_name = '监督抽检/平台客诉' AND menu_type = 'C'
);

SET @supervisionMenuId = LAST_INSERT_ID();

-- 若已存在则取已有的 ID
SET @supervisionMenuId = COALESCE(
    (SELECT menu_id FROM sys_menu WHERE menu_name = '监督抽检/平台客诉' AND menu_type = 'C' LIMIT 1),
    @supervisionMenuId
);

-- ----------------------------------------------------------
-- 2. 抽检单按钮权限(5 个)
-- ----------------------------------------------------------
INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '抽检登记查询', @supervisionMenuId, 1, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:inspection:list', '#', 1, NOW(), 1, NOW(), '门店抽检当日审核登记查询'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:inspection:list');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '抽检登记新增', @supervisionMenuId, 2, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:inspection:add', '#', 1, NOW(), 1, NOW(), '新增门店抽检登记'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:inspection:add');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '抽检登记编辑', @supervisionMenuId, 3, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:inspection:edit', '#', 1, NOW(), 1, NOW(), '编辑门店抽检登记'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:inspection:edit');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '抽检登记删除', @supervisionMenuId, 4, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:inspection:remove', '#', 1, NOW(), 1, NOW(), '删除门店抽检登记'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:inspection:remove');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '抽检登记导出', @supervisionMenuId, 5, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:inspection:export', '#', 1, NOW(), 1, NOW(), '导出抽检登记'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:inspection:export');

-- ----------------------------------------------------------
-- 3. 案件按钮权限(2 个)
-- ----------------------------------------------------------
INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '案件查询', @supervisionMenuId, 6, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:case:list', '#', 1, NOW(), 1, NOW(), '不合格案件查询'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:case:list');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '案件处理', @supervisionMenuId, 7, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:case:process', '#', 1, NOW(), 1, NOW(), '处理不合格案件各节点'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:case:process');

-- ----------------------------------------------------------
-- 4. 客诉按钮权限(3 个)
-- ----------------------------------------------------------
INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '客诉查询', @supervisionMenuId, 8, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:complaint:list', '#', 1, NOW(), 1, NOW(), '平台客诉查询'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:complaint:list');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '客诉新增', @supervisionMenuId, 9, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:complaint:add', '#', 1, NOW(), 1, NOW(), '新增平台客诉登记'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:complaint:add');

INSERT IGNORE INTO sys_menu (menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark)
SELECT '客诉处理', @supervisionMenuId, 10, '#', '', 1, 0, 'F', '0', '0', 'risk:supervision:complaint:process', '#', 1, NOW(), 1, NOW(), '处理平台客诉各环节'
WHERE NOT EXISTS (SELECT 1 FROM sys_menu WHERE perms = 'risk:supervision:complaint:process');

-- ============================================================
-- 完成
-- ============================================================