-- ============================================
-- 资金服务平台初始化数据
-- ============================================

USE fund_platform;

-- ============================================
-- 1. 初始化租户
-- ============================================
INSERT INTO sys_tenant (tenant_id, tenant_code, tenant_name, tenant_type, contact_name, contact_phone, contact_email, status, expire_time) VALUES
(1, 'SYSTEM', '系统管理租户', 1, '系统管理员', '13800138000', 'admin@fundplatform.com', 1, '2099-12-31 23:59:59'),
(2, 'DEMO', '演示租户', 1, '演示用户', '13800138001', 'demo@fundplatform.com', 1, '2099-12-31 23:59:59');

-- ============================================
-- 2. 初始化部门
-- ============================================
INSERT INTO sys_dept (dept_id, tenant_id, dept_name, parent_id, dept_code, dept_level, sort_order, leader, leader_phone, status, remark) VALUES
(1, 1, '总部', 0, 'HQ', 1, 1, '总经理', '13800138000', 1, '公司总部'),
(2, 1, '技术部', 1, 'TECH', 2, 1, '技术总监', '13800138001', 1, '负责技术研发'),
(3, 1, '财务部', 1, 'FINANCE', 2, 2, '财务总监', '13800138002', 1, '负责财务管理'),
(4, 1, '市场部', 1, 'MARKET', 2, 3, '市场总监', '13800138003', 1, '负责市场拓展'),
(5, 1, '前端开发组', 2, 'FE', 3, 1, '前端组长', '13800138004', 1, '前端开发'),
(6, 1, '后端开发组', 2, 'BE', 3, 2, '后端组长', '13800138005', 1, '后端开发'),
(7, 1, '测试组', 2, 'TEST', 3, 3, '测试组长', '13800138006', 1, '测试团队');

-- ============================================
-- 3. 初始化角色
-- ============================================
INSERT INTO sys_role (role_id, tenant_id, role_code, role_name, role_type, data_scope, sort_order, status, remark) VALUES
(1, 1, 'super_admin', '超级管理员', 'system', 'all', 1, 1, '系统超级管理员，拥有所有权限'),
(2, 1, 'tenant_admin', '租户管理员', 'system', 'all', 2, 1, '租户管理员，管理租户内所有资源'),
(3, 1, 'project_manager', '项目经理', 'custom', 'dept', 3, 1, '负责项目管理'),
(4, 1, 'finance_manager', '财务经理', 'custom', 'dept', 4, 1, '负责财务管理'),
(5, 1, 'developer', '开发人员', 'custom', 'self', 5, 1, '开发人员'),
(6, 1, 'tester', '测试人员', 'custom', 'self', 6, 1, '测试人员'),
(7, 1, 'sales', '销售人员', 'custom', 'self', 7, 1, '销售人员');

