event_weight.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package model
  2. import (
  3. "math"
  4. "time"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. )
  7. type EventWeight struct {
  8. ID int64 `json:"id"`
  9. PastureId int64 `json:"pastureId"`
  10. CowId int64 `json:"cowId"`
  11. EarNumber string `json:"earNumber"`
  12. DayAge int32 `json:"dayAge"`
  13. LactationDay int64 `json:"lactationDay"`
  14. PenId int32 `json:"penId"`
  15. Lact int32 `json:"lact"`
  16. Weight int32 `json:"weight"`
  17. Height int32 `json:"height"`
  18. WeightAt int64 `json:"weightAt"`
  19. Remarks string `json:"remarks"`
  20. OperationId int32 `json:"operationId"`
  21. OperationName string `json:"operationName"`
  22. MessageId int64 `json:"messageId"`
  23. MessageName string `json:"messageName"`
  24. CreatedAt int64 `json:"created_at"`
  25. UpdatedAt int64 `json:"updated_at"`
  26. }
  27. func (c *EventWeight) TableName() string {
  28. return "event_weight"
  29. }
  30. func NewEventWeight(pastureId int64, cow *Cow, currentUser *SystemUser, item *pasturePb.EventWeight) *EventWeight {
  31. return &EventWeight{
  32. PastureId: pastureId,
  33. CowId: cow.Id,
  34. EarNumber: cow.EarNumber,
  35. Weight: int32(item.Weight * 1000),
  36. Height: item.Height,
  37. Lact: cow.Lact,
  38. DayAge: cow.GetDayAge(),
  39. WeightAt: int64(item.WeightAt),
  40. Remarks: item.Remarks,
  41. MessageId: currentUser.Id,
  42. MessageName: currentUser.Name,
  43. OperationId: item.OperationId,
  44. OperationName: item.OperationName,
  45. }
  46. }
  47. type EventWeightSlice []*EventWeight
  48. func (e EventWeightSlice) ToPB(cowInfo *Cow, eventCowLogList []*EventCowLog) *pasturePb.CowGrowthCurveData {
  49. res := &pasturePb.CowGrowthCurveData{
  50. Chart: &pasturePb.CowBehaviorCurveChart{
  51. Headers: make([]string, 0),
  52. Weight: make([]float32, 0),
  53. EventList: make([]*pasturePb.EventHappen, 0),
  54. },
  55. Table: make([]*pasturePb.CowBehaviorCurveTable, len(e)),
  56. }
  57. for i, v := range e {
  58. weightAtFormat := ""
  59. if v.WeightAt > 0 {
  60. weightAtFormat = time.Unix(v.WeightAt, 0).Local().Format(LayoutDate2)
  61. }
  62. weight := float32(0)
  63. if v.Weight > 0 {
  64. weight = float32(v.Weight) / 1000
  65. }
  66. avgWeight := float32(0)
  67. if i > 0 {
  68. avgWeight = float32(v.Weight-e[i-1].Weight) / float32(v.WeightAt-e[i-1].WeightAt) / 24 / 1000
  69. }
  70. // 阶段日增重
  71. stageDayAddWeight, monthAvgAddWeight := float32(0), float32(0)
  72. if i > 0 {
  73. stageWeight := float64(v.Weight-e[i-1].Weight) / 1000
  74. t1 := time.Unix(v.WeightAt, 0).Local().Truncate(24 * time.Hour)
  75. t2 := time.Unix(e[i-1].WeightAt, 0).Local().Truncate(24 * time.Hour)
  76. diff := t1.Sub(t2)
  77. days := int32(diff.Hours() / 24)
  78. stageDayAddWeight = float32(math.Round(stageWeight/float64(days)*100) / 100)
  79. }
  80. if stageDayAddWeight > 0 {
  81. monthAvgAddWeight = stageDayAddWeight * 30
  82. }
  83. res.Table[i] = &pasturePb.CowBehaviorCurveTable{
  84. Weight: weight,
  85. AvgWeight: avgWeight,
  86. StageDayAddWeight: stageDayAddWeight,
  87. MonthAvgAddWeight: monthAvgAddWeight,
  88. WeightAtFormat: weightAtFormat,
  89. AdmissionAge: cowInfo.AdmissionAge,
  90. }
  91. res.Chart.Headers = append(res.Chart.Headers, weightAtFormat)
  92. res.Chart.Weight = append(res.Chart.Weight, weight)
  93. }
  94. for _, event := range eventCowLogList {
  95. dateTime := ""
  96. if event.EventAt > 0 {
  97. dateTime = time.Unix(event.EventAt, 0).Local().Format(LayoutDate2)
  98. }
  99. res.Chart.EventList = append(res.Chart.EventList, &pasturePb.EventHappen{
  100. Name: event.EventTypeName,
  101. DateTime: dateTime,
  102. EventTypeKind: event.EventType,
  103. })
  104. }
  105. return res
  106. }