health_warning.go 1.5 KB

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