maochaoying 2 years ago
parent
commit
bc79af2e82
  1. 8
      src/controller/chicken.controller.js
  2. 27
      src/controller/device.controller.js
  3. 92
      src/model/control.model.js
  4. 36
      src/model/device.model.js
  5. 3
      src/router/chicken.route.js
  6. 12
      src/router/device.route.js
  7. 22
      src/service/chicken.service.js
  8. 45
      src/service/device.service.js
  9. 14
      src/service/feeding.service.js

8
src/controller/chicken.controller.js

@ -4,6 +4,7 @@ const {
getAllChickenInfo, getAllChickenInfo,
getChickenInfoByHouseId, getChickenInfoByHouseId,
getBatchNumbersByHouseId, getBatchNumbersByHouseId,
getFeedConversionRatio,
} = require("../service/chicken.service"); } = require("../service/chicken.service");
const Response = require("../utils/response"); const Response = require("../utils/response");
@ -63,6 +64,13 @@ class ChickenController {
const res = await getBatchNumbersByHouseId(house_id); const res = await getBatchNumbersByHouseId(house_id);
ctx.body = Response(0, "查询批次号简易信息成功", res); 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(); module.exports = new ChickenController();

27
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();

92
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;

36
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;

3
src/router/chicken.route.js

@ -9,6 +9,7 @@ const {
all, all,
batchs, batchs,
batchnum, batchnum,
ratio,
} = require("../controller/chicken.controller"); } = require("../controller/chicken.controller");
router.post("/add", auth, add); router.post("/add", auth, add);
@ -23,4 +24,6 @@ router.get("/batchs", auth, batchs);
router.get("/batchnum", auth, batchnum); router.get("/batchnum", auth, batchnum);
router.get("/ratio", auth, ratio);
module.exports = router; module.exports = router;

12
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;

22
src/service/chicken.service.js

@ -10,6 +10,7 @@ const { getFactoryById } = require("./factory.service");
const { getVarietyById } = require("./variety.service"); const { getVarietyById } = require("./variety.service");
const { getBatchCoopByIds } = require("./coop.service"); const { getBatchCoopByIds } = require("./coop.service");
const { getAccountInfo } = require("./account.service"); const { getAccountInfo } = require("./account.service");
const { getTotalFeedingByCoopId } = require("./feeding.service");
const { Op, Sequelize } = require("sequelize"); const { Op, Sequelize } = require("sequelize");
const { DATE_FILTER } = require("../constant/constant"); const { DATE_FILTER } = require("../constant/constant");
class ChickenService { class ChickenService {
@ -171,6 +172,27 @@ class ChickenService {
const arr = res?.map((item) => item.dataValues); const arr = res?.map((item) => item.dataValues);
return arr; 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(); module.exports = new ChickenService();

45
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();

14
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) { async addFeeding(coop_id, feeding, water_intake) {
const res = await Feeding.create({ const res = await Feeding.create({
coop_id, coop_id,

Loading…
Cancel
Save