SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `sys_ai_param` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `uuid` varchar(255) DEFAULT NULL COMMENT '编号',
  `name` varchar(255) DEFAULT NULL COMMENT '名称',
  `model_code` varchar(255) DEFAULT NULL COMMENT '模型编码',
  `provider` varchar(255) DEFAULT NULL COMMENT '供应商',
  `chat_url` varchar(512) DEFAULT NULL COMMENT '聊天地址',
  `api_key` varchar(512) DEFAULT NULL COMMENT 'API密钥',
  `model_name` varchar(255) DEFAULT NULL COMMENT '模型名称',
  `system_prompt` text COMMENT '系统提示词',
  `max_context_messages` int(11) DEFAULT NULL COMMENT '上下文轮数',
  `default_flag` int(1) NOT NULL DEFAULT '0' COMMENT '默认模型{1:是,0:否}',
  `sort` int(11) DEFAULT NULL COMMENT '排序',
  `status` int(1) NOT NULL DEFAULT '1' COMMENT '状态{1:正常,0:冻结}',
  `deleted` int(1) NOT NULL DEFAULT '0' COMMENT '是否删除{1:是,0:否}',
  `tenant_id` bigint(20) DEFAULT NULL COMMENT '租户[sys_tenant]',
  `create_by` bigint(20) DEFAULT NULL COMMENT '添加人员[sys_user]',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
  `update_by` bigint(20) DEFAULT NULL COMMENT '修改人员[sys_user]',
  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
  `memo` varchar(255) DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_ai_param_model_code` (`model_code`),
  KEY `idx_ai_param_deleted_code` (`deleted`,`model_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `sys_ai_param`
(`uuid`, `name`, `model_code`, `provider`, `chat_url`, `api_key`, `model_name`, `system_prompt`, `max_context_messages`, `default_flag`, `sort`, `status`, `deleted`, `tenant_id`, `create_by`, `create_time`, `update_by`, `update_time`, `memo`)
SELECT '6702082748514305', '通用助手', 'mock-general', 'mock', NULL, NULL, 'mock-general', '你是WMS系统内的智能助手，回答时优先保持准确、简洁，并结合上下文帮助用户理解仓储业务。', 12, 1, 1, 1, 0, 1, 2, NOW(), 2, NOW(), '默认演示模型'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1
    FROM `sys_ai_param`
    WHERE `model_code` = 'mock-general'
);

INSERT INTO `sys_ai_param`
(`uuid`, `name`, `model_code`, `provider`, `chat_url`, `api_key`, `model_name`, `system_prompt`, `max_context_messages`, `default_flag`, `sort`, `status`, `deleted`, `tenant_id`, `create_by`, `create_time`, `update_by`, `update_time`, `memo`)
SELECT '6702082748514306', '创意助手', 'mock-creative', 'mock', NULL, NULL, 'mock-creative', '你是WMS系统内的智能助手，回答时可以更灵活地组织表达，但结论必须准确。', 12, 0, 2, 1, 0, 1, 2, NOW(), 2, NOW(), '演示创意模型'
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1
    FROM `sys_ai_param`
    WHERE `model_code` = 'mock-creative'
);

SET @ai_parent_menu_id := (
    SELECT `id`
    FROM `sys_menu`
    WHERE `component` = 'aiParam'
    LIMIT 1
);

INSERT INTO `sys_menu`
(`name`, `parent_id`, `parent_name`, `path`, `path_name`, `route`, `component`, `brief`, `code`, `type`, `authority`, `icon`, `sort`, `meta`, `tenant_id`, `status`, `deleted`, `create_time`, `create_by`, `update_time`, `update_by`, `memo`)
SELECT 'menu.aiParam', 1, 'menu.system', '1', 'menu.system', '/system/aiParam', 'aiParam', NULL, NULL, 0, NULL, 'SmartToy', 9, NULL, 1, 1, 0, NOW(), 2, NOW(), 2, NULL
FROM DUAL
WHERE @ai_parent_menu_id IS NULL;

