Files
nxxmdata/backend/controllers/farmController.js
shenquanyi 2bd1d8c032 buider
2025-08-27 15:36:36 +08:00

310 lines
7.1 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 farmController.js
* @description 处理养殖场相关的请求
*/
const { Farm, Animal, Device } = require('../models');
/**
* 获取所有养殖场
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.getAllFarms = async (req, res) => {
try {
const farms = await Farm.findAll({
include: [
{
model: Animal,
as: 'animals',
attributes: ['id', 'type', 'count', 'health_status']
},
{
model: Device,
as: 'devices',
attributes: ['id', 'name', 'type', 'status']
}
]
});
res.status(200).json({
success: true,
data: farms
});
} catch (error) {
console.error('获取养殖场列表失败:', error);
res.status(500).json({
success: false,
message: '获取养殖场列表失败',
error: error.message
});
}
};
/**
* 获取单个养殖场
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.getFarmById = async (req, res) => {
try {
const { id } = req.params;
const farm = await Farm.findByPk(id);
if (!farm) {
return res.status(404).json({
success: false,
message: '养殖场不存在'
});
}
res.status(200).json({
success: true,
data: farm
});
} 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.createFarm = async (req, res) => {
try {
const { name, type, owner, longitude, latitude, address, phone, area, capacity, status, description } = req.body;
// 验证必填字段
if (!name || !type) {
return res.status(400).json({
success: false,
message: '名称、类型和位置为必填项'
});
}
// 构建location对象
const location = {};
// 处理经度
if (longitude !== undefined && longitude !== null && longitude !== '') {
const lng = parseFloat(longitude);
if (!isNaN(lng)) {
location.lng = lng;
}
}
// 处理纬度
if (latitude !== undefined && latitude !== null && latitude !== '') {
const lat = parseFloat(latitude);
if (!isNaN(lat)) {
location.lat = lat;
}
}
// 验证location对象不能为空至少需要经纬度之一
if (Object.keys(location).length === 0) {
return res.status(400).json({
success: false,
message: '名称、类型和位置为必填项'
});
}
const farm = await Farm.create({
name,
type,
location,
address,
contact: owner,
phone,
status: status || 'active'
});
res.status(201).json({
success: true,
message: '养殖场创建成功',
data: farm
});
} catch (error) {
console.error('创建养殖场失败:', error);
res.status(500).json({
success: false,
message: '创建养殖场失败',
error: error.message
});
}
};
/**
* 更新养殖场
* @param {Object} req - 请求对象
* @param {Object} res - 响应对象
*/
exports.updateFarm = async (req, res) => {
try {
const { id } = req.params;
const { name, owner, longitude, latitude, address, phone, area, capacity, status, description } = req.body;
const farm = await Farm.findByPk(id);
if (!farm) {
return res.status(404).json({
success: false,
message: '养殖场不存在'
});
}
// 构建location对象 - 创建新对象以确保Sequelize检测到变化
const location = { ...(farm.location || {}) };
// 处理经度
if (longitude !== undefined) {
if (longitude !== null && longitude !== '') {
location.lng = parseFloat(longitude);
} else {
delete location.lng; // 清空经度
}
}
// 处理纬度
if (latitude !== undefined) {
if (latitude !== null && latitude !== '') {
location.lat = parseFloat(latitude);
} else {
delete location.lat; // 清空纬度
}
}
await farm.update({
name,
type: farm.type || 'farm',
location,
address,
contact: owner,
phone,
status: status || 'active'
});
res.status(200).json({
success: true,
message: '养殖场更新成功',
data: farm
});
} 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.deleteFarm = async (req, res) => {
try {
const { id } = req.params;
const farm = await Farm.findByPk(id);
if (!farm) {
return res.status(404).json({
success: false,
message: '养殖场不存在'
});
}
await farm.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.getFarmAnimals = async (req, res) => {
try {
const { id } = req.params;
const farm = await Farm.findByPk(id);
if (!farm) {
return res.status(404).json({
success: false,
message: '养殖场不存在'
});
}
const animals = await Animal.findAll({
where: { farm_id: id }
});
res.status(200).json({
success: true,
data: animals
});
} 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.getFarmDevices = async (req, res) => {
try {
const { id } = req.params;
const farm = await Farm.findByPk(id);
if (!farm) {
return res.status(404).json({
success: false,
message: '养殖场不存在'
});
}
const devices = await Device.findAll({
where: { farm_id: id }
});
res.status(200).json({
success: true,
data: devices
});
} catch (error) {
console.error(`获取养殖场(ID: ${req.params.id})的设备数据失败:`, error);
res.status(500).json({
success: false,
message: '获取养殖场设备数据失败',
error: error.message
});
}
};