phone_add.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package service_phone
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "time"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkServicePhoneAddParam(req *pb_v1.ServicePhoneAddRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. case req.ClassId == 0:
  24. return status.Error(10003, "分类不能为空")
  25. case req.Phone == "":
  26. return status.Error(10003, "电话不能为空")
  27. case req.Name == "":
  28. return status.Error(10003, "姓名不能为空")
  29. }
  30. return nil
  31. }
  32. func ServicePhoneAdd(ctx context.Context, req *pb_v1.ServicePhoneAddRequest) (reply *pb_v1.ServicePhoneAddReply, err error) {
  33. reply = &pb_v1.ServicePhoneAddReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. // 参数检查
  47. err = checkServicePhoneAddParam(req)
  48. if err != nil {
  49. return nil, err
  50. }
  51. now := time.Now()
  52. dbname := utils.GetGardenDbName(req.GardenId)
  53. class := dbmodel.NewServicePhoneClass(dbname)
  54. where := map[string]interface{}{
  55. "id":req.ClassId,
  56. }
  57. err = class.Find(database.DB(), where)
  58. if err != nil && err != gorm.ErrRecordNotFound{
  59. return nil, errors.DataBaseError
  60. }
  61. if class.ID == 0 {
  62. return nil, errors.ErrRecordNotFound
  63. }
  64. enable := int32(1)
  65. if !req.Enable {
  66. enable = 2
  67. }
  68. p := dbmodel.TServicePhone{
  69. Enable:enable,
  70. ClassId:req.ClassId,
  71. Name:req.Name,
  72. Phone:req.Phone,
  73. UpdatedAt:now,
  74. CreatedAt:now,
  75. }
  76. p.SetTable(dbname)
  77. err = p.Insert(database.DB())
  78. if err != nil {
  79. return nil, errors.DataBaseError
  80. }
  81. reply.Id = p.ID
  82. return reply, nil
  83. }