-- QA 初始化数据 (UUID直写, 无变量)
USE mokeegatewayqa;

-- 管理员 (密码: admin123, BCrypt)
INSERT INTO sys_user (id, username, password, real_name, email, phone, post, status)
VALUES (REPLACE(UUID(),'-',''), 'admin', '$2b$10$ffAZHF6lmUuNV6OJITADXeduD5mMTS0.7kElPI7zno2zGn/RF5uC6', '管理员', 'admin@mokee.com', '13800000000', '管理员', 1);

-- 测试系统
INSERT INTO sys_system (id, system_code, system_name, front_url, backend_url, gateway_url, status, description)
VALUES (REPLACE(UUID(),'-',''), 'test', '测试系统', 'http://127.0.0.1:5001', 'http://127.0.0.1:9000', 'http://127.0.0.1:9000/test', 1, '测试用业务系统');

-- Gateway 系统
INSERT INTO sys_system (id, system_code, system_name, front_url, backend_url, gateway_url, icon, sort_order, status, description, doc_token)
VALUES (REPLACE(UUID(),'-',''), 'gateway', '网关管理平台', 'http://127.0.0.1:5001', 'http://127.0.0.1:9000', 'http://127.0.0.1:9000/gateway', '', 0, 1, '统一登录网关管理后台', 'gateway1234567890');

-- 查询已插入的ID
SELECT id INTO @uid FROM sys_user WHERE username='admin';
SELECT id INTO @test_sid FROM sys_system WHERE system_code='test';
SELECT id INTO @gw_sid FROM sys_system WHERE system_code='gateway';

-- 管理员绑定两个系统
INSERT INTO sys_user_system (id, user_id, system_id) VALUES (REPLACE(UUID(),'-',''), @uid, @test_sid);
INSERT INTO sys_user_system (id, user_id, system_id) VALUES (REPLACE(UUID(),'-',''), @uid, @gw_sid);

-- 测试系统角色
INSERT INTO sys_role (id, system_id, role_name, role_code, description, status) VALUES (REPLACE(UUID(),'-',''), @test_sid, '管理员', 'admin', '系统管理员', 1);
INSERT INTO sys_role (id, system_id, role_name, role_code, description, status) VALUES (REPLACE(UUID(),'-',''), @test_sid, '普通用户', 'user', '普通用户', 1);

-- Gateway 系统角色
INSERT INTO sys_role (id, system_id, role_name, role_code, description, status) VALUES (REPLACE(UUID(),'-',''), @gw_sid, '超级管理员', 'super_admin', '网关平台超级管理员', 1);
INSERT INTO sys_role (id, system_id, role_name, role_code, description, status) VALUES (REPLACE(UUID(),'-',''), @gw_sid, '普通管理员', 'admin', '网关平台普通管理员', 1);

-- 查询角色ID
SELECT id INTO @test_admin_role FROM sys_role WHERE system_id=@test_sid AND role_code='admin';
SELECT id INTO @gw_super_admin FROM sys_role WHERE system_id=@gw_sid AND role_code='super_admin';

-- 分配角色给管理员
INSERT INTO sys_user_role (id, user_id, role_id) VALUES (REPLACE(UUID(),'-',''), @uid, @test_admin_role);
INSERT INTO sys_user_role (id, user_id, role_id) VALUES (REPLACE(UUID(),'-',''), @uid, @gw_super_admin);

-- 测试系统菜单 (6个)
INSERT INTO sys_menu (id, system_id, parent_id, menu_name, menu_path, component, icon, menu_type, sort_order, status) VALUES
(REPLACE(UUID(),'-',''), @test_sid, '0', '系统管理', '/system', 'Layout', 'Setting', 1, 1, 1),
(REPLACE(UUID(),'-',''), @test_sid, '0', '业务管理', '/biz', 'Layout', 'Document', 1, 2, 1);
SELECT id INTO @m1 FROM sys_menu WHERE system_id=@test_sid AND menu_name='系统管理' LIMIT 1;
SELECT id INTO @m2 FROM sys_menu WHERE system_id=@test_sid AND menu_name='业务管理' LIMIT 1;
INSERT INTO sys_menu (id, system_id, parent_id, menu_name, menu_path, component, icon, menu_type, sort_order, status) VALUES
(REPLACE(UUID(),'-',''), @test_sid, @m1, '用户管理', '/system/user', 'system/user/index', 'User', 2, 1, 1),
(REPLACE(UUID(),'-',''), @test_sid, @m1, '角色管理', '/system/role', 'system/role/index', 'Avatar', 2, 2, 1),
(REPLACE(UUID(),'-',''), @test_sid, @m2, '订单管理', '/biz/order', 'biz/order/index', 'Tickets', 2, 1, 1),
(REPLACE(UUID(),'-',''), @test_sid, @m2, '报表查询', '/biz/report', 'biz/report/index', 'TrendCharts', 2, 2, 1);

