92 lines
2.7 KiB
JavaScript
92 lines
2.7 KiB
JavaScript
|
|
const sequelize = require('../config/database');
|
||
|
|
const SmartCollar = require('../models/SmartCollar');
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 初始化智能项圈表并添加测试数据
|
||
|
|
*/
|
||
|
|
async function initSmartCollarTable() {
|
||
|
|
try {
|
||
|
|
console.log('开始初始化智能项圈表...');
|
||
|
|
|
||
|
|
// 同步模型到数据库(创建表)
|
||
|
|
await SmartCollar.sync({
|
||
|
|
alter: true, // 这会修改已存在的表结构以匹配模型定义
|
||
|
|
force: false // 设为 true 会删除现有表并重新创建,但会丢失现有数据
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('智能项圈表同步成功');
|
||
|
|
|
||
|
|
// 检查是否已有测试数据
|
||
|
|
const existingCount = await SmartCollar.count();
|
||
|
|
if (existingCount === 0) {
|
||
|
|
console.log('开始添加测试数据...');
|
||
|
|
|
||
|
|
// 测试数据
|
||
|
|
const testData = [
|
||
|
|
{
|
||
|
|
collar_id: 'CL001',
|
||
|
|
name: '智能项圈001',
|
||
|
|
status: 'active',
|
||
|
|
battery: 85,
|
||
|
|
remark: '用于示范的项圈',
|
||
|
|
created_at: new Date('2023-09-15 10:00:00'),
|
||
|
|
updated_at: new Date('2023-09-20 14:30:00')
|
||
|
|
},
|
||
|
|
{
|
||
|
|
collar_id: 'CL002',
|
||
|
|
name: '智能项圈002',
|
||
|
|
status: 'inactive',
|
||
|
|
battery: 100,
|
||
|
|
remark: '',
|
||
|
|
created_at: new Date('2023-09-16 11:20:00'),
|
||
|
|
updated_at: new Date('2023-09-16 11:20:00')
|
||
|
|
},
|
||
|
|
{
|
||
|
|
collar_id: 'CL003',
|
||
|
|
name: '智能项圈003',
|
||
|
|
status: 'maintenance',
|
||
|
|
battery: 20,
|
||
|
|
remark: '电池需要更换',
|
||
|
|
created_at: new Date('2023-09-10 09:15:00'),
|
||
|
|
updated_at: new Date('2023-09-21 16:45:00')
|
||
|
|
},
|
||
|
|
{
|
||
|
|
collar_id: 'CL004',
|
||
|
|
name: '智能项圈004',
|
||
|
|
status: 'active',
|
||
|
|
battery: 90,
|
||
|
|
remark: '养殖场A区',
|
||
|
|
created_at: new Date('2023-09-05 14:20:00'),
|
||
|
|
updated_at: new Date('2023-09-18 09:30:00')
|
||
|
|
},
|
||
|
|
{
|
||
|
|
collar_id: 'CL005',
|
||
|
|
name: '智能项圈005',
|
||
|
|
status: 'active',
|
||
|
|
battery: 75,
|
||
|
|
remark: '养殖场B区',
|
||
|
|
created_at: new Date('2023-09-08 16:10:00'),
|
||
|
|
updated_at: new Date('2023-09-19 11:45:00')
|
||
|
|
}
|
||
|
|
];
|
||
|
|
|
||
|
|
// 批量创建测试数据
|
||
|
|
await SmartCollar.bulkCreate(testData);
|
||
|
|
|
||
|
|
console.log('测试数据添加成功');
|
||
|
|
} else {
|
||
|
|
console.log(`智能项圈表已有 ${existingCount} 条数据,跳过添加测试数据`);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log('智能项圈表初始化完成');
|
||
|
|
} catch (error) {
|
||
|
|
console.error('智能项圈表初始化失败:', error);
|
||
|
|
process.exit(1);
|
||
|
|
} finally {
|
||
|
|
// 关闭数据库连接
|
||
|
|
await sequelize.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 执行初始化
|
||
|
|
initSmartCollarTable();
|