add.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package user
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/util"
  9. "property-company/consts"
  10. "property-company/errors"
  11. dbmodel "property-company/model"
  12. pb_v1 "property-company/pb/v1"
  13. "strings"
  14. "time"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkCompanyUserAddParam(req *pb_v1.CompanyUserAddRequest) error {
  21. switch {
  22. case req.Password == "":
  23. return status.Error(10003, "密码不能为空")
  24. case req.Phone == "":
  25. return status.Error(10003, "联系人电话不能为空")
  26. case req.UserName == "":
  27. return status.Error(10003, "账户名不能为空")
  28. case req.RealName == "":
  29. return status.Error(10003, "真实姓名不能为空")
  30. case req.Cid < 1:
  31. return status.Error(10003, "物业公司不能为空")
  32. }
  33. return nil
  34. }
  35. //
  36. func CompanyUserAdd(ctx context.Context, req *pb_v1.CompanyUserAddRequest) (reply *pb_v1.CompanyUserAddReply, err error) {
  37. reply = &pb_v1.CompanyUserAddReply{}
  38. // 捕获各个task中的异常并返回给调用者
  39. defer func() {
  40. if r := recover(); r != nil {
  41. err = fmt.Errorf("%+v", r)
  42. e := &status.Status{}
  43. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  44. logger.Error("err",
  45. zap.String("system_err", err.Error()),
  46. zap.Stack("stacktrace"))
  47. }
  48. }
  49. }()
  50. err = checkCompanyUserAddParam(req)
  51. if err != nil {
  52. return nil, err
  53. }
  54. now := time.Now()
  55. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  56. user := dbmodel.TUser{
  57. Cid: req.Cid,
  58. User: req.UserName,
  59. Phone: req.Phone,
  60. Email: req.Email,
  61. Password: passwd,
  62. UpdatedAt: now,
  63. CreatedAt: now,
  64. Super: false,
  65. RealName: req.RealName,
  66. }
  67. db := database.DB().Begin()
  68. err = user.Insert(db)
  69. if err != nil {
  70. db.Rollback()
  71. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  72. return nil, errors.UserExist
  73. }
  74. return nil, errors.DataBaseError
  75. }
  76. /*
  77. openimId := fmt.Sprintf("property%d", user.ID)
  78. mreq := pb_v1.OpenImRegisterRequest{UserId: openimId, Platform: 5, NickName: req.RealName}
  79. _, err = pb.Thirdparty.OpenImRegister(ctx, &mreq)
  80. if err == nil {
  81. where := map[string]interface{}{
  82. "id": user.ID,
  83. }
  84. values := map[string]interface{}{
  85. "openim_id": openimId,
  86. }
  87. err = user.Update(db, where, values)
  88. if err != nil {
  89. db.Rollback()
  90. return nil, errors.DataBaseError
  91. }
  92. }*/
  93. db.Commit()
  94. reply.Id = user.ID
  95. return reply, nil
  96. }