修改管理后台
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
* @description 处理数据统计相关的请求
|
||||
*/
|
||||
|
||||
const { Farm, Animal, Device, Alert, SensorData } = require('../models');
|
||||
const { Farm, Animal, Device, Alert, SensorData, IotCattle } = require('../models');
|
||||
const { sequelize } = require('../config/database-simple');
|
||||
const { Op } = require('sequelize');
|
||||
|
||||
@@ -20,7 +20,7 @@ exports.getAnimalCount = async (req, res) => {
|
||||
|
||||
// 执行精确的SQL查询统计动物总数
|
||||
const animalCountResult = await sequelize.query(
|
||||
'SELECT SUM(count) as total_animals FROM animals',
|
||||
'SELECT COUNT(*) as total_animals FROM iot_cattle',
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
raw: true
|
||||
@@ -29,7 +29,7 @@ exports.getAnimalCount = async (req, res) => {
|
||||
|
||||
// 获取按类型分组的动物数量
|
||||
const animalsByTypeResult = await sequelize.query(
|
||||
'SELECT type, SUM(count) as total_count FROM animals GROUP BY type',
|
||||
'SELECT cate as animal_type, COUNT(*) as total_count FROM iot_cattle GROUP BY cate',
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
raw: true
|
||||
@@ -38,7 +38,7 @@ exports.getAnimalCount = async (req, res) => {
|
||||
|
||||
// 获取按健康状态分组的动物数量
|
||||
const animalsByHealthResult = await sequelize.query(
|
||||
'SELECT health_status, SUM(count) as total_count FROM animals GROUP BY health_status',
|
||||
'SELECT level as health_status, COUNT(*) as total_count FROM iot_cattle GROUP BY level',
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
raw: true
|
||||
@@ -47,7 +47,7 @@ exports.getAnimalCount = async (req, res) => {
|
||||
|
||||
// 获取按农场分组的动物数量
|
||||
const animalsByFarmResult = await sequelize.query(
|
||||
'SELECT farm_id, SUM(count) as total_count FROM animals GROUP BY farm_id',
|
||||
'SELECT org_id as farm_id, COUNT(*) as total_count FROM iot_cattle GROUP BY org_id',
|
||||
{
|
||||
type: sequelize.QueryTypes.SELECT,
|
||||
raw: true
|
||||
@@ -256,7 +256,7 @@ exports.getDashboardStats = async (req, res) => {
|
||||
// 从数据库获取真实统计数据
|
||||
const [farmCount, animalCount, deviceCount, alertCount, onlineDeviceCount, alertsByLevel] = await Promise.all([
|
||||
Farm.count(),
|
||||
Animal.sum('count') || 0,
|
||||
IotCattle.count(), // 修改:使用IotCattle表而不是Animal表
|
||||
Device.count(),
|
||||
Alert.count(),
|
||||
Device.count({ where: { status: 'online' } }),
|
||||
@@ -398,33 +398,33 @@ exports.getAnimalStats = async (req, res) => {
|
||||
|
||||
// 从数据库获取真实动物统计数据
|
||||
const [totalAnimals, animalsByType, animalsByHealth] = await Promise.all([
|
||||
Animal.sum('count') || 0,
|
||||
Animal.findAll({
|
||||
IotCattle.count(),
|
||||
IotCattle.findAll({
|
||||
attributes: [
|
||||
'type',
|
||||
[sequelize.fn('SUM', sequelize.col('count')), 'total_count']
|
||||
'cate',
|
||||
[sequelize.fn('COUNT', sequelize.col('id')), 'total_count']
|
||||
],
|
||||
group: ['type'],
|
||||
group: ['cate'],
|
||||
raw: true
|
||||
}),
|
||||
Animal.findAll({
|
||||
IotCattle.findAll({
|
||||
attributes: [
|
||||
'health_status',
|
||||
[sequelize.fn('SUM', sequelize.col('count')), 'total_count']
|
||||
'level',
|
||||
[sequelize.fn('COUNT', sequelize.col('id')), 'total_count']
|
||||
],
|
||||
group: ['health_status'],
|
||||
group: ['level'],
|
||||
raw: true
|
||||
})
|
||||
]);
|
||||
|
||||
// 格式化数据
|
||||
const formattedAnimalsByType = animalsByType.map(item => ({
|
||||
type: item.type,
|
||||
type: item.cate,
|
||||
count: parseInt(item.total_count) || 0
|
||||
}));
|
||||
|
||||
const formattedAnimalsByHealth = animalsByHealth.map(item => ({
|
||||
health_status: item.health_status,
|
||||
health_status: item.level,
|
||||
count: parseInt(item.total_count) || 0
|
||||
}));
|
||||
|
||||
@@ -812,13 +812,13 @@ exports.getMonthlyTrends = async (req, res) => {
|
||||
}
|
||||
}
|
||||
}),
|
||||
Animal.sum('count', {
|
||||
IotCattle.count({
|
||||
where: {
|
||||
created_at: {
|
||||
[Op.lte]: endDate
|
||||
}
|
||||
}
|
||||
}) || 0,
|
||||
}),
|
||||
Device.count({
|
||||
where: {
|
||||
created_at: {
|
||||
|
||||
Reference in New Issue
Block a user