123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package crontab
- import (
- "kpt-pasture/model"
- "time"
- "gitee.com/xuyiping_admin/pkg/logger/zaplog"
- "gitee.com/xuyiping_admin/pkg/xerr"
- "go.uber.org/zap"
- )
- const (
- MaxDayHigh = 500
- MaxRumina = 200
- MaxCowCount = 10
- MaxNB = 8
- Days = 2
- )
- func (e *Entry) UpdatePenBehaviorDaily() error {
- pastureList := e.FindPastureList()
- if pastureList == nil || len(pastureList) == 0 {
- return nil
- }
- for _, pasture := range pastureList {
- e.BehaviorDaily(pasture.Id, Days)
- }
- return nil
- }
- // BehaviorDaily 更新牛只每日行为数据
- func (e *Entry) BehaviorDaily(pastureId int64, days int32) {
- for i := days; i >= 1; i-- {
- targetDate := time.Now().AddDate(0, 0, -int(i)).Format(model.LayoutDate2)
- // 1. 更新milk_daily表
- if err := e.updateMilkDaily(pastureId, targetDate); err != nil {
- zaplog.Error("UpdateBehaviorDaily", zap.Any("pastureId", pastureId), zap.Any("targetDate", targetDate), zap.Any("err", err))
- continue
- }
- // 2. 删除PenBehaviorDay表中的旧数据
- if err := e.DB.Where("pasture_id = ? AND heat_date = ?", pastureId, targetDate).
- Delete(new(model.PenBehaviorDay)).Error; err != nil {
- zaplog.Error("UpdateBehaviorDaily", zap.Any("delete error", err))
- continue
- }
- // 3. 插入新的PenBehaviorDay数据
- if err := e.insertBarBehaviorDay(pastureId, targetDate); err != nil {
- zaplog.Error("UpdateBehaviorDaily", zap.Any("insert error", err))
- continue
- }
- }
- }
- // updateMilkDaily 更新milk_daily表中的行为数据
- func (e *Entry) updateMilkDaily(pastureId int64, targetDate string) error {
- milkDailyModelList := make([]*model.MilkDailyModel, 0)
- if err := e.DB.Model(new(model.NeckActiveHabit)).
- Select(`cow_id,heat_date, ROUND(AVG(filter_high), 0) as day_high, ROUND(AVG(rumina)*12, 0) as day_rumina, ROUND(AVG(intake)*12, 0) as day_intake,
- ROUND(AVG(inactive)*12, 0) as day_inactive, ROUND(AVG(active)*12, 0) as day_active, COUNT(1) as nb`).
- Where("pasture_id = ? AND heat_date = ?", pastureId, targetDate).
- Where("cow_id > ?", 0).
- Group("cow_id").
- Having("nb >= ?", MaxNB).
- Find(&milkDailyModelList).Error; err != nil {
- return xerr.WithStack(err)
- }
- for _, v := range milkDailyModelList {
- if err := e.DB.Model(new(model.MilkDaily)).
- Where("pasture_id = ?", pastureId).
- Where("cow_id = ? AND heat_date = ?", v.CowId, targetDate).
- Updates(map[string]interface{}{
- "day_high": v.DayHigh,
- "day_rumina": v.DayRumina,
- "day_intake": v.DayIntake,
- "day_inactive": v.DayInactive,
- "day_active": v.DayActive,
- }).Error; err != nil {
- continue
- }
- }
- return nil
- }
- // insertBarBehaviorDay 插入栏舍行为数据
- func (e *Entry) insertBarBehaviorDay(pastureId int64, targetDate string) error {
- penBehaviorList := make([]*model.PenBehaviorDay, 0)
- if err := e.DB.Model(new(model.MilkDaily)).
- Select(`heat_date, pasture_id, pen_id, COUNT(1) as cow_count, ROUND(AVG(d.day_yield), 1) as day_milk,
- ROUND(AVG(d.week_avg), 1) as week_milk,ROUND(AVG(d.day_high), 0) as day_high,
- ROUND(AVG(d.day_rumina), 0) as day_rumina,ROUND(AVG(d.day_intake), 0) as day_intake,
- ROUND(AVG(d.day_inactive), 0) as day_inactive,ROUND(AVG(d.day_gasp), 0) as day_gasp,
- ROUND(AVG(d.day_active), 0) as day_active,STD(d.day_rumina) as rumina_std`).
- Where("pasture_id = ?", pastureId).
- Where("heat_date = ?", targetDate).
- Where("day_high > ?", MaxDayHigh).
- Where("day_rumina > ?", MaxRumina).
- Group("pen_id").
- Having("cow_count >= ?", MaxCowCount).
- Order("pen_id").
- Find(&penBehaviorList).Error; err != nil {
- return xerr.WithStack(err)
- }
- if len(penBehaviorList) > 0 {
- if err := e.DB.Create(&penBehaviorList).Error; err != nil {
- return xerr.WithStack(err)
- }
- }
- return nil
- }
|