const sequelize = require('./config/database'); const { DataTypes } = require('sequelize'); // 定义环境监测时刻表模型 const EnvironmentSchedule = sequelize.define('EnvironmentSchedule', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, farm_id: { type: DataTypes.INTEGER, allowNull: false, comment: '农场ID' }, device_id: { type: DataTypes.INTEGER, allowNull: false, comment: '设备ID' }, schedule_time: { type: DataTypes.TIME, allowNull: false, comment: '监测时刻(HH:MM:SS)' }, temperature: { type: DataTypes.DECIMAL(5, 2), allowNull: true, comment: '温度值(摄氏度)' }, humidity: { type: DataTypes.DECIMAL(5, 2), allowNull: true, comment: '湿度值(百分比)' }, monitoring_date: { type: DataTypes.DATEONLY, allowNull: false, defaultValue: DataTypes.NOW, comment: '监测日期' }, status: { type: DataTypes.ENUM('active', 'inactive', 'maintenance'), defaultValue: 'active', comment: '监测状态' }, notes: { type: DataTypes.TEXT, allowNull: true, comment: '备注信息' } }, { tableName: 'environment_schedules', timestamps: true, indexes: [ { fields: ['farm_id', 'monitoring_date', 'schedule_time'] }, { fields: ['device_id'] } ] }); async function createEnvironmentScheduleTable() { try { await sequelize.authenticate(); console.log('数据库连接成功'); // 创建表 await EnvironmentSchedule.sync({ force: true }); console.log('环境监测时刻表创建成功'); // 生成示例数据 const scheduleData = []; const today = new Date(); const schedules = [ '06:00:00', '08:00:00', '10:00:00', '12:00:00', '14:00:00', '16:00:00', '18:00:00', '20:00:00' ]; // 为过去7天生成数据 for (let day = 0; day < 7; day++) { const monitoringDate = new Date(today); monitoringDate.setDate(today.getDate() - day); schedules.forEach(time => { // 农场1的数据 scheduleData.push({ farm_id: 1, device_id: 1, schedule_time: time, temperature: (18 + Math.random() * 15).toFixed(2), // 18-33度 humidity: (45 + Math.random() * 35).toFixed(2), // 45-80% monitoring_date: monitoringDate.toISOString().split('T')[0], status: 'active', notes: `定时监测数据 - ${time}` }); // 农场2的数据(如果存在) scheduleData.push({ farm_id: 2, device_id: 2, schedule_time: time, temperature: (16 + Math.random() * 18).toFixed(2), // 16-34度 humidity: (40 + Math.random() * 40).toFixed(2), // 40-80% monitoring_date: monitoringDate.toISOString().split('T')[0], status: 'active', notes: `定时监测数据 - ${time}` }); }); } // 批量插入数据 await EnvironmentSchedule.bulkCreate(scheduleData); console.log(`成功插入 ${scheduleData.length} 条环境监测时刻数据`); // 验证数据 const totalCount = await EnvironmentSchedule.count(); console.log(`环境监测时刻表总记录数: ${totalCount}`); const todayCount = await EnvironmentSchedule.count({ where: { monitoring_date: today.toISOString().split('T')[0] } }); console.log(`今日监测记录数: ${todayCount}`); // 显示部分数据样例 const sampleData = await EnvironmentSchedule.findAll({ limit: 5, order: [['monitoring_date', 'DESC'], ['schedule_time', 'ASC']] }); console.log('\n数据样例:'); sampleData.forEach(record => { console.log(`日期: ${record.monitoring_date}, 时间: ${record.schedule_time}, 温度: ${record.temperature}°C, 湿度: ${record.humidity}%`); }); } catch (error) { console.error('创建环境监测时刻表失败:', error); } finally { await sequelize.close(); } } // 导出模型和创建函数 module.exports = { EnvironmentSchedule, createEnvironmentScheduleTable }; // 如果直接运行此文件,则执行创建操作 if (require.main === module) { createEnvironmentScheduleTable(); }