package crontab import ( "kpt-pasture/model" "gitee.com/xuyiping_admin/pkg/xerr" ) func (e *Entry) HealthWarning(pastureId int64, processIds []int64) (err error) { newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0) if err = e.DB.Model(new(model.NeckActiveHabit)). Where("pasture_id = ?", pastureId). Where("id IN (?)", processIds). Where("score BETWEEN ? AND ?", 1, 84). Order("heat_date,neck_ring_number,frameid"). Find(&newNeckActiveHabitList).Error; err != nil { return xerr.WithStack(err) } var ( lastCowID int64 lastHeatDate string lastScore int32 healthWarningList []*model.NeckRingHealthWarning ) for _, habit := range newNeckActiveHabitList { // 计算 sumChew 和 chew3dago sumChew := habit.SumRumina + habit.SumIntake chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake // 判断是否满足 isWorse 条件 isWorse := 1 if habit.CowId == lastCowID && habit.HeatDate == lastHeatDate && habit.Score >= lastScore { isWorse = 0 } // 更新状态 lastCowID = habit.CowId lastHeatDate = habit.HeatDate lastScore = habit.Score // 如果满足条件,添加到结果中 if isWorse == 1 { newHealthWarning := model.NewNeckRingHealthWarning(habit, sumChew, chew3dago) healthWarningList = append(healthWarningList, newHealthWarning) } } if len(healthWarningList) > 0 { if err = e.DB.Create(&healthWarningList).Error; err != nil { return xerr.WithStack(err) } } return nil }