maochaoying 2 years ago
parent
commit
829e85ffa0
  1. 9
      src/controller/market.controller.js
  2. 24
      src/model/config.model.js
  3. 26
      src/service/chicken.service.js
  4. 38
      src/service/config.service.js
  5. 18
      src/service/coop.service.js
  6. 9
      src/service/house.service.js

9
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);
}

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

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

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

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

9
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) {

Loading…
Cancel
Save