statistic_service.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package backend
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "kpt-tmr-group/model"
  8. "kpt-tmr-group/pkg/logger/zaplog"
  9. "kpt-tmr-group/pkg/xerr"
  10. operationPb "kpt-tmr-group/proto/go/backend/operation"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. )
  15. // PastureDetailById 获取指定牧场详情
  16. func (s *StoreEntry) PastureDetailById(ctx context.Context, pastureId int64) (*model.GroupPasture, error) {
  17. result := &model.GroupPasture{Id: pastureId}
  18. if err := s.DB.Where("is_delete = ? and is_show = ?", operationPb.IsShow_OK, operationPb.IsShow_OK).First(result).Error; err != nil {
  19. return nil, xerr.WithStack(err)
  20. }
  21. return result, nil
  22. }
  23. // SearchFormulaEstimateList 配方评估
  24. func (s *StoreEntry) SearchFormulaEstimateList(ctx context.Context, req *operationPb.SearchFormulaEstimateRequest) (*model.FormulaEstimateResponse, error) {
  25. pastureDetail, err := s.PastureDetailById(ctx, int64(req.PastureId))
  26. if err != nil {
  27. zaplog.Error("SearchFormulaEstimateList", zap.Any("Err", err), zap.Any("req", req))
  28. return nil, xerr.Customf("该牧场数据错误")
  29. }
  30. pastureClient := model.NewPastureClient(pastureDetail)
  31. body := &model.FormulaEstimateRequest{
  32. Name: req.Name,
  33. Page: req.Pagination.Page,
  34. Offset: req.Pagination.PageOffset,
  35. PageCount: req.Pagination.PageSize,
  36. ReturnType: "map",
  37. ParamMaps: &model.FormulaEstimateParams{
  38. PastureId: fmt.Sprintf("%d", req.PastureId),
  39. StartTime: req.StartTime,
  40. StopTime: req.EndTime,
  41. InputDatetime: nil,
  42. Search: fmt.Sprintf("%d", req.SearchType),
  43. TempletId: fmt.Sprintf("%d", req.TemplateId),
  44. Barid: fmt.Sprintf("%d", req.BarnId),
  45. },
  46. }
  47. url := fmt.Sprintf("%s/%s", pastureDetail.Domain, "authdata/GetDataByName")
  48. result, err := pastureClient.DoPost(url, body)
  49. if err != nil {
  50. return nil, xerr.WithStack(err)
  51. }
  52. response := &model.FormulaEstimateResponse{}
  53. if err = json.Unmarshal(result, response); err != nil {
  54. return nil, xerr.WithStack(err)
  55. }
  56. if response.Code == 400 {
  57. if response.Msg == "登录超时" {
  58. } else {
  59. return nil, xerr.WithStack(err)
  60. }
  61. }
  62. return response, nil
  63. }
  64. func (s *StoreEntry) SearchAnalysisAccuracy(ctx context.Context, req *operationPb.SearchAnalysisAccuracyRequest) (*model.SearchAnalysisAccuracyResponse, error) {
  65. dataList := &model.CommonValueRatio{
  66. MaxValue: "",
  67. MiddleValue: "",
  68. MinValue: "",
  69. PastureName: make([]string, 0),
  70. DateDay: make([]string, 0),
  71. DataList: make([][]string, 0),
  72. PastureIds: make([]int32, 0),
  73. }
  74. res := &model.SearchAnalysisAccuracyResponse{
  75. Code: http.StatusOK,
  76. Msg: "ok",
  77. Data: &model.AnalysisAccuracyData{
  78. Chart: &model.Chart{
  79. MixedFodderAccurateRatio: dataList,
  80. MixedFodderCorrectRatio: dataList,
  81. SprinkleFodderAccurateRatio: dataList,
  82. SprinkleFodderCorrectRatio: dataList,
  83. },
  84. Table: &model.Table{
  85. TitleList: make([]*model.TableList, 0),
  86. DataList: make([]*interface{}, 0),
  87. },
  88. },
  89. }
  90. res.Data.Table.TitleList = append(res.Data.Table.TitleList, &model.TableList{
  91. Name: "title",
  92. Value: "牧场",
  93. })
  94. analysisAccuracy := make([]*model.OptionsAnalysisAccuracy, 0)
  95. pref := s.DB.Model(new(model.AnalysisAccuracy))
  96. if req.EndDate != "" && req.StartDate != "" {
  97. pref.Where("date_day BETWEEN ? AND ?", req.StartDate, req.EndDate)
  98. }
  99. if req.CattleParentCategoryId > 0 {
  100. pref.Where("cattle_parent_category_id = ?", req.CattleParentCategoryId)
  101. }
  102. if req.FeedFormulaId > 0 {
  103. pref.Where("feed_formula_id = ?", req.FeedFormulaId)
  104. }
  105. if len(req.PastureIds) > 0 {
  106. pref.Where("pasture_id IN ?", req.PastureIds)
  107. }
  108. 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").
  109. Group("pasture_id,pasture_name,date_day").Order("pasture_name,date_day").Find(&analysisAccuracy).Debug().Error; err != nil {
  110. return nil, xerr.WithStack(err)
  111. }
  112. mixedFodderAccurateRatio := make([]float64, 0)
  113. mapPastureName := make(map[string]bool, 0)
  114. mapDateDay := make(map[string]bool, 0)
  115. mapRatio := make(map[string]bool, 0)
  116. tableList := make([]*model.TableList, 0)
  117. for k, v := range analysisAccuracy {
  118. if _, ok := mapPastureName[v.PastureName]; !ok {
  119. res.Data.Chart.MixedFodderAccurateRatio.PastureName = append(res.Data.Chart.MixedFodderAccurateRatio.PastureName, v.PastureName)
  120. res.Data.Chart.MixedFodderAccurateRatio.PastureIds = append(res.Data.Chart.MixedFodderAccurateRatio.PastureIds, int32(v.PastureId))
  121. mapPastureName[v.PastureName] = true
  122. }
  123. dataDayFormat := strings.TrimRight(v.DateDay, "T00:00:00+08:00")
  124. if _, ok := mapDateDay[v.DateDay]; !ok {
  125. res.Data.Chart.MixedFodderAccurateRatio.DateDay = append(res.Data.Chart.MixedFodderAccurateRatio.DateDay, dataDayFormat)
  126. mapDateDay[v.DateDay] = true
  127. }
  128. valueRatio1 := float64(v.AllIweight/v.AllLweight) / 100.0
  129. if _, ok := mapRatio[v.DateDay]; !ok {
  130. valueRatio := make([]string, 0)
  131. for _, a := range analysisAccuracy {
  132. if a.DateDay == v.DateDay {
  133. valueRatio = append(valueRatio, strconv.FormatFloat(float64(a.AllIweight/a.AllLweight)/100.0, 'f', 2, 64))
  134. }
  135. }
  136. res.Data.Chart.MixedFodderAccurateRatio.DataList = append(res.Data.Chart.MixedFodderAccurateRatio.DataList, valueRatio)
  137. mapRatio[v.DateDay] = true
  138. tableList = append(tableList, &model.TableList{
  139. Name: fmt.Sprintf("date%d", k),
  140. Value: dataDayFormat,
  141. })
  142. }
  143. mixedFodderAccurateRatio = append(mixedFodderAccurateRatio, valueRatio1)
  144. }
  145. mixedFodderAccurateRatioMaxValue, mixedFodderAccurateRatioMiddleValue, mixedFodderAccurateRatioMinValue := calculateRatio(mixedFodderAccurateRatio)
  146. res.Data.Chart.MixedFodderAccurateRatio.MaxValue = strconv.FormatFloat(mixedFodderAccurateRatioMaxValue, 'f', 2, 64)
  147. res.Data.Chart.MixedFodderAccurateRatio.MiddleValue = strconv.FormatFloat(mixedFodderAccurateRatioMiddleValue, 'f', 2, 64)
  148. res.Data.Chart.MixedFodderAccurateRatio.MinValue = strconv.FormatFloat(mixedFodderAccurateRatioMinValue, 'f', 2, 64)
  149. res.Data.Table.TitleList = append(res.Data.Table.TitleList, tableList...)
  150. return res, nil
  151. }
  152. func calculateRatio(res []float64) (float64, float64, float64) {
  153. var maxValue, middleValue, minValue, allValue float64 = 0, 0, 0, 0
  154. for _, v := range res {
  155. if v > maxValue {
  156. maxValue = v
  157. }
  158. if v <= maxValue {
  159. minValue = v
  160. }
  161. allValue += v
  162. }
  163. middleValue = allValue / float64(len(res))
  164. return maxValue, middleValue, minValue
  165. }