Files
nxxmdata/backend/controllers/animalController.js
2025-08-25 15:00:46 +08:00

248 lines
6.0 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 animalController.js
* @description 处理动物相关的请求
*/
const { Animal, Farm } = require('../models');
/**
* 获取所有动物
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.getAllAnimals = async (req, res) => {
try {
const animals = await Animal.findAll({
include: [{ model: Farm, as: 'farm', attributes: ['id', 'name', 'location'] }]
});
res.status(200).json({
success: true,
data: animals
});
} catch (error) {
console.error('获取动物列表失败:', error);
res.status(500).json({
success: false,
message: '获取动物列表失败',
error: error.message
});
}
};
/**
* 获取单个动物
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.getAnimalById = async (req, res) => {
try {
const { id } = req.params;
const animal = await Animal.findByPk(id, {
include: [{ model: Farm, as: 'farm', attributes: ['id', 'name'] }]
});
if (!animal) {
return res.status(404).json({
success: false,
message: '动物不存在'
});
}
res.status(200).json({
success: true,
data: animal
});
} catch (error) {
console.error(`获取动物(ID: ${req.params.id})失败:`, error);
res.status(500).json({
success: false,
message: '获取动物详情失败',
error: error.message
});
}
};
/**
* 创建动物
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.createAnimal = async (req, res) => {
try {
const { type, count, farm_id, health_status, last_inspection, notes } = req.body;
// 验证必填字段
if (!type || !count || !farm_id) {
return res.status(400).json({
success: false,
message: '类型、数量和养殖场ID为必填项'
});
}
// 验证养殖场是否存在
const farm = await Farm.findByPk(farm_id);
if (!farm) {
return res.status(404).json({
success: false,
message: '指定的养殖场不存在'
});
}
const animal = await Animal.create({
type,
count,
farm_id,
health_status: health_status || 'healthy',
last_inspection: last_inspection || new Date(),
notes
});
res.status(201).json({
success: true,
message: '动物创建成功',
data: animal
});
} catch (error) {
console.error('创建动物失败:', error);
res.status(500).json({
success: false,
message: '创建动物失败',
error: error.message
});
}
};
/**
* 更新动物
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.updateAnimal = async (req, res) => {
try {
const { id } = req.params;
const { type, count, farm_id, health_status, last_inspection, notes } = req.body;
console.log('=== 动物更新请求 ===');
console.log('动物ID:', id);
console.log('请求数据:', { type, count, health_status, farm_id, last_inspection, notes });
const animal = await Animal.findByPk(id);
if (!animal) {
console.log('动物不存在, ID:', id);
return res.status(404).json({
success: false,
message: '动物不存在'
});
}
console.log('更新前的动物数据:', animal.toJSON());
// 如果更新了养殖场ID验证养殖场是否存在
if (farm_id && farm_id !== animal.farm_id) {
const farm = await Farm.findByPk(farm_id);
if (!farm) {
console.log('养殖场不存在, farm_id:', farm_id);
return res.status(404).json({
success: false,
message: '指定的养殖场不存在'
});
}
console.log('养殖场验证通过, farm_id:', farm_id);
}
console.log('准备更新动物数据...');
const updateResult = await animal.update({
type,
count,
farm_id,
health_status,
last_inspection,
notes
});
console.log('更新操作结果:', updateResult ? '成功' : '失败');
// 重新获取更新后的数据
await animal.reload();
console.log('更新后的动物数据:', animal.toJSON());
res.status(200).json({
success: true,
message: '动物更新成功',
data: animal
});
console.log('响应发送成功');
} catch (error) {
console.error(`更新动物(ID: ${req.params.id})失败:`, error);
res.status(500).json({
success: false,
message: '更新动物失败',
error: error.message
});
}
};
/**
* 删除动物
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.deleteAnimal = async (req, res) => {
try {
const { id } = req.params;
const animal = await Animal.findByPk(id);
if (!animal) {
return res.status(404).json({
success: false,
message: '动物不存在'
});
}
await animal.destroy();
res.status(200).json({
success: true,
message: '动物删除成功'
});
} catch (error) {
console.error(`删除动物(ID: ${req.params.id})失败:`, error);
res.status(500).json({
success: false,
message: '删除动物失败',
error: error.message
});
}
};
/**
* 按类型统计动物数量
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.getAnimalStatsByType = async (req, res) => {
try {
const { sequelize } = require('../config/database-simple');
const stats = await Animal.findAll({
attributes: [
'type',
[sequelize.fn('SUM', sequelize.col('count')), 'total']
],
group: ['type']
});
res.status(200).json({
success: true,
data: stats
});
} catch (error) {
console.error('获取动物类型统计失败:', error);
res.status(500).json({
success: false,
message: '获取动物类型统计失败',
error: error.message
});
}
};