SET @ai_parent_menu_id := COALESCE(
    @ai_parent_menu_id,
    LAST_INSERT_ID(),
    (
        SELECT `id`
        FROM `sys_menu`
        WHERE `component` = 'aiParam'
        LIMIT 1
    )
);

SET @ai_query_menu_id := (
    SELECT `id`
    FROM `sys_menu`
    WHERE `parent_id` = @ai_parent_menu_id
      AND `name` = 'Query AiParam'
    LIMIT 1
);

INSERT INTO `sys_menu`
(`name`, `parent_id`, `parent_name`, `path`, `path_name`, `route`, `component`, `brief`, `code`, `type`, `authority`, `icon`, `sort`, `meta`, `tenant_id`, `status`, `deleted`, `create_time`, `create_by`, `update_time`, `update_by`, `memo`)
SELECT 'Query AiParam', @ai_parent_menu_id, NULL, CONCAT('1,', @ai_parent_menu_id), NULL, NULL, NULL, NULL, NULL, 1, 'system:aiParam:list', NULL, 0, NULL, 1, 1, 0, NOW(), 2, NOW(), 2, NULL
FROM DUAL
WHERE @ai_query_menu_id IS NULL;

SET @ai_query_menu_id := COALESCE(
    @ai_query_menu_id,
    LAST_INSERT_ID(),
    (
        SELECT `id`
        FROM `sys_menu`
        WHERE `parent_id` = @ai_parent_menu_id
          AND `name` = 'Query AiParam'
        LIMIT 1
    )
);

SET @ai_create_menu_id := (
    SELECT `id`
    FROM `sys_menu`
    WHERE `parent_id` = @ai_parent_menu_id
      AND `name` = 'Create AiParam'
    LIMIT 1
);

INSERT INTO `sys_menu`
(`name`, `parent_id`, `parent_name`, `path`, `path_name`, `route`, `component`, `brief`, `code`, `type`, `authority`, `icon`, `sort`, `meta`, `tenant_id`, `status`, `deleted`, `create_time`, `create_by`, `update_time`, `update_by`, `memo`)
SELECT 'Create AiParam', @ai_parent_menu_id, NULL, CONCAT('1,', @ai_parent_menu_id), NULL, NULL, NULL, NULL, NULL, 1, 'system:aiParam:save', NULL, 1, NULL, 1, 1, 0, NOW(), 2, NOW(), 2, NULL
FROM DUAL
WHERE @ai_create_menu_id IS NULL;

SET @ai_create_menu_id := COALESCE(
    @ai_create_menu_id,
    LAST_INSERT_ID(),
    (
        SELECT `id`
        FROM `sys_menu`
        WHERE `parent_id` = @ai_parent_menu_id
          AND `name` = 'Create AiParam'
        LIMIT 1
    )
);

SET @ai_update_menu_id := (
    SELECT `id`
    FROM `sys_menu`
    WHERE `parent_id` = @ai_parent_menu_id
      AND `name` = 'Update AiParam'
    LIMIT 1
);

INSERT INTO `sys_menu`
(`name`, `parent_id`, `parent_name`, `path`, `path_name`, `route`, `component`, `brief`, `code`, `type`, `authority`, `icon`, `sort`, `meta`, `tenant_id`, `status`, `deleted`, `create_time`, `create_by`, `update_time`, `update_by`, `memo`)
SELECT 'Update AiParam', @ai_parent_menu_id, NULL, CONCAT('1,', @ai_parent_menu_id), NULL, NULL, NULL, NULL, NULL, 1, 'system:aiParam:update', NULL, 2, NULL, 1, 1, 0, NOW(), 2, NOW(), 2, NULL
FROM DUAL
WHERE @ai_update_menu_id IS NULL;

SET @ai_update_menu_id := COALESCE(
    @ai_update_menu_id,
    LAST_INSERT_ID(),
    (
        SELECT `id`
        FROM `sys_menu`
        WHERE `parent_id` = @ai_parent_menu_id
          AND `name` = 'Update AiParam'
        LIMIT 1
    )
);

