cow.go 7.8 KB

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