110 lines
3.7 KiB
JavaScript
110 lines
3.7 KiB
JavaScript
const { Farm } = require('./models');
|
||
|
||
/**
|
||
* 验证farms数据导入结果
|
||
*/
|
||
async function verifyFarmsImport() {
|
||
try {
|
||
console.log('验证farms数据导入结果...');
|
||
|
||
// 获取所有farms数据
|
||
const farms = await Farm.findAll({
|
||
order: [['id', 'ASC']]
|
||
});
|
||
|
||
console.log(`\n✅ 数据库中共有 ${farms.length} 个农场`);
|
||
|
||
// 验证API静态数据(前3个)
|
||
const apiStaticFarms = farms.slice(0, 3);
|
||
console.log('\n📊 API静态数据验证:');
|
||
apiStaticFarms.forEach(farm => {
|
||
console.log(` ID: ${farm.id}, Name: ${farm.name}, Type: ${farm.type}, Address: ${farm.address}`);
|
||
});
|
||
|
||
// 验证种子数据(后8个)
|
||
const seedFarms = farms.slice(3);
|
||
console.log('\n🌱 种子数据验证:');
|
||
seedFarms.forEach(farm => {
|
||
console.log(` ID: ${farm.id}, Name: ${farm.name}, Type: ${farm.type}, Address: ${farm.address}`);
|
||
});
|
||
|
||
// 验证数据完整性
|
||
console.log('\n🔍 数据完整性检查:');
|
||
|
||
const missingFields = [];
|
||
farms.forEach(farm => {
|
||
if (!farm.name) missingFields.push(`Farm ${farm.id}: 缺少name字段`);
|
||
if (!farm.type) missingFields.push(`Farm ${farm.id}: 缺少type字段`);
|
||
if (!farm.location) missingFields.push(`Farm ${farm.id}: 缺少location字段`);
|
||
if (!farm.status) missingFields.push(`Farm ${farm.id}: 缺少status字段`);
|
||
});
|
||
|
||
if (missingFields.length === 0) {
|
||
console.log(' ✅ 所有农场数据字段完整');
|
||
} else {
|
||
console.log(' ❌ 发现缺失字段:');
|
||
missingFields.forEach(field => console.log(` ${field}`));
|
||
}
|
||
|
||
// 验证ID连续性
|
||
const ids = farms.map(farm => farm.id);
|
||
const expectedIds = Array.from({length: farms.length}, (_, i) => i + 1);
|
||
const idsMatch = JSON.stringify(ids) === JSON.stringify(expectedIds);
|
||
|
||
if (idsMatch) {
|
||
console.log(' ✅ ID序列连续 (1 到 ' + farms.length + ')');
|
||
} else {
|
||
console.log(' ❌ ID序列不连续');
|
||
console.log(` 实际ID: [${ids.join(', ')}]`);
|
||
console.log(` 期望ID: [${expectedIds.join(', ')}]`);
|
||
}
|
||
|
||
// 统计农场类型
|
||
const typeStats = {};
|
||
farms.forEach(farm => {
|
||
typeStats[farm.type] = (typeStats[farm.type] || 0) + 1;
|
||
});
|
||
|
||
console.log('\n📈 农场类型统计:');
|
||
Object.entries(typeStats).forEach(([type, count]) => {
|
||
console.log(` ${type}: ${count} 个`);
|
||
});
|
||
|
||
// 验证地理位置数据
|
||
console.log('\n🗺️ 地理位置数据验证:');
|
||
const locationErrors = [];
|
||
farms.forEach(farm => {
|
||
try {
|
||
if (typeof farm.location === 'string') {
|
||
const location = JSON.parse(farm.location);
|
||
if (!location.lat || !location.lng) {
|
||
locationErrors.push(`Farm ${farm.id} (${farm.name}): 缺少经纬度信息`);
|
||
}
|
||
} else if (typeof farm.location === 'object') {
|
||
if (!farm.location.lat || !farm.location.lng) {
|
||
locationErrors.push(`Farm ${farm.id} (${farm.name}): 缺少经纬度信息`);
|
||
}
|
||
} else {
|
||
locationErrors.push(`Farm ${farm.id} (${farm.name}): location字段格式错误`);
|
||
}
|
||
} catch (error) {
|
||
locationErrors.push(`Farm ${farm.id} (${farm.name}): location JSON解析失败`);
|
||
}
|
||
});
|
||
|
||
if (locationErrors.length === 0) {
|
||
console.log(' ✅ 所有农场地理位置数据有效');
|
||
} else {
|
||
console.log(' ❌ 发现地理位置数据问题:');
|
||
locationErrors.forEach(error => console.log(` ${error}`));
|
||
}
|
||
|
||
console.log('\n🎉 farms数据导入验证完成!');
|
||
|
||
} catch (error) {
|
||
console.error('❌ 验证失败:', error.message);
|
||
}
|
||
}
|
||
|
||
// 执行验证
|
||
verifyFarmsImport(); |