From 2a269c2886dfd3e96f3febb50cf02c00626c5bf3 Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Sun, 16 Apr 2023 10:34:30 +0800 Subject: [PATCH] 123 --- src/controller/control.controller.js | 13 ++++++-- src/controller/device.controller.js | 13 ++++---- src/model/control.model.js | 5 +++ src/model/device.model.js | 5 +++ src/router/control.route.js | 4 ++- src/service/control.service.js | 63 ++++++++++++++++++++++++++++++++++-- src/service/device.service.js | 14 ++++++-- 7 files changed, 104 insertions(+), 13 deletions(-) diff --git a/src/controller/control.controller.js b/src/controller/control.controller.js index 38701d6..d19d2ea 100644 --- a/src/controller/control.controller.js +++ b/src/controller/control.controller.js @@ -1,18 +1,20 @@ const { getSettingByCoopId, changeSetting, + getControlInfoList, } = require("../service/control.service"); const Response = require("../utils/response"); class ControlController { async detail(ctx, next) { - const { coop_id } = ctx.request.query; - const res = await getSettingByCoopId(coop_id); + const { house_id, coop_id } = ctx.request.body; + const res = await getSettingByCoopId(house_id, coop_id); ctx.body = Response(0, "获取环境控制设定成功", res); } async change(ctx, next) { const { + house_id, coop_id, temperature_start, temperature_end, @@ -23,6 +25,7 @@ class ControlController { current_h2s, } = ctx.request.body; const res = await changeSetting( + house_id, coop_id, temperature_start, temperature_end, @@ -34,6 +37,12 @@ class ControlController { ); ctx.body = Response(0, "改变环境设定值成功", res); } + + async list(ctx, next) { + const { house_id, batch_number, coop_id } = ctx.request.body; + const res = await getControlInfoList(house_id, batch_number, coop_id); + ctx.body = Response(0, "查询智能控制列表成功", res); + } } module.exports = new ControlController(); diff --git a/src/controller/device.controller.js b/src/controller/device.controller.js index cf51807..595051d 100644 --- a/src/controller/device.controller.js +++ b/src/controller/device.controller.js @@ -6,20 +6,21 @@ const Response = require("../utils/response"); class DeviceController { async detail(ctx, next) { - const { coop_id } = ctx.request.query; - const res = await getStatusByCoopId(coop_id); + const { house_id, coop_id } = ctx.request.query; + const res = await getStatusByCoopId(house_id, coop_id); ctx.body = Response(0, "获取鸡舍设备状态成功", res); } async change(ctx, next) { - const { coop_id, egg_gathering, illumination, air_conditioner } = + const { house_id, coop_id, egg_gathering, illumination, air_conditioner } = ctx.request.body; - const res = await changeStatus( + const res = await changeStatus({ + house_id, coop_id, egg_gathering, illumination, - air_conditioner - ); + air_conditioner, + }); ctx.body = Response(0, "改变鸡舍设备状态成功", res); } } diff --git a/src/model/control.model.js b/src/model/control.model.js index 1c15cbc..388f699 100644 --- a/src/model/control.model.js +++ b/src/model/control.model.js @@ -88,6 +88,11 @@ const Control = seq.define("chicken_control", { defaultValue: 0, comment: "默认的h2s值", }, + house_id: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡场id", + }, coop_id: { type: DataTypes.INTEGER, allowNull: false, diff --git a/src/model/device.model.js b/src/model/device.model.js index dc8e2b3..8a49f37 100644 --- a/src/model/device.model.js +++ b/src/model/device.model.js @@ -28,6 +28,11 @@ const Device = seq.define("chicken_device", { allowNull: false, comment: "鸡舍id", }, + house_id: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡场id", + }, }); // 强制同步数据库(创建数据表) diff --git a/src/router/control.route.js b/src/router/control.route.js index c40ab68..eaa8ad3 100644 --- a/src/router/control.route.js +++ b/src/router/control.route.js @@ -3,10 +3,12 @@ const Router = require("koa-router"); const router = new Router({ prefix: "/control" }); const { auth } = require("../middleware/auth.middleware"); -const { detail, change } = require("../controller/control.controller"); +const { detail, change, list } = require("../controller/control.controller"); router.get("/detail", auth, detail); router.post("/change", auth, change); +router.post("/list", auth, list); + module.exports = router; diff --git a/src/service/control.service.js b/src/service/control.service.js index a81b592..31c6ad5 100644 --- a/src/service/control.service.js +++ b/src/service/control.service.js @@ -1,10 +1,69 @@ const Control = require("../model/control.model"); +const Chicken = require("../model/chicken.model"); +const Device = require("../model/device.model"); +const Coop = require("../model/coop.model"); +const { Op, Sequelize } = require("sequelize"); +const { getCoopListById } = require("./coop.service"); class ControlService { - async getSettingByCoopId(coop_id) { - return null; + async getSettingByCoopId(house_id, coop_id) { + const res = await Control.findOne({ + where: { + house_id, + coop_id, + }, + }); + return res ? res.dataValues : null; + } + + async getControlInfoList(house_id, batch_number = "", coop_ids) { + const res = await getCoopListById(house_id); + // 查询该舍的环控信息 + const p = res.map(async (item) => { + const coop_id = item.id; + const controlInfo = await Control.findOne({ + where: { + house_id, + coop_id, + }, + }); + item.controlInfo = controlInfo; + // 设备信息 + const deviceInfo = await Device.findOne({ + where: { + house_id, + coop_id, + }, + }); + item.deviceInfo = deviceInfo; + // 查询批次号和日龄 + const allChickenInfo = await Chicken.findAll({ + where: { + coop_id: { + [Op.like]: `%${coop_id}%`, + }, + batch_number: { + [Op.like]: `%${batch_number}%`, + }, + }, + }); + const singleArr = allChickenInfo?.filter((item) => + item.dataValues?.coop_id?.split(",").includes(coop_id + "") + ); + if (singleArr && singleArr.length > 0) { + item.chickenInfo = singleArr[0]; + } + return item; + }); + let real = await Promise.all(p); + if (coop_ids) { + const a = real.filter((item) => item.id == coop_ids); + return a; + } + return real; } async changeSetting( + house_id, coop_id, temperature_start, temperature_end, diff --git a/src/service/device.service.js b/src/service/device.service.js index b9269cd..7dc1b27 100644 --- a/src/service/device.service.js +++ b/src/service/device.service.js @@ -1,19 +1,27 @@ const Device = require("../model/device.model"); class DeviceService { - async getStatusByCoopId(coop_id) { + async getStatusByCoopId(house_id, coop_id) { const res = await Device.findOne({ where: { + house_id, coop_id, }, }); return res ? res.dataValues : null; } - async changeStatus(coop_id, egg_gathering, illumination, air_conditioner) { + async changeStatus({ + house_id, + coop_id, + egg_gathering, + illumination, + air_conditioner, + }) { // 先查询是否有 const deviceInfo = await Device.findOne({ where: { coop_id, + house_id, }, }); if (deviceInfo) { @@ -25,6 +33,7 @@ class DeviceService { }, { where: { + house_id, coop_id, }, } @@ -32,6 +41,7 @@ class DeviceService { return res; } else { const res = await Device.create({ + house_id, coop_id, egg_gathering, illumination,