event_weight.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package model
  2. import (
  3. "time"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. )
  6. type EventWeight struct {
  7. ID int64 `json:"id"`
  8. PastureId int64 `json:"pastureId"`
  9. CowId int64 `json:"cowId"`
  10. EarNumber string `json:"earNumber"`
  11. DayAge int32 `json:"dayAge"`
  12. LactationDay int64 `json:"lactationDay"`
  13. PenId int32 `json:"penId"`
  14. Lact int32 `json:"lact"`
  15. Weight int32 `json:"weight"`
  16. Height int32 `json:"height"`
  17. WeightAt int64 `json:"weightAt"`
  18. Remarks string `json:"remarks"`
  19. OperationId int32 `json:"operationId"`
  20. OperationName string `json:"operationName"`
  21. MessageId int64 `json:"messageId"`
  22. MessageName string `json:"messageName"`
  23. CreatedAt int64 `json:"created_at"`
  24. UpdatedAt int64 `json:"updated_at"`
  25. }
  26. func (c *EventWeight) TableName() string {
  27. return "event_weight"
  28. }
  29. func NewEventWeight(pastureId int64, cow *Cow, currentUser *SystemUser, item *pasturePb.EventWeight) *EventWeight {
  30. return &EventWeight{
  31. PastureId: pastureId,
  32. CowId: cow.Id,
  33. EarNumber: cow.EarNumber,
  34. Weight: int32(item.Weight * 1000),
  35. Height: item.Height,
  36. Lact: cow.Lact,
  37. DayAge: cow.GetDayAge(),
  38. WeightAt: int64(item.WeightAt),
  39. Remarks: item.Remarks,
  40. MessageId: currentUser.Id,
  41. MessageName: currentUser.Name,
  42. OperationId: item.OperationId,
  43. OperationName: item.OperationName,
  44. }
  45. }
  46. type EventWeightSlice []*EventWeight
  47. func (e EventWeightSlice) ToPB() []*pasturePb.CowGrowthCurveData {
  48. res := make([]*pasturePb.CowGrowthCurveData, len(e))
  49. for i, v := range e {
  50. weightAtFormat := ""
  51. if v.WeightAt > 0 {
  52. weightAtFormat = time.Unix(v.WeightAt, 0).Format(LayoutTime)
  53. }
  54. avgWeight := float32(0)
  55. if i > 0 {
  56. avgWeight = float32(v.Weight-e[i-1].Weight) / float32(v.WeightAt-e[i-1].WeightAt) / 24 / 1000
  57. }
  58. res[i] = &pasturePb.CowGrowthCurveData{
  59. Weight: float32(v.Weight) / 1000,
  60. WeightAtFormat: weightAtFormat,
  61. AvgWeight: 0, // 平均体重
  62. DayAddWeight: 0, // 日增重
  63. AvgDayAddWeight: avgWeight, // 阶段日增重
  64. MonthAddWeight: avgWeight * 30, // 月增重
  65. }
  66. }
  67. return res
  68. }