| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 | package modelimport (	"kpt-pasture/util"	"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"`	AdmissionAge  int32  `json:"admissionAge"`	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.GetEventDayAge(int64(item.WeightAt)),		WeightAt:      int64(item.WeightAt),		Remarks:       item.Remarks,		MessageId:     currentUser.Id,		MessageName:   currentUser.Name,		OperationId:   item.OperationId,		OperationName: item.OperationName,		AdmissionAge:  cow.GetEventAdmissionAge(int64(item.WeightAt)),	}}type EventWeightSlice []*EventWeightfunc (e EventWeightSlice) ToPB(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 := float64(0)		if i > 0 {			avgWeight = float64(v.Weight-e[i-1].Weight) / float64(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:         float32(util.RoundToTwoDecimals(avgWeight)),			StageDayAddWeight: stageDayAddWeight,			MonthAvgAddWeight: monthAvgAddWeight,			WeightAtFormat:    weightAtFormat,			AdmissionAge:      v.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}
 |