cow.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. type CowSlice []*Cow
  51. func (c CowSlice) ToPB(
  52. penList []*Pen,
  53. cowTypeMap map[pasturePb.CowType_Kind]string,
  54. breedStatusMap map[pasturePb.BreedStatus_Kind]string,
  55. cowKindMap map[pasturePb.CowKind_Kind]string,
  56. ) []*pasturePb.SearchCowList {
  57. res := make([]*pasturePb.SearchCowList, len(c))
  58. for i, v := range c {
  59. penName := ""
  60. for _, pen := range penList {
  61. if v.PenId != int64(pen.Id) {
  62. continue
  63. }
  64. penName = pen.Name
  65. }
  66. res[i] = &pasturePb.SearchCowList{
  67. CowId: int32(v.Id),
  68. Sex: v.Sex,
  69. NeckRingNumber: v.NeckRingNumber,
  70. EarNumber: v.EarNumber,
  71. PenId: int32(v.PenId),
  72. PenName: penName,
  73. CowType: int32(v.CowType),
  74. Lact: v.Lact,
  75. CowTypeName: cowTypeMap[v.CowType],
  76. BreedStatus: v.BreedStatus,
  77. BreedStatusName: breedStatusMap[v.BreedStatus],
  78. CowKind: v.CowKind,
  79. CowKindName: cowKindMap[v.CowKind],
  80. BirthAt: int32(v.BirthAt),
  81. BirthWeight: int32(v.BirthWeight) / 1000,
  82. CurrentWeight: int32(v.CurrentWeight) / 1000,
  83. LastWeightAt: int32(v.LastWeightAt),
  84. }
  85. }
  86. return res
  87. }
  88. func NewCow(req *pasturePb.EventEnterData) *Cow {
  89. var isPregnant = pasturePb.IsShow_No
  90. if req.BreedStatusId == pasturePb.BreedStatus_Pregnant {
  91. isPregnant = pasturePb.IsShow_Ok
  92. }
  93. return &Cow{
  94. Sex: req.Sex,
  95. EarNumber: req.EarNumber,
  96. PenId: int64(req.PenId),
  97. Lact: req.Lact,
  98. CowType: req.CowTypeId,
  99. BreedStatus: req.BreedStatusId,
  100. CowKind: req.CowKindId,
  101. SourceId: req.CowSourceId,
  102. FatherId: int64(req.FatherId),
  103. MotherId: int64(req.MotherId),
  104. IsRemove: pasturePb.IsShow_Ok,
  105. IsPregnant: isPregnant,
  106. WeaningAt: int64(req.WeaningAt),
  107. BirthAt: int64(req.BirthAt),
  108. FirstMatingAt: int64(req.MatingAt),
  109. LastMatingAt: int64(req.MatingAt),
  110. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  111. }
  112. }
  113. func NewCalfCow(motherId, fatherId int64, calf *CalvingCalf) *Cow {
  114. return &Cow{
  115. Sex: calf.Sex,
  116. EarNumber: calf.EarNumber,
  117. PenId: int64(calf.PenId),
  118. CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  119. BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配
  120. CowKind: calf.CowKind, // 牛只品种
  121. BirthWeight: calf.BrithWeight,
  122. SourceId: pasturePb.CowSource_Calving, // 产犊方式
  123. FatherId: fatherId,
  124. MotherId: motherId,
  125. IsRemove: pasturePb.IsShow_Ok,
  126. IsPregnant: pasturePb.IsShow_No,
  127. }
  128. }
  129. type BarCowStruct struct {
  130. Number int32 `json:"number"`
  131. TypeId pasturePb.CowType_Kind `json:"type_id"`
  132. }
  133. type BarCowStructSlice []*BarCowStruct
  134. func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
  135. var pb []*pasturePb.BarCowStruct
  136. for _, v := range b {
  137. name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
  138. pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
  139. }
  140. return pb
  141. }
  142. // GetDayAge 日龄
  143. func (c *Cow) GetDayAge() int32 {
  144. if c.BirthAt <= 0 {
  145. return 0
  146. }
  147. return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))
  148. }
  149. // GetCalvingAge 产后天数
  150. func (c *Cow) GetCalvingAge() int64 {
  151. if c.CalvingAt <= 0 {
  152. return 0
  153. }
  154. return int64(math.Floor(float64(time.Now().Unix()-c.CalvingAt) / 86400))
  155. }
  156. // GetDaysPregnant 怀孕天数
  157. func (c *Cow) GetDaysPregnant() int32 {
  158. if c.BreedStatus == pasturePb.BreedStatus_Pregnant && c.IsRemove == pasturePb.IsShow_No && c.IsPregnant == pasturePb.IsShow_Ok {
  159. return int32(math.Floor(float64(time.Now().Unix()-c.LastMatingAt) / 86400))
  160. }
  161. return 0
  162. }
  163. // GetLactationDays 泌乳天数
  164. func (c *Cow) GetLactationDays() int32 {
  165. if c.BreedStatus == pasturePb.BreedStatus_Calving && c.IsRemove == pasturePb.IsShow_Ok {
  166. return int32(math.Floor(float64(time.Now().Unix()-c.LastCalvingAt) / 86400))
  167. }
  168. return 0
  169. }
  170. // GetAdmissionAge 入场天数
  171. func (c *Cow) GetAdmissionAge() int32 {
  172. if c.AdmissionAt > 0 && c.IsRemove == pasturePb.IsShow_Ok {
  173. return int32(math.Floor(float64(time.Now().Unix()-c.AdmissionAt) / 86400))
  174. }
  175. return 0
  176. }
  177. // GetDayWeight 日增重
  178. func (c *Cow) GetDayWeight() float64 {
  179. if c.CurrentWeight-c.LastSecondWeight > 0 && c.LastWeightAt > c.LastSecondWeightAt {
  180. days := int32(math.Floor(float64(c.LastWeightAt-c.LastSecondWeightAt) / 86400))
  181. if days <= 0 {
  182. return 0
  183. }
  184. return math.Round(1.0 * float64(c.CurrentWeight-c.LastSecondWeight) / float64(days))
  185. }
  186. return 0
  187. }
  188. // GetAverageDailyWeight 平均日增重
  189. func (c *Cow) GetAverageDailyWeight() float64 {
  190. if c.CurrentWeight-c.BirthWeight > 0 && c.LastWeightAt > c.BirthAt {
  191. days := int32(math.Floor(float64(c.LastWeightAt-c.BirthAt) / 86400))
  192. if days <= 0 {
  193. return 0
  194. }
  195. dailyWeight := math.Round(1.0 * float64(c.CurrentWeight-c.BirthWeight) / float64(days))
  196. return dailyWeight
  197. }
  198. return 0
  199. }