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.
140 lines
3.9 KiB
140 lines
3.9 KiB
const Chicken = require("../model/chicken.model");
|
|
const { generateSerialNumber, haveSame } = require("../utils/common");
|
|
const { getHouseById } = require("./house.service");
|
|
const { getFactoryById } = require("./factory.service");
|
|
const { getVarietyById } = require("./variety.service");
|
|
const { getBatchCoopByIds } = require("./coop.service");
|
|
const { getAccountInfo } = require("./account.service");
|
|
const { Op } = 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
|
|
) {
|
|
const batch_number = generateSerialNumber();
|
|
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,
|
|
});
|
|
return res ? res.dataValues : null;
|
|
}
|
|
|
|
async getChickenByCoopIdAndHouseId(coop_id, house_id) {
|
|
const whereOpt = {};
|
|
coop_id && Object.assign(whereOpt, { coop_id });
|
|
house_id && Object.assign(whereOpt, { house_id });
|
|
const res = await Chicken.findOne({
|
|
where: whereOpt,
|
|
});
|
|
return res ? res.dataValues : null;
|
|
}
|
|
|
|
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;
|
|
return someId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
async getChickenByBatchId(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,
|
|
});
|
|
// 根据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,
|
|
},
|
|
});
|
|
// 批量拿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;
|
|
}
|
|
}
|
|
|
|
module.exports = new ChickenService();
|