|
@ -0,0 +1,48 @@ |
|
|
|
|
|
const moment = require("moment"); |
|
|
|
|
|
const Feeding = require("../model/feeding.model"); |
|
|
|
|
|
class FeedingService { |
|
|
|
|
|
async getAllFeeding(coop_id) { |
|
|
|
|
|
const res = await Feeding.findAll({ |
|
|
|
|
|
where: { |
|
|
|
|
|
coop_id, |
|
|
|
|
|
}, |
|
|
|
|
|
}); |
|
|
|
|
|
const arr = res.map((item) => item.dataValues); |
|
|
|
|
|
// 进行归类 根据时间
|
|
|
|
|
|
let obj = {}; |
|
|
|
|
|
arr.map((item) => { |
|
|
|
|
|
const time = moment(item.createdAt).format("YYYY-MM-DD"); |
|
|
|
|
|
if (obj[time]) { |
|
|
|
|
|
// 需要添加进list
|
|
|
|
|
|
obj[time] = { |
|
|
|
|
|
feedingList: obj[time].feedingList + parseInt(item.feeding), |
|
|
|
|
|
waterIntakeList: |
|
|
|
|
|
obj[time].waterIntakeList + parseInt(item.water_intake), |
|
|
|
|
|
}; |
|
|
|
|
|
} else { |
|
|
|
|
|
obj[time] = { |
|
|
|
|
|
feedingList: parseInt(item.feeding), |
|
|
|
|
|
waterIntakeList: parseInt(item.water_intake), |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
return { |
|
|
|
|
|
list: arr, |
|
|
|
|
|
echarts: { |
|
|
|
|
|
xData: Object.keys(obj), |
|
|
|
|
|
yData: Object.values(obj), |
|
|
|
|
|
}, |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async addFeeding(coop_id, feeding, water_intake) { |
|
|
|
|
|
const res = await Feeding.create({ |
|
|
|
|
|
coop_id, |
|
|
|
|
|
feeding, |
|
|
|
|
|
water_intake, |
|
|
|
|
|
}); |
|
|
|
|
|
return res ? res.dataValues : null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = new FeedingService(); |