device_statistic.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package project
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "github.com/jaryhe/gopkgs/logger"
  8. "go.uber.org/zap"
  9. "smart-site-management/errors"
  10. "smart-site-management/model"
  11. "smart-site-management/pb/v1"
  12. )
  13. func ProjectDeviceStatistics(ctx context.Context, req *v1.ProjectDeviceStatisticsRequest) (reply *v1.ProjectDeviceStatisticsReply, err error) {
  14. reply = &v1.ProjectDeviceStatisticsReply{}
  15. if req.ProjectId == 0 {
  16. return nil, errors.ParamsError
  17. }
  18. pt := &model.TDeviceTypeItem{}
  19. m, _, err := pt.AllMap(database.DB())
  20. if err != nil {
  21. logger.Error("DeviceList",
  22. zap.String("err", err.Error()))
  23. return nil, errors.DataBaseError
  24. }
  25. p := &model.TDevice{}
  26. list, err := p.StatisticWithProject(database.DB(), req.ProjectId)
  27. if err != nil {
  28. return nil, errors.DataBaseError
  29. }
  30. reply.List = make([]*v1.ProjectDeviceStatistics, len(list))
  31. for i, v := range list {
  32. item := &v1.ProjectDeviceStatistics{
  33. DeviceType:m[v.TypeCode],
  34. Total:v.Total,
  35. Online:v.Online,
  36. }
  37. reply.List[i] = item
  38. }
  39. return reply, nil
  40. }