Files
nxxmdata/backend/models/Pen.js
2025-09-12 20:08:42 +08:00

179 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 栏舍模型
* @file Pen.js
* @description 栏舍信息数据模型
*/
const { DataTypes, Model } = require('sequelize');
const sequelize = require('../config/database');
class Pen extends Model {
// 获取动物类型文本
getAnimalTypeText() {
return this.animal_type || '未知';
}
// 获取状态文本
getStatusText() {
return this.status ? '开启' : '关闭';
}
// 获取容量使用率(如果有当前动物数量的话)
getCapacityUsageRate() {
// 这里可以根据实际业务需求计算
return 0;
}
// 检查容量是否足够
isCapacitySufficient(requiredCapacity) {
return (this.capacity - this.getCurrentAnimalCount()) >= requiredCapacity;
}
// 获取当前动物数量(需要根据实际业务实现)
getCurrentAnimalCount() {
// 这里应该查询当前栏舍中的动物数量
// 暂时返回0实际实现时需要关联查询
return 0;
}
}
Pen.init({
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
comment: '栏舍ID'
},
name: {
type: DataTypes.STRING(50),
allowNull: false,
comment: '栏舍名称',
validate: {
notEmpty: {
msg: '栏舍名称不能为空'
},
len: {
args: [1, 50],
msg: '栏舍名称长度应在1-50个字符之间'
}
}
},
animal_type: {
type: DataTypes.ENUM('马', '牛', '羊', '家禽', '猪'),
allowNull: false,
comment: '动物类型',
validate: {
notEmpty: {
msg: '动物类型不能为空'
},
isIn: {
args: [['马', '牛', '羊', '家禽', '猪']],
msg: '动物类型必须是:马、牛、羊、家禽、猪中的一个'
}
}
},
pen_type: {
type: DataTypes.STRING(50),
allowNull: true,
comment: '栏舍类型',
validate: {
len: {
args: [0, 50],
msg: '栏舍类型长度不能超过50个字符'
}
}
},
responsible: {
type: DataTypes.STRING(20),
allowNull: false,
comment: '负责人',
validate: {
notEmpty: {
msg: '负责人不能为空'
},
len: {
args: [1, 20],
msg: '负责人姓名长度应在1-20个字符之间'
}
}
},
capacity: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '容量',
validate: {
min: {
args: 1,
msg: '容量不能小于1'
},
max: {
args: 10000,
msg: '容量不能超过10000'
},
isInt: {
msg: '容量必须是整数'
}
}
},
status: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: true,
comment: '状态true-开启false-关闭'
},
description: {
type: DataTypes.TEXT,
allowNull: true,
comment: '备注信息',
validate: {
len: {
args: [0, 1000],
msg: '备注信息长度不能超过1000个字符'
}
}
},
farm_id: {
type: DataTypes.BIGINT,
allowNull: true,
comment: '所属农场ID',
references: {
model: 'farms',
key: 'id'
}
},
creator: {
type: DataTypes.STRING(50),
allowNull: false,
defaultValue: 'admin',
comment: '创建人'
}
}, {
sequelize,
modelName: 'Pen',
tableName: 'pens',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
comment: '栏舍管理表',
indexes: [
{
fields: ['name']
},
{
fields: ['animal_type']
},
{
fields: ['farm_id']
},
{
fields: ['status']
},
{
fields: ['created_at']
}
]
});
module.exports = Pen;