-- 设置会话变量
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

USE `technological_brain`;

-- 开启事务
START TRANSACTION;

-- 清空所有相关表数据（注意顺序：先清空关联表，再清空主表）
DELETE FROM `sys_user_role`;
DELETE FROM `sys_role_permission`;
DELETE FROM `sys_user`;
DELETE FROM `sys_role`;
DELETE FROM `sys_permission`;

-- 系统管理模块
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`) 
WITH system_menu AS (
    SELECT REPLACE(UUID(), '-', '') as id
)
SELECT 
    id,
    '系统管理',
    'system',
    1,
    NULL,
    '/system',
    'Layout',
    'setting',
    1,
    1,
    0,
    NOW(),
    NOW()
FROM system_menu;

-- 系统管理子菜单
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '用户管理',
    'system:user',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'system'),
    '/system/user',
    'system/user/index',
    'user',
    1,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '角色管理',
    'system:role',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'system'),
    '/system/role',
    'system/role/index',
    'peoples',
    2,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '权限管理',
    'system:permission',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'system'),
    '/system/permission',
    'system/permission/index',
    'tree-table',
    3,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '参数管理',
    'system:param',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'system'),
    '/system/param',
    'system/param/index',
    'dict',
    4,
    1,
    0,
    NOW(),
    NOW();

-- 人才管理模块
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
WITH talent_menu AS (
    SELECT REPLACE(UUID(), '-', '') as id
)
SELECT 
    id,
    '人才管理',
    'talent',
    1,
    NULL,
    '/talent',
    'Layout',
    'user',
    2,
    1,
    0,
    NOW(),
    NOW()
FROM talent_menu;

-- 人才管理子菜单
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '人才信息',
    'talent:basic',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'talent'),
    '/talent/basic',
    'talent/basic/index',
    'profile',
    1,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '工作单位',
    'talent:unit',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'talent'),
    '/talent/unit',
    'talent/unit/index',
    'company',
    2,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '履历信息',
    'talent:resume',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'talent'),
    '/talent/resume',
    'talent/resume/index',
    'education',
    3,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '工作领域',
    'talent:domain',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'talent'),
    '/talent/domain',
    'talent/domain/index',
    'skill',
    4,
    1,
    0,
    NOW(),
    NOW();

-- 科研管理模块
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
WITH research_menu AS (
    SELECT REPLACE(UUID(), '-', '') as id
)
SELECT 
    id,
    '科研管理',
    'research',
    1,
    NULL,
    '/research',
    'Layout',
    'research',
    3,
    1,
    0,
    NOW(),
    NOW()
FROM research_menu;

-- 科研管理子菜单
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '科研项目',
    'research:project',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'research'),
    '/research/project',
    'research/project/index',
    'project',
    1,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '立项项目',
    'research:initiation',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'research'),
    '/research/initiation',
    'research/initiation/index',
    'form',
    2,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '科技报告',
    'research:report',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'research'),
    '/research/report',
    'research/report/index',
    'documentation',
    3,
    1,
    0,
    NOW(),
    NOW();

-- 成果管理模块
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
WITH achievement_menu AS (
    SELECT REPLACE(UUID(), '-', '') as id
)
SELECT 
    id,
    '成果管理',
    'achievement',
    1,
    NULL,
    '/achievement',
    'Layout',
    'trophy',
    4,
    1,
    0,
    NOW(),
    NOW()
FROM achievement_menu;

-- 成果管理子菜单
INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '论文专著',
    'achievement:paper',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'achievement'),
    '/achievement/paper',
    'achievement/paper/index',
    'document',
    1,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '专利信息',
    'achievement:patent',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'achievement'),
    '/achievement/patent',
    'achievement/patent/index',
    'patent',
    2,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '学术奖励',
    'achievement:award',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'achievement'),
    '/achievement/award',
    'achievement/award/index',
    'award',
    3,
    1,
    0,
    NOW(),
    NOW();

INSERT INTO `sys_permission` (`id`, `permission_name`, `permission_code`, `permission_type`, `parent_id`, `path`, `component`, `icon`, `sort`, `status`, `flag`, `create_time`, `update_time`)
SELECT 
    REPLACE(UUID(), '-', ''),
    '荣誉称号',
    'achievement:honor',
    1,
    (SELECT id FROM `sys_permission` WHERE `permission_code` = 'achievement'),
    '/achievement/honor',
    'achievement/honor/index',
    'medal',
    4,
    1,
    0,
    NOW(),
    NOW();

-- 创建角色
INSERT INTO `sys_role` (`id`, `role_name`, `role_code`, `role_desc`, `login_type`, `sort`, `status`, `flag`, `create_time`, `update_time`, `create_by`, `update_by`) 
VALUES 
-- 超级管理员角色
(REPLACE(UUID(), '-', ''),
'超级管理员',
'SUPER_ADMIN',
'系统超级管理员，拥有所有权限',
2,
1,
1,
0,
NOW(),
NOW(),
'system',
'system'),
-- 普通管理员角色
(REPLACE(UUID(), '-', ''),
'普通管理员',
'ADMIN',
'系统管理员，拥有除系统管理外的所有权限',
2,
2,
1,
0,
NOW(),
NOW(),
'system',
'system');

-- 创建用户
INSERT INTO `sys_user` (`id`, `username`, `password`, `real_name`, `email`, `phone`, `status`, `flag`, `create_time`, `update_time`, `create_by`, `update_by`) 
VALUES 
-- 超级管理员用户
(REPLACE(UUID(), '-', ''),
'admin',
'$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', -- 密码：admin123
'系统超级管理员',
'admin@example.com',
'13800138000',
1,
0,
NOW(),
NOW(),
'system',
'system'),
-- 普通管理员用户
(REPLACE(UUID(), '-', ''),
'manager',
'$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', -- 密码：manager123
'系统管理员',
'manager@example.com',
'13800138001',
1,
0,
NOW(),
NOW(),
'system',
'system');

-- 建立用户角色关联
INSERT INTO `sys_user_role` (`id`, `user_id`, `role_id`, `create_time`, `create_by`) 
SELECT 
    REPLACE(UUID(), '-', ''),
    u.id,
    r.id,
    NOW(),
    'system'
FROM `sys_user` u, `sys_role` r 
WHERE (u.username = 'admin' AND r.role_code = 'SUPER_ADMIN')
   OR (u.username = 'manager' AND r.role_code = 'ADMIN');

-- 为超级管理员角色分配所有权限
INSERT INTO `sys_role_permission` (`id`, `role_id`, `permission_id`, `create_time`, `create_by`)
SELECT 
    REPLACE(UUID(), '-', ''),
    r.id,
    p.id,
    NOW(),
    'system'
FROM `sys_role` r, `sys_permission` p
WHERE r.role_code = 'SUPER_ADMIN';

-- 为普通管理员角色分配除系统管理外的所有权限
INSERT INTO `sys_role_permission` (`id`, `role_id`, `permission_id`, `create_time`, `create_by`)
SELECT 
    REPLACE(UUID(), '-', ''),
    r.id,
    p.id,
    NOW(),
    'system'
FROM `sys_role` r, `sys_permission` p
WHERE r.role_code = 'ADMIN'
AND p.permission_code NOT LIKE 'system%';

-- 初始化参数类型
-- 清空参数相关表
DELETE FROM `sys_param`;
DELETE FROM `sys_param_type`;

-- 插入参数类型
INSERT INTO `sys_param_type` (`id`, `type_code`, `type_name`, `sort_order`, `status`, `is_system`, `remark`, `create_time`, `update_time`, `create_by`, `update_by`) VALUES
-- 基础信息类
(REPLACE(UUID(), '-', ''), 'SEX', '性别', 1, 1, 1, '基础信息类参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'HIGHEST_EDU', '最高学历', 2, 1, 1, '基础信息类参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'HIGHEST_SCHOOL', '最高学历毕业学校', 3, 1, 1, '基础信息类参数', NOW(), NOW(), 'system', 'system'),
-- 人才相关
(REPLACE(UUID(), '-', ''), 'CLASSIFY_GRADE', '分类定级人才', 4, 1, 1, '人才相关参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'TITLE', '职称', 5, 1, 1, '人才相关参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'HIGHEST_POSITION', '最高技术职务资格', 6, 1, 1, '人才相关参数', NOW(), NOW(), 'system', 'system'),
-- 项目相关
(REPLACE(UUID(), '-', ''), 'PROJECT_TYPE', '项目类型', 7, 1, 1, '项目相关参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'PROJECT_STATUS', '项目当前状态', 8, 1, 1, '项目相关参数', NOW(), NOW(), 'system', 'system'),
-- 学科行业类
(REPLACE(UUID(), '-', ''), 'PRIMARY_SUBJECT', '学科大类', 9, 1, 1, '学科行业类参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'SUB_SUBJECT', '学科小类', 10, 1, 1, '学科行业类参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'INDUSTRY_CATEGORY', '行业门类', 11, 1, 1, '学科行业类参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'INDUSTRY_NAME', '行业名称', 12, 1, 1, '学科行业类参数', NOW(), NOW(), 'system', 'system'),
-- 企业相关
(REPLACE(UUID(), '-', ''), 'COMPANY_TYPE', '企业类型', 13, 1, 1, '企业相关参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'COMPANY_STATUS', '企业状态', 14, 1, 1, '企业相关参数', NOW(), NOW(), 'system', 'system'),
-- 资金相关
(REPLACE(UUID(), '-', ''), 'CURRENCY_CODE', '货币代码', 15, 1, 1, '资金相关参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'FUNDING_FROM', '资金来源组织', 16, 1, 1, '资金相关参数', NOW(), NOW(), 'system', 'system'),
(REPLACE(UUID(), '-', ''), 'FUNDING_NAME', '资金名称', 17, 1, 1, '资金相关参数', NOW(), NOW(), 'system', 'system');

-- 插入参数值
-- 性别参数值
INSERT INTO `sys_param` (`id`, `param_type`, `param_code`, `param_name`, `param_value`, `sort_order`, `status`, `is_system`, `remark`, `create_time`, `update_time`, `create_by`, `update_by`)
SELECT 
    REPLACE(UUID(), '-', ''),
    'SEX',
    'MALE',
    '男',
    '0',
    1,
    1,
    1,
    '性别-男',
    NOW(),
    NOW(),
    'system',
    'system'
UNION ALL
SELECT 
    REPLACE(UUID(), '-', ''),
    'SEX',
    'FEMALE',
    '女',
    '1',
    2,
    1,
    1,
    '性别-女',
    NOW(),
    NOW(),
    'system',
    'system';

-- 提交事务
COMMIT;

-- 恢复外键检查
SET FOREIGN_KEY_CHECKS = 1;