health_waning.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package crontab
  2. import (
  3. "fmt"
  4. "kpt-pasture/model"
  5. "time"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. "go.uber.org/zap"
  10. )
  11. func (e *Entry) NeckRingHealthWarning() error {
  12. pastureList := e.FindPastureList()
  13. if pastureList == nil || len(pastureList) == 0 {
  14. return nil
  15. }
  16. for _, pasture := range pastureList {
  17. e.DB.Model(new(model.NeckRingHealthWarning)).
  18. Where("pasture_id = ?", pasture.Id).
  19. Delete(new(model.NeckRingHealthWarning))
  20. if err := e.UpdateNeckRingHealth(pasture.Id); err != nil {
  21. zaplog.Error("NeckRingHealthWarning",
  22. zap.Any("UpdateNeckRingHealth", err),
  23. zap.Any("pasture", pasture),
  24. )
  25. }
  26. }
  27. return nil
  28. }
  29. func (e *Entry) UpdateNeckRingHealth(pastureId int64) error {
  30. neckRingConfigureList, err := e.FindSystemNeckRingConfigure(pastureId)
  31. if err != nil {
  32. return xerr.WithStack(err)
  33. }
  34. healthValue := int32(0)
  35. for _, v := range neckRingConfigureList {
  36. if v.Name != model.HealthWarning {
  37. continue
  38. }
  39. healthValue = int32(v.Value)
  40. }
  41. newNeckRingHealthWarningList, err := e.FindNewNeckRingHealthWarning(pastureId, healthValue)
  42. if err != nil {
  43. return xerr.WithStack(err)
  44. }
  45. if len(newNeckRingHealthWarningList) > 0 {
  46. if err = e.DB.Model(new(model.NeckRingHealthWarning)).
  47. Create(&newNeckRingHealthWarningList).Error; err != nil {
  48. zaplog.Error("UpdateNeckRingHealth",
  49. zap.Any("error", err),
  50. zap.Any("newNeckRingHealthWarningList", newNeckRingHealthWarningList),
  51. )
  52. }
  53. }
  54. return nil
  55. }
  56. func (e *Entry) FindNewNeckRingHealthWarning(pastureId int64, healthValue int32) ([]*model.NeckRingHealthWarning, error) {
  57. nowTime := time.Now().Local()
  58. startTime := nowTime.AddDate(0, 0, -1).Format(model.LayoutDate2)
  59. type MaxId struct {
  60. Id int64
  61. }
  62. maxIds := make([]*MaxId, 0)
  63. sqlQuery := fmt.Sprintf(`SELECT MAX(d.id) AS id FROM neck_ring_health d
  64. WHERE d.heat_date>= %s AND d.pasture_id = %d AND NOT EXISTS
  65. (SELECT 1 FROM neck_active_habit h WHERE h.cow_id=d.cow_id AND h.heat_date=%s AND d.heat_date=%s)
  66. GROUP BY d.cow_id`, startTime, pastureId, nowTime.Format(model.LayoutDate2), startTime)
  67. if err := e.DB.Raw(sqlQuery).
  68. Scan(&maxIds).Error; err != nil {
  69. zaplog.Error("FindNewNeckRingHealthWarning", zap.Any("error", err), zap.Any("sqlQuery", sqlQuery))
  70. return nil, xerr.WithStack(err)
  71. }
  72. neckRingHealthList := make([]*model.NeckRingHealth, 0)
  73. if len(maxIds) > 0 {
  74. if err := e.DB.Model(new(model.NeckRingHealth)).
  75. Where("pasture_id = ?", pastureId).
  76. Where("heat_date >= ?", startTime).
  77. Where("id IN (?)", maxIds).
  78. Group("cow_id").
  79. Find(&neckRingHealthList).Error; err != nil {
  80. return nil, xerr.WithStack(err)
  81. }
  82. }
  83. newNeckRingHealthWarningList := make([]*model.NeckRingHealthWarning, 0)
  84. for _, v := range neckRingHealthList {
  85. if v.HeatDate == "" {
  86. continue
  87. }
  88. cowInfo, err := e.GetCowById(pastureId, v.CowId)
  89. if err != nil || cowInfo == nil {
  90. continue
  91. }
  92. newScore := calculateNewScore(v)
  93. zaplog.Info("calculateNewScore",
  94. zap.Any("newScore", newScore),
  95. zap.Any("v", v),
  96. zap.Any("healthValue", healthValue),
  97. zap.Any("cowInfo", cowInfo),
  98. )
  99. if newScore > healthValue {
  100. continue
  101. }
  102. if e.HistoryNeckRingHealthWarning(pastureId, cowInfo.NeckRingNumber, v.HeatDate) {
  103. continue
  104. }
  105. if e.FindNeckRingError(pastureId, cowInfo.NeckRingNumber) {
  106. continue
  107. }
  108. newNeckRingHealthWarning := model.NewNeckRingHealthWarning(pastureId, v, cowInfo, newScore)
  109. zaplog.Info("newNeckRingHealthWarning",
  110. zap.Any("newNeckRingHealthWarning", newNeckRingHealthWarning),
  111. zap.Any("pastureId", pastureId),
  112. zap.Any("neckRingHealth", v),
  113. zap.Any("cowInfo", cowInfo),
  114. zap.Any("newScore", newScore),
  115. )
  116. newNeckRingHealthWarningList = append(newNeckRingHealthWarningList, newNeckRingHealthWarning)
  117. }
  118. return newNeckRingHealthWarningList, nil
  119. }
  120. func (e *Entry) HistoryNeckRingHealthWarning(pastureId int64, neckRingNumber string, heatDate string) bool {
  121. var count int64
  122. if err := e.DB.Model(new(model.NeckRingHealthWarning)).
  123. Where("pasture_id = ?", pastureId).
  124. Where("neck_ring_number = ?", neckRingNumber).
  125. Where("heat_date = ?", heatDate).
  126. Where("is_show = ?", pasturePb.IsShow_Ok).
  127. Count(&count).Error; err != nil {
  128. return false
  129. }
  130. return count > 0
  131. }
  132. func (e *Entry) FindNeckRingError(pastureId int64, neckRingNumber string) bool {
  133. var count int64
  134. if err := e.DB.Model(new(model.NeckRing)).
  135. Where("pasture_id = ?", pastureId).
  136. Where("neck_ring_number = ?", neckRingNumber).
  137. Where("status = ?", pasturePb.IsShow_No).
  138. Where("error_kind = ?", pasturePb.NeckRingNumberError_Suspected_Fall_Off).
  139. Count(&count).Error; err != nil {
  140. return false
  141. }
  142. return count > 0
  143. }