cow.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. CowType pasturePb.CowType_Kind `json:"cow_type"`
  18. BreedStatus pasturePb.BreedStatus_Kind `json:"breed_status"`
  19. CowKind pasturePb.CowKind_Kind `json:"cow_kind"`
  20. BirthWeight int64 `json:"birth_weight"`
  21. CurrentWeight int64 `json:"current_weight"`
  22. SourceId pasturePb.CowSource_Kind `json:"source_id"`
  23. FatherId int64 `json:"father_id"`
  24. MotherId int64 `json:"mother_id"`
  25. IsRemove pasturePb.IsShow_Kind `json:"is_remove"`
  26. IsPregnant pasturePb.IsShow_Kind `json:"is_pregnant"`
  27. WeaningAt int64 `json:"weaning_at"`
  28. BirthAt int64 `json:"birth_at"`
  29. FirstMatingAt int64 `json:"first_mating_at"`
  30. LastEstrusAt int64 `json:"last_estrus_at"`
  31. LastCalvingAt int64 `json:"last_calving_at"`
  32. LastMatingAt int64 `json:"last_mating_at"`
  33. LastBullId int64 `json:"last_bull_id"`
  34. LastPregnantCheckAt int64 `json:"last_pregnant_check_at"`
  35. LastDryMilkAt int64 `json:"last_dry_milk_at"`
  36. LastSecondWeight int64 `json:"last_second_weight"`
  37. LastSecondWeightAt int64 `json:"last_second_weight_at"`
  38. LastWeightAt int64 `json:"last_weight_at"`
  39. CreatedAt int64 `json:"created_at"`
  40. UpdatedAt int64 `json:"updated_at"`
  41. }
  42. func (c *Cow) TableName() string {
  43. return "cow"
  44. }
  45. func NewCow(req *pasturePb.SearchEnterData) *Cow {
  46. var isPregnant = pasturePb.IsShow_No
  47. if req.BreedStatusId == pasturePb.BreedStatus_Pregnant {
  48. isPregnant = pasturePb.IsShow_Ok
  49. }
  50. return &Cow{
  51. Sex: req.Sex,
  52. EarNumber: req.EarNumber,
  53. PenId: int64(req.PenId),
  54. Lact: req.Lact,
  55. CowType: req.CowTypeId,
  56. BreedStatus: req.BreedStatusId,
  57. CowKind: req.CowKindId,
  58. SourceId: req.CowSourceId,
  59. FatherId: int64(req.FatherId),
  60. MotherId: int64(req.MotherId),
  61. IsRemove: pasturePb.IsShow_Ok,
  62. IsPregnant: isPregnant,
  63. WeaningAt: int64(req.WeaningAt),
  64. BirthAt: int64(req.BirthAt),
  65. FirstMatingAt: int64(req.MatingAt),
  66. LastMatingAt: int64(req.MatingAt),
  67. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  68. }
  69. }
  70. func NewCalfCow(motherId, fatherId int64, calf *EventCalvingCalf) *Cow {
  71. return &Cow{
  72. Sex: calf.Sex,
  73. EarNumber: calf.EarNumber,
  74. PenId: int64(calf.PenId),
  75. CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  76. BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配
  77. CowKind: calf.CowKind, // 牛只品种
  78. BirthWeight: calf.Weight,
  79. SourceId: pasturePb.CowSource_Calving, // 产犊方式
  80. FatherId: fatherId,
  81. MotherId: motherId,
  82. IsRemove: pasturePb.IsShow_Ok,
  83. IsPregnant: pasturePb.IsShow_No,
  84. }
  85. }
  86. type BarCowStruct struct {
  87. Number int32 `json:"number"`
  88. TypeId pasturePb.CowType_Kind `json:"type_id"`
  89. }
  90. type BarCowStructSlice []*BarCowStruct
  91. func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
  92. var pb []*pasturePb.BarCowStruct
  93. for _, v := range b {
  94. name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
  95. pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
  96. }
  97. return pb
  98. }
  99. // GetDayAge 日龄
  100. func (c *Cow) GetDayAge() int32 {
  101. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  102. }
  103. // GetDaysPregnant 在胎天数
  104. func (c *Cow) GetDaysPregnant() int32 {
  105. if c.BreedStatus == pasturePb.BreedStatus_Pregnant {
  106. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  107. }
  108. return 0
  109. }
  110. // GetLactationDays 泌乳天数
  111. func (c *Cow) GetLactationDays() int32 {
  112. if c.BreedStatus == pasturePb.BreedStatus_Calving {
  113. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  114. }
  115. return 0
  116. }
  117. // GetDayWeight 日增重
  118. func (c *Cow) GetDayWeight() float64 {
  119. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt {
  120. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  121. if days <= 0 {
  122. return 0
  123. }
  124. return math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  125. }
  126. return 0
  127. }
  128. // GetAverageDailyWeight 平均日增重
  129. func (c *Cow) GetAverageDailyWeight() float64 {
  130. if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt {
  131. days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400))
  132. if days <= 0 {
  133. return 0
  134. }
  135. dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days))
  136. return dailyWeight
  137. }
  138. return 0
  139. }