del.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "gorm.io/gorm"
  13. "time"
  14. "xingjia-management-gateway/apis"
  15. "xingjia-management-gateway/consts"
  16. "xingjia-management-gateway/errors"
  17. "xingjia-management-gateway/impl/v1/operation_log"
  18. dbmodel "xingjia-management-gateway/model"
  19. )
  20. func checkUserDelParam(req *apis.UserDelRequest) error {
  21. switch {
  22. case req.DelId == 0:
  23. return status.Error(10003, "删除账号不能为空")
  24. case req.SelfId == 0:
  25. return status.Error(10003, "未知操作者")
  26. }
  27. return nil
  28. }
  29. //
  30. func UserDel(ctx context.Context, req *apis.UserDelRequest) (reply *apis.UserDelReply, err error) {
  31. reply = &apis.UserDelReply{}
  32. // 捕获各个task中的异常并返回给调用者
  33. defer func() {
  34. if r := recover(); r != nil {
  35. err = fmt.Errorf("%+v", r)
  36. e := &status.Status{}
  37. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  38. logger.Error("err",
  39. zap.String("system_err", err.Error()),
  40. zap.Stack("stacktrace"))
  41. }
  42. }
  43. }()
  44. if err = checkUserDelParam(req); err != nil {
  45. return nil, err
  46. }
  47. if err = checkAdmin(req.SelfId); err != nil {
  48. return nil, err
  49. }
  50. if req.DelId == req.SelfId {
  51. return nil, status.Error(10003, "超级管理员不能删除")
  52. }
  53. old := &dbmodel.TUser{}
  54. where := map[string]interface{}{
  55. "id": req.DelId,
  56. }
  57. err = old.Find(database.DB(), where)
  58. if err != nil && err != gorm.ErrRecordNotFound {
  59. return nil, errors.DataBaseError
  60. }
  61. if old.ID == 0 {
  62. return nil, errors.ErrRecordNotFound
  63. }
  64. reply.RealName = old.RealName
  65. reply.User = old.User
  66. reply.UserType = old.UserType
  67. p := &dbmodel.TUser{}
  68. where = map[string]interface{}{
  69. "id": req.DelId,
  70. }
  71. db := database.DB().Begin()
  72. defer func() {
  73. if err != nil {
  74. db.Rollback()
  75. }
  76. }()
  77. err = p.Delete(db, where)
  78. if err != nil {
  79. return nil, errors.DataBaseError
  80. }
  81. // 操作日志
  82. content := getUserContent(*old)
  83. logReq := &apis.OperationLogAddRequest{
  84. OriginContent: content,
  85. TargetContent: "",
  86. Uid: req.SelfId,
  87. User: req.SelfName,
  88. Module: consts.OperationModuleAccount,
  89. OperationTime: time.Now().Unix(),
  90. Operation: consts.OperationActionUserDel,
  91. }
  92. err = operation_log.OperationLogAddHandle(logReq, db)
  93. if err != nil {
  94. return nil, err
  95. }
  96. db.Commit()
  97. return reply, nil
  98. }