This commit is contained in:
shenquanyi
2025-08-27 15:36:36 +08:00
parent ec72c6a8b5
commit 2bd1d8c032
100 changed files with 25780 additions and 20 deletions

View File

@@ -79,10 +79,37 @@ exports.getFarmById = async (req, res) => {
*/
exports.createFarm = async (req, res) => {
try {
const { name, type, location, address, contact, phone, status } = req.body;
const { name, type, owner, longitude, latitude, address, phone, area, capacity, status, description } = req.body;
// 验证必填字段
if (!name || !type || !location) {
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: '名称、类型和位置为必填项'
@@ -94,9 +121,9 @@ exports.createFarm = async (req, res) => {
type,
location,
address,
contact,
contact: owner,
phone,
status
status: status || 'active'
});
res.status(201).json({
@@ -122,7 +149,7 @@ exports.createFarm = async (req, res) => {
exports.updateFarm = async (req, res) => {
try {
const { id } = req.params;
const { name, type, location, address, contact, phone, status } = req.body;
const { name, owner, longitude, latitude, address, phone, area, capacity, status, description } = req.body;
const farm = await Farm.findByPk(id);
@@ -133,14 +160,35 @@ exports.updateFarm = async (req, res) => {
});
}
// 构建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,
type: farm.type || 'farm',
location,
address,
contact,
contact: owner,
phone,
status
status: status || 'active'
});
res.status(200).json({