update.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "git.getensh.com/common/gopkgs/util"
  11. "go.uber.org/zap"
  12. "google.golang.org/grpc/status"
  13. "gorm.io/gorm"
  14. "property-company/consts"
  15. "property-company/errors"
  16. dbmodel "property-company/model"
  17. pb_v1 "property-company/pb/v1"
  18. "strings"
  19. "time"
  20. )
  21. func checkCompanyUserUpdateParam(req *pb_v1.CompanyUserUpdateRequest) error {
  22. switch {
  23. case req.Phone == "":
  24. return status.Error(10003, "联系人电话不能为空")
  25. case req.UserName == "":
  26. return status.Error(10003, "账户名不能为空")
  27. case req.RealName == "":
  28. return status.Error(10003, "真实姓名不能为空")
  29. case req.Cid < 1:
  30. return status.Error(10003, "物业公司不能为空")
  31. case req.Id < 1:
  32. return status.Error(10003, "用户id不能为空")
  33. }
  34. return nil
  35. }
  36. //
  37. func CompanyUserUpdate(ctx context.Context, req *pb_v1.CompanyUserUpdateRequest) (reply *pb_v1.CompanyUserUpdateReply, err error) {
  38. reply = &pb_v1.CompanyUserUpdateReply{}
  39. // 捕获各个task中的异常并返回给调用者
  40. defer func() {
  41. if r := recover(); r != nil {
  42. err = fmt.Errorf("%+v", r)
  43. e := &status.Status{}
  44. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  45. logger.Error("err",
  46. zap.String("system_err", err.Error()),
  47. zap.Stack("stacktrace"))
  48. }
  49. }
  50. }()
  51. err = checkCompanyUserUpdateParam(req)
  52. if err != nil {
  53. return nil, err
  54. }
  55. now := time.Now()
  56. passwd := ""
  57. if req.Password != "" {
  58. passwd, _ = util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  59. }
  60. where := map[string]interface{}{
  61. "id": req.Id,
  62. "cid": req.Cid,
  63. }
  64. values := map[string]interface{}{
  65. "email": req.Email,
  66. "user": req.UserName,
  67. "phone": req.Phone,
  68. "updated_at": now,
  69. "real_name": req.RealName,
  70. }
  71. if passwd != "" {
  72. values["password"] = passwd
  73. }
  74. src := &dbmodel.TUser{}
  75. err = src.Find(database.DB(), where)
  76. if err != nil && err != gorm.ErrRecordNotFound {
  77. return nil, errors.DataBaseError
  78. }
  79. if src.ID == 0 {
  80. return nil, errors.ErrRecordNotFound
  81. }
  82. reply.Origin = &pb_v1.CompanyUserUpdateRequest{
  83. Id: req.Id,
  84. Cid: req.Cid,
  85. UserName: src.User,
  86. Email: src.Email,
  87. Phone: src.Phone,
  88. RealName: src.RealName,
  89. }
  90. user := dbmodel.TUser{}
  91. db := database.DB().Begin()
  92. err = user.Update(db, where, values)
  93. if err != nil {
  94. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  95. db.Rollback()
  96. return nil, errors.UserExist
  97. }
  98. db.Rollback()
  99. return nil, errors.DataBaseError
  100. }
  101. db.Commit()
  102. return reply, nil
  103. }