|
@@ -248,16 +248,20 @@ func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTime
|
|
}, nil
|
|
}, 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)
|
|
penWeightList := make([]*model.PenWeight, 0)
|
|
- if err := s.DB.Model(new(model.Cow)).
|
|
|
|
|
|
+ pref := s.DB.Model(new(model.Cow)).
|
|
Select(`
|
|
Select(`
|
|
pen_id,
|
|
pen_id,
|
|
CEILING(AVG(current_weight) / 1000 ) AS avg_weight,
|
|
CEILING(AVG(current_weight) / 1000 ) AS avg_weight,
|
|
CEILING(SUM(current_weight) / 1000 ) AS all_weight,
|
|
CEILING(SUM(current_weight) / 1000 ) AS all_weight,
|
|
COUNT(*) AS cow_count`,
|
|
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 {
|
|
Find(&penWeightList).Error; err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -268,7 +272,7 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
|
|
AvgWeight: make([]int32, 0),
|
|
AvgWeight: make([]int32, 0),
|
|
CowCount: make([]int32, 0),
|
|
CowCount: make([]int32, 0),
|
|
}
|
|
}
|
|
- if len(penWeightList) == 0 {
|
|
|
|
|
|
+ if len(penWeightList) <= 0 {
|
|
return &pasturePb.PenWeightResponse{
|
|
return &pasturePb.PenWeightResponse{
|
|
Code: http.StatusOK,
|
|
Code: http.StatusOK,
|
|
Message: "ok",
|
|
Message: "ok",
|
|
@@ -279,10 +283,16 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
cowList := make([]*model.Cow, 0)
|
|
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)
|
|
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 {
|
|
Find(&cowList).Error; err != nil {
|
|
return nil, xerr.WithStack(err)
|
|
return nil, xerr.WithStack(err)
|
|
}
|
|
}
|
|
@@ -292,8 +302,11 @@ func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequ
|
|
Code: http.StatusOK,
|
|
Code: http.StatusOK,
|
|
Message: "ok",
|
|
Message: "ok",
|
|
Data: &pasturePb.PenWeightData{
|
|
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
|
|
}, nil
|
|
}
|
|
}
|