update.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "time"
  15. "xingjia-management-gateway/apis"
  16. "xingjia-management-gateway/consts"
  17. "xingjia-management-gateway/errors"
  18. "xingjia-management-gateway/impl/v1/operation_log"
  19. dbmodel "xingjia-management-gateway/model"
  20. )
  21. func checkUserUpdateParam(req *apis.UserUpdateRequest) error {
  22. switch {
  23. case req.UpdateId == 0:
  24. return status.Error(10003, "未输入目标")
  25. case req.SelfId == 0:
  26. return status.Error(10003, "未知操作者")
  27. case req.UserType > 0 && req.UserType != consts.UserTypeNormal && req.UserType != consts.UserTypeTemp:
  28. return status.Error(10003, "不支持的账号类型")
  29. case req.UserType > 0 && req.UserType == consts.UserTypeTemp && (req.EffectiveStart < 1 || req.EffectiveEnd < 1 || req.EffectiveStart > req.EffectiveEnd):
  30. return status.Error(10003, "临时账号请输入有效时间")
  31. }
  32. return nil
  33. }
  34. //
  35. func UserUpdate(ctx context.Context, req *apis.UserUpdateRequest) (reply *apis.UserUpdateReply, err error) {
  36. reply = &apis.UserUpdateReply{}
  37. // 捕获各个task中的异常并返回给调用者
  38. defer func() {
  39. if r := recover(); r != nil {
  40. err = fmt.Errorf("%+v", r)
  41. e := &status.Status{}
  42. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  43. logger.Error("err",
  44. zap.String("system_err", err.Error()),
  45. zap.Stack("stacktrace"))
  46. }
  47. }
  48. }()
  49. if err = checkUserUpdateParam(req); err != nil {
  50. return nil, err
  51. }
  52. if err = checkAdmin(req.SelfId); err != nil {
  53. return nil, err
  54. }
  55. old := &dbmodel.TUser{}
  56. where := map[string]interface{}{
  57. "id": req.UpdateId,
  58. }
  59. err = old.Find(database.DB(), where)
  60. if err != nil && err != gorm.ErrRecordNotFound {
  61. return nil, errors.DataBaseError
  62. }
  63. if old.ID == 0 {
  64. return nil, errors.ErrRecordNotFound
  65. }
  66. if old.UserType == consts.UserTypeSuper {
  67. return nil, status.Error(10003, "超级管理员类型不能更改")
  68. }
  69. values := map[string]interface{}{}
  70. now := time.Now()
  71. if req.UserType == consts.UserTypeNormal && old.UserType != req.UserType {
  72. values["effective_end"] = 0
  73. values["user_type"] = req.UserType
  74. }
  75. if req.UserType == consts.UserTypeTemp && (req.EffectiveStart != old.EffectiveStart || req.EffectiveEnd != old.EffectiveEnd) {
  76. values["effective_start"] = req.EffectiveStart
  77. values["effective_end"] = req.EffectiveEnd
  78. }
  79. if req.User != "" && req.User != old.User {
  80. values["user"] = req.User
  81. }
  82. if req.RealName != "" && req.RealName != old.RealName {
  83. values["real_name"] = req.RealName
  84. }
  85. if req.Password != "" {
  86. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  87. values["password"] = passwd
  88. }
  89. if len(values) == 0 {
  90. return reply, nil
  91. }
  92. values["updated_at"] = now
  93. db := database.DB().Begin()
  94. defer func() {
  95. if err != nil {
  96. db.Rollback()
  97. }
  98. }()
  99. p := dbmodel.TUser{}
  100. err = p.Update(db, where, values)
  101. if err != nil {
  102. return nil, errors.DataBaseError
  103. }
  104. err = p.Find(db, where)
  105. if err != nil {
  106. return nil, errors.DataBaseError
  107. }
  108. // 操作日志
  109. content := getUserContent(*old)
  110. tcontent := getUserContent(p)
  111. logReq := &apis.OperationLogAddRequest{
  112. OriginContent: content,
  113. TargetContent: tcontent,
  114. Uid: req.SelfId,
  115. User: req.SelfName,
  116. Module: consts.OperationModuleAccount,
  117. OperationTime: time.Now().Unix(),
  118. Operation: consts.OperationActionUserResetPassword,
  119. }
  120. err = operation_log.OperationLogAddHandle(logReq, db)
  121. if err != nil {
  122. return nil, err
  123. }
  124. db.Commit()
  125. return reply, nil
  126. }