|
|
@ -1,22 +1,33 @@ |
|
|
|
const Chicken = require("../model/chicken.model"); |
|
|
|
const { getDayLife } = require("../utils/common"); |
|
|
|
const { getDayLife, haveSame } = require("../utils/common"); |
|
|
|
const { getDieTotal } = require("./die.service"); |
|
|
|
const { |
|
|
|
getBatchCoopByIds, |
|
|
|
getEmptyCoopList, |
|
|
|
getCoopNumbyHouseId, |
|
|
|
} = require("./coop.service"); |
|
|
|
const { Op } = require("sequelize"); |
|
|
|
class OverviewService { |
|
|
|
async getAllOverViewList(house_id) { |
|
|
|
const res = await Chicken.findAll({ |
|
|
|
where: { |
|
|
|
async getAllOverViewList(house_id, coop_ids = [], batch_ids = []) { |
|
|
|
// 需要根据coop_ids 和 batch_ids进行模糊查询
|
|
|
|
let whereObj = { |
|
|
|
house_id, |
|
|
|
is_marketed: 0, |
|
|
|
}; |
|
|
|
batch_ids && |
|
|
|
batch_ids.length > 0 && |
|
|
|
Object.assign(whereObj, { |
|
|
|
batch_number: { |
|
|
|
[Op.in]: batch_ids, |
|
|
|
}, |
|
|
|
}); |
|
|
|
const res = await Chicken.findAll({ |
|
|
|
where: whereObj, |
|
|
|
}); |
|
|
|
const arr = res?.map((item) => item.dataValues); |
|
|
|
const p = arr.map(async (item) => { |
|
|
|
const ids = item.coop_id.split(","); |
|
|
|
if (coop_ids.length == 0) { |
|
|
|
const allCoops = await getBatchCoopByIds(ids); |
|
|
|
item.allCoops = allCoops |
|
|
|
.map((item) => { |
|
|
@ -28,12 +39,47 @@ class OverviewService { |
|
|
|
item.now_number = item.chicken_number - dieNumber; |
|
|
|
item.day_life = getDayLife(new Date(), item.real_life_init_time); |
|
|
|
return item; |
|
|
|
} |
|
|
|
// 判断ids和coop_ids有无交集
|
|
|
|
if ( |
|
|
|
haveSame( |
|
|
|
ids.map((item) => parseInt(item)), |
|
|
|
coop_ids |
|
|
|
) |
|
|
|
) { |
|
|
|
const allCoops = await getBatchCoopByIds(ids); |
|
|
|
item.allCoops = allCoops |
|
|
|
.map((item) => { |
|
|
|
return item.coop_name; |
|
|
|
}) |
|
|
|
.join(","); |
|
|
|
// 需要减去死淘
|
|
|
|
const dieNumber = await getDieTotal(ids, house_id); |
|
|
|
item.now_number = item.chicken_number - dieNumber; |
|
|
|
item.day_life = getDayLife(new Date(), item.real_life_init_time); |
|
|
|
return item; |
|
|
|
} |
|
|
|
}); |
|
|
|
const result = await Promise.all(p); |
|
|
|
const emptyCoop = await getEmptyCoopList(house_id); |
|
|
|
const realCoop = emptyCoop.filter((item) => { |
|
|
|
if (coop_ids.length == 0) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
if (coop_ids.includes(parseInt(item.id))) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
return false; |
|
|
|
}); |
|
|
|
const length = emptyCoop?.length; |
|
|
|
const totalCount = await getCoopNumbyHouseId(house_id); |
|
|
|
const hasCoopCount = totalCount - (emptyCoop?.length || 0); |
|
|
|
const real = [...result, ...emptyCoop]; |
|
|
|
const hasCoopCount = totalCount - (length || 0); |
|
|
|
let real = []; |
|
|
|
if (batch_ids.length == 0) { |
|
|
|
real = [...result, ...realCoop]; |
|
|
|
} else { |
|
|
|
real = [...result]; |
|
|
|
} |
|
|
|
return { |
|
|
|
list: real, |
|
|
|
coopNum: hasCoopCount, |
|
|
|