cow.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 int32 `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. AbortionAge int64 `json:"abortionAge"` // 流产天数
  21. CowType pasturePb.CowType_Kind `json:"cowType"`
  22. BreedStatus pasturePb.BreedStatus_Kind `json:"breedStatus"`
  23. CowKind pasturePb.CowKind_Kind `json:"cowKind"`
  24. BirthWeight int64 `json:"birthWeight"`
  25. CurrentWeight int64 `json:"currentWeight"`
  26. SourceId pasturePb.CowSource_Kind `json:"sourceId"`
  27. FatherNumber string `json:"fatherNumber"`
  28. MotherNumber string `json:"motherNumber"`
  29. IsRemove pasturePb.IsShow_Kind `json:"isRemove"`
  30. IsPregnant pasturePb.IsShow_Kind `json:"isPregnant"`
  31. HealthStatus pasturePb.HealthStatus_Kind `json:"healthStatus"`
  32. WeaningAt int64 `json:"weaningAt"`
  33. CalvingAt int64 `json:"calvingAt"`
  34. BirthAt int64 `json:"birthAti"`
  35. AdmissionAt int64 `json:"admissionAt"`
  36. FirstMatingAt int64 `json:"firstMatingAt"`
  37. LastEstrusAt int64 `json:"lastEstrusAt"`
  38. LastCalvingAt int64 `json:"lastCalvingAt"`
  39. LastMatingAt int64 `json:"lastMatingAt"`
  40. LastBullNumber string `json:"lastBullNumber"`
  41. LastPregnantCheckAt int64 `json:"lastPregnantCheckAt"`
  42. LastDryMilkAt int64 `json:"lastDryMilkAt"`
  43. LastSecondWeight int64 `json:"lastSecondWeight"`
  44. LastSecondWeightAt int64 `json:"lastSecondWeightAt"`
  45. LastAbortionAt int64 `json:"lastAbortionAt"`
  46. LastWeightAt int64 `json:"lastWeightAt"`
  47. CreatedAt int64 `json:"createdAt"`
  48. UpdatedAt int64 `json:"updatedAt"`
  49. }
  50. func (c *Cow) TableName() string {
  51. return "cow"
  52. }
  53. type CowSlice []*Cow
  54. func (c CowSlice) ToPB(
  55. penMap map[int32]*Pen,
  56. cowTypeMap map[pasturePb.CowType_Kind]string,
  57. breedStatusMap map[pasturePb.BreedStatus_Kind]string,
  58. cowKindMap map[pasturePb.CowKind_Kind]string,
  59. ) []*pasturePb.SearchCowList {
  60. res := make([]*pasturePb.SearchCowList, len(c))
  61. for i, v := range c {
  62. penName := ""
  63. if pen, ok := penMap[v.PenId]; ok {
  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: 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.EventEnterRequest) *Cow {
  89. var isPregnant = pasturePb.IsShow_No
  90. if req.BreedStatus == pasturePb.BreedStatus_Pregnant {
  91. isPregnant = pasturePb.IsShow_Ok
  92. }
  93. return &Cow{
  94. Sex: req.Sex,
  95. EarNumber: req.EarNumber,
  96. PenId: req.PenId,
  97. Lact: req.Lact,
  98. CowType: req.CowType,
  99. BreedStatus: req.BreedStatus,
  100. CowKind: req.CowKind,
  101. SourceId: req.CowSource,
  102. FatherNumber: req.FatherNumber,
  103. MotherNumber: req.MotherNumber,
  104. IsRemove: pasturePb.IsShow_Ok,
  105. HealthStatus: pasturePb.HealthStatus_Health,
  106. IsPregnant: isPregnant,
  107. WeaningAt: int64(req.WeaningAt),
  108. BirthAt: int64(req.BirthAt),
  109. FirstMatingAt: int64(req.MatingAt),
  110. LastMatingAt: int64(req.MatingAt),
  111. LastPregnantCheckAt: int64(req.PregnancyCheckAt),
  112. }
  113. }
  114. func NewCalfCow(motherId int64, fatherNumber string, calf *CalvingCalf) *Cow {
  115. return &Cow{
  116. Sex: calf.Sex,
  117. EarNumber: calf.EarNumber,
  118. PenId: calf.PenId,
  119. CowType: pasturePb.CowType_Lactating_Calf, // 哺乳犊牛
  120. BreedStatus: pasturePb.BreedStatus_UnBreed, // 未配
  121. CowKind: calf.CowKind, // 牛只品种
  122. BirthWeight: calf.BirthWeight,
  123. BirthAt: calf.BirthAt,
  124. SourceId: pasturePb.CowSource_Calving, // 产犊方式
  125. FatherNumber: fatherNumber,
  126. MotherNumber: fmt.Sprintf("%d", 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. }
  203. func (c *Cow) GetAbortionAge() int32 {
  204. if c.LastAbortionAt > 0 && c.IsRemove == pasturePb.IsShow_Ok {
  205. return int32(math.Floor(float64(time.Now().Unix()-c.LastAbortionAt) / 86400))
  206. }
  207. return 0
  208. }