diff --git a/src/controller/environment.controller.js b/src/controller/environment.controller.js index 5eb675e..3e84a5b 100644 --- a/src/controller/environment.controller.js +++ b/src/controller/environment.controller.js @@ -51,8 +51,8 @@ class EnvironmentController { } async realtime(ctx, next) { - const { plies_number } = ctx.request.body; - const res = await getRealTimeData(plies_number); + const { house_id, coop_id, plies_number } = ctx.request.body; + const res = await getRealTimeData(house_id, coop_id, plies_number); ctx.body = Response(0, "获取环境监测实时数据成功", res); } } diff --git a/src/service/environment.service.js b/src/service/environment.service.js index 7008b4a..5877f69 100644 --- a/src/service/environment.service.js +++ b/src/service/environment.service.js @@ -276,10 +276,89 @@ class EnvironmentService { } // 获取环境实时监测数据 - async getRealTimeData(plies_number) { - const res = await Environment.findAll(); + async getRealTimeData(house_id, coop_id, plies_number) { + const res = await Environment.findAll({ + where: { + house_id, + coop_id, + }, + order: [["log_time", "DESC"]], + }); const arr = res?.map((item) => item.dataValues); - return arr; + let filterArr = []; + // 对arr进行筛选 将旧数据甩出 + arr.map((item) => { + let temp = filterArr.filter( + (it) => + it.position_index == item.position_index && + it.positionM == item.positionM + ); + if (temp && temp.length > 0) { + } else { + filterArr.push(item); + } + }); + console.log(filterArr); + let lastArr = []; + filterArr.map((item) => { + const position_index = item.position_index; + const pliesFlag = position_index.split("")[1]; + if (plies_number == "1") { + if (["1", "4"].includes(pliesFlag)) { + // 第一层 + const temp = lastArr.filter((i) => i.index == item.position_index); + if (temp && temp.length > 0) { + lastArr.map((itl) => { + if (itl.index == item.position_index) { + itl.data?.push(item); + } + }); + } else { + lastArr.push({ + index: item.position_index, + data: [item], + }); + } + } + } + if (plies_number == "2") { + if (["2", "5"].includes(pliesFlag)) { + // 第二层 + const temp = lastArr.filter((i) => i.index == item.position_index); + if (temp && temp.length > 0) { + lastArr.map((itl) => { + if (itl.index == item.position_index) { + itl.data?.push(item); + } + }); + } else { + lastArr.push({ + index: item.position_index, + data: [item], + }); + } + } + } + if (plies_number == "3") { + if (["3", "6"].includes(pliesFlag)) { + // 第三层 + const temp = lastArr.filter((i) => i.index == item.position_index); + if (temp && temp.length > 0) { + lastArr.map((itl) => { + if (itl.index == item.position_index) { + itl.data?.push(item); + } + }); + } else { + lastArr.push({ + index: item.position_index, + data: [item], + }); + } + } + } + }); + return lastArr; } }