maochaoying 2 years ago
parent
commit
4327658dd0
  1. 9
      src/controller/environment.controller.js
  2. 77
      src/service/environment.service.js

9
src/controller/environment.controller.js

@ -39,8 +39,13 @@ class EnvironmentController {
} }
async statistics(ctx, next) { async statistics(ctx, next) {
const { coop_id, time_id, indicator_id } = ctx.request.body;
const res = await getEnvironmentHistoryList(coop_id, time_id, indicator_id);
const { coop_id, time_id, indicator_id, house_id } = ctx.request.body;
const res = await getEnvironmentHistoryList(
house_id,
coop_id,
time_id,
indicator_id
);
ctx.body = Response(0, "获取环境监测历史数据成功", res); ctx.body = Response(0, "获取环境监测历史数据成功", res);
} }
} }

77
src/service/environment.service.js

@ -49,8 +49,9 @@ class EnvironmentService {
// return null; // return null;
} }
async getEnvironmentHistoryList(coop_id, time_id, indicator_id) {
async getEnvironmentHistoryList(house_id, coop_id, time_id, indicator_id) {
let whereObj = { let whereObj = {
house_id,
coop_id, coop_id,
}; };
// 需要根据id进行筛选 7天、全部、24小时 // 需要根据id进行筛选 7天、全部、24小时
@ -75,17 +76,69 @@ class EnvironmentService {
where: whereObj, where: whereObj,
}); });
const arr = res.map((item) => item.dataValues); const arr = res.map((item) => item.dataValues);
// 根据传来的indicator_id 筛选出对应的数据
const temperatureList = arr.map((item) => item.temperature);
const humidityList = arr.map((item) => item.humidity);
const co2List = arr.map((item) => item.co2);
const nh3List = arr.map((item) => item.nh3);
const illuminationList = arr.map((item) => item.illumination);
const windSpeedList = arr.map((item) => item.wind_speed);
const h2sList = arr.map((item) => item.h2s);
const timeList = arr.map((item) =>
moment(item.log_time).format("YYYY-MM-DD HH:mm:ss")
);
// 对数据进行处理
let timeList = [];
let temperatureList = [];
let humidityList = [];
let co2List = [];
let nh3List = [];
let illuminationList = [];
let windSpeedList = [];
let h2sList = [];
if (time_id == "1") {
// 24小时的数据 需要展示该小时的平均数据 默认展示7条 前端可以滚动展示
let obj = {};
arr.map((item) => {
const hour = moment(item.log_time).format("HH");
if (obj[hour]) {
obj[hour] = [...obj[hour], item];
} else {
obj[hour] = [item];
}
});
timeList = Object.keys(obj)?.map((item) => item + ":00");
let dataObj = [];
Object.values(obj)?.map((item) => {
const len = item.length;
if (len != 0) {
let tempTotal = 0;
let humidityTotal = 0;
let co2Total = 0;
let nh3Total = 0;
let illuminationTotal = 0;
let windSpeedTotal = 0;
let h2sTotal = 0;
item.map((i) => {
tempTotal += parseInt(i.temperature);
humidityTotal += parseInt(i.humidity);
co2Total += parseInt(i.co2);
nh3Total += parseInt(i.nh3);
illuminationTotal += parseInt(i.illumination);
windSpeedTotal += parseInt(i.wind_speed);
h2sTotal += parseInt(i.h2s);
});
dataObj.push({
temperature: tempTotal / len,
humidity: humidityTotal / len,
co2: co2Total / len,
nh3: nh3Total / len,
illumination: illuminationTotal / len,
wind_speed: windSpeedTotal / len,
h2s: h2sTotal / len,
});
}
});
temperatureList = dataObj.map((item) => item.temperature);
humidityList = dataObj.map((item) => item.humidity);
co2List = dataObj.map((item) => item.co2);
nh3List = dataObj.map((item) => item.nh3);
illuminationList = dataObj.map((item) => item.illumination);
windSpeedList = dataObj.map((item) => item.wind_speed);
h2sList = dataObj.map((item) => item.h2s);
} else {
// 7天和全部的按照 每天进行汇总,计算出每天的平均并返回 默认展示7条 前端可以滚动展示
// TODO
}
// 根据所选监测数据进行筛选 // 根据所选监测数据进行筛选
if (indicator_id == "1") { if (indicator_id == "1") {
return { return {

Loading…
Cancel
Save