-- 角色绑定菜单
INSERT INTO sys_role_menu (id, role_id, menu_id) SELECT REPLACE(UUID(),'-',''), @test_admin_role, id FROM sys_menu WHERE system_id=@test_sid;

-- Gateway 顶级目录 (4个)
INSERT INTO sys_menu (id, system_id, parent_id, menu_name, menu_path, component, icon, menu_type, sort_order, status) VALUES
(REPLACE(UUID(),'-',''), @gw_sid, '0', '首页', '/admin/dashboard', '', 'HomeFilled', 1, 1, 1),
(REPLACE(UUID(),'-',''), @gw_sid, '0', '系统管理', NULL, '', 'Monitor', 1, 2, 1),
(REPLACE(UUID(),'-',''), @gw_sid, '0', '用户权限', NULL, '', 'User', 1, 3, 1),
(REPLACE(UUID(),'-',''), @gw_sid, '0', '日志审计', NULL, '', 'Notebook', 1, 4, 1);

SELECT id INTO @g_sys FROM sys_menu WHERE system_id=@gw_sid AND menu_name='系统管理' LIMIT 1;
SELECT id INTO @g_usr FROM sys_menu WHERE system_id=@gw_sid AND menu_name='用户权限' LIMIT 1;
SELECT id INTO @g_log FROM sys_menu WHERE system_id=@gw_sid AND menu_name='日志审计' LIMIT 1;

-- 子菜单
INSERT INTO sys_menu (id, system_id, parent_id, menu_name, menu_path, component, icon, menu_type, sort_order, status) VALUES
(REPLACE(UUID(),'-',''), @gw_sid, @g_sys, '系统列表', '/admin/system', '', 'List', 2, 1, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_sys, '接口管理', '/admin/api', '', 'Link', 2, 2, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_sys, '数据字典', '/admin/dict', '', 'Collection', 2, 3, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_usr, '用户管理', '/admin/user', '', 'UserFilled', 2, 1, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_usr, '角色管理', '/admin/role', '', 'Avatar', 2, 2, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_usr, '菜单管理', '/admin/menu', '', 'Menu', 2, 3, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_log, '登录日志', '/admin/log/login', '', 'Document', 2, 1, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_log, '操作日志', '/admin/log/operation', '', 'Tickets', 2, 2, 1),
(REPLACE(UUID(),'-',''), @gw_sid, @g_log, 'Token日志', '/admin/log/token', '', 'Key', 2, 3, 1);

-- 查询子菜单ID 准备加按钮权限
SELECT id INTO @sys_list FROM sys_menu WHERE system_id=@gw_sid AND menu_name='系统列表' LIMIT 1;
SELECT id INTO @usr_list FROM sys_menu WHERE system_id=@gw_sid AND menu_name='用户管理' LIMIT 1;
SELECT id INTO @role_list FROM sys_menu WHERE system_id=@gw_sid AND menu_name='角色管理' LIMIT 1;
SELECT id INTO @menu_list FROM sys_menu WHERE system_id=@gw_sid AND menu_name='菜单管理' LIMIT 1;
SELECT id INTO @api_list FROM sys_menu WHERE system_id=@gw_sid AND menu_name='接口管理' LIMIT 1;
SELECT id INTO @dict_list FROM sys_menu WHERE system_id=@gw_sid AND menu_name='数据字典' LIMIT 1;

