statistic.go 2.3 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-company/errors"
  13. dbmodel "property-company/model"
  14. pb_v1 "property-company/pb/v1"
  15. "time"
  16. )
  17. func checkStatisticParam(req *pb_v1.CompanyStatisticRequest) error {
  18. switch {
  19. case req.Cid == 0:
  20. return status.Error(10003, "小区不能为空")
  21. }
  22. return nil
  23. }
  24. func CompanyStatistic(ctx context.Context, req *pb_v1.CompanyStatisticRequest) (reply *pb_v1.CompanyStatisticReply, err error) {
  25. reply = &pb_v1.CompanyStatisticReply{}
  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. obj := dbmodel.TStatisticObj{}
  44. where := map[string]interface{}{
  45. "cid": req.Cid,
  46. }
  47. objList, err := obj.List(database.DB(), where, nil, -1, -1)
  48. if err != nil {
  49. return nil, errors.DataBaseError
  50. }
  51. for _, v := range objList {
  52. switch v.ObjType {
  53. case ObjTypeHouse:
  54. reply.HouseCount = v.Total
  55. case ObjTypeSpace:
  56. reply.SpaceCount = v.Total
  57. case ObjTypeGarden:
  58. reply.GardenCount = v.Total
  59. case ObjTypeUser:
  60. reply.UserCount = v.Total
  61. }
  62. }
  63. now := time.Now()
  64. nowDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  65. end := nowDay.Unix()
  66. start := end - 3600*24*6
  67. where["date_timestamp >="] = start
  68. where["date_timestamp <="] = end
  69. deal := dbmodel.TStatisticDeal{}
  70. dealList, err := deal.List(database.DB(), where, nil, -1, -1)
  71. m := map[int64]dbmodel.TStatisticDeal{}
  72. for _, v := range dealList {
  73. m[v.DateTimestamp] = v
  74. }
  75. reply.DealList = make([]*pb_v1.DealItem, 7)
  76. for i, _ := range reply.DealList {
  77. timestamp := start + int64(i*3600*24)
  78. reply.DealList[i] = &pb_v1.DealItem{
  79. DealCount: m[timestamp].DealCount,
  80. DealAmount: m[timestamp].DealAmount,
  81. DateTimestamp: timestamp,
  82. }
  83. }
  84. return reply, err
  85. }