BEGIN;

ALTER TABLE biz_data_access DISABLE TRIGGER ALL;

DELETE FROM auth_role_permission;
DELETE FROM auth_permission;
DELETE FROM auth_user_role;
DELETE FROM auth_role;
DELETE FROM auth_user;

INSERT INTO auth_user (username, password, real_name, status)
VALUES ('admin', '$2a$10$2JRCyrbZANZdGD4sgplVjuIOPvK1P/Be1/4iwXwkUqpbEDo2AHcuC', '系统管理员', 'ACTIVE');

INSERT INTO auth_role (code, name, description, type, data_scope, status)
VALUES
    ('SYSTEM_ADMIN', '系统管理员', '系统超级管理员', 'SYSTEM', 'ALL', 'ENABLED'),
    ('PROJECT_ADMIN', '项目管理员', '项目管理员', 'PROJECT', 'PROJECT', 'ENABLED'),
    ('EMPLOYEE', '普通员工', '普通员工', 'DEPARTMENT', 'SELF', 'ENABLED');

INSERT INTO auth_permission (code, name, type, resource, method, description, sort_order) VALUES
    ('dashboard:view', '查看仪表盘', 'MENU', '/dashboard', 'GET', '查看仪表盘', 1),
    ('system:user:menu', '用户管理', 'MENU', '/system/users', 'GET', '用户管理菜单', 101),
    ('system:role:menu', '角色管理', 'MENU', '/system/roles', 'GET', '角色管理菜单', 201),
    ('system:user:list', '用户列表', 'BUTTON', '/api/users', 'GET', '查看用户列表', 100),
    ('system:user:create', '创建用户', 'BUTTON', '/api/users', 'POST', '创建新用户', 101),
    ('system:user:update', '更新用户', 'BUTTON', '/api/users', 'PUT', '更新用户信息', 102),
    ('system:user:delete', '删除用户', 'BUTTON', '/api/users', 'DELETE', '删除用户', 103),
    ('system:role:list', '角色列表', 'BUTTON', '/api/roles', 'GET', '查看角色列表', 200),
    ('system:role:create', '创建角色', 'BUTTON', '/api/roles', 'POST', '创建新角色', 201),
    ('system:role:update', '更新角色', 'BUTTON', '/api/roles', 'PUT', '更新角色信息', 202),
    ('system:role:delete', '删除角色', 'BUTTON', '/api/roles', 'DELETE', '删除角色', 203),
    ('system:role:assignPermissions', '分配权限', 'BUTTON', '/api/roles/*/permissions', 'POST', '为角色分配权限', 204),
    ('system:permission:list', '权限列表', 'BUTTON', '/api/permissions', 'GET', '查看权限列表', 300),
    ('system:permission:create', '创建权限', 'BUTTON', '/api/permissions', 'POST', '创建新权限', 301),
    ('system:permission:update', '更新权限', 'BUTTON', '/api/permissions', 'PUT', '更新权限信息', 302),
    ('system:permission:delete', '删除权限', 'BUTTON', '/api/permissions', 'DELETE', '删除权限', 303),
    ('project:list', '项目列表', 'MENU', '/api/projects', 'GET', '查看项目列表', 400),
    ('project:create', '创建项目', 'BUTTON', '/api/projects', 'POST', '创建新项目', 401),
    ('project:update', '更新项目', 'BUTTON', '/api/projects', 'PUT', '更新项目信息', 402),
    ('project:delete', '删除项目', 'BUTTON', '/api/projects', 'DELETE', '删除项目', 403),
    ('space:list', '空间列表', 'MENU', '/api/spaces', 'GET', '查看空间列表', 500),
    ('space:create', '创建空间', 'BUTTON', '/api/spaces', 'POST', '创建新空间', 501),
    ('space:update', '更新空间', 'BUTTON', '/api/spaces', 'PUT', '更新空间信息', 502),
    ('space:delete', '删除空间', 'BUTTON', '/api/spaces', 'DELETE', '删除空间', 503),
    ('asset:list', '资产列表', 'MENU', '/api/assets', 'GET', '查看资产列表', 600),
    ('asset:create', '创建资产', 'BUTTON', '/api/assets', 'POST', '创建新资产', 601),
    ('asset:update', '更新资产', 'BUTTON', '/api/assets', 'PUT', '更新资产信息', 602),
    ('asset:delete', '删除资产', 'BUTTON', '/api/assets', 'DELETE', '删除资产', 603),
    ('audit:view', '查看审计日志', 'MENU', '/api/audit', 'GET', '查看审计日志', 700),
    ('audit:export', '导出审计日志', 'BUTTON', '/api/audit/export', 'GET', '导出审计日志', 701),
    ('finance:list', '财务列表', 'MENU', '/api/finance', 'GET', '查看财务列表', 800),
    ('finance:create', '创建财务记录', 'BUTTON', '/api/finance', 'POST', '创建财务记录', 801),
    ('finance:update', '更新财务记录', 'BUTTON', '/api/finance', 'PUT', '更新财务记录', 802),
    ('finance:delete', '删除财务记录', 'BUTTON', '/api/finance', 'DELETE', '删除财务记录', 803);

INSERT INTO auth_role_permission (role_id, permission_id)
SELECT r.id, p.id
FROM auth_role r, auth_permission p
WHERE r.code = 'SYSTEM_ADMIN';

ALTER TABLE biz_data_access ENABLE TRIGGER ALL;

COMMIT;
