event_weight.go 3.7 KB

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