|
@@ -114,7 +114,17 @@ func (s *StoreEntry) WeightRange(ctx context.Context, req *pasturePb.WeightRange
|
|
}
|
|
}
|
|
|
|
|
|
if len(cowWeightRange) == 0 {
|
|
if len(cowWeightRange) == 0 {
|
|
- return nil, xerr.Customf("没有数据")
|
|
+ return &pasturePb.WeightRangeResponse{
|
|
|
|
+ Code: http.StatusOK,
|
|
|
|
+ Message: "ok",
|
|
|
|
+ Data: &pasturePb.WeightRangeData{
|
|
|
|
+ CowList: make([]*pasturePb.CowList, 0),
|
|
|
|
+ WeightBarChart: &pasturePb.WeightBarChart{
|
|
|
|
+ Header: make([]string, 0),
|
|
|
|
+ Data: make([]int32, 0),
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ }, nil
|
|
}
|
|
}
|
|
header := make([]string, 0)
|
|
header := make([]string, 0)
|
|
data := make([]int32, 0)
|
|
data := make([]int32, 0)
|
|
@@ -150,7 +160,7 @@ func (s *StoreEntry) WeightRange(ctx context.Context, req *pasturePb.WeightRange
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTimelyRequest) (*pasturePb.MatingTimelyResponse, error) {
|
|
+func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTimelyRequest) (*model.MatingTimelyResponse, error) {
|
|
matingTimelyChart := make([]*model.MatingTimelyChart, 0)
|
|
matingTimelyChart := make([]*model.MatingTimelyChart, 0)
|
|
sql := `SELECT calving_age,cow_type, DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') AS reality_day, lact_group
|
|
sql := `SELECT calving_age,cow_type, DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') AS reality_day, lact_group
|
|
FROM (
|
|
FROM (
|
|
@@ -179,37 +189,36 @@ func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTime
|
|
if req.StartDayAt > 0 && req.EndDayAt > 0 {
|
|
if req.StartDayAt > 0 && req.EndDayAt > 0 {
|
|
whereSql += fmt.Sprintf("AND reality_day BETWEEN %d AND %d", req.StartDayAt, req.EndDayAt)
|
|
whereSql += fmt.Sprintf("AND reality_day BETWEEN %d AND %d", req.StartDayAt, req.EndDayAt)
|
|
}
|
|
}
|
|
- if err := s.DB.Raw(sql).Find(&matingTimelyChart).Error; err != nil {
|
|
+ if err := s.DB.Raw(fmt.Sprintf("%s %s", sql, whereSql)).Find(&matingTimelyChart).Error; err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- chart := &pasturePb.MatingTimelyChart{
|
|
+ chart := &model.CowMatingChart{
|
|
- Lact0: make([]int32, 0),
|
|
+ Lact0: make([][]string, 0),
|
|
- Lact1: make([]int32, 0),
|
|
+ Lact1: make([][]string, 0),
|
|
- Lact2: make([]int32, 0),
|
|
+ Lact2: make([][]string, 0),
|
|
- Lact3: make([]int32, 0),
|
|
+ Lact3: make([][]string, 0),
|
|
}
|
|
}
|
|
if len(matingTimelyChart) == 0 {
|
|
if len(matingTimelyChart) == 0 {
|
|
- return &pasturePb.MatingTimelyResponse{
|
|
+ return &model.MatingTimelyResponse{
|
|
Code: http.StatusOK,
|
|
Code: http.StatusOK,
|
|
Message: "ok",
|
|
Message: "ok",
|
|
- Data: &pasturePb.MatingTimelyData{
|
|
+ Data: &model.MatingTimelyData{
|
|
CowList: make([]*pasturePb.CowList, 0),
|
|
CowList: make([]*pasturePb.CowList, 0),
|
|
Chart: chart,
|
|
Chart: chart,
|
|
},
|
|
},
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
-
|
|
|
|
for _, v := range matingTimelyChart {
|
|
for _, v := range matingTimelyChart {
|
|
switch v.LactGroup {
|
|
switch v.LactGroup {
|
|
case "0":
|
|
case "0":
|
|
- chart.Lact0 = append(chart.Lact0, v.CalvingAge)
|
|
+ chart.Lact0 = append(chart.Lact0, []string{fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
|
|
case "1":
|
|
case "1":
|
|
- chart.Lact1 = append(chart.Lact1, v.CalvingAge)
|
|
+ chart.Lact1 = append(chart.Lact1, []string{fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
|
|
case "2":
|
|
case "2":
|
|
- chart.Lact2 = append(chart.Lact2, v.CalvingAge)
|
|
+ chart.Lact2 = append(chart.Lact2, []string{fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
|
|
case "3+":
|
|
case "3+":
|
|
- chart.Lact3 = append(chart.Lact3, v.CalvingAge)
|
|
+ chart.Lact3 = append(chart.Lact3, []string{fmt.Sprintf("%d", v.CalvingAge), v.RealityDay})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -229,12 +238,62 @@ func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTime
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
- return &pasturePb.MatingTimelyResponse{
|
|
+ return &model.MatingTimelyResponse{
|
|
Code: http.StatusOK,
|
|
Code: http.StatusOK,
|
|
Message: "ok",
|
|
Message: "ok",
|
|
- Data: &pasturePb.MatingTimelyData{
|
|
+ Data: &model.MatingTimelyData{
|
|
CowList: model.EventMatingSlice(eventMatingList).ToPB2(),
|
|
CowList: model.EventMatingSlice(eventMatingList).ToPB2(),
|
|
Chart: chart,
|
|
Chart: chart,
|
|
},
|
|
},
|
|
}, nil
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+func (s *StoreEntry) PenWeight(ctx context.Context, req *pasturePb.PenWeightRequest) (*pasturePb.PenWeightResponse, error) {
|
|
|
|
+ penWeightList := make([]*model.PenWeight, 0)
|
|
|
|
+ if err := 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").
|
|
|
|
+ Find(&penWeightList).Error; err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ chart := &pasturePb.PenWeightChart{
|
|
|
|
+ Header: make([]string, 0),
|
|
|
|
+ AllWeight: make([]int32, 0),
|
|
|
|
+ AvgWeight: make([]int32, 0),
|
|
|
|
+ CowCount: make([]int32, 0),
|
|
|
|
+ }
|
|
|
|
+ if len(penWeightList) == 0 {
|
|
|
|
+ return &pasturePb.PenWeightResponse{
|
|
|
|
+ Code: http.StatusOK,
|
|
|
|
+ Message: "ok",
|
|
|
|
+ Data: &pasturePb.PenWeightData{
|
|
|
|
+ CowList: make([]*pasturePb.CowList, 0),
|
|
|
|
+ Chart: chart,
|
|
|
|
+ },
|
|
|
|
+ }, nil
|
|
|
|
+ }
|
|
|
|
+ cowList := make([]*model.Cow, 0)
|
|
|
|
+ pref := s.DB.Model(new(model.Cow)).
|
|
|
|
+ Where("is_remove = ?", pasturePb.IsShow_Ok)
|
|
|
|
+ if err := pref.Where("pen_id IN ?", req.PenId).
|
|
|
|
+ Order("pen_id").
|
|
|
|
+ Find(&cowList).Error; err != nil {
|
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ penMap := s.PenMap(ctx)
|
|
|
|
+ return &pasturePb.PenWeightResponse{
|
|
|
|
+ Code: http.StatusOK,
|
|
|
|
+ Message: "ok",
|
|
|
|
+ Data: &pasturePb.PenWeightData{
|
|
|
|
+ CowList: model.CowSlice(cowList).ToPB2(penMap, penWeightList),
|
|
|
|
+ Chart: model.PenWeightSlice(penWeightList).ToPB(penMap),
|
|
|
|
+ },
|
|
|
|
+ }, nil
|
|
|
|
+}
|