-- ========================================
-- PMS价格管理功能菜单创建语句 (修复版)
-- 包含：价格管理主菜单、子菜单、权限点定义
-- 按照RuoYi框架的菜单结构标准创建
-- 修复版本：v1.2 (解决MySQL子查询和menu_id自增问题)
-- ========================================
-- 设置字符集
SET NAMES utf8mb4;
-- =============================================
-- 0. 获取当前最大菜单ID
-- =============================================
SET @max_menu_id = (
        SELECT COALESCE(MAX(menu_id), 0)
        FROM sys_menu
    );
-- 首先获取PMS系统主菜单ID
SET @pms_main_menu_id = (
        SELECT menu_id
        FROM sys_menu
        WHERE menu_name = 'PMS系统'
            AND parent_id = 0
        LIMIT 1
    );
-- 如果PMS系统主菜单不存在，则创建
INSERT IGNORE INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @max_menu_id + 1,
        'PMS系统',
        0,
        6,
        'pms',
        NULL,
        NULL,
        1,
        0,
        'M',
        '0',
        '0',
        NULL,
        'hotel',
        103,
        1,
        NOW(),
        1,
        NOW(),
        'PMS酒店管理系统主菜单'
    );
-- 重新获取PMS系统主菜单ID
SET @pms_main_menu_id = (
        SELECT menu_id
        FROM sys_menu
        WHERE menu_name = 'PMS系统'
            AND parent_id = 0
        LIMIT 1
    );
-- ========================================
-- 1. 价格管理主菜单
-- ========================================
SET @pricing_menu_id = @max_menu_id + 2;
-- 插入价格管理主菜单
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @pricing_menu_id,
        '价格管理',
        @pms_main_menu_id,
        3,
        'pricing',
        NULL,
        NULL,
        1,
        0,
        'M',
        '0',
        '0',
        NULL,
        'money',
        103,
        1,
        NOW(),
        1,
        NOW(),
        '价格管理主菜单'
    );
-- ========================================
-- 2. 价格管理子菜单
-- ========================================
SET @pricing_rules_menu_id = @max_menu_id + 3;
SET @special_pricing_menu_id = @max_menu_id + 4;
SET @pricing_history_menu_id = @max_menu_id + 5;
-- 2.1 价格规则管理
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @pricing_rules_menu_id,
        '价格规则',
        @pricing_menu_id,
        1,
        'pricing-rules',
        'pms/pricing-rules',
        NULL,
        1,
        0,
        'C',
        '0',
        '0',
        'pms:pricingRule:list',
        'guide',
        103,
        1,
        NOW(),
        1,
        NOW(),
        '价格规则管理'
    );
-- 2.2 特殊日期价格
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @special_pricing_menu_id,
        '特殊日期价格',
        @pricing_menu_id,
        2,
        'special-dates',
        'pms/special-dates',
        NULL,
        1,
        0,
        'C',
        '0',
        '0',
        'pms:specialPricing:list',
        'date-range',
        103,
        1,
        NOW(),
        1,
        NOW(),
        '特殊日期价格管理'
    );
-- 2.3 价格计算历史
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @pricing_history_menu_id,
        '计算历史',
        @pricing_menu_id,
        3,
        'pricing-history',
        'pms/pricing-history',
        NULL,
        1,
        0,
        'C',
        '0',
        '0',
        'pms:pricingCalculation:list',
        'time-range',
        103,
        1,
        NOW(),
        1,
        NOW(),
        '价格计算历史'
    );
-- ========================================
-- 3. 价格规则权限按钮
-- ========================================
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @max_menu_id + 6,
        '价格规则查询',
        @pricing_rules_menu_id,
        1,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:query',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 7,
        '价格规则新增',
        @pricing_rules_menu_id,
        2,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:add',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 8,
        '价格规则修改',
        @pricing_rules_menu_id,
        3,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:edit',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 9,
        '价格规则删除',
        @pricing_rules_menu_id,
        4,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:remove',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 10,
        '价格规则导出',
        @pricing_rules_menu_id,
        5,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:export',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 11,
        '价格规则启用',
        @pricing_rules_menu_id,
        6,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:enable',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 12,
        '价格规则禁用',
        @pricing_rules_menu_id,
        7,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:disable',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 13,
        '价格规则复制',
        @pricing_rules_menu_id,
        8,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:copy',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 14,
        '价格规则冲突检测',
        @pricing_rules_menu_id,
        9,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingRule:conflict',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    );
-- ========================================
-- 4. 特殊日期价格权限按钮
-- ========================================
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @max_menu_id + 15,
        '特殊价格查询',
        @special_pricing_menu_id,
        1,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:query',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 16,
        '特殊价格新增',
        @special_pricing_menu_id,
        2,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:add',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 17,
        '特殊价格修改',
        @special_pricing_menu_id,
        3,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:edit',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 18,
        '特殊价格删除',
        @special_pricing_menu_id,
        4,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:remove',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 19,
        '特殊价格批量操作',
        @special_pricing_menu_id,
        5,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:batch',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 20,
        '特殊价格导出',
        @special_pricing_menu_id,
        6,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:export',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 21,
        '特殊价格启用',
        @special_pricing_menu_id,
        7,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:enable',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 22,
        '特殊价格禁用',
        @special_pricing_menu_id,
        8,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:specialPricing:disable',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    );
-- ========================================
-- 5. 价格计算历史权限按钮
-- ========================================
INSERT INTO sys_menu (
        menu_id,
        menu_name,
        parent_id,
        order_num,
        path,
        component,
        query_param,
        is_frame,
        is_cache,
        menu_type,
        visible,
        status,
        perms,
        icon,
        create_dept,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
    )
VALUES (
        @max_menu_id + 23,
        '计算历史查询',
        @pricing_history_menu_id,
        1,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:query',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 24,
        '价格计算',
        @pricing_history_menu_id,
        2,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:calculate',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 25,
        '计算详情',
        @pricing_history_menu_id,
        3,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:detail',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 26,
        '计算历史删除',
        @pricing_history_menu_id,
        4,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:remove',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 27,
        '计算历史导出',
        @pricing_history_menu_id,
        5,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:export',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 28,
        '价格趋势分析',
        @pricing_history_menu_id,
        6,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:trend',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    ),
    (
        @max_menu_id + 29,
        '规则效果分析',
        @pricing_history_menu_id,
        7,
        '#',
        '',
        NULL,
        1,
        0,
        'F',
        '0',
        '0',
        'pms:pricingCalculation:analysis',
        '#',
        103,
        1,
        NOW(),
        1,
        NOW(),
        ''
    );
-- ========================================
-- 输出菜单ID信息
-- ========================================
SELECT 'PMS系统主菜单ID' AS menu_type,
    @pms_main_menu_id AS menu_id
UNION ALL
SELECT '价格管理主菜单ID' AS menu_type,
    @pricing_menu_id AS menu_id
UNION ALL
SELECT '价格规则菜单ID' AS menu_type,
    @pricing_rules_menu_id AS menu_id
UNION ALL
SELECT '特殊日期价格菜单ID' AS menu_type,
    @special_pricing_menu_id AS menu_id
UNION ALL
SELECT '价格计算历史菜单ID' AS menu_type,
    @pricing_history_menu_id AS menu_id;