cow.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package model
  2. import (
  3. "fmt"
  4. "math"
  5. "time"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. )
  8. type Cow struct {
  9. Id int64 `json:"id"`
  10. Sex pasturePb.Genders_Kind `json:"sex"`
  11. NeckRingNumber string `json:"neck_ring_number"`
  12. EarNumber string `json:"ear_number"`
  13. EarOldNumber string `json:"ear_old_number"`
  14. PenId int64 `json:"pen_id"`
  15. Lact int32 `json:"lact"`
  16. DayAge int32 `json:"day_age"`
  17. CalvingAge int64 `json:"calving_age"`
  18. PregnancyAge int64 `json:"pregnancy_age"` // 怀孕天数 孕检结果有阳性更新,产犊后至0
  19. AdmissionAge int64 `json:"admission_age"`
  20. CowType pasturePb.CowType_Kind `json:"cow_type"`
  21. BreedStatus pasturePb.BreedStatus_Kind `json:"breed_status"`
  22. CowKind pasturePb.CowKind_Kind `json:"cow_kind"`
  23. BirthWeight int64 `json:"birth_weight"`
  24. CurrentWeight int64 `json:"current_weight"`
  25. SourceId pasturePb.CowSource_Kind `json:"source_id"`
  26. FatherId int64 `json:"father_id"`
  27. MotherId int64 `json:"mother_id"`
  28. IsRemove pasturePb.IsShow_Kind `json:"is_remove"`
  29. IsPregnant pasturePb.IsShow_Kind `json:"is_pregnant"`
  30. WeaningAt int64 `json:"weaning_at"`
  31. CalvingAt int64 `json:"calving_at"`
  32. BirthAt int64 `json:"birth_at"`
  33. AdmissionAt int64 `json:"admission_at"`
  34. FirstMatingAt int64 `json:"first_mating_at"`
  35. LastEstrusAt int64 `json:"last_estrus_at"`
  36. LastCalvingAt int64 `json:"last_calving_at"`
  37. LastMatingAt int64 `json:"last_mating_at"`
  38. LastBullId int64 `json:"last_bull_id"`
  39. LastPregnantCheckAt int64 `json:"last_pregnant_check_at"`
  40. LastDryMilkAt int64 `json:"last_dry_milk_at"`
  41. LastSecondWeight int64 `json:"last_second_weight"`
  42. LastSecondWeightAt int64 `json:"last_second_weight_at"`
  43. LastWeightAt int64 `json:"last_weight_at"`
  44. CreatedAt int64 `json:"created_at"`
  45. UpdatedAt int64 `json:"updated_at"`
  46. }
  47. func (c *Cow) TableName() string {
  48. return "cow"
  49. }
  50. func NewCow(req *pasturePb.SearchEnterData) *Cow {
  51. var isPregnant = pasturePb.IsShow_No
  52. if req.BreedStatusId == pasturePb.BreedStatus_Pregnant {
  53. isPregnant = pasturePb.IsShow_Ok
  54. }
  55. return &Cow{
  56. Sex: req.Sex,
  57. EarNumber: req.EarNumber,
  58. PenId: int64(req.PenId),
  59. Lact: req.Lact,
  60. CowType: req.CowTypeId,
  61. BreedStatus: req.BreedStatusId,
  62. CowKind: req.CowKindId,
  63. SourceId: req.CowSourceId,
  64. FatherId: int64(req.FatherId),
  65. MotherId: int64(req.MotherId),
  66. IsRemove: pasturePb.IsShow_Ok,
  67. IsPregnant: isPregnant,
  68. WeaningAt: int64(req.WeaningAt),
  69. BirthAt: int64(req.BirthAt),
  70. FirstMatingAt: int64(req.MatingAt),
  71. LastMatingAt: int64(req.MatingAt),
  72. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  73. }
  74. }
  75. func NewCalfCow(motherId, fatherId int64, calf *EventCalvingCalf) *Cow {
  76. return &Cow{
  77. Sex: calf.Sex,
  78. EarNumber: calf.EarNumber,
  79. PenId: int64(calf.PenId),
  80. CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  81. BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配
  82. CowKind: calf.CowKind, // 牛只品种
  83. BirthWeight: calf.Weight,
  84. SourceId: pasturePb.CowSource_Calving, // 产犊方式
  85. FatherId: fatherId,
  86. MotherId: motherId,
  87. IsRemove: pasturePb.IsShow_Ok,
  88. IsPregnant: pasturePb.IsShow_No,
  89. }
  90. }
  91. type BarCowStruct struct {
  92. Number int32 `json:"number"`
  93. TypeId pasturePb.CowType_Kind `json:"type_id"`
  94. }
  95. type BarCowStructSlice []*BarCowStruct
  96. func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
  97. var pb []*pasturePb.BarCowStruct
  98. for _, v := range b {
  99. name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
  100. pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
  101. }
  102. return pb
  103. }
  104. // GetDayAge 日龄
  105. func (c *Cow) GetDayAge() int32 {
  106. if c.BirthAt <= 0 {
  107. return 0
  108. }
  109. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  110. }
  111. // GetCalvingAge 产后天数
  112. func (c *Cow) GetCalvingAge() int64 {
  113. if c.CalvingAt <= 0 {
  114. return 0
  115. }
  116. return int64(math.Floor(float64(time.Now().Unix()-c.CalvingAt) / 86400))
  117. }
  118. // GetDaysPregnant 怀孕天数
  119. func (c *Cow) GetDaysPregnant() int32 {
  120. if c.BreedStatus == pasturePb.BreedStatus_Pregnant && c.IsRemove == pasturePb.IsShow_No && c.IsPregnant == pasturePb.IsShow_Ok {
  121. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  122. }
  123. return 0
  124. }
  125. // GetLactationDays 泌乳天数
  126. func (c *Cow) GetLactationDays() int32 {
  127. if c.BreedStatus == pasturePb.BreedStatus_Calving && c.IsRemove == pasturePb.IsShow_Ok {
  128. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  129. }
  130. return 0
  131. }
  132. // GetAdmissionAge 入场天数
  133. func (c *Cow) GetAdmissionAge() int32 {
  134. if c.AdmissionAt > 0 && c.IsRemove == pasturePb.IsShow_Ok {
  135. return int32(math.Floor(float64(time.Now().Unix()-c.AdmissionAt) / 86400))
  136. }
  137. return 0
  138. }
  139. // GetDayWeight 日增重
  140. func (c *Cow) GetDayWeight() float64 {
  141. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt {
  142. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  143. if days <= 0 {
  144. return 0
  145. }
  146. return math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  147. }
  148. return 0
  149. }
  150. // GetAverageDailyWeight 平均日增重
  151. func (c *Cow) GetAverageDailyWeight() float64 {
  152. if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt {
  153. days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400))
  154. if days <= 0 {
  155. return 0
  156. }
  157. dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days))
  158. return dailyWeight
  159. }
  160. return 0
  161. }