-- ============================================
-- 4. 初始化用户
-- 密码: admin123 (BCrypt加密: $2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO)
-- ============================================
INSERT INTO sys_user (user_id, tenant_id, username, password, real_name, gender, phone, email, dept_id, position, status) VALUES
(1, 1, 'admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO', '系统管理员', 1, '13800138000', 'admin@fundplatform.com', 1, '系统管理员', 1),
(2, 1, 'zhangsan', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO', '张三', 1, '13800138001', 'zhangsan@fundplatform.com', 2, '技术总监', 1),
(3, 1, 'lisi', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO', '李四', 1, '13800138002', 'lisi@fundplatform.com', 3, '财务总监', 1),
(4, 1, 'wangwu', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO', '王五', 1, '13800138003', 'wangwu@fundplatform.com', 4, '市场总监', 1),
(5, 1, 'dev01', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO', '开发一号', 1, '13800138004', 'dev01@fundplatform.com', 6, '后端开发', 1),
(6, 1, 'test01', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iAt6Z5EO', '测试一号', 2, '13800138005', 'test01@fundplatform.com', 7, '测试工程师', 1);

-- ============================================
-- 5. 初始化菜单
-- ============================================
INSERT INTO sys_menu (menu_id, tenant_id, menu_name, menu_type, parent_id, menu_level, icon, path, component, permission, sort_order, status) VALUES
-- 系统管理
(1, 0, '系统管理', 'dir', 0, 1, 'Setting', '/system', NULL, NULL, 1, 1),
(2, 0, '用户管理', 'menu', 1, 2, 'User', 'user', 'system/user/index', 'user:list', 1, 1),
(3, 0, '新增用户', 'button', 2, 3, NULL, NULL, NULL, 'user:add', 1, 1),
(4, 0, '编辑用户', 'button', 2, 3, NULL, NULL, NULL, 'user:edit', 2, 1),
(5, 0, '删除用户', 'button', 2, 3, NULL, NULL, NULL, 'user:delete', 3, 1),
(6, 0, '角色管理', 'menu', 1, 2, 'Role', 'role', 'system/role/index', 'role:list', 2, 1),
(7, 0, '新增角色', 'button', 6, 3, NULL, NULL, NULL, 'role:add', 1, 1),
(8, 0, '编辑角色', 'button', 6, 3, NULL, NULL, NULL, 'role:edit', 2, 1),
(9, 0, '删除角色', 'button', 6, 3, NULL, NULL, NULL, 'role:delete', 3, 1),
(10, 0, '菜单管理', 'menu', 1, 2, 'Menu', 'menu', 'system/menu/index', 'menu:list', 3, 1),
(11, 0, '部门管理', 'menu', 1, 2, 'Dept', 'dept', 'system/dept/index', 'dept:list', 4, 1),
(12, 0, '租户管理', 'menu', 1, 2, 'Tenant', 'tenant', 'system/tenant/index', 'tenant:list', 5, 1),
(13, 0, '操作日志', 'menu', 1, 2, 'Log', 'log', 'system/log/index', 'log:list', 6, 1),

-- 客户管理
(20, 0, '客户管理', 'dir', 0, 1, 'Customer', '/customer', NULL, NULL, 2, 1),
(21, 0, '客户列表', 'menu', 20, 2, 'CustomerList', 'customer', 'customer/customer/index', 'customer:list', 1, 1),
(22, 0, '新增客户', 'button', 21, 3, NULL, NULL, NULL, 'customer:add', 1, 1),
(23, 0, '编辑客户', 'button', 21, 3, NULL, NULL, NULL, 'customer:edit', 2, 1),
(24, 0, '删除客户', 'button', 21, 3, NULL, NULL, NULL, 'customer:delete', 3, 1),
(25, 0, '联系人管理', 'menu', 20, 2, 'Contact', 'contact', 'customer/contact/index', 'contact:list', 2, 1),

-- 项目管理
(30, 0, '项目管理', 'dir', 0, 1, 'Project', '/project', NULL, NULL, 3, 1),
(31, 0, '项目列表', 'menu', 30, 2, 'ProjectList', 'project', 'project/project/index', 'project:list', 1, 1),
(32, 0, '新增项目', 'button', 31, 3, NULL, NULL, NULL, 'project:add', 1, 1),
(33, 0, '编辑项目', 'button', 31, 3, NULL, NULL, NULL, 'project:edit', 2, 1),
(34, 0, '删除项目', 'button', 31, 3, NULL, NULL, NULL, 'project:delete', 3, 1),
(35, 0, '项目成员', 'menu', 30, 2, 'ProjectMember', 'member', 'project/member/index', 'member:list', 2, 1),

-- 需求工单
(40, 0, '需求工单', 'dir', 0, 1, 'Requirement', '/requirement', NULL, NULL, 4, 1),
(41, 0, '需求列表', 'menu', 40, 2, 'RequirementList', 'requirement', 'requirement/requirement/index', 'requirement:list', 1, 1),
(42, 0, '新增需求', 'button', 41, 3, NULL, NULL, NULL, 'requirement:add', 1, 1),
(43, 0, '编辑需求', 'button', 41, 3, NULL, NULL, NULL, 'requirement:edit', 2, 1),
(44, 0, '删除需求', 'button', 41, 3, NULL, NULL, NULL, 'requirement:delete', 3, 1),

-- 支出管理
(50, 0, '支出管理', 'dir', 0, 1, 'Expense', '/expense', NULL, NULL, 5, 1),
(51, 0, '支出类型', 'menu', 50, 2, 'ExpenseType', 'type', 'expense/type/index', 'expense-type:list', 1, 1),
(52, 0, '支出列表', 'menu', 50, 2, 'ExpenseList', 'expense', 'expense/expense/index', 'expense:list', 2, 1),
(53, 0, '新增支出', 'button', 52, 3, NULL, NULL, NULL, 'expense:add', 1, 1),
(54, 0, '编辑支出', 'button', 52, 3, NULL, NULL, NULL, 'expense:edit', 2, 1),

-- 应收款管理
(60, 0, '应收款管理', 'dir', 0, 1, 'Receivable', '/receivable', NULL, NULL, 6, 1),
(61, 0, '应收款列表', 'menu', 60, 2, 'ReceivableList', 'receivable', 'receivable/receivable/index', 'receivable:list', 1, 1),
(62, 0, '收款记录', 'menu', 60, 2, 'Receipt', 'receipt', 'receivable/receipt/index', 'receipt:list', 2, 1);

-- ============================================
-- 6. 初始化用户角色关联
-- ============================================
INSERT INTO sys_user_role (id, tenant_id, user_id, role_id) VALUES
(1, 1, 1, 1),  -- admin -> 超级管理员
(2, 1, 2, 2),  -- zhangsan -> 租户管理员
(3, 1, 3, 4),  -- lisi -> 财务经理
(4, 1, 4, 3),  -- wangwu -> 项目经理
(5, 1, 5, 5),  -- dev01 -> 开发人员
(6, 1, 6, 6);  -- test01 -> 测试人员

-- ============================================
-- 7. 初始化角色菜单关联（超级管理员拥有所有权限）
-- ============================================
INSERT INTO sys_role_menu (id, tenant_id, role_id, menu_id)
SELECT NULL, 1, 1, menu_id FROM sys_menu WHERE status = 1;

-- 租户管理员权限（除租户管理外的所有权限）
INSERT INTO sys_role_menu (id, tenant_id, role_id, menu_id)
SELECT NULL, 1, 2, menu_id FROM sys_menu WHERE status = 1 AND menu_id != 12;

-- 项目经理权限
INSERT INTO sys_role_menu (tenant_id, role_id, menu_id) VALUES
(1, 3, 30), (1, 3, 31), (1, 3, 32), (1, 3, 33), (1, 3, 34),
(1, 3, 35), (1, 3, 40), (1, 3, 41), (1, 3, 42), (1, 3, 43),
(1, 3, 44), (1, 3, 60), (1, 3, 61), (1, 3, 62);

-- 财务经理权限
INSERT INTO sys_role_menu (tenant_id, role_id, menu_id) VALUES
(1, 4, 50), (1, 4, 51), (1, 4, 52), (1, 4, 53), (1, 4, 54),
(1, 4, 60), (1, 4, 61), (1, 4, 62);

-- ============================================
-- 8. 初始化支出类型
-- ============================================
INSERT INTO expense_type (type_id, tenant_id, type_code, type_name, parent_id, type_level, sort_order, description, status) VALUES
(1, 1, 'personnel', '人力成本', 0, 1, 1, '人员相关支出', 1),
(2, 1, 'salary', '工资薪金', 1, 2, 1, '员工工资', 1),
(3, 1, 'bonus', '奖金福利', 1, 2, 2, '奖金和福利', 1),
(4, 1, 'social', '社保公积金', 1, 2, 3, '社保和公积金', 1),
(5, 1, 'operation', '运营成本', 0, 1, 2, '日常运营支出', 1),
(6, 1, 'rent', '房租物业', 5, 2, 1, '房租和物业费', 1),
(7, 1, 'utilities', '水电费', 5, 2, 2, '水电费', 1),
(8, 1, 'office', '办公用品', 5, 2, 3, '办公用品采购', 1),
(9, 1, 'project', '项目成本', 0, 1, 3, '项目相关支出', 1),
(10, 1, 'outsourcing', '外包费用', 9, 2, 1, '外包服务费', 1),
(11, 1, 'material', '材料费', 9, 2, 2, '项目材料费', 1),
(12, 1, 'travel', '差旅费', 9, 2, 3, '项目差旅费', 1);

-- ============================================
-- 9. 初始化客户数据
-- ============================================
INSERT INTO customer (customer_id, tenant_id, customer_code, customer_name, customer_short, customer_type, industry, scale, level, contact_name, contact_phone, email, address, status, cooperation_start) VALUES
(1, 1, 'CUST2024001', '北京科技有限公司', '北京科技', 'enterprise', '互联网', 'medium', 'A', '张总', '13800138001', 'ceo@bjtech.com', '北京市海淀区中关村大街1号', 1, '2024-01-01'),
(2, 1, 'CUST2024002', '上海金融投资有限公司', '上海金融', 'enterprise', '金融', 'large', 'A', '李总', '13800138002', 'ceo@shfinance.com', '上海市浦东新区陆家嘴环路1000号', 1, '2024-02-01'),
(3, 1, 'CUST2024003', '广州贸易发展有限公司', '广州贸易', 'enterprise', '贸易', 'small', 'B', '王总', '13800138003', 'ceo@gztrade.com', '广州市天河区珠江新城华夏路10号', 1, '2024-03-01');

-- ============================================
-- 10. 初始化项目数据
-- ============================================
INSERT INTO project (project_id, tenant_id, project_code, project_name, project_short, customer_id, project_type, project_manager_id, start_date, end_date, budget_amount, contract_amount, status, progress, description) VALUES
(1, 1, 'PROJ2024001', '北京科技ERP系统开发', 'ERP系统', 1, 'development', 4, '2024-01-15', '2024-06-30', 500000.00, 600000.00, 'ongoing', 60, '北京科技ERP管理系统开发项目'),
(2, 1, 'PROJ2024002', '上海金融数据分析平台', '数据分析平台', 2, 'development', 4, '2024-02-01', '2024-08-31', 800000.00, 1000000.00, 'ongoing', 40, '金融数据分析与可视化平台'),
(3, 1, 'PROJ2024003', '广州贸易官网建设', '官网建设', 3, 'development', 4, '2024-03-15', '2024-05-15', 100000.00, 120000.00, 'completed', 100, '企业官网设计与开发');

-- ============================================
-- 11. 初始化项目成员
-- ============================================
INSERT INTO project_member (member_id, tenant_id, project_id, user_id, role, join_date, workload, status) VALUES
(1, 1, 1, 4, 'pm', '2024-01-15', 50.00, 1),
(2, 1, 1, 5, 'dev', '2024-01-15', 100.00, 1),
(3, 1, 1, 6, 'test', '2024-02-01', 80.00, 1),
(4, 1, 2, 4, 'pm', '2024-02-01', 50.00, 1),
(5, 1, 2, 5, 'dev', '2024-02-01', 80.00, 1),
(6, 1, 3, 4, 'pm', '2024-03-15', 30.00, 1);
