You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
8.7 KiB
316 lines
8.7 KiB
const Chicken = require("../model/chicken.model");
|
|
const Market = require("../model/market.model");
|
|
const {
|
|
generateSerialNumber,
|
|
haveSame,
|
|
getTimeLastDate,
|
|
getDayLife,
|
|
} = require("../utils/common");
|
|
const { getHouseById } = require("./house.service");
|
|
const { getFactoryById } = require("./factory.service");
|
|
const { getVarietyById } = require("./variety.service");
|
|
const {
|
|
getBatchCoopByIds,
|
|
batchUpdateCoopStatusByIds,
|
|
} = 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 {
|
|
async addChicken(
|
|
house_id,
|
|
coop_id,
|
|
put_time,
|
|
variety_id,
|
|
chicken_number,
|
|
chicken_day_life,
|
|
chicken_counter_balance,
|
|
factory_id,
|
|
log_user_id
|
|
) {
|
|
// batchnumber需要更改为 时间(到天的日期)20230403- + 鸡场ID- + 第几次进
|
|
// 加鸡场ID区分哪一个鸡场
|
|
// 需要查询今天进鸡的最大的批次号递增1
|
|
const currentDayDataRes = await Chicken.findAll({
|
|
where: {
|
|
[Sequelize.Op.and]: [
|
|
{
|
|
house_id,
|
|
},
|
|
Sequelize.where(
|
|
Sequelize.fn("DATE", Sequelize.col("createdAt")), // 表对应的字段
|
|
Sequelize.literal("CURRENT_DATE")
|
|
),
|
|
],
|
|
},
|
|
});
|
|
const currentDayData = currentDayDataRes?.map((item) => item.dataValues);
|
|
let batch_number = "";
|
|
if (currentDayData?.length > 0) {
|
|
batch_number =
|
|
generateSerialNumber() +
|
|
"-" +
|
|
house_id +
|
|
"-" +
|
|
(parseInt(
|
|
currentDayData[currentDayData.length - 1]?.batch_number?.split("-")[2]
|
|
) +
|
|
1);
|
|
} else {
|
|
batch_number = generateSerialNumber() + "-" + house_id + "-" + "1";
|
|
}
|
|
// 生成real_life_init_time
|
|
const real_life_init_time = getTimeLastDate(put_time, chicken_day_life);
|
|
// 进鸡数量不能超过 鸡笼最大量
|
|
const res = await Chicken.create({
|
|
house_id,
|
|
coop_id: coop_id.join(","),
|
|
put_time,
|
|
variety_id,
|
|
chicken_number,
|
|
chicken_day_life,
|
|
chicken_counter_balance,
|
|
factory_id,
|
|
log_user_id,
|
|
batch_number,
|
|
real_life_init_time,
|
|
});
|
|
// 新进鸡苗后需要将进入的鸡舍状态变为非空舍
|
|
const coopRes = await batchUpdateCoopStatusByIds(coop_id, 0);
|
|
return res ? res.dataValues : null;
|
|
}
|
|
|
|
async getChickenByCoopIdAndHouseId(coop_id, house_id) {
|
|
const res = await Chicken.findAll({
|
|
where: {
|
|
house_id,
|
|
},
|
|
});
|
|
const arr = res.map((item) => item.dataValues);
|
|
const searchVal = arr.filter((item) => {
|
|
const coopArr = item.coop_id.split(",");
|
|
return coopArr.includes(coop_id + "");
|
|
});
|
|
return searchVal && searchVal[0];
|
|
}
|
|
|
|
async getChickenInfoById(id, log_name) {
|
|
const res = await Chicken.findOne({
|
|
where: {
|
|
id,
|
|
},
|
|
});
|
|
if (res) {
|
|
const someId = res.dataValues;
|
|
const houseInfo = await getHouseById(someId.house_id);
|
|
const factoryName = await getFactoryById(someId.factory_id);
|
|
const varietyName = await getVarietyById(someId.variety_id);
|
|
const allCoops = await getBatchCoopByIds(someId.coop_id.split(","));
|
|
someId.houseName = houseInfo.house_name;
|
|
someId.log_name = log_name;
|
|
someId.factoryName = factoryName;
|
|
someId.varietyName = varietyName;
|
|
someId.allCoops = allCoops;
|
|
// 已经出栏的日龄为出栏时间 - real_life
|
|
// 未出栏的为当前时间 - real_life
|
|
if (someId.is_marketed) {
|
|
// 查询出栏时间
|
|
const marketInfo = await Market.findOne({
|
|
where: {
|
|
batch_id: someId.batch_number,
|
|
},
|
|
});
|
|
someId.day_life = getDayLife(
|
|
marketInfo.out_time,
|
|
someId.real_life_init_time
|
|
);
|
|
} else {
|
|
someId.day_life = getDayLife(new Date(), someId.real_life_init_time);
|
|
}
|
|
|
|
return someId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async getChickenByBatchId(batch_number) {
|
|
const res = await Chicken.findOne({
|
|
where: {
|
|
batch_number,
|
|
is_marketed: 0,
|
|
},
|
|
});
|
|
return res ? res.dataValues : null;
|
|
}
|
|
|
|
async getAllChickenByBatchId(batch_number) {
|
|
const res = await Chicken.findOne({
|
|
where: {
|
|
batch_number,
|
|
},
|
|
});
|
|
return res ? res.dataValues : null;
|
|
}
|
|
|
|
// 仅查询未出栏的
|
|
async getAllChickenInfo(
|
|
batch_number = "",
|
|
coop_ids = [],
|
|
put_time = "0",
|
|
house_id
|
|
) {
|
|
let selectObj = {
|
|
batch_number: {
|
|
[Op.like]: `%${batch_number}%`,
|
|
},
|
|
};
|
|
house_id && Object.assign(selectObj, { house_id });
|
|
DATE_FILTER[put_time] &&
|
|
Object.assign(selectObj, { put_time: DATE_FILTER[put_time] });
|
|
const res = await Chicken.findAll({
|
|
where: selectObj,
|
|
order: [["put_time", "DESC"]],
|
|
});
|
|
// 根据coop_ids进行筛选一次
|
|
const arr = res.filter((item) => {
|
|
if (coop_ids.length > 0) {
|
|
if (haveSame(item.coop_id.split(","), coop_ids)) {
|
|
return true;
|
|
}
|
|
} else {
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
const p = arr.map(async (item) => {
|
|
const varietyName = await getVarietyById(item.variety_id);
|
|
const allCoops = await getBatchCoopByIds(item.coop_id.split(","));
|
|
item.dataValues.varietyName = varietyName;
|
|
item.dataValues.allCoops = allCoops;
|
|
return item;
|
|
});
|
|
const real = Promise.all(p);
|
|
return real;
|
|
}
|
|
|
|
async getChickenInfoByHouseId(house_id) {
|
|
const res = await Chicken.findAll({
|
|
where: {
|
|
house_id,
|
|
is_marketed: 0,
|
|
},
|
|
});
|
|
// 批量拿coopinfo
|
|
const arr = res?.map((item) => item.dataValues);
|
|
const p = arr.map(async (item) => {
|
|
const varietyName = await getVarietyById(item.variety_id);
|
|
const allCoops = await getBatchCoopByIds(item.coop_id.split(","));
|
|
const userinfo = await getAccountInfo({ id: item.log_user_id });
|
|
item.varietyName = varietyName;
|
|
item.allCoops = allCoops;
|
|
item.username = userinfo.name;
|
|
return item;
|
|
});
|
|
const real = await Promise.all(p);
|
|
return real;
|
|
}
|
|
|
|
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, // 未出栏
|
|
},
|
|
});
|
|
const arr = res?.map((item) => item.dataValues);
|
|
const result = arr.filter(
|
|
(item) =>
|
|
getDayLife(new Date(), item.real_life_init_time) > market_life_min
|
|
);
|
|
|
|
return result;
|
|
}
|
|
|
|
async getBatchNumbers(house_id) {
|
|
try {
|
|
const res = await Chicken.findAll({
|
|
where: {
|
|
house_id,
|
|
is_marketed: 0, // 未出栏
|
|
},
|
|
});
|
|
const arr = res?.map((item) => item.dataValues);
|
|
return arr;
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
}
|
|
|
|
async getNoRatioCoopsByBatchNumber(batch_number) {
|
|
const res = await Chicken.findOne({
|
|
where: {
|
|
batch_number,
|
|
},
|
|
});
|
|
const coop_id_str = res?.dataValues?.coop_id;
|
|
const coop_ids = coop_id_str.split(",");
|
|
const coopInfoList = await getBatchCoopByIds(coop_ids);
|
|
const arr = coopInfoList.filter((item) => !item.feed_conversion);
|
|
return arr;
|
|
}
|
|
|
|
async getFeedConversionRatio(house_id, batch_number = "") {
|
|
// 需要根据batch_id进行模糊查询
|
|
const res = await Chicken.findAll({
|
|
where: {
|
|
house_id,
|
|
batch_number: {
|
|
[Op.like]: `%${batch_number}%`,
|
|
},
|
|
},
|
|
});
|
|
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;
|
|
// 需要计算出平均的料肉比
|
|
let total = 0;
|
|
let length = 0;
|
|
coopInfo.map((item) => {
|
|
if (item.feed_conversion) {
|
|
total += item.feed_conversion;
|
|
length += 1;
|
|
}
|
|
});
|
|
if (length > 0) {
|
|
item.average = total / length;
|
|
}
|
|
return item;
|
|
});
|
|
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();
|