178 lines
5.1 KiB
JavaScript
178 lines
5.1 KiB
JavaScript
const DeviceWarning = require('../models/DeviceWarning');
|
|
const { Op, fn, col } = require('sequelize');
|
|
const sequelize = require('../config/database');
|
|
|
|
// 获取设备预警列表
|
|
exports.getDeviceWarnings = async (req, res) => {
|
|
try {
|
|
const { page = 1, pageSize = 20, deviceType, alertType, status, farmerName } = req.query;
|
|
const limit = parseInt(pageSize);
|
|
const offset = (parseInt(page) - 1) * limit;
|
|
|
|
const where = {};
|
|
if (deviceType) {
|
|
where.deviceType = deviceType;
|
|
}
|
|
if (alertType) {
|
|
where.alertType = alertType;
|
|
}
|
|
if (status) {
|
|
where.status = status;
|
|
}
|
|
if (farmerName) {
|
|
where.farmerName = { [Op.like]: `%${farmerName}%` };
|
|
}
|
|
|
|
const { count, rows } = await DeviceWarning.findAndCountAll({
|
|
where,
|
|
limit,
|
|
offset,
|
|
order: [['alertTime', 'DESC']],
|
|
});
|
|
|
|
res.status(200).json({
|
|
code: 200,
|
|
message: '获取成功',
|
|
data: {
|
|
list: rows,
|
|
total: count,
|
|
page: parseInt(page),
|
|
pageSize: limit,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error('获取设备预警列表失败:', error);
|
|
res.status(500).json({ code: 500, message: '获取设备预警列表失败', error: error.message });
|
|
}
|
|
};
|
|
|
|
// 根据ID获取单个设备预警详情
|
|
exports.getDeviceWarningById = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const warning = await DeviceWarning.findByPk(id);
|
|
|
|
if (!warning) {
|
|
return res.status(404).json({ code: 404, message: '设备预警未找到' });
|
|
}
|
|
|
|
res.status(200).json({ code: 200, message: '获取成功', data: warning });
|
|
} catch (error) {
|
|
console.error('获取设备预警详情失败:', error);
|
|
res.status(500).json({ code: 500, message: '获取设备预警详情失败', error: error.message });
|
|
}
|
|
};
|
|
|
|
// 创建新的设备预警
|
|
exports.createDeviceWarning = async (req, res) => {
|
|
try {
|
|
const newWarning = await DeviceWarning.create(req.body);
|
|
res.status(201).json({ code: 201, message: '创建成功', data: newWarning });
|
|
} catch (error) {
|
|
console.error('创建设备预警失败:', error);
|
|
res.status(500).json({ code: 500, message: '创建设备预警失败', error: error.message });
|
|
}
|
|
};
|
|
|
|
// 更新设备预警
|
|
exports.updateDeviceWarning = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const [updatedRows] = await DeviceWarning.update(req.body, {
|
|
where: { id },
|
|
});
|
|
|
|
if (updatedRows === 0) {
|
|
return res.status(404).json({ code: 404, message: '设备预警未找到或无更新' });
|
|
}
|
|
|
|
const updatedWarning = await DeviceWarning.findByPk(id);
|
|
res.status(200).json({ code: 200, message: '更新成功', data: updatedWarning });
|
|
} catch (error) {
|
|
console.error('更新设备预警失败:', error);
|
|
res.status(500).json({ code: 500, message: '更新设备预警失败', error: error.message });
|
|
}
|
|
};
|
|
|
|
// 删除设备预警
|
|
exports.deleteDeviceWarning = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const deletedRows = await DeviceWarning.destroy({
|
|
where: { id },
|
|
});
|
|
|
|
if (deletedRows === 0) {
|
|
return res.status(404).json({ code: 404, message: '设备预警未找到' });
|
|
}
|
|
|
|
res.status(200).json({ code: 200, message: '删除成功' });
|
|
} catch (error) {
|
|
console.error('删除设备预警失败:', error);
|
|
res.status(500).json({ code: 500, message: '删除设备预警失败', error: error.message });
|
|
}
|
|
};
|
|
|
|
// 更新预警状态
|
|
exports.updateWarningStatus = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { status, resolvedBy } = req.body;
|
|
|
|
const warning = await DeviceWarning.findByPk(id);
|
|
if (!warning) {
|
|
return res.status(404).json({ code: 404, message: '设备预警未找到' });
|
|
}
|
|
|
|
warning.status = status;
|
|
if (status === 'resolved') {
|
|
warning.resolvedBy = resolvedBy;
|
|
warning.resolvedAt = new Date();
|
|
}
|
|
await warning.save();
|
|
|
|
res.status(200).json({ code: 200, message: '状态更新成功', data: warning });
|
|
} catch (error) {
|
|
console.error('更新预警状态失败:', error);
|
|
res.status(500).json({ code: 500, message: '更新预警状态失败', error: error.message });
|
|
}
|
|
};
|
|
|
|
// 获取预警统计
|
|
exports.getWarningStats = async (req, res) => {
|
|
try {
|
|
// 分别查询每种设备类型的活跃预警数量
|
|
const earTagCount = await DeviceWarning.count({
|
|
where: {
|
|
deviceType: '智能耳标',
|
|
status: 'active'
|
|
}
|
|
});
|
|
|
|
const neckbandCount = await DeviceWarning.count({
|
|
where: {
|
|
deviceType: '智能项圈',
|
|
status: 'active'
|
|
}
|
|
});
|
|
|
|
const hostCount = await DeviceWarning.count({
|
|
where: {
|
|
deviceType: '智能主机',
|
|
status: 'active'
|
|
}
|
|
});
|
|
|
|
const result = {
|
|
earTag: earTagCount,
|
|
neckband: neckbandCount,
|
|
host: hostCount
|
|
};
|
|
|
|
res.status(200).json({ code: 200, message: '获取成功', data: result });
|
|
} catch (error) {
|
|
console.error('获取预警统计失败:', error);
|
|
res.status(500).json({ code: 500, message: '获取预警统计失败', error: error.message });
|
|
}
|
|
};
|