charge_urge_info.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge
  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. "gorm.io/gorm"
  13. "property-garden/errors"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkChargeUrgeInfoParam(req *pb_v1.ChargeUrgeInfoRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. }
  23. return nil
  24. }
  25. func ChargeUrgeInfo(ctx context.Context, req *pb_v1.ChargeUrgeInfoRequest) (reply *pb_v1.ChargeUrgeInfoReply, err error) {
  26. reply = &pb_v1.ChargeUrgeInfoReply{}
  27. // 捕获各个task中的异常并返回给调用者
  28. defer func() {
  29. if r := recover(); r != nil {
  30. err = fmt.Errorf("%+v", r)
  31. e := &status.Status{}
  32. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  33. logger.Error("err",
  34. zap.String("system_err", err.Error()),
  35. zap.Stack("stacktrace"))
  36. }
  37. }
  38. }()
  39. // 参数检查
  40. err = checkChargeUrgeInfoParam(req)
  41. if err != nil {
  42. return nil, err
  43. }
  44. dbname := utils.GetGardenDbName(req.GardenId)
  45. urge := dbmodel.NewChargeUrge(dbname)
  46. where := map[string]interface{}{
  47. "id":1,
  48. }
  49. err = urge.Find(database.DB(), where)
  50. if err != nil && err != gorm.ErrRecordNotFound{
  51. return nil, errors.DataBaseError
  52. }
  53. if urge.ID == 0 {
  54. reply.UrgeDay = 0
  55. reply.UrgeTarget = 1
  56. reply.UrgeMsgType = 1
  57. return reply, nil
  58. }
  59. reply.UrgeDay = urge.UrgeDay
  60. reply.UrgeTarget = urge.UrgeTarget
  61. reply.UrgeMsgType = urge.UrgeMsgType
  62. reply.Id = 1
  63. return reply, nil
  64. }