health_warning.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  5. "go.uber.org/zap"
  6. )
  7. func (e *Entry) HealthWarning(pastureId int64, processIds []int64) {
  8. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  9. if err := e.DB.Model(new(model.NeckActiveHabit)).
  10. Where("pasture_id = ?", pastureId).
  11. Where("id IN (?)", processIds).
  12. Where("score BETWEEN ? AND ?", model.MinScore, model.MaxScore).
  13. Order("neck_ring_number,heat_date,frameid").
  14. Find(&newNeckActiveHabitList).Error; err != nil {
  15. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("processIds", processIds))
  16. }
  17. var (
  18. lastCowID int64
  19. lastHeatDate string
  20. lastScore int32
  21. healthWarningList []*model.NeckRingHealthWarning
  22. )
  23. for _, habit := range newNeckActiveHabitList {
  24. // 计算 sumChew 和 chew3dago
  25. sumChew := habit.SumRumina + habit.SumIntake
  26. chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake
  27. // 判断是否满足 isWorse 条件
  28. isWorse := 1
  29. if habit.CowId == lastCowID && habit.HeatDate == lastHeatDate && habit.Score >= lastScore {
  30. isWorse = 0
  31. }
  32. // 更新状态
  33. lastCowID = habit.CowId
  34. lastHeatDate = habit.HeatDate
  35. lastScore = habit.Score
  36. // 如果满足条件,添加到结果中
  37. if isWorse == 1 {
  38. newHealthWarning := model.NewNeckRingHealthWarning(habit, sumChew, chew3dago)
  39. healthWarningList = append(healthWarningList, newHealthWarning)
  40. }
  41. }
  42. if len(healthWarningList) > 0 {
  43. if err := e.DB.Create(&healthWarningList).Error; err != nil {
  44. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("healthWarningList", healthWarningList))
  45. }
  46. }
  47. }