Files
nxxmdata/backend/models/Farm.js
2025-08-25 15:00:46 +08:00

97 lines
2.0 KiB
JavaScript

/**
* Farm 模型定义
* @file Farm.js
* @description 定义养殖场模型,用于数据库操作
*/
const { DataTypes } = require('sequelize');
const BaseModel = require('./BaseModel');
const { sequelize } = require('../config/database-simple');
/**
* 养殖场模型
* @typedef {Object} Farm
* @property {number} id - 养殖场唯一标识
* @property {string} name - 养殖场名称
* @property {string} type - 养殖场类型
* @property {Object} location - 地理位置
* @property {number} location.lat - 纬度
* @property {number} location.lng - 经度
* @property {Date} created_at - 创建时间
* @property {Date} updated_at - 更新时间
*/
class Farm extends BaseModel {
/**
* 获取养殖场的所有动物
* @returns {Promise<Array>} 动物列表
*/
async getAnimals() {
return await this.getAnimals();
}
/**
* 获取养殖场的所有设备
* @returns {Promise<Array>} 设备列表
*/
async getDevices() {
return await this.getDevices();
}
/**
* 获取养殖场的所有预警
* @returns {Promise<Array>} 预警列表
*/
async getAlerts() {
return await this.getAlerts();
}
}
// 初始化Farm模型
Farm.init({
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(100),
allowNull: false
},
type: {
type: DataTypes.STRING(50),
allowNull: false
},
location: {
type: DataTypes.JSON,
allowNull: false,
defaultValue: {}
},
address: {
type: DataTypes.STRING(255),
allowNull: true
},
contact: {
type: DataTypes.STRING(50),
allowNull: true
},
phone: {
type: DataTypes.STRING(20),
allowNull: true
},
status: {
type: DataTypes.ENUM('active', 'inactive', 'maintenance'),
defaultValue: 'active'
}
}, {
sequelize,
tableName: 'farms',
modelName: 'Farm',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
});
/**
* 导出养殖场模型
* @exports Farm
*/
module.exports = Farm;