修改管理后台

This commit is contained in:
shenquanyi
2025-09-12 20:08:42 +08:00
parent 39d61c6f9b
commit 80a24c2d60
286 changed files with 75316 additions and 9452 deletions

View File

@@ -1,115 +1,215 @@
/**
* Animal 模型定义
* @file Animal.js
* @description 定义动物模型,用于数据库操作
* 动物信息模型
*/
const { DataTypes } = require('sequelize');
const BaseModel = require('./BaseModel');
const { sequelize } = require('../config/database-simple');
const { DataTypes, Model } = require('sequelize');
const sequelize = require('../config/database');
/**
* 动物模型
* @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();
class Animal extends Model {
// 获取动物类型文本
getAnimalTypeText() {
const typeMap = {
1: '牛',
2: '羊',
3: '猪',
4: '马'
};
return typeMap[this.animal_type] || '未知';
}
/**
* 更新动物数量
* @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;
}
// 获取品种文本
getBreedText() {
const breedMap = {
1: '西藏高山牦牛',
2: '荷斯坦奶牛',
3: '西门塔尔牛',
4: '安格斯牛',
5: '小尾寒羊',
6: '波尔山羊'
};
return breedMap[this.breed] || '未知品种';
}
/**
* 更新健康状态
* @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;
// 获取品类文本
getCategoryText() {
const categoryMap = {
1: '乳肉兼用',
2: '肉用',
3: '乳用',
4: '种用'
};
return categoryMap[this.category] || '未知品类';
}
// 获取来源类型文本
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, '-');
}
return '';
}
// 格式化入场日期
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, '-');
}
return '';
}
}
// 初始化Animal模型
Animal.init({
id: {
type: DataTypes.INTEGER,
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true
autoIncrement: true,
comment: '动物ID'
},
type: {
collar_number: {
type: DataTypes.STRING(50),
allowNull: false
allowNull: false,
comment: '项圈编号'
},
count: {
ear_tag: {
type: DataTypes.STRING(50),
allowNull: true,
comment: '动物耳号'
},
animal_type: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
defaultValue: 1,
comment: '动物类型1-牛2-羊3-猪4-马'
},
breed: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '品种1-西藏高山牦牛2-荷斯坦奶牛3-西门塔尔牛4-安格斯牛5-小尾寒羊6-波尔山羊'
},
category: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '品类1-乳肉兼用2-肉用3-乳用4-种用'
},
source_type: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 1,
comment: '来源类型1-合作社2-农户3-养殖场4-进口'
},
birth_date: {
type: DataTypes.DATE,
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: '断奶体重'
},
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,
references: {
model: 'farms',
key: 'id'
}
defaultValue: 1,
comment: '状态1-正常2-生病3-死亡'
},
health_status: {
type: DataTypes.ENUM('healthy', 'sick', 'quarantine', 'treatment'),
defaultValue: 'healthy'
},
last_inspection: {
created_at: {
type: DataTypes.DATE,
allowNull: true
allowNull: false,
defaultValue: DataTypes.NOW,
comment: '创建时间'
},
notes: {
type: DataTypes.TEXT,
allowNull: true
updated_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
comment: '更新时间'
}
}, {
sequelize,
tableName: 'animals',
modelName: 'Animal',
tableName: 'animals',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
updatedAt: 'updated_at',
indexes: [
{
fields: ['collar_number']
},
{
fields: ['ear_tag']
},
{
fields: ['farm_id']
}
]
});
/**
* 导出动物模型
* @exports Animal
*/
module.exports = Animal;