health_warning.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. if habit.ChangeFilter <= -99 {
  27. habit.ChangeFilter = 0
  28. }
  29. if habit.ChewFilter <= -99 {
  30. habit.ChewFilter = 0
  31. }
  32. if habit.SumMinHigh <= -99 {
  33. habit.SumMinHigh = 0
  34. }
  35. if habit.SumMaxHigh <= -99 {
  36. habit.SumMaxHigh = 0
  37. }
  38. if habit.SumMinChew <= -99 {
  39. habit.SumMinChew = 0
  40. }
  41. sumChew := habit.SumRumina + habit.SumIntake
  42. chew3dago := habit.BeforeThreeSumRumina + habit.BeforeThreeSumIntake
  43. isWorse := 1
  44. if habit.CowId == lastCowID && habit.HeatDate == lastHeatDate && habit.Score >= lastScore {
  45. isWorse = 0
  46. }
  47. lastCowID = habit.CowId
  48. lastHeatDate = habit.HeatDate
  49. lastScore = habit.Score
  50. if isWorse == 1 {
  51. newHealthWarning := model.NewNeckRingHealth(habit, sumChew, chew3dago)
  52. if count := e.FindHealthWarning(pastureId, newHealthWarning); count <= 0 {
  53. continue
  54. }
  55. healthWarningList = append(healthWarningList, newHealthWarning)
  56. }
  57. }
  58. if len(healthWarningList) > 0 {
  59. if err := e.DB.Create(&healthWarningList).Error; err != nil {
  60. zaplog.Error("HealthWarning", zap.Any("error", err), zap.Any("healthWarningList", healthWarningList))
  61. }
  62. }
  63. }
  64. func (e *Entry) NeckRingHealthWarning() error {
  65. pastureList := e.FindPastureList()
  66. if pastureList == nil || len(pastureList) == 0 {
  67. return nil
  68. }
  69. for _, pasture := range pastureList {
  70. if err := e.UpdateNeckRingHealth(pasture.Id); err != nil {
  71. zaplog.Error("NeckRingHealthWarning", zap.Any("UpdateNeckRingHealth", err), zap.Any("pasture", pasture))
  72. }
  73. }
  74. return nil
  75. }
  76. func (e *Entry) UpdateNeckRingHealth(pastureId int64) error {
  77. neckRingConfigureList, err := e.FindSystemNeckRingConfigure(pastureId)
  78. if err != nil {
  79. return xerr.WithStack(err)
  80. }
  81. healthValue := int32(0)
  82. for _, v := range neckRingConfigureList {
  83. if v.Name != model.HealthWarning {
  84. continue
  85. }
  86. healthValue = int32(v.Value)
  87. }
  88. nowTime := time.Now().Local()
  89. neckRingHealthList := make([]*model.NeckRingHealth, 0)
  90. if err = e.DB.Model(new(model.NeckRingHealth)).
  91. 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").
  92. Where("pasture_id = ?", pastureId).
  93. Where("heat_date >= ?", nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)).
  94. Group("neck_ring_number").
  95. Find(&neckRingHealthList).Error; err != nil {
  96. return xerr.WithStack(err)
  97. }
  98. newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
  99. for _, v := range neckRingHealthList {
  100. newScore := calculateNewScore(v)
  101. if newScore > healthValue {
  102. continue
  103. }
  104. cowInfo, err := e.GetCowById(pastureId, v.CowId)
  105. if err != nil {
  106. continue
  107. }
  108. if cowInfo == nil {
  109. continue
  110. }
  111. newNeckRingHealthWarning := model.NewNeckRingHealthWarning(pastureId, v, cowInfo, newScore)
  112. newNeckRingHealthWarningList = append(newNeckRingHealthWarningList, newNeckRingHealthWarning)
  113. }
  114. if len(newNeckRingHealthWarningList) > 0 {
  115. if err = e.DB.Model(new(model.NeckRingHealthWarning)).
  116. Create(&newNeckRingHealthWarningList).Error; err != nil {
  117. zaplog.Error("UpdateNeckRingHealth", zap.Any("error", err), zap.Any("newNeckRingHealthWarningList", newNeckRingHealthWarningList))
  118. }
  119. }
  120. return nil
  121. }
  122. func (e *Entry) FindHealthWarning(pastureId int64, data *model.NeckRingHealth) int64 {
  123. var count int64
  124. if err := e.DB.Model(new(model.NeckRingHealth)).
  125. Where("pasture_id = ?", pastureId).
  126. Where("cow_id = ?", data.CowId).
  127. Where("heat_date = ?", data.HeatDate).
  128. Where("score = ?", data.Score).
  129. Count(&count).Error; err != nil {
  130. zaplog.Error("FindHealthWarning", zap.Any("error", err))
  131. }
  132. return count
  133. }
  134. func calculateNewScore(data *model.NeckRingHealth) int32 {
  135. return data.Score
  136. }