Files
nxxmdata/backend/models/Animal.js

215 lines
4.5 KiB
JavaScript
Raw Normal View History

/**
2025-09-12 20:08:42 +08:00
* 动物信息模型
*/
2025-09-12 20:08:42 +08:00
const { DataTypes, Model } = require('sequelize');
const sequelize = require('../config/database');
2025-09-12 20:08:42 +08:00
class Animal extends Model {
// 获取动物类型文本
getAnimalTypeText() {
const typeMap = {
1: '牛',
2: '羊',
3: '猪',
4: '马'
};
return typeMap[this.animal_type] || '未知';
}
// 获取品种文本
getBreedText() {
const breedMap = {
1: '西藏高山牦牛',
2: '荷斯坦奶牛',
3: '西门塔尔牛',
4: '安格斯牛',
5: '小尾寒羊',
6: '波尔山羊'
};
return breedMap[this.breed] || '未知品种';
}
// 获取品类文本
getCategoryText() {
const categoryMap = {
1: '乳肉兼用',
2: '肉用',
3: '乳用',
4: '种用'
};
return categoryMap[this.category] || '未知品类';
}
2025-09-12 20:08:42 +08:00
// 获取来源类型文本
getSourceTypeText() {
const sourceMap = {
1: '合作社',
2: '农户',
3: '养殖场',
4: '进口'
};
return sourceMap[this.source_type] || '未知来源';
}
// 格式化出生日期
getBirthDateFormatted() {
if (this.birth_date) {
const date = new Date(this.birth_date);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).replace(/\//g, '-');
}
2025-09-12 20:08:42 +08:00
return '';
}
2025-09-12 20:08:42 +08:00
// 格式化入场日期
getEntryDateFormatted() {
if (this.entry_date) {
const date = new Date(this.entry_date);
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
}).replace(/\//g, '-');
}
2025-09-12 20:08:42 +08:00
return '';
}
}
Animal.init({
id: {
2025-09-12 20:08:42 +08:00
type: DataTypes.BIGINT,
primaryKey: true,
2025-09-12 20:08:42 +08:00
autoIncrement: true,
comment: '动物ID'
},
collar_number: {
type: DataTypes.STRING(50),
allowNull: false,
comment: '项圈编号'
},
2025-09-12 20:08:42 +08:00
ear_tag: {
type: DataTypes.STRING(50),
2025-09-12 20:08:42 +08:00
allowNull: true,
comment: '动物耳号'
},
2025-09-12 20:08:42 +08:00
animal_type: {
type: DataTypes.INTEGER,
allowNull: false,
2025-09-12 20:08:42 +08:00
defaultValue: 1,
comment: '动物类型1-牛2-羊3-猪4-马'
},
2025-09-12 20:08:42 +08:00
breed: {
type: DataTypes.INTEGER,
allowNull: false,
2025-09-12 20:08:42 +08:00
defaultValue: 1,
comment: '品种1-西藏高山牦牛2-荷斯坦奶牛3-西门塔尔牛4-安格斯牛5-小尾寒羊6-波尔山羊'
},
category: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '品类1-乳肉兼用2-肉用3-乳用4-种用'
},
2025-09-12 20:08:42 +08:00
source_type: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '来源类型1-合作社2-农户3-养殖场4-进口'
},
2025-09-12 20:08:42 +08:00
birth_date: {
type: DataTypes.DATE,
2025-09-12 20:08:42 +08:00
allowNull: true,
comment: '出生日期'
},
birth_weight: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00,
comment: '出生体重'
},
weaning_weight: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true,
defaultValue: 0.00,
comment: '断奶体重'
},
2025-09-12 20:08:42 +08:00
weaning_age: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 0,
comment: '断奶日龄'
},
entry_date: {
type: DataTypes.DATE,
allowNull: true,
comment: '入场日期'
},
calving_count: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: 0,
comment: '历史已产胎次'
},
left_teat_count: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '乳头数(左)'
},
right_teat_count: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '乳头数(右)'
},
farm_id: {
type: DataTypes.INTEGER,
allowNull: true,
comment: '农场ID'
},
status: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '状态1-正常2-生病3-死亡'
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
comment: '创建时间'
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
comment: '更新时间'
}
}, {
sequelize,
modelName: 'Animal',
2025-09-12 20:08:42 +08:00
tableName: 'animals',
timestamps: true,
createdAt: 'created_at',
2025-09-12 20:08:42 +08:00
updatedAt: 'updated_at',
indexes: [
{
fields: ['collar_number']
},
{
fields: ['ear_tag']
},
{
fields: ['farm_id']
}
]
});
module.exports = Animal;