115 lines
2.4 KiB
JavaScript
115 lines
2.4 KiB
JavaScript
/**
|
|
* Animal 模型定义
|
|
* @file Animal.js
|
|
* @description 定义动物模型,用于数据库操作
|
|
*/
|
|
const { DataTypes } = require('sequelize');
|
|
const BaseModel = require('./BaseModel');
|
|
const { sequelize } = require('../config/database-simple');
|
|
|
|
/**
|
|
* 动物模型
|
|
* @typedef {Object} Animal
|
|
* @property {number} id - 动物唯一标识
|
|
* @property {string} type - 动物类型
|
|
* @property {number} count - 数量
|
|
* @property {number} farmId - 所属养殖场ID
|
|
* @property {Date} created_at - 创建时间
|
|
* @property {Date} updated_at - 更新时间
|
|
*/
|
|
class Animal extends BaseModel {
|
|
/**
|
|
* 获取动物所属的养殖场
|
|
* @returns {Promise<Object>} 养殖场信息
|
|
*/
|
|
async getFarm() {
|
|
return await this.getFarm();
|
|
}
|
|
|
|
/**
|
|
* 更新动物数量
|
|
* @param {Number} count 新数量
|
|
* @returns {Promise<Boolean>} 更新结果
|
|
*/
|
|
async updateCount(count) {
|
|
try {
|
|
if (count < 0) {
|
|
throw new Error('数量不能为负数');
|
|
}
|
|
|
|
this.count = count;
|
|
await this.save();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('更新动物数量失败:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 更新健康状态
|
|
* @param {String} status 新状态
|
|
* @returns {Promise<Boolean>} 更新结果
|
|
*/
|
|
async updateHealthStatus(status) {
|
|
try {
|
|
this.health_status = status;
|
|
await this.save();
|
|
return true;
|
|
} catch (error) {
|
|
console.error('更新健康状态失败:', error);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 初始化Animal模型
|
|
Animal.init({
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
primaryKey: true,
|
|
autoIncrement: true
|
|
},
|
|
type: {
|
|
type: DataTypes.STRING(50),
|
|
allowNull: false
|
|
},
|
|
count: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false,
|
|
defaultValue: 0
|
|
},
|
|
farm_id: {
|
|
type: DataTypes.INTEGER,
|
|
allowNull: false,
|
|
references: {
|
|
model: 'farms',
|
|
key: 'id'
|
|
}
|
|
},
|
|
health_status: {
|
|
type: DataTypes.ENUM('healthy', 'sick', 'quarantine', 'treatment'),
|
|
defaultValue: 'healthy'
|
|
},
|
|
last_inspection: {
|
|
type: DataTypes.DATE,
|
|
allowNull: true
|
|
},
|
|
notes: {
|
|
type: DataTypes.TEXT,
|
|
allowNull: true
|
|
}
|
|
}, {
|
|
sequelize,
|
|
tableName: 'animals',
|
|
modelName: 'Animal',
|
|
timestamps: true,
|
|
createdAt: 'created_at',
|
|
updatedAt: 'updated_at'
|
|
});
|
|
|
|
/**
|
|
* 导出动物模型
|
|
* @exports Animal
|
|
*/
|
|
module.exports = Animal; |