dashboard.go 919 B

123456789101112131415161718192021222324252627282930313233343536
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "net/http"
  6. "gitee.com/xuyiping_admin/pkg/xerr"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. )
  9. func (s *StoreEntry) Bar(ctx context.Context) (*pasturePb.BarCowStructResponse, error) {
  10. barCowStructList := make([]*model.BarCowStruct, 0)
  11. var count int32 = 0
  12. if err := s.DB.Model(new(model.Cow)).
  13. Select("COUNT(*) AS number ,cow_type").
  14. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  15. Group("cow_type").
  16. Find(&barCowStructList).Error; err != nil {
  17. return nil, xerr.WithStack(err)
  18. }
  19. cowTypeMap := s.CowTypeMap()
  20. for _, v := range barCowStructList {
  21. count += v.Number
  22. }
  23. return &pasturePb.BarCowStructResponse{
  24. Code: http.StatusOK,
  25. Message: "ok",
  26. Data: &pasturePb.BarCowStructData{
  27. List: model.BarCowStructSlice(barCowStructList).ToPB(cowTypeMap, count),
  28. Total: 38563,
  29. },
  30. }, nil
  31. }