add.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "strings"
  15. "time"
  16. "xingjia-management-gateway/apis"
  17. "xingjia-management-gateway/consts"
  18. "xingjia-management-gateway/errors"
  19. "xingjia-management-gateway/impl/v1/operation_log"
  20. dbmodel "xingjia-management-gateway/model"
  21. )
  22. func checkUserAddParam(req *apis.UserAddRequest) error {
  23. switch {
  24. case req.RealName == "":
  25. return status.Error(10003, "请输入姓名")
  26. case req.Password == "":
  27. return status.Error(10003, "请输入密码")
  28. case req.User == "":
  29. return status.Error(10003, "请输入账号")
  30. case req.UserType != consts.UserTypeNormal && req.UserType != consts.UserTypeTemp:
  31. return status.Error(10003, "不支持的账号类型")
  32. case req.UserType == consts.UserTypeTemp && (req.EffectiveStart < 1 || req.EffectiveEnd < 1 || req.EffectiveStart > req.EffectiveEnd):
  33. return status.Error(10003, "临时账号请输入有效时间")
  34. case req.SelfId == 0:
  35. return status.Error(10003, "未知操作者")
  36. }
  37. return nil
  38. }
  39. func checkAdmin(selfId int64) error {
  40. p := &dbmodel.TUser{}
  41. where := map[string]interface{}{
  42. "id": selfId,
  43. }
  44. err := p.Find(database.DB(), where)
  45. if err != nil && err != gorm.ErrRecordNotFound {
  46. return errors.DataBaseError
  47. }
  48. if p.ID == 0 {
  49. return errors.ErrRecordNotFound
  50. }
  51. if p.UserType != consts.UserTypeSuper {
  52. return errors.PermissionError
  53. }
  54. return nil
  55. }
  56. func getUserContent(p dbmodel.TUser) string {
  57. userType := "普通账号"
  58. effectiveStart := "-"
  59. effectiveEnd := "-"
  60. if p.UserType == consts.UserTypeTemp {
  61. userType = "临时账号"
  62. effectiveStart = fmt.Sprintf("%d", time.Unix(p.EffectiveStart, 0))
  63. effectiveEnd = fmt.Sprintf("%d", time.Unix(p.EffectiveEnd, 0))
  64. }
  65. m := map[string]interface{}{
  66. "账号名": p.User,
  67. "姓名": p.RealName,
  68. "账号类型": userType,
  69. "有效开始时间": effectiveStart,
  70. "有效结束时间": effectiveEnd,
  71. }
  72. bytes, _ := json.Marshal(m)
  73. return string(bytes)
  74. }
  75. //
  76. func UserAdd(ctx context.Context, req *apis.UserAddRequest) (reply *apis.UserAddReply, err error) {
  77. reply = &apis.UserAddReply{}
  78. // 捕获各个task中的异常并返回给调用者
  79. defer func() {
  80. if r := recover(); r != nil {
  81. err = fmt.Errorf("%+v", r)
  82. e := &status.Status{}
  83. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  84. logger.Error("err",
  85. zap.String("system_err", err.Error()),
  86. zap.Stack("stacktrace"))
  87. }
  88. }
  89. }()
  90. if err = checkUserAddParam(req); err != nil {
  91. return nil, err
  92. }
  93. if err = checkAdmin(req.SelfId); err != nil {
  94. return nil, err
  95. }
  96. now := time.Now()
  97. passwd, _ := util.GetMd5Pass(req.Password, consts.CRYPTO_KEY)
  98. p := &dbmodel.TUser{
  99. User: req.User,
  100. Password: passwd,
  101. EffectiveStart: req.EffectiveStart,
  102. EffectiveEnd: req.EffectiveEnd,
  103. UserType: req.UserType,
  104. CreatedAt: now,
  105. UpdatedAt: now,
  106. RealName: req.RealName,
  107. }
  108. db := database.DB().Begin()
  109. err = p.Insert(db)
  110. defer func() {
  111. if err != nil {
  112. db.Rollback()
  113. }
  114. }()
  115. if err != nil {
  116. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  117. return nil, status.Error(10003, "账号名已存在,请使用其他账号名 或 删除同名账号")
  118. }
  119. return nil, errors.DataBaseError
  120. }
  121. reply.Uid = p.ID
  122. // 操作日志
  123. content := getUserContent(*p)
  124. logReq := &apis.OperationLogAddRequest{
  125. OriginContent: "",
  126. TargetContent: content,
  127. Uid: req.SelfId,
  128. User: req.SelfName,
  129. Module: consts.OperationModuleAccount,
  130. OperationTime: now.Unix(),
  131. Operation: consts.OperationActionUserAdd,
  132. }
  133. err = operation_log.OperationLogAddHandle(logReq, db)
  134. if err != nil {
  135. return nil, err
  136. }
  137. db.Commit()
  138. return reply, nil
  139. }