From bc79af2e82fe0ad74c5e4b57a16d0cb069f4c348 Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Fri, 31 Mar 2023 10:07:07 +0800 Subject: [PATCH] 123 --- src/controller/chicken.controller.js | 8 ++++ src/controller/device.controller.js | 27 +++++++++++ src/model/control.model.js | 92 ++++++++++++++++++++++++++++++++++++ src/model/device.model.js | 36 ++++++++++++++ src/router/chicken.route.js | 3 ++ src/router/device.route.js | 12 +++++ src/service/chicken.service.js | 22 +++++++++ src/service/device.service.js | 45 ++++++++++++++++++ src/service/feeding.service.js | 14 ++++++ 9 files changed, 259 insertions(+) create mode 100644 src/controller/device.controller.js create mode 100644 src/model/control.model.js create mode 100644 src/model/device.model.js create mode 100644 src/router/device.route.js create mode 100644 src/service/device.service.js diff --git a/src/controller/chicken.controller.js b/src/controller/chicken.controller.js index 2181e42..72c78af 100644 --- a/src/controller/chicken.controller.js +++ b/src/controller/chicken.controller.js @@ -4,6 +4,7 @@ const { getAllChickenInfo, getChickenInfoByHouseId, getBatchNumbersByHouseId, + getFeedConversionRatio, } = require("../service/chicken.service"); const Response = require("../utils/response"); @@ -63,6 +64,13 @@ class ChickenController { const res = await getBatchNumbersByHouseId(house_id); ctx.body = Response(0, "查询批次号简易信息成功", res); } + + // 获取料肉比 + async ratio(ctx, next) { + const { house_id } = ctx.request.query; + const res = await getFeedConversionRatio(house_id); + ctx.body = Response(0, "查询料肉比成功", res); + } } module.exports = new ChickenController(); diff --git a/src/controller/device.controller.js b/src/controller/device.controller.js new file mode 100644 index 0000000..cf51807 --- /dev/null +++ b/src/controller/device.controller.js @@ -0,0 +1,27 @@ +const { + getStatusByCoopId, + changeStatus, +} = require("../service/device.service"); +const Response = require("../utils/response"); + +class DeviceController { + async detail(ctx, next) { + const { coop_id } = ctx.request.query; + const res = await getStatusByCoopId(coop_id); + ctx.body = Response(0, "获取鸡舍设备状态成功", res); + } + + async change(ctx, next) { + const { coop_id, egg_gathering, illumination, air_conditioner } = + ctx.request.body; + const res = await changeStatus( + coop_id, + egg_gathering, + illumination, + air_conditioner + ); + ctx.body = Response(0, "改变鸡舍设备状态成功", res); + } +} + +module.exports = new DeviceController(); diff --git a/src/model/control.model.js b/src/model/control.model.js new file mode 100644 index 0000000..75cd031 --- /dev/null +++ b/src/model/control.model.js @@ -0,0 +1,92 @@ +const { DataTypes } = require("sequelize"); + +const seq = require("../db/seq"); + +// 鸡苗出栏 +const Control = seq.define("chicken_control", { + // id 会被sequelize自动创建, 管理 + temperature_start: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: 0, + comment: "温度范围起始值温度", + }, + temperature_end: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: 0, + comment: "温度范围结束值温度", + }, + temperature_default_start: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认起始温度", + }, + temperature_default_end: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认结束温度", + }, + humidity_start: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: 0, + comment: "湿度范围起始值", + }, + humidity_end: { + type: DataTypes.STRING, + allowNull: false, + defaultValue: 0, + comment: "湿度范围结束值", + }, + humidity_default_start: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认起始湿度", + }, + humidity_default_end: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认结束湿度", + }, + current_co2: { + type: DataTypes.STRING, + allowNull: false, + comment: "当前co2含量", + }, + default_co2: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认的co2值", + }, + current_nh3: { + type: DataTypes.STRING, + allowNull: false, + comment: "当前nh3含量", + }, + default_nh3: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认的nh3值", + }, + current_h2s: { + type: DataTypes.STRING, + allowNull: false, + comment: "当前h2s含量", + }, + default_h2s: { + type: DataTypes.STRING, + allowNull: false, + comment: "默认的h2s值", + }, + coop_id: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡舍id", + }, +}); + +// 强制同步数据库(创建数据表) +// Control.sync({ force: true }); + +module.exports = Control; diff --git a/src/model/device.model.js b/src/model/device.model.js new file mode 100644 index 0000000..dc8e2b3 --- /dev/null +++ b/src/model/device.model.js @@ -0,0 +1,36 @@ +const { DataTypes } = require("sequelize"); + +const seq = require("../db/seq"); + +// 鸡苗品种 +const Device = seq.define("chicken_device", { + // id 会被sequelize自动创建, 管理 + egg_gathering: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: 0, + comment: "急蛋设备开关", + }, + illumination: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: 0, + comment: "光照开关", + }, + air_conditioner: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: 0, + comment: "空调开关", + }, + coop_id: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡舍id", + }, +}); + +// 强制同步数据库(创建数据表) +// Device.sync({ force: true }); + +module.exports = Device; diff --git a/src/router/chicken.route.js b/src/router/chicken.route.js index 7e80c97..2e6936e 100644 --- a/src/router/chicken.route.js +++ b/src/router/chicken.route.js @@ -9,6 +9,7 @@ const { all, batchs, batchnum, + ratio, } = require("../controller/chicken.controller"); router.post("/add", auth, add); @@ -23,4 +24,6 @@ router.get("/batchs", auth, batchs); router.get("/batchnum", auth, batchnum); +router.get("/ratio", auth, ratio); + module.exports = router; diff --git a/src/router/device.route.js b/src/router/device.route.js new file mode 100644 index 0000000..c455458 --- /dev/null +++ b/src/router/device.route.js @@ -0,0 +1,12 @@ +const Router = require("koa-router"); + +const router = new Router({ prefix: "/device" }); + +const { auth } = require("../middleware/auth.middleware"); +const { detail, change } = require("../controller/device.controller"); + +router.get("/detail", auth, detail); + +router.post("/change", auth, change); + +module.exports = router; diff --git a/src/service/chicken.service.js b/src/service/chicken.service.js index 0ab610f..1054e91 100644 --- a/src/service/chicken.service.js +++ b/src/service/chicken.service.js @@ -10,6 +10,7 @@ const { getFactoryById } = require("./factory.service"); const { getVarietyById } = require("./variety.service"); const { getBatchCoopByIds } = require("./coop.service"); const { getAccountInfo } = require("./account.service"); +const { getTotalFeedingByCoopId } = require("./feeding.service"); const { Op, Sequelize } = require("sequelize"); const { DATE_FILTER } = require("../constant/constant"); class ChickenService { @@ -171,6 +172,27 @@ class ChickenService { const arr = res?.map((item) => item.dataValues); return arr; } + + async getFeedConversionRatio(house_id) { + const res = await Chicken.findAll({ + where: { + house_id, + }, + }); + const arr = res.map((item) => item.dataValues); + const p = arr.map(async (item) => { + const coop_ids = item.coop_id.split(","); + // 根据coop_ids 批量查询鸡舍信息 + const coopInfo = await getBatchCoopByIds(coop_ids); + item.coopInfo = coopInfo; + // 料肉比=消耗饲料总量/增重总量 + // 监测到的重量 - 进鸡时的重量 = 增重 + // getTotalFeedingByCoopId() + return item; + }); + const realRes = await Promise.all(p); + return realRes; + } } module.exports = new ChickenService(); diff --git a/src/service/device.service.js b/src/service/device.service.js new file mode 100644 index 0000000..b9269cd --- /dev/null +++ b/src/service/device.service.js @@ -0,0 +1,45 @@ +const Device = require("../model/device.model"); +class DeviceService { + async getStatusByCoopId(coop_id) { + const res = await Device.findOne({ + where: { + coop_id, + }, + }); + return res ? res.dataValues : null; + } + + async changeStatus(coop_id, egg_gathering, illumination, air_conditioner) { + // 先查询是否有 + const deviceInfo = await Device.findOne({ + where: { + coop_id, + }, + }); + if (deviceInfo) { + const res = await Device.update( + { + egg_gathering, + illumination, + air_conditioner, + }, + { + where: { + coop_id, + }, + } + ); + return res; + } else { + const res = await Device.create({ + coop_id, + egg_gathering, + illumination, + air_conditioner, + }); + return res; + } + } +} + +module.exports = new DeviceService(); diff --git a/src/service/feeding.service.js b/src/service/feeding.service.js index 703eefd..93514a6 100644 --- a/src/service/feeding.service.js +++ b/src/service/feeding.service.js @@ -35,6 +35,20 @@ class FeedingService { }; } + async getTotalFeedingByCoopId(coop_id) { + const res = await Feeding.findAll({ + where: { + coop_id, + }, + }); + const arr = res.map((item) => item.dataValues); + let total = 0; + arr.map((item) => { + total += item.feeding; + }); + return total; + } + async addFeeding(coop_id, feeding, water_intake) { const res = await Feeding.create({ coop_id,