cow.go 7.8 KB

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