neck_ring_estrus_warning.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. res[i] = &pasturePb.EstrusItems{
  79. Id: int32(v.Id),
  80. CowId: int32(v.CowId),
  81. EarNumber: v.EarNumber,
  82. PenId: cow.PenId,
  83. PenName: cow.PenName,
  84. DayAge: cow.DayAge,
  85. MatingTimes: cow.MatingTimes,
  86. Lact: cow.Lact,
  87. CalvingAge: cow.CalvingAge,
  88. AbortionAge: cow.AbortionAge,
  89. OptimumMatingStartTime: pzHour.Add(-4 * time.Hour).Format(LayoutTime),
  90. OptimumMatingEndTime: pzHour.Add(4 * time.Hour).Format(LayoutTime),
  91. LastBreedEventDetails: lastBreedEventDetails,
  92. Level: v.Level,
  93. EstrusInterval: 0,
  94. BestMatingTime: pzHour.Format(LayoutTime),
  95. }
  96. }
  97. return res
  98. }
  99. const (
  100. Nb1 = 12
  101. Nb2 = 8
  102. MinNb1 = 10
  103. )
  104. type EstrusWarning struct {
  105. NeckRingEstrusId int64 `json:"neckRingEstrusId"`
  106. HighChange string `json:"highChange"`
  107. CowId int64 `json:"cowId"`
  108. DateTime string `json:"dateTime"`
  109. Nb1 int32 `json:"nb1"`
  110. Nb2 int32 `json:"nb2"`
  111. }
  112. type DashboardNeckRingEstrusWarning struct {
  113. Level pasturePb.EstrusLevel_Kind
  114. Count int32
  115. }