package model import ( "math" "time" pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow" ) type EventWeight struct { ID int64 `json:"id"` PastureId int64 `json:"pastureId"` CowId int64 `json:"cowId"` EarNumber string `json:"earNumber"` DayAge int32 `json:"dayAge"` LactationDay int64 `json:"lactationDay"` PenId int32 `json:"penId"` Lact int32 `json:"lact"` Weight int32 `json:"weight"` Height int32 `json:"height"` WeightAt int64 `json:"weightAt"` Remarks string `json:"remarks"` OperationId int32 `json:"operationId"` OperationName string `json:"operationName"` MessageId int64 `json:"messageId"` MessageName string `json:"messageName"` CreatedAt int64 `json:"created_at"` UpdatedAt int64 `json:"updated_at"` } func (c *EventWeight) TableName() string { return "event_weight" } func NewEventWeight(pastureId int64, cow *Cow, currentUser *SystemUser, item *pasturePb.EventWeight) *EventWeight { return &EventWeight{ PastureId: pastureId, CowId: cow.Id, EarNumber: cow.EarNumber, Weight: int32(item.Weight * 1000), Height: item.Height, Lact: cow.Lact, DayAge: cow.GetDayAge(), WeightAt: int64(item.WeightAt), Remarks: item.Remarks, MessageId: currentUser.Id, MessageName: currentUser.Name, OperationId: item.OperationId, OperationName: item.OperationName, } } type EventWeightSlice []*EventWeight func (e EventWeightSlice) ToPB(cowInfo *Cow, eventCowLogList []*EventCowLog) *pasturePb.CowGrowthCurveData { res := &pasturePb.CowGrowthCurveData{ Chart: &pasturePb.CowBehaviorCurveChart{ Headers: make([]string, 0), Weight: make([]float32, 0), EventList: make([]*pasturePb.EventHappen, 0), }, Table: make([]*pasturePb.CowBehaviorCurveTable, len(e)), } for i, v := range e { weightAtFormat := "" if v.WeightAt > 0 { weightAtFormat = time.Unix(v.WeightAt, 0).Local().Format(LayoutDate2) } weight := float32(0) if v.Weight > 0 { weight = float32(v.Weight) / 1000 } avgWeight := float32(0) if i > 0 { avgWeight = float32(v.Weight-e[i-1].Weight) / float32(v.WeightAt-e[i-1].WeightAt) / 24 / 1000 } // 阶段日增重 stageDayAddWeight, monthAvgAddWeight := float32(0), float32(0) if i > 0 { stageWeight := float64(v.Weight-e[i-1].Weight) / 1000 t1 := time.Unix(v.WeightAt, 0).Local().Truncate(24 * time.Hour) t2 := time.Unix(e[i-1].WeightAt, 0).Local().Truncate(24 * time.Hour) diff := t1.Sub(t2) days := int32(diff.Hours() / 24) stageDayAddWeight = float32(math.Round(stageWeight/float64(days)*100) / 100) } if stageDayAddWeight > 0 { monthAvgAddWeight = stageDayAddWeight * 30 } res.Table[i] = &pasturePb.CowBehaviorCurveTable{ Weight: weight, AvgWeight: avgWeight, StageDayAddWeight: stageDayAddWeight, MonthAvgAddWeight: monthAvgAddWeight, WeightAtFormat: weightAtFormat, AdmissionAge: cowInfo.AdmissionAge, } res.Chart.Headers = append(res.Chart.Headers, weightAtFormat) res.Chart.Weight = append(res.Chart.Weight, weight) } for _, event := range eventCowLogList { dateTime := "" if event.EventAt > 0 { dateTime = time.Unix(event.EventAt, 0).Local().Format(LayoutDate2) } res.Chart.EventList = append(res.Chart.EventList, &pasturePb.EventHappen{ Name: event.EventTypeName, DateTime: dateTime, EventTypeKind: event.EventType, }) } return res }