소스 검색

analysis: update

ping 1 개월 전
부모
커밋
aeafa89e3a
6개의 변경된 파일26개의 추가작업 그리고 28개의 파일을 삭제
  1. 1 1
      go.mod
  2. 2 0
      go.sum
  3. 7 1
      http/handler/analysis/analysis.go
  4. 3 14
      model/cow.go
  5. 12 11
      module/backend/analysis.go
  6. 1 1
      module/backend/interface.go

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250228083339-f6b9576918dd
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250301090359-debc53863000
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 2 - 0
go.sum

@@ -271,6 +271,8 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250228081242-8794bd89a603/go.mod h1:B
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250228082905-a9a0b77f1247/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250228083339-f6b9576918dd h1:hqSeKVOL1YQHz7WoHmGKlm+T3MoczJvJ9kDd7qoPdMM=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250228083339-f6b9576918dd/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250301090359-debc53863000 h1:gGF+69uDuE1ocf7k5tbvdqs730wEYYyMA2Vtsu2z/R8=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250301090359-debc53863000/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 7 - 1
http/handler/analysis/analysis.go

@@ -19,7 +19,13 @@ func WeightScatterPlot(c *gin.Context) {
 		return
 	}
 
-	res, err := middleware.Dependency(c).StoreEventHub.OpsService.WeightScatterPlot(c, &req)
+	pagination := &pasturePb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.WeightScatterPlot(c, &req, pagination)
 	if err != nil {
 		apierr.ClassifiedAbort(c, err)
 		return

+ 3 - 14
model/cow.go

@@ -2,7 +2,6 @@ package model
 
 import (
 	"fmt"
-	"kpt-pasture/util"
 	"math"
 	"time"
 
@@ -290,26 +289,16 @@ func (c CowSlice) ToPB(
 	return res
 }
 
-func (c CowSlice) ToPB2(penMap map[int32]*Pen, penWeightSlice PenWeightSlice) []*pasturePb.CowList {
+func (c CowSlice) ToPB2(penWeightSlice PenWeightSlice) []*pasturePb.CowList {
 	res := make([]*pasturePb.CowList, len(c))
 	for i, v := range c {
-		penName := ""
-		if pen, ok := penMap[v.PenId]; ok {
-			penName = pen.Name
-		}
-
 		penWeight := penWeightSlice.GetPenWeight(v.PenId)
-		lastWeightDay := util.Ceil(float64(v.LastWeightAt-v.LastSecondWeightAt) / 86400)
 		penAvgWeight := float32(0)
-		previousStageDailyWeight := float32(0)
 		cowPenAvgWeightDiffValue := float32(0)
 
 		if penWeight != nil {
 			penAvgWeight = float32(penWeight.AvgWeight) / 1000
 			cowPenAvgWeightDiffValue = float32(v.CurrentWeight-int64(penWeight.AvgWeight)) / 1000
-			if lastWeightDay > 0 {
-				previousStageDailyWeight = float32(v.CurrentWeight-v.LastSecondWeight) / 1000 / float32(lastWeightDay)
-			}
 		}
 
 		res[i] = &pasturePb.CowList{
@@ -317,14 +306,14 @@ func (c CowSlice) ToPB2(penMap map[int32]*Pen, penWeightSlice PenWeightSlice) []
 			DayAge:                   v.DayAge,
 			AverageDailyWeightGain:   float32(v.GetAverageDailyWeight()),
 			EarNumber:                v.EarNumber,
-			PenName:                  penName,
+			PenName:                  v.PenName,
 			BirthAt:                  int32(v.BirthAt),
 			BirthWeight:              float32(v.BirthWeight) / 1000,
 			CurrentWeight:            float32(v.CurrentWeight) / 1000,
 			LastWeightAt:             int32(v.LastWeightAt),
 			AdmissionAge:             v.AdmissionAge,
 			AdmissionWeight:          float32(v.AbortionAge) / 1000,
-			PreviousStageDailyWeight: previousStageDailyWeight,
+			PreviousStageDailyWeight: float32(v.GetPreviousStageDailyWeight()),
 			PenAvgWeight:             penAvgWeight,
 			CowPenAvgWeightDiffValue: cowPenAvgWeightDiffValue,
 		}

+ 12 - 11
module/backend/analysis.go

@@ -16,7 +16,7 @@ import (
 )
 
 // WeightScatterPlot 体重散点图 获取图表数据
-func (s *StoreEntry) WeightScatterPlot(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error) {
+func (s *StoreEntry) WeightScatterPlot(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest, pagination *pasturePb.PaginationModel) (*pasturePb.GrowthCurvesResponse, error) {
 	userModel, err := s.GetUserModel(ctx)
 	if err != nil {
 		return nil, xerr.Custom("当前用户信息错误,请退出重新登录")
@@ -37,10 +37,13 @@ func (s *StoreEntry) WeightScatterPlot(ctx context.Context, req *pasturePb.Searc
 		pref.Where("birth_at BETWEEN ? AND ?", t0.Unix(), t1.Unix()+86399)
 	}
 
-	if err = pref.Find(&cowList).Error; err != nil {
+	var count int64
+	if err = pref.Count(&count).
+		Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).
+		Find(&cowList).Error; err != nil {
 		return nil, err
 	}
-	penList, err := s.GetPenList(ctx, userModel.AppPasture.Id)
 	if err != nil {
 		return nil, xerr.WithStack(err)
 	}
@@ -53,18 +56,15 @@ func (s *StoreEntry) WeightScatterPlot(ctx context.Context, req *pasturePb.Searc
 	cowData := make([]*pasturePb.CowList, 0)
 	for _, cow := range cowList {
 		currentWeight := float32(cow.CurrentWeight) / 1000
-		penName := ""
-		for _, v := range penList {
-			if cow.PenId != v.Id {
-				continue
-			}
-			penName = v.Name
+		admissionAtFormat := ""
+		if cow.AdmissionAt > 0 {
+			admissionAtFormat = time.Unix(cow.AdmissionAt, 0).Format(model.LayoutDate2)
 		}
 		cowData = append(cowData, &pasturePb.CowList{
 			CowId:                    int32(cow.Id),
 			EarNumber:                cow.EarNumber,
 			DayAge:                   cow.GetDayAge(),
-			PenName:                  penName,
+			PenName:                  cow.PenName,
 			CurrentWeight:            currentWeight,
 			BirthAt:                  int32(cow.BirthAt),
 			BirthWeight:              float32(cow.BirthWeight) / 1000,
@@ -72,6 +72,7 @@ func (s *StoreEntry) WeightScatterPlot(ctx context.Context, req *pasturePb.Searc
 			AverageDailyWeightGain:   float32(cow.GetAverageDailyWeight()),
 			PreviousStageDailyWeight: float32(cow.GetPreviousStageDailyWeight()),
 			AdmissionAge:             cow.GetAdmissionAge(),
+			AdmissionAtFormat:        admissionAtFormat,
 		})
 
 		chartsList.CowId = append(chartsList.CowId, int32(cow.Id))
@@ -335,7 +336,7 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
 		Code: http.StatusOK,
 		Msg:  "ok",
 		Data: &pasturePb.PenWeightData{
-			CowList:  model.CowSlice(cowList).ToPB2(penMap, penWeightList),
+			CowList:  model.CowSlice(cowList).ToPB2(penWeightList),
 			Total:    int32(count),
 			Page:     pagination.Page,
 			PageSize: pagination.PageSize,

+ 1 - 1
module/backend/interface.go

@@ -255,7 +255,7 @@ type GoodsService interface {
 
 //go:generate mockgen -destination mock/AnalyseService.go -package kptservicemock kpt-pasture/module/backend AnalyseService
 type AnalyseService interface {
-	WeightScatterPlot(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error)
+	WeightScatterPlot(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest, pagination *pasturePb.PaginationModel) (*pasturePb.GrowthCurvesResponse, error)
 	WeightRange(ctx context.Context, req *pasturePb.WeightRangeRequest) (*pasturePb.WeightRangeResponse, error)
 	MatingTimely(ctx context.Context, req *pasturePb.MatingTimelyRequest) (*model.MatingTimelyResponse, error)
 	PenWeight(ctx context.Context, req *pasturePb.PenWeightRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PenWeightResponse, error)