SET @ai_delete_menu_id := (
    SELECT `id`
    FROM `sys_menu`
    WHERE `parent_id` = @ai_parent_menu_id
      AND `name` = 'Delete AiParam'
    LIMIT 1
);

INSERT INTO `sys_menu`
(`name`, `parent_id`, `parent_name`, `path`, `path_name`, `route`, `component`, `brief`, `code`, `type`, `authority`, `icon`, `sort`, `meta`, `tenant_id`, `status`, `deleted`, `create_time`, `create_by`, `update_time`, `update_by`, `memo`)
SELECT 'Delete AiParam', @ai_parent_menu_id, NULL, CONCAT('1,', @ai_parent_menu_id), NULL, NULL, NULL, NULL, NULL, 1, 'system:aiParam:remove', NULL, 3, NULL, 1, 1, 0, NOW(), 2, NOW(), 2, NULL
FROM DUAL
WHERE @ai_delete_menu_id IS NULL;

SET @ai_delete_menu_id := COALESCE(
    @ai_delete_menu_id,
    LAST_INSERT_ID(),
    (
        SELECT `id`
        FROM `sys_menu`
        WHERE `parent_id` = @ai_parent_menu_id
          AND `name` = 'Delete AiParam'
        LIMIT 1
    )
);

SET @ai_export_menu_id := (
    SELECT `id`
    FROM `sys_menu`
    WHERE `parent_id` = @ai_parent_menu_id
      AND `name` = 'Export AiParam'
    LIMIT 1
);

INSERT INTO `sys_menu`
(`name`, `parent_id`, `parent_name`, `path`, `path_name`, `route`, `component`, `brief`, `code`, `type`, `authority`, `icon`, `sort`, `meta`, `tenant_id`, `status`, `deleted`, `create_time`, `create_by`, `update_time`, `update_by`, `memo`)
SELECT 'Export AiParam', @ai_parent_menu_id, NULL, CONCAT('1,', @ai_parent_menu_id), NULL, NULL, NULL, NULL, NULL, 1, 'system:aiParam:list', NULL, 4, NULL, 1, 1, 0, NOW(), 2, NOW(), 2, NULL
FROM DUAL
WHERE @ai_export_menu_id IS NULL;

SET @ai_export_menu_id := COALESCE(
    @ai_export_menu_id,
    LAST_INSERT_ID(),
    (
        SELECT `id`
        FROM `sys_menu`
        WHERE `parent_id` = @ai_parent_menu_id
          AND `name` = 'Export AiParam'
        LIMIT 1
    )
);

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, @ai_parent_menu_id
FROM DUAL
WHERE @ai_parent_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_menu`
      WHERE `role_id` = 1
        AND `menu_id` = @ai_parent_menu_id
  );

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, @ai_query_menu_id
FROM DUAL
WHERE @ai_query_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_menu`
      WHERE `role_id` = 1
        AND `menu_id` = @ai_query_menu_id
  );

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, @ai_create_menu_id
FROM DUAL
WHERE @ai_create_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_menu`
      WHERE `role_id` = 1
        AND `menu_id` = @ai_create_menu_id
  );

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, @ai_update_menu_id
FROM DUAL
WHERE @ai_update_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_menu`
      WHERE `role_id` = 1
        AND `menu_id` = @ai_update_menu_id
  );

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, @ai_delete_menu_id
FROM DUAL
WHERE @ai_delete_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_menu`
      WHERE `role_id` = 1
        AND `menu_id` = @ai_delete_menu_id
  );

INSERT INTO `sys_role_menu` (`role_id`, `menu_id`)
SELECT 1, @ai_export_menu_id
FROM DUAL
WHERE @ai_export_menu_id IS NOT NULL
  AND NOT EXISTS (
      SELECT 1
      FROM `sys_role_menu`
      WHERE `role_id` = 1
        AND `menu_id` = @ai_export_menu_id
  );

SET FOREIGN_KEY_CHECKS = 1;
