-- 创建用户
INSERT INTO sys_users (account, username, password_hash, avatar_id, created_at)
VALUES ('admin', '管理员', '$2a$10$d/xYJVUFTmpSVUaV3uawZ.7hj6FanbooMJ9YjsFOmQKNopWc6oR9i', NULL,
        '2025-10-18 03:05:54.692714');

-- 批量插入菜单及关联权限
WITH input(name, path, component, type, sort, status, code)
         AS (VALUES ('首页', '/', '/system/home/index', 1, 0, 1, 'home'),
                    ('用户管理', '/user', '/system/user/index', 1, 1, 1, 'user'),
                    ('角色管理', '/role', '/system/role/index', 1, 2, 1, 'role'),
                    ('菜单管理', '/menu', '/system/menu/index', 1, 3, 1, 'menu'),
                    ('接口管理', '/sys_api', '/system/api/index', 1, 4, 1, 'api'),
                    ('文件管理', '/file', '/system/file/index', 1, 5, 1, 'file')),
     -- 插入菜单
     inserted_menus AS (
         INSERT INTO sys_menus (name, path, component, type, sort, status)
             SELECT name, path, component, type, sort, status FROM input RETURNING id),
     menu_rows AS (SELECT id, ROW_NUMBER() OVER () AS rn FROM inserted_menus),
     -- 插入权限
     inserted_permissions AS (
         INSERT INTO sys_permissions (type, code)
             SELECT 0, code FROM input RETURNING id),
     permission_rows AS (SELECT id, ROW_NUMBER() OVER () AS rn FROM inserted_permissions)
-- 关联权限
INSERT
INTO sys_menu_permission(menu_id, permission_id)
SELECT m.id, p.id
FROM menu_rows m
         JOIN permission_rows p ON m.rn = p.rn;

-- 批量插入接口及关联权限
WITH inserted_apis AS (
    -- 插入菜单
    INSERT INTO sys_apis (group_name, name, method, path)
        VALUES ('用户管理', '用户列表', 'GET', '/admin/user'),
               ('用户管理', '用户信息', 'GET', '/admin/user/info'),
               ('用户管理', '查询用户', 'GET', '/admin/user/{id}'),
               ('用户管理', '用户角色', 'GET', '/admin/user/{id}/roles'),
               ('用户管理', '创建用户', 'POST', '/admin/user'),
               ('用户管理', '修改用户', 'PATCH', '/admin/user/{id}'),
               ('用户管理', '删除用户', 'DELETE', '/admin/user/{id}'),
               ('用户管理', '分配用户角色', 'PUT', '/admin/user/{id}/roles'),
               ('用户管理', '修改密码', 'PATCH', '/admin/user/{id}/password'),
               --
               ('角色管理', '角色列表', 'GET', '/admin/role'),
               ('角色管理', '创建角色', 'POST', '/admin/role'),
               ('角色管理', '所有角色', 'GET', '/admin/role/all'),
               ('角色管理', '单个角色', 'GET', '/admin/role/{id}'),
               ('角色管理', '角色菜单', 'GET', '/admin/role/{id}/menus'),
               ('角色管理', '角色接口', 'GET', '/admin/role/{id}/apis'),
               ('角色管理', '修改角色', 'PATCH', '/admin/role/{id}'),
               ('角色管理', '删除角色', 'DELETE', '/admin/role/{id}'),
               ('角色管理', '分配角色菜单', 'PUT', '/admin/role/{id}/menus'),
               ('角色管理', '分配角色接口', 'PUT', '/admin/role/{id}/apis'),
               --
               ('接口管理', '接口列表', 'GET', '/admin/api'),
               ('接口管理', '所有接口', 'GET', '/admin/api/all'),
               ('接口管理', '接口分组', 'GET', '/admin/api/group_names'),
               ('接口管理', '创建接口', 'POST', '/admin/api'),
               ('接口管理', '修改接口', 'PATCH', '/admin/api/{id}'),
               ('接口管理', '删除接口', 'DELETE', '/admin/api/{id}'),
               --
               ('菜单管理', '菜单列表', 'GET', '/admin/menu'),
               ('菜单管理', '创建菜单', 'POST', '/admin/menu'),
               ('菜单管理', '所有菜单', 'GET', '/admin/menu/all'),
               ('菜单管理', '修改菜单', 'PATCH', '/admin/menu/{id}'),
               ('菜单管理', '删除菜单', 'DELETE', '/admin/menu/{id}'),
               --
               ('文件管理', '文件列表', 'GET', '/admin/file'),
               ('文件管理', '上传文件', 'POST', '/admin/file') RETURNING id),
     api_rows AS (SELECT id, ROW_NUMBER() OVER () AS rn
                  FROM inserted_apis),
     -- 创建权限
     inserted_permissions AS (
         INSERT INTO sys_permissions (type)
             SELECT 1 FROM api_rows RETURNING id),
     permission_rows AS (SELECT id, ROW_NUMBER() OVER () AS rn
                         FROM inserted_permissions)
INSERT
INTO sys_api_permission(api_id, permission_id)
SELECT a.id, b.id
FROM api_rows a
         INNER JOIN permission_rows b ON a.rn = b.rn;
