statistic.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package statistic
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. func checkStatisticParam(req *pb_v1.StatisticRequest) error {
  18. switch {
  19. case req.GardenId == 0:
  20. return status.Error(10003, "小区不能为空")
  21. }
  22. return nil
  23. }
  24. func Statistic(ctx context.Context, req *pb_v1.StatisticRequest) (reply *pb_v1.StatisticReply, err error) {
  25. reply = &pb_v1.StatisticReply{}
  26. // 捕获各个task中的异常并返回给调用者
  27. defer func() {
  28. if r := recover(); r != nil {
  29. err = fmt.Errorf("%+v", r)
  30. e := &status.Status{}
  31. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  32. logger.Error("err",
  33. zap.String("system_err", err.Error()),
  34. zap.Stack("stacktrace"))
  35. }
  36. }
  37. }()
  38. // 参数检查
  39. err = checkStatisticParam(req)
  40. if err != nil {
  41. return nil, err
  42. }
  43. dbname := utils.GetGardenDbName(req.GardenId)
  44. obj := dbmodel.NewStatisticObj(dbname)
  45. objList, err := obj.List(database.DB(), nil, nil, -1, -1)
  46. if err != nil {
  47. return nil, errors.DataBaseError
  48. }
  49. repair := dbmodel.NewStatisticRepair(dbname)
  50. repairList, err := repair.List(database.DB(), nil, nil, -1, -1)
  51. if err != nil {
  52. return nil, errors.DataBaseError
  53. }
  54. for _, v := range objList {
  55. switch v.ObjType {
  56. case ObjTypeHouse:
  57. reply.HouseCount = v.Total
  58. case ObjTypeSpace:
  59. reply.SpaceCount = v.Total
  60. case ObjTypeVehicle:
  61. reply.VehicleCount = v.Total
  62. case ObjTypeUser:
  63. reply.UserCount = v.Total
  64. }
  65. }
  66. reply.List = []*pb_v1.StatisticWaitFinish{}
  67. for _, v := range repairList {
  68. item := &pb_v1.StatisticWaitFinish{
  69. Type:StatisticDesc[int(v.HandleType)],
  70. WaitCount:v.Total - v.Finish,
  71. FinishCount:v.Finish,
  72. }
  73. if item.WaitCount < 0 {
  74. item.WaitCount = 0
  75. }
  76. if item.FinishCount < 0 {
  77. item.FinishCount = 0
  78. }
  79. reply.List = append(reply.List, item)
  80. }
  81. return reply, err
  82. }