package crontab import ( "fmt" "kpt-pasture/model" "gitee.com/xuyiping_admin/pkg/logger/zaplog" "go.uber.org/zap" ) // PenBehavior 栏舍行为曲线 func (e *Entry) PenBehavior(pastureId int64, processIds []int64) { neckRingOriginalList := make([]*model.NeckRingOriginal, 0) if err := e.DB.Model(new(model.NeckRingOriginal)). Where("id IN (?)", processIds). Where("pasture_id = ?", pastureId). Order("heat_date,neck_ring_number,frameid"). Find(&neckRingOriginalList).Error; err != nil { zaplog.Error("PenBehavior", zap.Any("error", err), zap.Any("processIds", processIds)) } cowIds := make([]int64, 0) for _, v := range neckRingOriginalList { cowIds = append(cowIds, v.Id) } cowInfoList, err := e.GetCowByIds(pastureId, cowIds) if err != nil { zaplog.Error("PenBehavior", zap.Any("error", err), zap.Any("cowIds", cowIds)) return } cowMap := make(map[string]*model.Cow) for _, v := range cowInfoList { if v.NeckRingNumber == "" { continue } cowMap[v.NeckRingNumber] = v } penData := make(map[string]*model.PenBehaviorData) for _, v := range neckRingOriginalList { cowInfo, ok := cowMap[v.NeckRingNumber] if !ok { zaplog.Error("PenBehavior", zap.Any("error", err), zap.Any("neckRingNumber", v.NeckRingNumber)) continue } key := fmt.Sprintf("%s_%d_%d", v.ActiveDate, cowInfo.PenId, v.Frameid) if penData[key] == nil { penData[key] = &model.PenBehaviorData{ PastureId: pastureId, PenId: cowInfo.PenId, PenName: cowInfo.PenName, HeatDate: v.ActiveDate, Frameid: v.Frameid, CowCount: 1, AvgHigh: v.High, } } else { penData[key].CowCount++ penData[key].AvgHigh += v.High penData[key].SumRumina += ifThenElse(v.Rumina >= 8, 1, 0) penData[key].SumIntake += ifThenElse(v.Intake >= 8, 1, 0) penData[key].SumRest += ifThenElse(v.Inactive >= 8, 1, 0) penData[key].SumGasp += ifThenElse(v.Gasp >= 8, 1, 0) } } // 计算平均值 for _, data := range penData { data.AvgHigh = data.AvgHigh / data.CowCount } } func ifThenElse(condition bool, a, b int32) int32 { if condition { return a } return b }