cow.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package model
  2. import (
  3. "math"
  4. "time"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. type Cow struct {
  8. Id int64 `json:"id"`
  9. Sex pasturePb.Genders_Kind `json:"sex"`
  10. NeckRingNumber string `json:"neck_ring_number"`
  11. EarNumber string `json:"ear_number"`
  12. EarOldNumber string `json:"ear_old_number"`
  13. PenId int64 `json:"pen_id"`
  14. Lact int32 `json:"lact"`
  15. TypeId pasturePb.CowType_Kind `json:"type_id"`
  16. BreedStatusId pasturePb.BreedStatus_Kind `json:"breed_status_id"`
  17. KindId pasturePb.CowKind_Kind `json:"kind_id"`
  18. BirthWeight int64 `json:"birth_weight"`
  19. CurrentWeight int64 `json:"current_weight"`
  20. SourceId pasturePb.CowSource_Kind `json:"source_id"`
  21. FatherId int64 `json:"father_id"`
  22. MotherId int64 `json:"mother_id"`
  23. IsRemove pasturePb.IsShow_Kind `json:"is_remove"`
  24. IsPregnant pasturePb.IsShow_Kind `json:"is_pregnant"`
  25. Status pasturePb.CowStatus_Kind `json:"status"`
  26. WeaningAt int64 `json:"weaning_at"`
  27. BirthAt int64 `json:"birth_at"`
  28. FirstMatingAt int64 `json:"first_mating_at"`
  29. LastEstrusAt int64 `json:"last_estrus_at"`
  30. LastCalvingAt int64 `json:"last_calving_at"`
  31. LastMatingAt int64 `json:"last_mating_at"`
  32. LastBullId int64 `json:"last_bull_id"`
  33. LastPregnantCheckAt int64 `json:"last_pregnant_check_at"`
  34. LastDryMilkAt int64 `json:"last_dry_milk_at"`
  35. LastSecondWeight int64 `json:"last_second_weight"`
  36. LastSecondWeightAt int64 `json:"last_second_weight_at"`
  37. LastWeightAt int64 `json:"last_weight_at"`
  38. CreatedAt int64 `json:"created_at"`
  39. UpdatedAt int64 `json:"updated_at"`
  40. }
  41. func (c *Cow) TableName() string {
  42. return "cow"
  43. }
  44. func NewCow(req *pasturePb.SearchEnterData) *Cow {
  45. var (
  46. status = pasturePb.CowStatus_Calving
  47. isPregnant = pasturePb.IsShow_No
  48. )
  49. if req.BreedStatusId == pasturePb.BreedStatus_InCheck_Pregnant ||
  50. req.BreedStatusId == pasturePb.BreedStatus_Recheck_Pregnant {
  51. isPregnant = pasturePb.IsShow_Ok
  52. status = pasturePb.CowStatus_Pregnant
  53. }
  54. return &Cow{
  55. Sex: req.Sex,
  56. EarNumber: req.EarNumber,
  57. PenId: int64(req.PenId),
  58. Lact: req.Lact,
  59. TypeId: req.CowTypeId,
  60. BreedStatusId: req.BreedStatusId,
  61. KindId: req.CowKindId,
  62. SourceId: req.CowSourceId,
  63. FatherId: int64(req.FatherId),
  64. MotherId: int64(req.MotherId),
  65. IsRemove: pasturePb.IsShow_Ok,
  66. IsPregnant: isPregnant,
  67. Status: status,
  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. TypeId: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  81. BreedStatusId: pasturePb.BreedStatus_Unmarried, // 未配
  82. KindId: pasturePb.CowKind_HST, // 荷斯坦
  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. Status: pasturePb.CowStatus_Calving, // 哺乳犊牛
  90. }
  91. }
  92. // GetDayAge 日龄
  93. func (c *Cow) GetDayAge() int32 {
  94. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  95. }
  96. // GetDaysPregnant 在胎天数
  97. func (c *Cow) GetDaysPregnant() int32 {
  98. if c.Status == pasturePb.CowStatus_Breeding || c.Status == pasturePb.CowStatus_Pregnant {
  99. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  100. }
  101. return 0
  102. }
  103. // GetLactationDays 泌乳天数
  104. func (c *Cow) GetLactationDays() int32 {
  105. if c.Status == pasturePb.CowStatus_Calving {
  106. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  107. }
  108. return 0
  109. }
  110. // GetDayWeight 日增重
  111. func (c *Cow) GetDayWeight() float64 {
  112. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt {
  113. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  114. if days <= 0 {
  115. return 0
  116. }
  117. return math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  118. }
  119. return 0
  120. }
  121. // GetAverageDailyWeight 平均日增重
  122. func (c *Cow) GetAverageDailyWeight() float64 {
  123. if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt {
  124. days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400))
  125. if days <= 0 {
  126. return 0
  127. }
  128. dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days))
  129. return dailyWeight
  130. }
  131. return 0
  132. }