cow.go 5.5 KB

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