wait_count.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house
  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-household/errors"
  13. dbmodel "property-household/model"
  14. pb_v1 "property-household/pb/v1"
  15. )
  16. func checkHouseholdWaitCountParam(req *pb_v1.HouseholdWaitCountRequest) error {
  17. switch {
  18. case req.GardenId < 1:
  19. return status.Error(10003, "id不能为空")
  20. case req.BuildingId < 0 && req.UnitId < 0 && req.HouseId < 0:
  21. return status.Error(10003, "id 不能为空")
  22. }
  23. return nil
  24. }
  25. //
  26. func HouseholdWaitCount(ctx context.Context, req *pb_v1.HouseholdWaitCountRequest) (reply *pb_v1.HouseholdWaitCountReply, err error) {
  27. reply = &pb_v1.HouseholdWaitCountReply{}
  28. // 捕获各个task中的异常并返回给调用者
  29. defer func() {
  30. if r := recover(); r != nil {
  31. err = fmt.Errorf("%+v", r)
  32. e := &status.Status{}
  33. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  34. logger.Error("err",
  35. zap.String("system_err", err.Error()),
  36. zap.Stack("stacktrace"))
  37. }
  38. }
  39. }()
  40. err = checkHouseholdWaitCountParam(req)
  41. if err != nil {
  42. return nil, err
  43. }
  44. p := dbmodel.THouse{}
  45. where := map[string]interface{}{
  46. "approve_status":HouseholdApproveStatusWait,
  47. }
  48. if req.GardenId > 0 {
  49. where["garden_id"] = req.GardenId
  50. }
  51. if req.BuildingId > 0 {
  52. where["building_id"] = req.BuildingId
  53. }
  54. if req.UnitId > 0 {
  55. where["unit_id"] = req.UnitId
  56. }
  57. if req.HouseId > 0 {
  58. where["house_id"] = req.HouseId
  59. }
  60. reply.Count, err = p.CountDistinct(database.DB(), where, nil, "uid")
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. return reply, nil
  65. }