neck_ring_estrus_warning.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package model
  2. import (
  3. "time"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. )
  6. type NeckRingEstrusWarning struct {
  7. Id int64 `json:"id"`
  8. NeckRingEstrusId int64 `json:"neckRingEstrusId"`
  9. PastureId int64 `json:"pastureId"`
  10. CowId int64 `json:"cowId"`
  11. EarNumber string `json:"earNumber"`
  12. NeckRingNumber string `json:"neckRingNumber"`
  13. FirstTime string `json:"firstTime"`
  14. DateTime string `json:"dateTime"`
  15. LastTime string `json:"lastTime"`
  16. IsPeak pasturePb.IsShow_Kind `json:"isPeak"`
  17. WarningKind pasturePb.Warning_Kind `json:"warningKind"`
  18. Level pasturePb.EstrusLevel_Kind `json:"level"`
  19. HighChange string `json:"highChange"`
  20. IsShow pasturePb.IsShow_Kind `json:"isShow"`
  21. CreatedAt int64 `json:"createdAt"`
  22. UpdatedAt int64 `json:"updatedAt"`
  23. }
  24. func (n *NeckRingEstrusWarning) TableName() string {
  25. return "neck_ring_estrus_warning"
  26. }
  27. func NewNeckRingEstrusWarning(
  28. neckRingEstrusId, pastureId, cowId int64,
  29. earNumber, neckRingNumber, firstTime, dateTime, lastTime string,
  30. warningKind pasturePb.Warning_Kind,
  31. level pasturePb.EstrusLevel_Kind,
  32. ) *NeckRingEstrusWarning {
  33. return &NeckRingEstrusWarning{
  34. NeckRingEstrusId: neckRingEstrusId,
  35. PastureId: pastureId,
  36. CowId: cowId,
  37. EarNumber: earNumber,
  38. NeckRingNumber: neckRingNumber,
  39. FirstTime: firstTime,
  40. DateTime: dateTime,
  41. LastTime: lastTime,
  42. WarningKind: warningKind,
  43. Level: level,
  44. IsShow: pasturePb.IsShow_Ok,
  45. }
  46. }
  47. // calculatePzHour 计算最佳配置时间
  48. func (n *NeckRingEstrusWarning) calculatePzHour(lact int32) time.Time {
  49. var pzHour time.Time
  50. dateTime, _ := time.Parse(LayoutTime, n.DateTime)
  51. firstTime, _ := time.Parse(LayoutTime, n.FirstTime)
  52. // 条件判断
  53. if n.IsPeak == pasturePb.IsShow_Ok || dateTime.Sub(firstTime).Hours() >= 8 {
  54. pzHour = dateTime.Add(8 * time.Hour) // v.datetime + INTERVAL 8 HOUR
  55. } else {
  56. pzHour = firstTime.Add(16 * time.Hour) // v.firsttime + INTERVAL 16 HOUR
  57. }
  58. // 胎次调整
  59. if lact >= 3 {
  60. pzHour = pzHour.Add(-1 * time.Hour) // 减去 1 小时
  61. }
  62. return pzHour
  63. }
  64. type NeckRingEstrusWarningSlice []*NeckRingEstrusWarning
  65. func (n NeckRingEstrusWarningSlice) ToPB(cowMap map[int64]*Cow, eventLogMap map[int64]string) []*pasturePb.EstrusItems {
  66. res := make([]*pasturePb.EstrusItems, len(n))
  67. for i, v := range n {
  68. cow, ok := cowMap[v.CowId]
  69. if !ok {
  70. cow = &Cow{Id: v.CowId}
  71. }
  72. lastBreedEventDetails := ""
  73. desc, ok := eventLogMap[cow.Id]
  74. if ok {
  75. lastBreedEventDetails = desc
  76. }
  77. pzHour := v.calculatePzHour(cow.Lact)
  78. estrusInterval := int32(0)
  79. if v.LastTime != "" {
  80. lastTime, _ := time.Parse(LayoutTime, v.LastTime)
  81. diff := pzHour.Sub(lastTime)
  82. estrusInterval = int32(diff.Hours() / 24)
  83. }
  84. res[i] = &pasturePb.EstrusItems{
  85. Id: int32(v.Id),
  86. CowId: int32(v.CowId),
  87. EarNumber: v.EarNumber,
  88. PenId: cow.PenId,
  89. PenName: cow.PenName,
  90. DayAge: cow.DayAge,
  91. MatingTimes: cow.MatingTimes,
  92. Lact: cow.Lact,
  93. CalvingAge: cow.CalvingAge,
  94. AbortionAge: cow.AbortionAge,
  95. OptimumMatingStartTime: pzHour.Add(-4 * time.Hour).Format(LayoutTime),
  96. OptimumMatingEndTime: pzHour.Add(4 * time.Hour).Format(LayoutTime),
  97. LastBreedEventDetails: lastBreedEventDetails,
  98. Level: v.Level,
  99. EstrusInterval: estrusInterval,
  100. BestMatingTime: pzHour.Format(LayoutTime),
  101. EstrusStartTime: pzHour.Add(-8 * time.Hour).Format(LayoutTime),
  102. EstrusEndTime: pzHour.Add(8 * time.Hour).Format(LayoutTime),
  103. }
  104. }
  105. return res
  106. }
  107. const (
  108. Nb1 = 12
  109. Nb2 = 8
  110. MinNb1 = 10
  111. )
  112. type EstrusWarning struct {
  113. NeckRingEstrusId int64 `json:"neckRingEstrusId"`
  114. HighChange string `json:"highChange"`
  115. CowId int64 `json:"cowId"`
  116. DateTime string `json:"dateTime"`
  117. Nb1 int32 `json:"nb1"`
  118. Nb2 int32 `json:"nb2"`
  119. }
  120. type DashboardNeckRingEstrusWarning struct {
  121. Level pasturePb.EstrusLevel_Kind
  122. Count int32
  123. }