cow.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. TypeId pasturePb.CowType_Kind `json:"type_id"`
  17. BreedStatusId pasturePb.BreedStatus_Kind `json:"breed_status_id"`
  18. KindId pasturePb.CowKind_Kind `json:"kind_id"`
  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. Status pasturePb.CowStatus_Kind `json:"status"`
  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 (
  47. status = pasturePb.CowStatus_Calving
  48. isPregnant = pasturePb.IsShow_No
  49. )
  50. if req.BreedStatusId == pasturePb.BreedStatus_InCheck_Pregnant ||
  51. req.BreedStatusId == pasturePb.BreedStatus_Recheck_Pregnant {
  52. isPregnant = pasturePb.IsShow_Ok
  53. status = pasturePb.CowStatus_Pregnant
  54. }
  55. return &Cow{
  56. Sex: req.Sex,
  57. EarNumber: req.EarNumber,
  58. PenId: int64(req.PenId),
  59. Lact: req.Lact,
  60. TypeId: req.CowTypeId,
  61. BreedStatusId: req.BreedStatusId,
  62. KindId: 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. Status: status,
  69. WeaningAt: int64(req.WeaningAt),
  70. BirthAt: int64(req.BirthAt),
  71. FirstMatingAt: int64(req.MatingAt),
  72. LastMatingAt: int64(req.MatingAt),
  73. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  74. }
  75. }
  76. func NewCalfCow(motherId, fatherId int64, calf *EventCalvingCalf) *Cow {
  77. return &Cow{
  78. Sex: calf.Sex,
  79. EarNumber: calf.EarNumber,
  80. PenId: int64(calf.PenId),
  81. TypeId: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  82. BreedStatusId: pasturePb.BreedStatus_Unmarried, // 未配
  83. KindId: pasturePb.CowKind_HST, // 荷斯坦
  84. BirthWeight: calf.Weight,
  85. SourceId: pasturePb.CowSource_Calving, // 产犊方式
  86. FatherId: fatherId,
  87. MotherId: motherId,
  88. IsRemove: pasturePb.IsShow_Ok,
  89. IsPregnant: pasturePb.IsShow_No,
  90. Status: pasturePb.CowStatus_Calving, // 哺乳犊牛
  91. }
  92. }
  93. type BarCowStruct struct {
  94. Number int32 `json:"number"`
  95. TypeId pasturePb.CowType_Kind `json:"type_id"`
  96. }
  97. type BarCowStructSlice []*BarCowStruct
  98. func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
  99. var pb []*pasturePb.BarCowStruct
  100. for _, v := range b {
  101. name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
  102. pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
  103. }
  104. return pb
  105. }
  106. // GetDayAge 日龄
  107. func (c *Cow) GetDayAge() int32 {
  108. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  109. }
  110. // GetDaysPregnant 在胎天数
  111. func (c *Cow) GetDaysPregnant() int32 {
  112. if c.Status == pasturePb.CowStatus_Breeding || c.Status == pasturePb.CowStatus_Pregnant {
  113. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  114. }
  115. return 0
  116. }
  117. // GetLactationDays 泌乳天数
  118. func (c *Cow) GetLactationDays() int32 {
  119. if c.Status == pasturePb.CowStatus_Calving {
  120. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  121. }
  122. return 0
  123. }
  124. // GetDayWeight 日增重
  125. func (c *Cow) GetDayWeight() float64 {
  126. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt {
  127. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  128. if days <= 0 {
  129. return 0
  130. }
  131. return math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  132. }
  133. return 0
  134. }
  135. // GetAverageDailyWeight 平均日增重
  136. func (c *Cow) GetAverageDailyWeight() float64 {
  137. if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt {
  138. days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400))
  139. if days <= 0 {
  140. return 0
  141. }
  142. dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days))
  143. return dailyWeight
  144. }
  145. return 0
  146. }