diff --git a/src/controller/market.controller.js b/src/controller/market.controller.js index d2b69d9..dcb2c76 100644 --- a/src/controller/market.controller.js +++ b/src/controller/market.controller.js @@ -4,6 +4,8 @@ const { getMarketByBatchId, getDefaultMarketInfo, } = require("../service/market.service"); +const { getCoopIdsByBatchId } = require("../service/chicken.service"); +const { changeIsEmptyByIds } = require("../service/coop.service"); const Response = require("../utils/response"); class MarketController { @@ -22,6 +24,13 @@ class MarketController { house_id, out_time ); + // 一旦出栏,需要将该批次号下的所有coop中的is_empty变为1,变为空舍 + try { + const coop_ids = await getCoopIdsByBatchId(batch_id); + await changeIsEmptyByIds(coop_ids, 1); + } catch (e) { + console.log(e); + } ctx.body = Response(0, "新增出栏记录成功", res); } diff --git a/src/model/config.model.js b/src/model/config.model.js new file mode 100644 index 0000000..f600bef --- /dev/null +++ b/src/model/config.model.js @@ -0,0 +1,24 @@ +const { DataTypes } = require("sequelize"); + +const seq = require("../db/seq"); + +const Config = seq.define("chicken_config", { + // id 会被sequelize自动创建, 管理 + house_id: { + type: DataTypes.INTEGER, + allowNull: false, + unique: true, + comment: "所属鸡场id, 相当于外键", + }, + market_life_min: { + type: DataTypes.INTEGER, + allowNull: false, + defaultValue: 0, + comment: "出栏日龄,限制最小的出栏日龄,单位天", + }, +}); + +// 强制同步数据库(创建数据表) +// Config.sync({ force: true }); + +module.exports = Config; diff --git a/src/service/chicken.service.js b/src/service/chicken.service.js index 41c48f3..f4817fb 100644 --- a/src/service/chicken.service.js +++ b/src/service/chicken.service.js @@ -14,6 +14,7 @@ const { } = require("./coop.service"); const { getAccountInfo } = require("./account.service"); const { getTotalFeedingByCoopId } = require("./feeding.service"); +const { getConfigByHouseId } = require("./config.service"); const { Op, Sequelize } = require("sequelize"); const { DATE_FILTER } = require("../constant/constant"); class ChickenService { @@ -201,14 +202,22 @@ class ChickenService { } async getBatchNumbersByHouseId(house_id) { + // 只显示未出栏、日龄大于等于36天的批次号(36天可配置) + const configInfo = await getConfigByHouseId(house_id); + const market_life_min = configInfo.market_life_min; const res = await Chicken.findAll({ where: { house_id, - is_marketed: 0, + is_marketed: 0, // 未出栏 }, }); const arr = res?.map((item) => item.dataValues); - return arr; + const result = arr.filter( + (item) => + getDayLife(new Date(), item.real_life_init_time) > market_life_min + ); + + return result; } async getFeedConversionRatio(house_id) { @@ -231,6 +240,19 @@ class ChickenService { const realRes = await Promise.all(p); return realRes; } + + async getCoopIdsByBatchId(batch_number) { + const res = await Chicken.findOne({ + where: { + batch_number, + }, + }); + if (res) { + const coop_ids = res.dataValues?.coop_id; + return coop_ids?.split(","); + } + return []; + } } module.exports = new ChickenService(); diff --git a/src/service/config.service.js b/src/service/config.service.js new file mode 100644 index 0000000..2b8ce2e --- /dev/null +++ b/src/service/config.service.js @@ -0,0 +1,38 @@ +const Config = require("../model/config.model"); +class ConfigService { + async initConfig(house_id, market_life_min = 0) { + const configInfo = await Config.findOne({ + where: { + house_id, + }, + }); + if (configInfo) { + await Config.update( + { + market_life_min, + }, + { + where: { + house_id, + }, + } + ); + } else { + await Config.create({ + house_id, + market_life_min, + }); + } + } + + async getConfigByHouseId(house_id) { + const res = await Config.findOne({ + where: { + house_id, + }, + }); + return res ? res.dataValues : null; + } +} + +module.exports = new ConfigService(); diff --git a/src/service/coop.service.js b/src/service/coop.service.js index a957386..fa75dbd 100644 --- a/src/service/coop.service.js +++ b/src/service/coop.service.js @@ -1,4 +1,4 @@ -const { Op } = require("sequelize"); +const { Op, where } = require("sequelize"); const Chicken = require("../model/chicken.model.js"); const Coop = require("../model/coop.model"); const { findDiffArr } = require("../utils/common"); @@ -164,6 +164,22 @@ class CoopService { }); return total; } + + async changeIsEmptyByIds(coop_ids, is_empty) { + const res = await Coop.update( + { + is_empty, + }, + { + where: { + id: { + [Op.in]: coop_ids, + }, + }, + } + ); + return res; + } } module.exports = new CoopService(); diff --git a/src/service/house.service.js b/src/service/house.service.js index a5a41e7..3231a36 100644 --- a/src/service/house.service.js +++ b/src/service/house.service.js @@ -1,6 +1,7 @@ const House = require("../model/house.model"); const Coop = require("../model/coop.model"); const { getAccountInfo } = require("./account.service"); +const { initConfig } = require("./config.service"); class HouseService { async getHouseListById(belong, role) { let res = null; @@ -36,7 +37,13 @@ class HouseService { house_name, area, }); - return res ? res.dataValues : null; + // 创建鸡场对应的config表哥 + if (res) { + const house_id = res.dataValues.id; + await initConfig(house_id); + return res.dataValues; + } + return null; } async getHouseByName(house_name) {