Bladeren bron

event: weight update

Yi 2 dagen geleden
bovenliggende
commit
3bcc6d6ae7
4 gewijzigde bestanden met toevoegingen van 21 en 8 verwijderingen
  1. 7 0
      model/cow.go
  2. 8 5
      model/event_weight.go
  3. 1 1
      module/backend/cow.go
  4. 5 2
      util/util_test.go

+ 7 - 0
model/cow.go

@@ -305,6 +305,13 @@ func (c *Cow) GetAdmissionAge() int32 {
 	return 0
 }
 
+func (c *Cow) GetEventAdmissionAge(eventAt int64) int32 {
+	if eventAt > 0 && c.AdmissionStatus == pasturePb.AdmissionStatus_Admission {
+		return int32(math.Floor(float64(eventAt-c.AdmissionAt) / 86400))
+	}
+	return 0
+}
+
 // GetAverageDailyWeight 平均日增重(最后一次称重 - 入场体重)÷在群天数
 func (c *Cow) GetAverageDailyWeight() float64 {
 	if c.AdmissionAge <= 0 {

+ 8 - 5
model/event_weight.go

@@ -1,6 +1,7 @@
 package model
 
 import (
+	"kpt-pasture/util"
 	"math"
 	"time"
 
@@ -16,6 +17,7 @@ type EventWeight struct {
 	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"`
@@ -47,12 +49,13 @@ func NewEventWeight(pastureId int64, cow *Cow, currentUser *SystemUser, item *pa
 		MessageName:   currentUser.Name,
 		OperationId:   item.OperationId,
 		OperationName: item.OperationName,
+		AdmissionAge:  cow.GetEventAdmissionAge(int64(item.WeightAt)),
 	}
 }
 
 type EventWeightSlice []*EventWeight
 
-func (e EventWeightSlice) ToPB(cowInfo *Cow, eventCowLogList []*EventCowLog) *pasturePb.CowGrowthCurveData {
+func (e EventWeightSlice) ToPB(eventCowLogList []*EventCowLog) *pasturePb.CowGrowthCurveData {
 	res := &pasturePb.CowGrowthCurveData{
 		Chart: &pasturePb.CowBehaviorCurveChart{
 			Headers:   make([]string, 0),
@@ -72,9 +75,9 @@ func (e EventWeightSlice) ToPB(cowInfo *Cow, eventCowLogList []*EventCowLog) *pa
 			weight = float32(v.Weight) / 1000
 		}
 
-		avgWeight := float32(0)
+		avgWeight := float64(0)
 		if i > 0 {
-			avgWeight = float32(v.Weight-e[i-1].Weight) / float32(v.WeightAt-e[i-1].WeightAt) / 24 / 1000
+			avgWeight = float64(v.Weight-e[i-1].Weight) / float64(v.WeightAt-e[i-1].WeightAt) / 24 / 1000
 		}
 
 		// 阶段日增重
@@ -93,11 +96,11 @@ func (e EventWeightSlice) ToPB(cowInfo *Cow, eventCowLogList []*EventCowLog) *pa
 		}
 		res.Table[i] = &pasturePb.CowBehaviorCurveTable{
 			Weight:            weight,
-			AvgWeight:         avgWeight,
+			AvgWeight:         float32(util.RoundToTwoDecimals(avgWeight)),
 			StageDayAddWeight: stageDayAddWeight,
 			MonthAvgAddWeight: monthAvgAddWeight,
 			WeightAtFormat:    weightAtFormat,
-			AdmissionAge:      cowInfo.AdmissionAge,
+			AdmissionAge:      v.AdmissionAge,
 		}
 		res.Chart.Headers = append(res.Chart.Headers, weightAtFormat)
 		res.Chart.Weight = append(res.Chart.Weight, weight)

+ 1 - 1
module/backend/cow.go

@@ -524,7 +524,7 @@ func (s *StoreEntry) CowGrowthCurve(ctx context.Context, req *pasturePb.CowGrowt
 	return &pasturePb.CowGrowthCurveResponse{
 		Code: http.StatusOK,
 		Msg:  "ok",
-		Data: model.EventWeightSlice(weightList).ToPB(cowInfo, eventCowLogList),
+		Data: model.EventWeightSlice(weightList).ToPB(eventCowLogList),
 	}, nil
 }
 

+ 5 - 2
util/util_test.go

@@ -535,7 +535,7 @@ type WeeklyActiveModel struct {
 }
 
 func Test_demo(t *testing.T) {
-	str := "insert into system_pasture_menu (pasture_id,menu_id,created_at,updated_at) values"
+	/*str := "insert into system_pasture_menu (pasture_id,menu_id,created_at,updated_at) values"
 	for i := 36; i <= 135; i++ {
 		str += fmt.Sprintf("(11,%d,1748922530,1748922530),", i)
 	}
@@ -555,5 +555,8 @@ func Test_demo(t *testing.T) {
 		fmt.Println("1")
 	} else {
 		fmt.Println("2")
-	}
+	}*/
+
+	num := 0.0334345466563453467868989087765434213344544345678
+	fmt.Println(RoundToTwoDecimals(num))
 }