event_weight.go 3.6 KB

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