charge_urge_set.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. pb_v1 "property-garden/pb/v1"
  15. "property-garden/utils"
  16. )
  17. func checkChargeUrgeSetParam(req *pb_v1.ChargeUrgeSetRequest) error {
  18. switch {
  19. case req.GardenId == 0:
  20. return status.Error(10003, "小区不能为空")
  21. }
  22. return nil
  23. }
  24. func ChargeUrgeSet(ctx context.Context, req *pb_v1.ChargeUrgeSetRequest) (reply *pb_v1.ChargeUrgeSetReply, err error) {
  25. reply = &pb_v1.ChargeUrgeSetReply{}
  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 = checkChargeUrgeSetParam(req)
  40. if err != nil {
  41. return nil, err
  42. }
  43. dbname := utils.GetGardenDbName(req.GardenId)
  44. // 先尝试更新
  45. urge := dbmodel.NewChargeUrge(dbname)
  46. where := map[string]interface{}{
  47. "id":1,
  48. }
  49. values := map[string]interface{}{
  50. "urge_day":req.UrgeDay,
  51. "urge_target":req.UrgeTarget,
  52. "urge_msg_type":req.UrgeMsgType,
  53. }
  54. if req.Id > 0 {
  55. err := urge.Update(database.DB(), where, values)
  56. if err != nil {
  57. return nil, errors.DataBaseError
  58. }
  59. return reply, nil
  60. }
  61. urge.UrgeMsgType = req.UrgeMsgType
  62. urge.UrgeTarget = req.UrgeTarget
  63. urge.UrgeDay = req.UrgeDay
  64. urge.ID = 1
  65. err = urge.Insert(database.DB())
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. return reply, nil
  70. }