analysis.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. )
  11. // GrowthCurve 生长曲线 获取图表数据
  12. func (s *StoreEntry) GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error) {
  13. // 查询数据
  14. cowList := make([]*model.Cow, 0)
  15. pref := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
  16. if req.GetCowId() != "" {
  17. pref.Where("id IN ?", strings.Split(req.CowId, ","))
  18. }
  19. if len(req.BirthDate) == 2 && req.BirthDate[0] != "" && req.BirthDate[1] != "" {
  20. t0, _ := time.Parse(time.RFC3339, req.BirthDate[0])
  21. t1, _ := time.Parse(time.RFC3339, req.BirthDate[1])
  22. pref.Where("birth_at BETWEEN ? AND ?", t0.Unix(), t1.Unix()+86399)
  23. }
  24. if err := pref.Find(&cowList).Error; err != nil {
  25. return nil, err
  26. }
  27. penList, err := s.GetPenList(ctx)
  28. if err != nil {
  29. return nil, xerr.WithStack(err)
  30. }
  31. // 计算图表数据
  32. chartsList := &pasturePb.Charts{
  33. CowId: make([]int32, 0),
  34. Weight: make([]float32, 0),
  35. DayAge: make([]int32, 0),
  36. }
  37. cowData := make([]*pasturePb.CowList, 0)
  38. for _, cow := range cowList {
  39. currentWeight := float32(cow.CurrentWeight) / 100
  40. penName := ""
  41. for _, v := range penList {
  42. if cow.PenId != v.Id {
  43. continue
  44. }
  45. penName = v.Name
  46. }
  47. cowData = append(cowData, &pasturePb.CowList{
  48. CowId: int32(cow.Id),
  49. EarNumber: cow.EarNumber,
  50. DayAge: cow.GetDayAge(),
  51. PenName: penName,
  52. CurrentWeight: currentWeight,
  53. BirthAt: int32(cow.BirthAt),
  54. BirthWeight: float32(cow.BirthWeight) / 100,
  55. LastWeightAt: int32(cow.LastWeightAt),
  56. DailyWeightGain: float32(cow.GetDayWeight() / 100),
  57. AverageDailyWeightGain: float32(cow.GetAverageDailyWeight() / 100),
  58. })
  59. chartsList.CowId = append(chartsList.CowId, int32(cow.Id))
  60. chartsList.Weight = append(chartsList.Weight, currentWeight)
  61. chartsList.DayAge = append(chartsList.DayAge, cow.GetDayAge())
  62. }
  63. // 返回数据
  64. return &pasturePb.GrowthCurvesResponse{
  65. Code: http.StatusOK,
  66. Message: "success",
  67. Data: &pasturePb.GrowthCurveData{
  68. Table: cowData,
  69. Charts: chartsList,
  70. },
  71. }, nil
  72. }