176 lines
5.6 KiB
JavaScript
176 lines
5.6 KiB
JavaScript
const { sequelize } = require('../config/database');
|
|
const School = require('../models/School');
|
|
const Class = require('../models/Class');
|
|
|
|
/**
|
|
* Script kiểm tra dữ liệu đã import
|
|
* Chạy: node src/scripts/verify-schools.js
|
|
*/
|
|
|
|
async function verifyImportedData() {
|
|
try {
|
|
console.log('🔍 Kiểm tra dữ liệu đã import...\n');
|
|
|
|
// Kết nối database
|
|
await sequelize.authenticate();
|
|
console.log('✅ Kết nối database thành công\n');
|
|
|
|
// Đếm số lượng trường
|
|
const schoolCount = await School.count();
|
|
const activeSchools = await School.count({ where: { is_active: true } });
|
|
|
|
console.log('📊 THỐNG KÊ TRƯỜNG HỌC');
|
|
console.log('='.repeat(60));
|
|
console.log(`Tổng số trường: ${schoolCount}`);
|
|
console.log(`Trường đang hoạt động: ${activeSchools}`);
|
|
console.log('');
|
|
|
|
// Thống kê theo loại trường
|
|
const schoolTypes = await School.findAll({
|
|
attributes: [
|
|
'school_type',
|
|
[sequelize.fn('COUNT', sequelize.col('id')), 'count']
|
|
],
|
|
group: ['school_type']
|
|
});
|
|
|
|
console.log('📚 Phân loại trường:');
|
|
schoolTypes.forEach(type => {
|
|
const typeName = {
|
|
'preschool': 'Mầm non',
|
|
'primary': 'Tiểu học',
|
|
'secondary': 'Trung học cơ sở',
|
|
'high_school': 'Trung học phổ thông'
|
|
}[type.school_type] || type.school_type;
|
|
|
|
console.log(` ${typeName.padEnd(25)} ${type.get('count')}`);
|
|
});
|
|
console.log('');
|
|
|
|
// Thống kê theo quận/huyện
|
|
const districts = await School.findAll({
|
|
attributes: [
|
|
'district',
|
|
[sequelize.fn('COUNT', sequelize.col('id')), 'count']
|
|
],
|
|
group: ['district'],
|
|
order: [[sequelize.fn('COUNT', sequelize.col('id')), 'DESC']],
|
|
limit: 10
|
|
});
|
|
|
|
console.log('🏙️ Top 10 Quận/Huyện có nhiều trường nhất:');
|
|
districts.forEach((district, index) => {
|
|
console.log(` ${(index + 1 + '.').padEnd(4)} ${(district.district || 'N/A').padEnd(30)} ${district.get('count')}`);
|
|
});
|
|
console.log('');
|
|
|
|
// Đếm số lượng lớp
|
|
const classCount = await Class.count();
|
|
|
|
console.log('📊 THỐNG KÊ LỚP HỌC');
|
|
console.log('='.repeat(60));
|
|
console.log(`Tổng số lớp: ${classCount}`);
|
|
console.log('');
|
|
|
|
// Thống kê lớp theo khối
|
|
const gradeStats = await Class.findAll({
|
|
attributes: [
|
|
'grade_level',
|
|
[sequelize.fn('COUNT', sequelize.col('id')), 'count']
|
|
],
|
|
group: ['grade_level'],
|
|
order: ['grade_level']
|
|
});
|
|
|
|
console.log('📝 Phân bổ lớp theo khối:');
|
|
gradeStats.forEach(stat => {
|
|
const grade = stat.grade_level || 'N/A';
|
|
console.log(` Khối ${grade}:`.padEnd(15) + `${stat.get('count')} lớp`);
|
|
});
|
|
console.log('');
|
|
|
|
// Lấy 5 trường có nhiều lớp nhất
|
|
const topSchools = await sequelize.query(`
|
|
SELECT
|
|
s.school_name,
|
|
s.school_code,
|
|
COUNT(c.id) as class_count
|
|
FROM schools s
|
|
LEFT JOIN classes c ON s.id = c.school_id
|
|
GROUP BY s.id, s.school_name, s.school_code
|
|
ORDER BY class_count DESC
|
|
LIMIT 5
|
|
`, {
|
|
type: sequelize.QueryTypes.SELECT
|
|
});
|
|
|
|
console.log('🏆 Top 5 trường có nhiều lớp nhất:');
|
|
topSchools.forEach((school, index) => {
|
|
console.log(` ${index + 1}. ${school.school_name.padEnd(45)} ${school.class_count} lớp`);
|
|
});
|
|
console.log('');
|
|
|
|
// Chi tiết một trường mẫu
|
|
const sampleSchool = await School.findOne({
|
|
include: [{
|
|
model: Class,
|
|
as: 'classes',
|
|
required: true
|
|
}],
|
|
order: [[sequelize.literal('(SELECT COUNT(*) FROM classes WHERE school_id = schools.id)'), 'DESC']]
|
|
});
|
|
|
|
if (sampleSchool) {
|
|
console.log('📖 CHI TIẾT MỘT TRƯỜNG MẪU');
|
|
console.log('='.repeat(60));
|
|
console.log(`Tên trường: ${sampleSchool.school_name}`);
|
|
console.log(`Mã trường: ${sampleSchool.school_code}`);
|
|
console.log(`Loại: ${sampleSchool.school_type}`);
|
|
console.log(`Địa chỉ: ${sampleSchool.address}`);
|
|
console.log(`Quận/Huyện: ${sampleSchool.district}`);
|
|
console.log(`Số điện thoại: ${sampleSchool.phone || 'N/A'}`);
|
|
|
|
if (sampleSchool.classes && sampleSchool.classes.length > 0) {
|
|
console.log(`\nSố lớp: ${sampleSchool.classes.length}`);
|
|
console.log('\nDanh sách một số lớp:');
|
|
sampleSchool.classes.slice(0, 10).forEach(cls => {
|
|
console.log(` - ${cls.class_name} (Khối ${cls.grade_level})`);
|
|
});
|
|
if (sampleSchool.classes.length > 10) {
|
|
console.log(` ... và ${sampleSchool.classes.length - 10} lớp khác`);
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('✅ Kiểm tra hoàn tất!');
|
|
console.log('='.repeat(60) + '\n');
|
|
|
|
} catch (error) {
|
|
console.error('❌ Lỗi khi kiểm tra dữ liệu:', error);
|
|
throw error;
|
|
} finally {
|
|
await sequelize.close();
|
|
console.log('🔌 Đã đóng kết nối database');
|
|
}
|
|
}
|
|
|
|
// Setup associations
|
|
School.hasMany(Class, { foreignKey: 'school_id', as: 'classes' });
|
|
Class.belongsTo(School, { foreignKey: 'school_id', as: 'school' });
|
|
|
|
// Chạy script
|
|
if (require.main === module) {
|
|
verifyImportedData()
|
|
.then(() => {
|
|
console.log('\n🎉 Script hoàn thành!');
|
|
process.exit(0);
|
|
})
|
|
.catch((error) => {
|
|
console.error('\n💥 Script thất bại:', error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
module.exports = { verifyImportedData };
|