statistic_service.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-tmr-group/model"
  6. "kpt-tmr-group/pkg/xerr"
  7. operationPb "kpt-tmr-group/proto/go/backend/operation"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*operationPb.SearchFormulaEstimateResponse, error) {
  14. startTime, err := time.Parse(model.LayoutTime, req.StartTime)
  15. if err != nil {
  16. return nil, xerr.WithStack(err)
  17. }
  18. endTime, err := time.Parse(model.LayoutTime, req.EndTime)
  19. if err != nil {
  20. return nil, xerr.WithStack(err)
  21. }
  22. startTimeUnix := startTime.Unix()
  23. endTimeUnix := endTime.Unix()
  24. formulaEstimate := make([]*model.FormulaEstimate, 0)
  25. var count int64 = 0
  26. pref := s.DB.Model(new(model.FormulaEstimate))
  27. if req.Name != "" {
  28. pref.Where("name like ?", fmt.Sprintf("%s%s%s", "%", req.Name, "%"))
  29. }
  30. if startTimeUnix > 0 && endTimeUnix > 0 && endTimeUnix >= startTimeUnix {
  31. pref.Where("created_at BETWEEN ? AND ?", startTimeUnix, endTimeUnix)
  32. }
  33. if req.SearchType == 1 {
  34. pref.Where("feed_formula_name = ?", req.Name)
  35. } else {
  36. pref.Where("barn_id = ?", req.Name)
  37. }
  38. if err = pref.Order("id desc").Count(&count).Limit(int(req.Pagination.PageSize)).Offset(int(req.Pagination.PageOffset)).
  39. Find(&formulaEstimate).Error; err != nil {
  40. return nil, xerr.WithStack(err)
  41. }
  42. return &operationPb.SearchFormulaEstimateResponse{
  43. Code: http.StatusOK,
  44. Msg: "ok",
  45. Data: &operationPb.SearchFormulaEstimate{
  46. Page: req.Pagination.Page,
  47. Total: int32(count),
  48. PageSize: req.Pagination.PageSize,
  49. List: model.FormulaEstimateSlice(formulaEstimate).ToPB(),
  50. },
  51. }, nil
  52. }
  53. func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
  54. dataList := &model.CommonValueRatio{
  55. MaxValue: "",
  56. MiddleValue: "",
  57. MinValue: "",
  58. PastureName: make([]string, 0),
  59. DateDay: make([]string, 0),
  60. DataList: make([][]string, 0),
  61. PastureIds: make([]int32, 0),
  62. }
  63. res := &model.SearchAnalysisAccuracyResponse{
  64. Code: http.StatusOK,
  65. Msg: "ok",
  66. Data: &model.AnalysisAccuracyData{
  67. Chart: &model.Chart{
  68. MixedFodderAccurateRatio: dataList,
  69. MixedFodderCorrectRatio: dataList,
  70. SprinkleFodderAccurateRatio: dataList,
  71. SprinkleFodderCorrectRatio: dataList,
  72. },
  73. Table: &model.Table{
  74. TitleList: make([]*model.TableList, 0),
  75. DataList: make([]*interface{}, 0),
  76. },
  77. },
  78. }
  79. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  80. Name: "title",
  81. Value: "牧场",
  82. })
  83. analysisAccuracy := make([]*model.OptionsAnalysisAccuracy, 0)
  84. pref := s.DB.Model(new(model.AnalysisAccuracy))
  85. if req.EndDate != "" && req.StartDate != "" {
  86. pref.Where("date_day BETWEEN ? AND ?", req.StartDate, req.EndDate)
  87. }
  88. if req.CattleParentCategoryId > 0 {
  89. pref.Where("cattle_parent_category_id = ?", req.CattleParentCategoryId)
  90. }
  91. if req.FeedFormulaId > 0 {
  92. pref.Where("feed_formula_id = ?", req.FeedFormulaId)
  93. }
  94. if len(req.PastureIds) > 0 {
  95. pref.Where("pasture_id IN ?", req.PastureIds)
  96. }
  97. if err := pref.Select("pasture_id,pasture_name,date_day,sum(iweight) as all_iweight,sum(lweight) as all_lweight,sum(oweight) as all_oweight,sum(actual_weight_minus) as all_actual_weight_minus,sum(allow_ratio) as all_allow_ratio,sum(alweight) as all_alweight").
  98. Group("pasture_id,pasture_name,date_day").Order("pasture_name,date_day").Find(&analysisAccuracy).Debug().Error; err != nil {
  99. return nil, xerr.WithStack(err)
  100. }
  101. mixedFodderAccurateRatio := make([]float64, 0)
  102. mapPastureName := make(map[string]bool, 0)
  103. mapDateDay := make(map[string]bool, 0)
  104. mapRatio := make(map[string]bool, 0)
  105. tableList := make([]*model.TableList, 0)
  106. for k, v := range analysisAccuracy {
  107. if _, ok := mapPastureName[v.PastureName]; !ok {
  108. res.Data.Chart.MixedFodderAccurateRatio.PastureName = append(res.Data.Chart.MixedFodderAccurateRatio.PastureName, v.PastureName)
  109. res.Data.Chart.MixedFodderAccurateRatio.PastureIds = append(res.Data.Chart.MixedFodderAccurateRatio.PastureIds, int32(v.PastureId))
  110. mapPastureName[v.PastureName] = true
  111. }
  112. dataDayFormat := strings.TrimRight(v.DateDay, "T00:00:00+08:00")
  113. if _, ok := mapDateDay[v.DateDay]; !ok {
  114. res.Data.Chart.MixedFodderAccurateRatio.DateDay = append(res.Data.Chart.MixedFodderAccurateRatio.DateDay, dataDayFormat)
  115. mapDateDay[v.DateDay] = true
  116. }
  117. valueRatio1 := float64(v.AllIweight/v.AllLweight) / 100.0
  118. if _, ok := mapRatio[v.DateDay]; !ok {
  119. valueRatio := make([]string, 0)
  120. for _, a := range analysisAccuracy {
  121. if a.DateDay == v.DateDay {
  122. valueRatio = append(valueRatio, strconv.FormatFloat(float64(a.AllIweight/a.AllLweight)/100.0, 'f', 2, 64))
  123. }
  124. }
  125. res.Data.Chart.MixedFodderAccurateRatio.DataList = append(res.Data.Chart.MixedFodderAccurateRatio.DataList, valueRatio)
  126. mapRatio[v.DateDay] = true
  127. tableList = append(tableList, &model.TableList{
  128. Name: fmt.Sprintf("date%d", k),
  129. Value: dataDayFormat,
  130. })
  131. }
  132. mixedFodderAccurateRatio = append(mixedFodderAccurateRatio, valueRatio1)
  133. }
  134. mixedFodderAccurateRatioMaxValue, mixedFodderAccurateRatioMiddleValue, mixedFodderAccurateRatioMinValue := calculateRatio(mixedFodderAccurateRatio)
  135. res.Data.Chart.MixedFodderAccurateRatio.MaxValue = strconv.FormatFloat(mixedFodderAccurateRatioMaxValue, 'f', 2, 64)
  136. res.Data.Chart.MixedFodderAccurateRatio.MiddleValue = strconv.FormatFloat(mixedFodderAccurateRatioMiddleValue, 'f', 2, 64)
  137. res.Data.Chart.MixedFodderAccurateRatio.MinValue = strconv.FormatFloat(mixedFodderAccurateRatioMinValue, 'f', 2, 64)
  138. res.Data.Table.TitleList = append(res.Data.Table.TitleList, tableList...)
  139. return res, nil
  140. }
  141. func calculateRatio(res []float64) (float64, float64, float64) {
  142. var maxValue, middleValue, minValue, allValue float64 = 0, 0, 0, 0
  143. for _, v := range res {
  144. if v > maxValue {
  145. maxValue = v
  146. }
  147. if v <= maxValue {
  148. minValue = v
  149. }
  150. allValue += v
  151. }
  152. middleValue = allValue / float64(len(res))
  153. return maxValue, middleValue, minValue
  154. }