analysis.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 int64(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. }
  73. func (s *StoreEntry) WeightRange(ctx context.Context, req *pasturePb.WeightRangeRequest) (*pasturePb.WeightRangeResponse, error) {
  74. cowWeightRange := make([]*model.CowWeightRange, 0)
  75. prefix := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
  76. if req.CowKind > 0 {
  77. prefix.Where("cow_kind = ?", req.CowKind)
  78. }
  79. if err := prefix.Select(`
  80. CASE
  81. WHEN current_weight BETWEEN 0 AND 50000 THEN '0-50'
  82. WHEN current_weight BETWEEN 50001 AND 100000 THEN '51-100'
  83. WHEN current_weight BETWEEN 100001 AND 150000 THEN '101-150'
  84. WHEN current_weight BETWEEN 150001 AND 200000 THEN '151-200'
  85. WHEN current_weight BETWEEN 200001 AND 250000 THEN '201-250'
  86. WHEN current_weight BETWEEN 250001 AND 300000 THEN '251-300'
  87. WHEN current_weight BETWEEN 300001 AND 350000 THEN '301-350'
  88. WHEN current_weight BETWEEN 350001 AND 400000 THEN '351-400'
  89. WHEN current_weight BETWEEN 400001 AND 450000 THEN '401-450'
  90. WHEN current_weight BETWEEN 450001 AND 500000 THEN '451-500'
  91. WHEN current_weight BETWEEN 500001 AND 550000 THEN '500-550'
  92. WHEN current_weight BETWEEN 550001 AND 600000 THEN '551-600'
  93. WHEN current_weight BETWEEN 600001 AND 650000 THEN '601-650'
  94. WHEN current_weight BETWEEN 650001 AND 700000 THEN '651-700'
  95. WHEN current_weight BETWEEN 700001 AND 750000 THEN '701-750'
  96. ELSE '750+'
  97. END AS weight_range,
  98. COUNT(*) AS count `,
  99. ).Group("weight_range").Order("MIN(current_weight)").Find(&cowWeightRange).Error; err != nil {
  100. return nil, err
  101. }
  102. if len(cowWeightRange) == 0 {
  103. return nil, xerr.Customf("没有数据")
  104. }
  105. header := make([]string, 0)
  106. data := make([]int32, 0)
  107. for _, v := range cowWeightRange {
  108. header = append(header, v.WeightRange)
  109. data = append(data, v.Count)
  110. }
  111. // 牛只详情列表
  112. pref := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
  113. if req.CowKind > 0 {
  114. pref.Where("cow_kind = ?", req.CowKind)
  115. }
  116. cowList := make([]*model.Cow, 0)
  117. if req.MinWeight >= 0 && req.MaxWeight >= 0 && req.MinWeight < req.MaxWeight {
  118. pref.Where("current_weight BETWEEN ? AND ? ", req.MinWeight*1000, req.MaxWeight*1000)
  119. }
  120. if err := pref.Find(&cowList).Error; err != nil {
  121. return nil, err
  122. }
  123. penMap := s.PenMap(ctx)
  124. return &pasturePb.WeightRangeResponse{
  125. Code: http.StatusOK,
  126. Message: "ok",
  127. Data: &pasturePb.WeightRangeData{
  128. CowList: model.CowSlice(cowList).WeightRangeToPB(penMap),
  129. WeightBarChart: &pasturePb.WeightBarChart{
  130. Header: header,
  131. Data: data,
  132. },
  133. },
  134. }, nil
  135. }