浏览代码

analysis: 栏舍区间增加分页功能

Yi 6 月之前
父节点
当前提交
b445ce8658
共有 5 个文件被更改,包括 36 次插入13 次删除
  1. 1 1
      go.mod
  2. 4 0
      go.sum
  3. 7 1
      http/handler/analysis/analysis.go
  4. 23 10
      module/backend/analysis.go
  5. 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-20241015015352-8c7829c17318
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20241017022959-48a2f1c177ac
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241010101255-0c6bd229b939
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eko/gocache v1.1.0

+ 4 - 0
go.sum

@@ -82,6 +82,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20241012064018-55ade50d8afc h1:AAMP3tan
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241012064018-55ade50d8afc/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241015015352-8c7829c17318 h1:u07PUyPLqJ+2Bzf9KD6bfJxp1vvXRTQG4Nq7N/J+ni8=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20241015015352-8c7829c17318/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241017014945-924b8364d224 h1:l4WMh/85PL5UxmGJG/0e4y5cs9FVq4HUhB3+kAU/xXE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241017014945-924b8364d224/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241017022959-48a2f1c177ac h1:P8dH2WtcZ3TnQdQHcKooG3IlDXH0XHPi33WPVB0xmKA=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20241017022959-48a2f1c177ac/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241008063555-121a776fd331 h1:qJcpJ3o+O7uxDqR0I9LijQmi607IKvhf4mGum/ZUPT0=

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

@@ -83,7 +83,13 @@ func PenWeight(c *gin.Context) {
 		return
 	}
 
-	res, err := middleware.Dependency(c).StoreEventHub.OpsService.PenWeight(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.PenWeight(c, &req, pagination)
 	if err != nil {
 		apierr.ClassifiedAbort(c, err)
 		return

+ 23 - 10
module/backend/analysis.go

@@ -248,16 +248,20 @@ func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTime
 	}, nil
 }
 
-func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequest) (*pasturePb.PenWeightResponse, error) {
+func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PenWeightResponse, error) {
 	penWeightList := make([]*model.PenWeight, 0)
-	if err := s.DB.Model(new(model.Cow)).
+	pref := s.DB.Model(new(model.Cow)).
 		Select(`
 			pen_id,
 			CEILING(AVG(current_weight) / 1000 ) AS avg_weight,
 			CEILING(SUM(current_weight) / 1000 ) AS all_weight,
 			COUNT(*) AS cow_count`,
-		).Where("is_remove = ?", pasturePb.IsShow_Ok).
-		Group("pen_id").
+		).Where("is_remove = ?", pasturePb.IsShow_Ok)
+	if len(req.PenId) > 0 {
+		pref.Where("pen_id IN ?", req.PenId)
+	}
+	if err := pref.Group("pen_id").
+		Order("pen_id").
 		Find(&penWeightList).Error; err != nil {
 		return nil, err
 	}
@@ -268,7 +272,7 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
 		AvgWeight: make([]int32, 0),
 		CowCount:  make([]int32, 0),
 	}
-	if len(penWeightList) == 0 {
+	if len(penWeightList) <= 0 {
 		return &pasturePb.PenWeightResponse{
 			Code:    http.StatusOK,
 			Message: "ok",
@@ -279,10 +283,16 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
 		}, nil
 	}
 	cowList := make([]*model.Cow, 0)
-	pref := s.DB.Model(new(model.Cow)).
+	var count int64 = 0
+	prefList := s.DB.Model(new(model.Cow)).
 		Where("is_remove = ?", pasturePb.IsShow_Ok)
-	if err := pref.Where("pen_id IN ?", req.PenId).
-		Order("pen_id").
+
+	if len(req.PenId) > 0 {
+		prefList.Where("pen_id IN ?", req.PenId)
+	}
+
+	if err := prefList.Count(&count).Limit(int(pagination.PageSize)).
+		Offset(int(pagination.PageOffset)).Order("pen_id").
 		Find(&cowList).Error; err != nil {
 		return nil, xerr.WithStack(err)
 	}
@@ -292,8 +302,11 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
 		Code:    http.StatusOK,
 		Message: "ok",
 		Data: &pasturePb.PenWeightData{
-			CowList: model.CowSlice(cowList).ToPB2(penMap, penWeightList),
-			Chart:   model.PenWeightSlice(penWeightList).ToPB(penMap),
+			CowList:  model.CowSlice(cowList).ToPB2(penMap, penWeightList),
+			Total:    int32(count),
+			Page:     pagination.Page,
+			PageSize: pagination.PageSize,
+			Chart:    model.PenWeightSlice(penWeightList).ToPB(penMap),
 		},
 	}, nil
 }

+ 1 - 1
module/backend/interface.go

@@ -208,7 +208,7 @@ type AnalyseService interface {
 	GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*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) (*pasturePb.PenWeightResponse, error)
+	PenWeight(ctx context.Context, req *pasturePb.PenWeightRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PenWeightResponse, error)
 }
 
 //go:generate mockgen -destination mock/DashboardService.go -package kptservicemock kpt-pasture/module/backend DashboardService