dashboard.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "kpt-pasture/util"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  11. "go.uber.org/zap"
  12. "gitee.com/xuyiping_admin/pkg/xerr"
  13. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  14. )
  15. func (s *StoreEntry) Bar(ctx context.Context) (*pasturePb.BarCowStructResponse, error) {
  16. barCowStructList := make([]*model.BarCowStruct, 0)
  17. var count int32 = 0
  18. if err := s.DB.Model(new(model.Cow)).
  19. Select("COUNT(*) AS number ,cow_type").
  20. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  21. Group("cow_type").
  22. Find(&barCowStructList).Error; err != nil {
  23. return nil, xerr.WithStack(err)
  24. }
  25. cowTypeMap := s.CowTypeMap()
  26. for _, v := range barCowStructList {
  27. count += v.Number
  28. }
  29. return &pasturePb.BarCowStructResponse{
  30. Code: http.StatusOK,
  31. Msg: "ok",
  32. Data: &pasturePb.BarCowStructData{
  33. List: model.BarCowStructSlice(barCowStructList).ToPB(cowTypeMap, count),
  34. Total: 38563,
  35. },
  36. }, nil
  37. }
  38. func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckRingResponse, error) {
  39. userModel, err := s.GetUserModel(ctx)
  40. if err != nil {
  41. return nil, xerr.WithStack(err)
  42. }
  43. estrusWarningCowList := make([]*model.NeckRingEstrus, 0)
  44. estrusWarningLevelItems := map[int32]int32{
  45. int32(pasturePb.EstrusLevel_Low): 0,
  46. int32(pasturePb.EstrusLevel_Middle): 0,
  47. int32(pasturePb.EstrusLevel_High): 0,
  48. }
  49. if err = s.DB.Model(new(model.EventEstrus)).
  50. Where("pasture_id = ?", userModel.AppPasture.Id).
  51. Where("is_show = ?", pasturePb.IsShow_Ok).
  52. Group("cow_id").Find(&estrusWarningCowList).Error; err != nil {
  53. zaplog.Error("NeckRingWarning", zap.Any("estrusWarningNumber", err))
  54. }
  55. for _, v := range estrusWarningCowList {
  56. estrusWarningLevelItems[int32(v.Level)] += estrusWarningLevelItems[int32(v.Level)]
  57. }
  58. healthWarningNumber := int64(0)
  59. if err = s.DB.Model(new(model.NeckRingHealthWarning)).
  60. Where("pasture_id = ?", userModel.AppPasture.Id).
  61. Where("is_show = ?", pasturePb.IsShow_Ok).
  62. Group("cow_id").
  63. Count(&healthWarningNumber).Error; err != nil {
  64. zaplog.Error("NeckRingWarning", zap.Any("estrusWarningNumber", err))
  65. }
  66. return &pasturePb.IndexNeckRingResponse{
  67. Code: http.StatusOK,
  68. Msg: "ok",
  69. Data: &pasturePb.NeckRingData{
  70. EstrusWarningNumber: int32(len(estrusWarningCowList)),
  71. HealthWarningNumber: int32(healthWarningNumber),
  72. AbortionWarningNumber: 0,
  73. StressWarningNumber: 0,
  74. EstrusWarningLevelItems: estrusWarningLevelItems,
  75. },
  76. }, nil
  77. }
  78. func (s *StoreEntry) FocusIndicators(ctx context.Context, dimension string) (*pasturePb.IndexFocusIndicatorsResponse, error) {
  79. userModel, err := s.GetUserModel(ctx)
  80. if err != nil {
  81. return nil, xerr.WithStack(err)
  82. }
  83. userFocusIndicators := userModel.SystemUser.IndicatorsKinds
  84. if len(userFocusIndicators) <= 0 {
  85. userFocusIndicators = "all_cow,output_number,input_number,fatten_cattle_number,sales_volume"
  86. }
  87. userFocusIndicatorsList := strings.Split(userFocusIndicators, ",")
  88. indicatorsDataList := make([]*model.IndicatorsData, 0)
  89. pref := s.DB.Model(new(model.IndicatorsData)).
  90. Where("pasture_id = ?", userModel.AppPasture.Id).
  91. Where("kind in (?)", userFocusIndicatorsList)
  92. /*if dimension == "Year" {
  93. pref.Where("date = ?", time.Now().Format(model.LayoutMonth))
  94. }*/
  95. nowTime := time.Now()
  96. if dimension == "Month" {
  97. pref.Where("date = ?", nowTime.Format(model.LayoutMonth))
  98. }
  99. if err = pref.Find(&indicatorsDataList).Error; err != nil {
  100. zaplog.Error("FocusIndicators", zap.Any("err", err))
  101. }
  102. indicatorsDetailsMap, err := s.GetIndicatorsDetailsMap(ctx)
  103. if err != nil {
  104. return nil, xerr.WithStack(err)
  105. }
  106. data := make([]*pasturePb.FocusData, 0)
  107. for _, v := range indicatorsDataList {
  108. indicatorsDetails, ok := indicatorsDetailsMap[v.Kind]
  109. if !ok {
  110. continue
  111. }
  112. onYear, onMonth := "", ""
  113. if dimension == "Year" {
  114. return nil, xerr.Custom("暂不支持该维度")
  115. }
  116. if dimension == "Month" {
  117. lastMonth := nowTime.AddDate(0, -1, 0).Format(model.LayoutMonth)
  118. oldIndicators, _ := s.GetIndicatorsDataByDate(userModel.AppPasture.Id, v.Kind, lastMonth)
  119. if oldIndicators != nil {
  120. if oldIndicators.Value != "" && oldIndicators.Value != "0" {
  121. oldValue, _ := strconv.ParseFloat(oldIndicators.Value, 64)
  122. currValue, _ := strconv.ParseFloat(v.Value, 64)
  123. onMonthValue := (oldValue - currValue) / oldValue * 100
  124. omv := util.RoundToTwoDecimals(onMonthValue)
  125. onMonth = strconv.FormatFloat(omv, 'f', 2, 64) + "%"
  126. }
  127. }
  128. }
  129. data = append(data, &pasturePb.FocusData{
  130. Kind: indicatorsDetails.Kind,
  131. Name: indicatorsDetails.Name,
  132. Value: v.Value,
  133. Describe: indicatorsDetails.Zh,
  134. UnitName: indicatorsDetails.Unit,
  135. OnMonth: onMonth,
  136. OnYear: onYear,
  137. })
  138. }
  139. return &pasturePb.IndexFocusIndicatorsResponse{
  140. Code: http.StatusOK,
  141. Msg: "ok",
  142. Data: data,
  143. }, err
  144. }