|
@ -4,16 +4,31 @@ const moment = require("moment"); |
|
|
const { Op } = require("sequelize"); |
|
|
const { Op } = require("sequelize"); |
|
|
class EnvironmentService { |
|
|
class EnvironmentService { |
|
|
async getEnvironmentByCoopId(house_id, coop_id) { |
|
|
async getEnvironmentByCoopId(house_id, coop_id) { |
|
|
|
|
|
// 目前没有栋舍概念 暂且注释,取平均值
|
|
|
const res = await Environment.findAll({ |
|
|
const res = await Environment.findAll({ |
|
|
where: { |
|
|
|
|
|
coop_id, |
|
|
|
|
|
house_id, |
|
|
|
|
|
}, |
|
|
|
|
|
order: [["log_time", "DESC"]], |
|
|
|
|
|
|
|
|
// where: {
|
|
|
|
|
|
// coop_id,
|
|
|
|
|
|
// house_id,
|
|
|
|
|
|
// },
|
|
|
|
|
|
// order: [["log_time", "DESC"]],
|
|
|
}); |
|
|
}); |
|
|
const arr = res.map((item) => item.dataValues); |
|
|
const arr = res.map((item) => item.dataValues); |
|
|
if (arr && arr.length > 0) { |
|
|
if (arr && arr.length > 0) { |
|
|
return arr[0]; |
|
|
|
|
|
|
|
|
const len = arr.length; |
|
|
|
|
|
let tempTotal = 0; |
|
|
|
|
|
let humidityTotal = 0; |
|
|
|
|
|
let co2Total = 0; |
|
|
|
|
|
arr.map((i) => { |
|
|
|
|
|
tempTotal += parseInt(i.temperature); |
|
|
|
|
|
humidityTotal += parseInt(i.humidity); |
|
|
|
|
|
co2Total += parseInt(i.co2); |
|
|
|
|
|
}); |
|
|
|
|
|
return { |
|
|
|
|
|
...arr[0], |
|
|
|
|
|
temperature: (tempTotal / len).toFixed(2), |
|
|
|
|
|
humidity: (humidityTotal / len).toFixed(2), |
|
|
|
|
|
co2: (co2Total / len).toFixed(2), |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
@ -51,8 +66,8 @@ class EnvironmentService { |
|
|
|
|
|
|
|
|
async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) { |
|
|
async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) { |
|
|
let whereObj = { |
|
|
let whereObj = { |
|
|
house_id, |
|
|
|
|
|
coop_id, |
|
|
|
|
|
|
|
|
// house_id,
|
|
|
|
|
|
// coop_id,
|
|
|
}; |
|
|
}; |
|
|
// 需要根据id进行筛选 7天、全部、24小时
|
|
|
// 需要根据id进行筛选 7天、全部、24小时
|
|
|
if (time_id == "1") { |
|
|
if (time_id == "1") { |
|
@ -240,30 +255,6 @@ class EnvironmentService { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
const p = env.map(async (item) => { |
|
|
const p = env.map(async (item) => { |
|
|
// 首先查询 position_index positionM 有没有数据 有则更新 无则创建
|
|
|
|
|
|
const envInfo = await Environment.findOne({ |
|
|
|
|
|
where: { |
|
|
|
|
|
position_index: item.position_index, |
|
|
|
|
|
positionM, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
if (envInfo) { |
|
|
|
|
|
const res = await Environment.update( |
|
|
|
|
|
{ |
|
|
|
|
|
temperature: item.temperature, |
|
|
|
|
|
humidity: item.humidity, |
|
|
|
|
|
co2: item.co2, |
|
|
|
|
|
log_time, |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
where: { |
|
|
|
|
|
position_index: item.position_index, |
|
|
|
|
|
positionM, |
|
|
|
|
|
}, |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
return res; |
|
|
|
|
|
} else { |
|
|
|
|
|
const res = await Environment.create({ |
|
|
const res = await Environment.create({ |
|
|
house_id: 0, |
|
|
house_id: 0, |
|
|
coop_id: 0, |
|
|
coop_id: 0, |
|
@ -279,7 +270,6 @@ class EnvironmentService { |
|
|
log_time, |
|
|
log_time, |
|
|
}); |
|
|
}); |
|
|
return res; |
|
|
return res; |
|
|
} |
|
|
|
|
|
}); |
|
|
}); |
|
|
const real = await Promise.all(p); |
|
|
const real = await Promise.all(p); |
|
|
return real; |
|
|
return real; |
|
|