health_warning.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package crontab
  2. import (
  3. "kpt-pasture/model"
  4. "time"
  5. "gitee.com/xuyiping_admin/pkg/xerr"
  6. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  7. "go.uber.org/zap"
  8. )
  9. func (e *Entry) HealthWarning(pastureId int64, processIds []int64) {
  10. newNeckActiveHabitList := make([]*model.NeckActiveHabit, 0)
  11. if err := e.DB.Model(new(model.NeckActiveHabit)).
  12. Where("pasture_id = ?", pastureId).
  13. Where("id IN (?)", processIds).
  14. Where("score BETWEEN ? AND ?", model.MinScore, model.MaxScore).
  15. Order("neck_ring_number,heat_date,frameid").
  16. Find(&newNeckActiveHabitList).Error; err != nil {
  17. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("processIds", processIds))
  18. }
  19. var (
  20. lastCowID int64
  21. lastHeatDate string
  22. lastScore int32
  23. healthWarningList []*model.NeckRingHealth
  24. )
  25. for _, habit := range newNeckActiveHabitList {
  26. // 计算 sumChew 和 chew3dago
  27. sumChew := habit.SumRumina + habit.SumIntake
  28. chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake
  29. // 判断是否满足 isWorse 条件
  30. isWorse := 1
  31. if habit.CowId == lastCowID && habit.HeatDate == lastHeatDate && habit.Score >= lastScore {
  32. isWorse = 0
  33. }
  34. // 更新状态
  35. lastCowID = habit.CowId
  36. lastHeatDate = habit.HeatDate
  37. lastScore = habit.Score
  38. // 如果满足条件,添加到结果中
  39. if isWorse == 1 {
  40. newHealthWarning := model.NewNeckRingHealth(habit, sumChew, chew3dago)
  41. if count := e.FindHealthWarning(pastureId, newHealthWarning); count <= 0 {
  42. continue
  43. }
  44. healthWarningList = append(healthWarningList, newHealthWarning)
  45. }
  46. }
  47. if len(healthWarningList) > 0 {
  48. if err := e.DB.Create(&healthWarningList).Error; err != nil {
  49. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("healthWarningList", healthWarningList))
  50. }
  51. }
  52. }
  53. func (e *Entry) NeckRingHealthWarning() error {
  54. pastureList := e.FindPastureList()
  55. if pastureList == nil || len(pastureList) == 0 {
  56. return nil
  57. }
  58. for _, pasture := range pastureList {
  59. if err := e.UpdateNeckRingHealth(pasture.Id); err != nil {
  60. zaplog.Error("NeckRingHealthWarning", zap.Any("UpdateNeckRingHealth", err), zap.Any("pasture", pasture))
  61. }
  62. }
  63. return nil
  64. }
  65. func (e *Entry) UpdateNeckRingHealth(pastureId int64) error {
  66. neckRingConfigureList, err := e.FindSystemNeckRingConfigure(pastureId)
  67. if err != nil {
  68. return xerr.WithStack(err)
  69. }
  70. healthValue := int32(0)
  71. for _, v := range neckRingConfigureList {
  72. if v.Name != model.HealthWarning {
  73. continue
  74. }
  75. healthValue = int32(v.Value)
  76. }
  77. nowTime := time.Now()
  78. neckRingHealthList := make([]*model.NeckRingHealth, 0)
  79. if err = e.DB.Model(new(model.NeckRingHealth)).
  80. Select("MAX(id) AS id,neck_ring_number,cow_id,score,max_high,created_at,min_high,min_chew,min_intake,sum_chew,before_three_sum_chew").
  81. Where("pasture_id = ?", pastureId).
  82. Where("heat_date >= ?", nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)).
  83. Group("neck_ring_number").
  84. Find(&neckRingHealthList).Error; err != nil {
  85. return xerr.WithStack(err)
  86. }
  87. newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
  88. for _, v := range neckRingHealthList {
  89. newScore := calculateNewScore(v)
  90. if newScore > healthValue {
  91. continue
  92. }
  93. cowInfo, err := e.GetCowById(pastureId, v.CowId)
  94. if err != nil {
  95. continue
  96. }
  97. if cowInfo == nil {
  98. continue
  99. }
  100. newNeckRingHealthWarning := model.NewNeckRingHealthWarning(pastureId, v, cowInfo, newScore)
  101. newNeckRingHealthWarningList = append(newNeckRingHealthWarningList, newNeckRingHealthWarning)
  102. }
  103. if len(newNeckRingHealthWarningList) > 0 {
  104. if err = e.DB.Model(new(model.NeckRingHealthWarning)).
  105. Create(&newNeckRingHealthWarningList).Error; err != nil {
  106. zaplog.Error("UpdateNeckRingHealth", zap.Any("error", err), zap.Any("newNeckRingHealthWarningList", newNeckRingHealthWarningList))
  107. }
  108. }
  109. return nil
  110. }
  111. func (e *Entry) FindHealthWarning(pastureId int64, data *model.NeckRingHealth) int64 {
  112. var count int64
  113. if err := e.DB.Model(new(model.NeckRingHealth)).
  114. Where("pasture_id = ?", pastureId).
  115. Where("cow_id = ?", data.CowId).
  116. Where("heat_date = ?", data.HeatDate).
  117. Where("score = ?", data.Score).
  118. Count(&count).Error; err != nil {
  119. zaplog.Error("FindHealthWarning", zap.Any("error", err))
  120. }
  121. return count
  122. }
  123. func calculateNewScore(data *model.NeckRingHealth) int32 {
  124. return data.Score
  125. }