165 lines
6.2 KiB
JavaScript
165 lines
6.2 KiB
JavaScript
const { sequelize } = require('./config/database-simple');
|
|
const { QueryTypes } = require('sequelize');
|
|
|
|
async function importData() {
|
|
try {
|
|
console.log('开始导入数据...');
|
|
|
|
// 连接数据库
|
|
await sequelize.authenticate();
|
|
console.log('数据库连接成功');
|
|
|
|
// 1. 插入养殖场数据
|
|
const farmData = [
|
|
{
|
|
name: '东方养殖场',
|
|
type: 'pig',
|
|
location: JSON.stringify({ lat: 39.9042, lng: 116.4074, address: '北京市朝阳区' }),
|
|
address: '北京市朝阳区东三环北路',
|
|
contact: '张三',
|
|
phone: '13800138001',
|
|
status: 'active',
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
},
|
|
{
|
|
name: '西部牧场',
|
|
type: 'cattle',
|
|
location: JSON.stringify({ lat: 30.5728, lng: 104.0668, address: '四川省成都市' }),
|
|
address: '四川省成都市高新区天府大道',
|
|
contact: '李四',
|
|
phone: '13800138002',
|
|
status: 'active',
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
},
|
|
{
|
|
name: '南方羊场',
|
|
type: 'sheep',
|
|
location: JSON.stringify({ lat: 23.1291, lng: 113.2644, address: '广东省广州市' }),
|
|
address: '广东省广州市天河区珠江新城',
|
|
contact: '王五',
|
|
phone: '13800138003',
|
|
status: 'active',
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
}
|
|
];
|
|
|
|
await sequelize.query(
|
|
`INSERT INTO farms (name, type, location, address, contact, phone, status, created_at, updated_at) VALUES ${farmData.map(() => '(?, ?, ?, ?, ?, ?, ?, ?, ?)').join(', ')}`,
|
|
{
|
|
replacements: farmData.flatMap(farm => [
|
|
farm.name, farm.type, farm.location, farm.address, farm.contact,
|
|
farm.phone, farm.status, farm.created_at, farm.updated_at
|
|
]),
|
|
type: QueryTypes.INSERT
|
|
}
|
|
);
|
|
console.log('养殖场数据导入成功');
|
|
|
|
// 获取刚插入的养殖场ID
|
|
const farms = await sequelize.query('SELECT id, name, type FROM farms WHERE name IN ("东方养殖场", "西部牧场", "南方羊场")', { type: QueryTypes.SELECT });
|
|
console.log('获取到养殖场:', farms);
|
|
|
|
// 2. 插入动物数据
|
|
const animalData = [];
|
|
const animalTypes = {
|
|
'pig': ['猪', '母猪', '仔猪'],
|
|
'cattle': ['肉牛', '奶牛', '小牛'],
|
|
'sheep': ['山羊', '绵羊', '羔羊']
|
|
};
|
|
|
|
for (const farm of farms) {
|
|
const types = animalTypes[farm.type] || ['未知'];
|
|
for (const type of types) {
|
|
animalData.push({
|
|
type: type,
|
|
count: Math.floor(Math.random() * 100) + 50,
|
|
farm_id: farm.id,
|
|
health_status: Math.random() > 0.2 ? 'healthy' : 'sick',
|
|
last_inspection: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000),
|
|
notes: `${type}群体健康状况良好`,
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
});
|
|
}
|
|
}
|
|
|
|
if (animalData.length > 0) {
|
|
await sequelize.query(
|
|
`INSERT INTO animals (type, count, farm_id, health_status, last_inspection, notes, created_at, updated_at) VALUES ${animalData.map(() => '(?, ?, ?, ?, ?, ?, ?, ?)').join(', ')}`,
|
|
{
|
|
replacements: animalData.flatMap(animal => [
|
|
animal.type, animal.count, animal.farm_id, animal.health_status,
|
|
animal.last_inspection, animal.notes, animal.created_at, animal.updated_at
|
|
]),
|
|
type: QueryTypes.INSERT
|
|
}
|
|
);
|
|
console.log('动物数据导入成功');
|
|
}
|
|
|
|
// 3. 插入设备数据
|
|
const deviceData = [];
|
|
const deviceTypes = ['温度传感器', '湿度传感器', '摄像头', '喂食器', '饮水器'];
|
|
const deviceStatuses = ['online', 'offline', 'maintenance'];
|
|
|
|
for (const farm of farms) {
|
|
for (let i = 0; i < 5; i++) {
|
|
const type = deviceTypes[Math.floor(Math.random() * deviceTypes.length)];
|
|
const status = deviceStatuses[Math.floor(Math.random() * deviceStatuses.length)];
|
|
|
|
deviceData.push({
|
|
name: `${type}_${farm.name}_${String(i + 1).padStart(3, '0')}`,
|
|
type: type,
|
|
status: status,
|
|
farm_id: farm.id,
|
|
last_maintenance: new Date(Date.now() - Math.random() * 90 * 24 * 60 * 60 * 1000),
|
|
installation_date: new Date(Date.now() - Math.random() * 730 * 24 * 60 * 60 * 1000),
|
|
metrics: JSON.stringify({
|
|
temperature: Math.round((Math.random() * 15 + 15) * 10) / 10,
|
|
humidity: Math.round((Math.random() * 40 + 40) * 10) / 10,
|
|
battery: Math.round(Math.random() * 100),
|
|
signal_strength: Math.round(Math.random() * 100)
|
|
}),
|
|
created_at: new Date(),
|
|
updated_at: new Date()
|
|
});
|
|
}
|
|
}
|
|
|
|
if (deviceData.length > 0) {
|
|
await sequelize.query(
|
|
`INSERT INTO devices (name, type, status, farm_id, last_maintenance, installation_date, metrics, created_at, updated_at) VALUES ${deviceData.map(() => '(?, ?, ?, ?, ?, ?, ?, ?, ?)').join(', ')}`,
|
|
{
|
|
replacements: deviceData.flatMap(device => [
|
|
device.name, device.type, device.status, device.farm_id,
|
|
device.last_maintenance, device.installation_date, device.metrics,
|
|
device.created_at, device.updated_at
|
|
]),
|
|
type: QueryTypes.INSERT
|
|
}
|
|
);
|
|
console.log('设备数据导入成功');
|
|
}
|
|
|
|
// 4. 验证导入结果
|
|
const [farmCount] = await sequelize.query('SELECT COUNT(*) as count FROM farms', { type: QueryTypes.SELECT });
|
|
const [animalCount] = await sequelize.query('SELECT COUNT(*) as count FROM animals', { type: QueryTypes.SELECT });
|
|
const [deviceCount] = await sequelize.query('SELECT COUNT(*) as count FROM devices', { type: QueryTypes.SELECT });
|
|
|
|
console.log('\n=== 数据导入完成 ===');
|
|
console.log(`养殖场总数: ${farmCount.count}`);
|
|
console.log(`动物总数: ${animalCount.count}`);
|
|
console.log(`设备总数: ${deviceCount.count}`);
|
|
|
|
} catch (error) {
|
|
console.error('数据导入失败:', error.message);
|
|
console.error('详细错误:', error);
|
|
} finally {
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
importData(); |