|
@@ -4,6 +4,10 @@ import (
|
|
|
"context"
|
|
|
"kpt-pasture/model"
|
|
|
"net/http"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
|
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
)
|
|
@@ -13,18 +17,24 @@ func (s *StoreEntry) GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowt
|
|
|
// 查询数据
|
|
|
cowList := make([]*model.Cow, 0)
|
|
|
pref := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
|
|
|
-
|
|
|
if req.GetCowId() != "" {
|
|
|
- pref.Where("id IN ?", req.GetCowId())
|
|
|
+ pref.Where("id IN ?", strings.Split(req.CowId, ","))
|
|
|
}
|
|
|
|
|
|
- if req.BirthStartDate > 0 && req.BirthEndDate > 0 && req.BirthStartDate <= req.BirthEndDate {
|
|
|
- pref.Where("birth_at BETWEEN ? AND ?", req.GetBirthStartDate(), req.GetBirthEndDate())
|
|
|
+ if len(req.BirthDate) == 2 && req.BirthDate[0] != "" && req.BirthDate[1] != "" {
|
|
|
+ t0, _ := time.Parse(time.RFC3339, req.BirthDate[0])
|
|
|
+ t1, _ := time.Parse(time.RFC3339, req.BirthDate[1])
|
|
|
+
|
|
|
+ pref.Where("birth_at BETWEEN ? AND ?", t0.Unix(), t1.Unix()+86399)
|
|
|
}
|
|
|
|
|
|
if err := pref.Find(&cowList).Error; err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
+ penList, err := s.GetPenList(ctx)
|
|
|
+ if err != nil {
|
|
|
+ return nil, xerr.WithStack(err)
|
|
|
+ }
|
|
|
// 计算图表数据
|
|
|
chartsList := &pasturePb.Charts{
|
|
|
CowId: make([]int32, 0),
|
|
@@ -34,22 +44,31 @@ func (s *StoreEntry) GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowt
|
|
|
cowData := make([]*pasturePb.CowList, 0)
|
|
|
for _, cow := range cowList {
|
|
|
currentWeight := float32(cow.CurrentWeight) / 100
|
|
|
+ penName := ""
|
|
|
+ for _, v := range penList {
|
|
|
+ if cow.PenId != v.Id {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ penName = v.Name
|
|
|
+ }
|
|
|
cowData = append(cowData, &pasturePb.CowList{
|
|
|
CowId: int32(cow.Id),
|
|
|
EarNumber: cow.EarNumber,
|
|
|
DayAge: cow.GetDayAge(),
|
|
|
- PenName: "",
|
|
|
+ PenName: penName,
|
|
|
CurrentWeight: currentWeight,
|
|
|
BirthAt: int32(cow.BirthAt),
|
|
|
BirthWeight: float32(cow.BirthWeight) / 100,
|
|
|
LastWeightAt: int32(cow.LastWeightAt),
|
|
|
- DailyWeightGain: 0,
|
|
|
- AverageDailyWeightGain: 0,
|
|
|
+ DailyWeightGain: float32(cow.GetDayWeight() / 100),
|
|
|
+ AverageDailyWeightGain: float32(cow.GetAverageDailyWeight() / 100),
|
|
|
})
|
|
|
+
|
|
|
chartsList.CowId = append(chartsList.CowId, int32(cow.Id))
|
|
|
chartsList.Weight = append(chartsList.Weight, currentWeight)
|
|
|
chartsList.DayAge = append(chartsList.DayAge, cow.GetDayAge())
|
|
|
}
|
|
|
+
|
|
|
// 返回数据
|
|
|
return &pasturePb.GrowthCurvesResponse{
|
|
|
Code: http.StatusOK,
|