-- 按钮权限 (menu_type=3)
INSERT INTO sys_menu (id, system_id, parent_id, menu_name, menu_path, component, icon, menu_type, sort_order, status, permission) VALUES
(REPLACE(UUID(),'-',''), @gw_sid, @sys_list, '新增系统', NULL, NULL, '', 3, 1, 1, 'system:system:add'),
(REPLACE(UUID(),'-',''), @gw_sid, @sys_list, '编辑系统', NULL, NULL, '', 3, 2, 1, 'system:system:edit'),
(REPLACE(UUID(),'-',''), @gw_sid, @sys_list, '删除系统', NULL, NULL, '', 3, 3, 1, 'system:system:delete'),
(REPLACE(UUID(),'-',''), @gw_sid, @usr_list, '新增用户', NULL, NULL, '', 3, 1, 1, 'system:user:add'),
(REPLACE(UUID(),'-',''), @gw_sid, @usr_list, '编辑用户', NULL, NULL, '', 3, 2, 1, 'system:user:edit'),
(REPLACE(UUID(),'-',''), @gw_sid, @usr_list, '删除用户', NULL, NULL, '', 3, 3, 1, 'system:user:delete'),
(REPLACE(UUID(),'-',''), @gw_sid, @role_list, '新增角色', NULL, NULL, '', 3, 1, 1, 'system:role:add'),
(REPLACE(UUID(),'-',''), @gw_sid, @role_list, '编辑角色', NULL, NULL, '', 3, 2, 1, 'system:role:edit'),
(REPLACE(UUID(),'-',''), @gw_sid, @role_list, '删除角色', NULL, NULL, '', 3, 3, 1, 'system:role:delete'),
(REPLACE(UUID(),'-',''), @gw_sid, @role_list, '分配菜单', NULL, NULL, '', 3, 4, 1, 'system:role:assignMenu'),
(REPLACE(UUID(),'-',''), @gw_sid, @menu_list, '新增菜单', NULL, NULL, '', 3, 1, 1, 'system:menu:add'),
(REPLACE(UUID(),'-',''), @gw_sid, @menu_list, '编辑菜单', NULL, NULL, '', 3, 2, 1, 'system:menu:edit'),
(REPLACE(UUID(),'-',''), @gw_sid, @menu_list, '删除菜单', NULL, NULL, '', 3, 3, 1, 'system:menu:delete'),
(REPLACE(UUID(),'-',''), @gw_sid, @api_list, '新增接口', NULL, NULL, '', 3, 1, 1, 'system:api:add'),
(REPLACE(UUID(),'-',''), @gw_sid, @api_list, '编辑接口', NULL, NULL, '', 3, 2, 1, 'system:api:edit'),
(REPLACE(UUID(),'-',''), @gw_sid, @api_list, '删除接口', NULL, NULL, '', 3, 3, 1, 'system:api:delete'),
(REPLACE(UUID(),'-',''), @gw_sid, @dict_list, '新增字典', NULL, NULL, '', 3, 1, 1, 'system:dict:add'),
(REPLACE(UUID(),'-',''), @gw_sid, @dict_list, '编辑字典', NULL, NULL, '', 3, 2, 1, 'system:dict:edit'),
(REPLACE(UUID(),'-',''), @gw_sid, @dict_list, '删除字典', NULL, NULL, '', 3, 3, 1, 'system:dict:delete');

-- 角色绑定所有菜单(含按钮)
INSERT INTO sys_role_menu (id, role_id, menu_id) SELECT REPLACE(UUID(),'-',''), @gw_super_admin, id FROM sys_menu WHERE system_id=@gw_sid;
INSERT INTO sys_role_menu (id, role_id, menu_id) SELECT REPLACE(UUID(),'-',''), (SELECT id FROM sys_role WHERE system_id=@gw_sid AND role_code='admin'), id FROM sys_menu WHERE system_id=@gw_sid;

-- 放行接口 (14条)
INSERT INTO sys_api (id, system_id, api_path, api_method, api_name, status) VALUES
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/admin/**', 'GET', '放行接口', 1),
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/admin/**', 'POST', '放行接口', 1),
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/admin/**', 'PUT', '放行接口', 1),
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/admin/**', 'DELETE', '放行接口', 1),
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/auth/**', 'GET', '放行接口', 1),
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/auth/**', 'POST', '放行接口', 1),
(REPLACE(UUID(),'-',''), @test_sid, '/zgapi/v1/auth/**', 'PUT', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/admin/**', 'GET', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/admin/**', 'POST', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/admin/**', 'PUT', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/admin/**', 'DELETE', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/auth/**', 'GET', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/auth/**', 'POST', '放行接口', 1),
(REPLACE(UUID(),'-',''), @gw_sid, '/zgapi/v1/auth/**', 'PUT', '放行接口', 1);
