86 lines
2.7 KiB
JavaScript
86 lines
2.7 KiB
JavaScript
|
|
const sequelize = require('../config/database');
|
||
|
|
const Department = require('../models/Department');
|
||
|
|
const Position = require('../models/Position');
|
||
|
|
|
||
|
|
// 测试数据
|
||
|
|
const departments = [
|
||
|
|
{
|
||
|
|
name: '防疫部门',
|
||
|
|
description: '负责动物防疫工作的部门',
|
||
|
|
positions: [
|
||
|
|
{ name: '防疫员', has_permission: true, permission_details: { basic: ['view', 'edit'], advanced: ['delete'] } },
|
||
|
|
{ name: '防疫管理员', has_permission: true, permission_details: { basic: ['view', 'edit', 'add', 'delete'], advanced: ['admin'] } }
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: '检疫部门',
|
||
|
|
description: '负责动物检疫工作的部门',
|
||
|
|
positions: [
|
||
|
|
{ name: '检疫员', has_permission: false, permission_details: null }
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: '冷配管理部门',
|
||
|
|
description: '负责动物冷配管理工作的部门',
|
||
|
|
positions: [
|
||
|
|
{ name: '冷配员', has_permission: true, permission_details: { basic: ['view', 'edit'], advanced: [] } }
|
||
|
|
]
|
||
|
|
},
|
||
|
|
{
|
||
|
|
name: '测试部门',
|
||
|
|
description: '用于系统测试的部门',
|
||
|
|
positions: [
|
||
|
|
{ name: '内部测试账号', has_permission: true, permission_details: { basic: ['view', 'edit', 'add', 'delete'], advanced: ['admin', 'debug'] } }
|
||
|
|
]
|
||
|
|
}
|
||
|
|
];
|
||
|
|
|
||
|
|
// 初始化数据库表并插入测试数据
|
||
|
|
async function seedDatabase() {
|
||
|
|
try {
|
||
|
|
console.log('开始同步数据库表结构...');
|
||
|
|
// 同步数据库表结构
|
||
|
|
await sequelize.sync({ alter: true });
|
||
|
|
console.log('数据库表结构同步完成!');
|
||
|
|
|
||
|
|
console.log('开始插入测试数据...');
|
||
|
|
|
||
|
|
// 逐个创建部门及其岗位
|
||
|
|
for (const deptData of departments) {
|
||
|
|
// 创建部门
|
||
|
|
const department = await Department.create({
|
||
|
|
name: deptData.name,
|
||
|
|
description: deptData.description,
|
||
|
|
created_by: 1,
|
||
|
|
updated_by: 1
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log(`已创建部门: ${department.name}`);
|
||
|
|
|
||
|
|
// 创建该部门下的岗位
|
||
|
|
for (const posData of deptData.positions) {
|
||
|
|
const position = await Position.create({
|
||
|
|
department_id: department.id,
|
||
|
|
name: posData.name,
|
||
|
|
has_permission: posData.has_permission,
|
||
|
|
permission_details: posData.permission_details,
|
||
|
|
created_by: 1,
|
||
|
|
updated_by: 1
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log(` - 已创建岗位: ${position.name} (权限: ${position.has_permission ? '已设置' : '未设置'})`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('测试数据插入完成!');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('数据库初始化失败:', error);
|
||
|
|
process.exit(1);
|
||
|
|
} finally {
|
||
|
|
// 关闭数据库连接
|
||
|
|
await sequelize.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 执行播种函数
|
||
|
|
seedDatabase();
|