-- 统一登录网关平台 — 初始化测试数据
-- 创建默认管理员和测试系统

-- 1. 创建默认管理员用户（密码: admin123）
INSERT INTO sys_user (username, password, real_name, email, phone, post, status, created_at, updated_at)
VALUES ('admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '系统管理员', 'admin@mokee.com', '13800000000', '系统管理员', 1, NOW(), NOW())
ON DUPLICATE KEY UPDATE username=username;

-- 2. 创建测试业务系统
INSERT INTO sys_system (system_code, system_name, front_url, backend_url, gateway_url, icon, status, description, created_at, updated_at)
VALUES ('test', '测试业务系统', 'http://127.0.0.1:5001', 'http://127.0.0.1:9000', 'http://127.0.0.1:9000', '', 1, '用于测试的业务系统', NOW(), NOW())
ON DUPLICATE KEY UPDATE system_code=system_code;

-- 3. 绑定管理员到测试系统
INSERT INTO sys_user_system (user_id, system_id, created_at)
SELECT u.id, s.id, NOW() FROM sys_user u, sys_system s
WHERE u.username = 'admin' AND s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_user_system us WHERE us.user_id = u.id AND us.system_id = s.id);

-- 4. 创建管理员角色
INSERT INTO sys_role (system_id, role_name, role_code, description, status, created_at, updated_at)
SELECT s.id, '系统管理员', 'admin', '系统管理员角色，拥有所有权限', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_role r WHERE r.system_id = s.id AND r.role_code = 'admin');

-- 5. 为用户分配管理员角色
INSERT INTO sys_user_role (user_id, role_id, created_at)
SELECT u.id, r.id, NOW() FROM sys_user u, sys_role r, sys_system s
WHERE u.username = 'admin' AND r.role_code = 'admin' AND r.system_id = s.id AND s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_user_role ur WHERE ur.user_id = u.id AND ur.role_id = r.id);

-- 6. 创建默认菜单
INSERT INTO sys_menu (system_id, parent_id, menu_name, menu_path, component, icon, menu_type, permission, sort_order, status, created_at, updated_at)
SELECT s.id, 0, '系统管理', '/system', 'system/index', 'Setting', 1, '', 1, 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_menu m WHERE m.system_id = s.id AND m.menu_name = '系统管理');

-- 7. 为管理员角色分配菜单
INSERT INTO sys_role_menu (role_id, menu_id, created_at)
SELECT r.id, m.id, NOW() FROM sys_role r, sys_menu m, sys_system s
WHERE r.role_code = 'admin' AND m.system_id = s.id AND s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_role_menu rm WHERE rm.role_id = r.id AND rm.menu_id = m.id);

-- 8. 配置放行接口（允许所有接口）
INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/admin/**', 'GET', '管理接口-GET', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/admin/**' AND a.api_method = 'GET');

INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/admin/**', 'POST', '管理接口-POST', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/admin/**' AND a.api_method = 'POST');

INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/admin/**', 'PUT', '管理接口-PUT', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/admin/**' AND a.api_method = 'PUT');

INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/admin/**', 'DELETE', '管理接口-DELETE', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/admin/**' AND a.api_method = 'DELETE');

INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/auth/**', 'GET', '认证接口-GET', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/auth/**' AND a.api_method = 'GET');

INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/auth/**', 'POST', '认证接口-POST', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/auth/**' AND a.api_method = 'POST');

INSERT INTO sys_api (system_id, api_path, api_method, api_name, status, created_at, updated_at)
SELECT s.id, '/zgapi/v1/auth/**', 'PUT', '认证接口-PUT', 1, NOW(), NOW()
FROM sys_system s WHERE s.system_code = 'test'
AND NOT EXISTS (SELECT 1 FROM sys_api a WHERE a.system_id = s.id AND a.api_path = '/zgapi/v1/auth/**' AND a.api_method = 'PUT');
