add.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package operation_log
  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. dbmodel "xingjia-management-gateway/model"
  18. )
  19. func checkOperationLogAddParam(req *apis.OperationLogAddRequest) error {
  20. switch {
  21. case req.Uid == 0:
  22. return status.Error(10003, "操作日志用户名不能为空")
  23. }
  24. return nil
  25. }
  26. func checkAdmin(selfId int64) error {
  27. p := &dbmodel.TUser{}
  28. where := map[string]interface{}{
  29. "id": selfId,
  30. }
  31. err := p.Find(database.DB(), where)
  32. if err != nil && err != gorm.ErrRecordNotFound {
  33. return errors.DataBaseError
  34. }
  35. if p.ID == 0 {
  36. return errors.ErrRecordNotFound
  37. }
  38. if p.UserType != consts.UserTypeSuper {
  39. return errors.PermissionError
  40. }
  41. return nil
  42. }
  43. func OperationLogAddHandle(req *apis.OperationLogAddRequest, db *gorm.DB) error {
  44. now := time.Now()
  45. p := &dbmodel.TOperationLog{
  46. Uid: req.Uid,
  47. User: req.User,
  48. OriginContent: req.OriginContent,
  49. TargetContent: req.TargetContent,
  50. OperationTime: req.OperationTime,
  51. Operation: req.Operation,
  52. CreatedAt: now,
  53. UpdatedAt: now,
  54. Module: req.Module,
  55. }
  56. err := p.Insert(db)
  57. if err != nil {
  58. return errors.DataBaseError
  59. }
  60. return nil
  61. }
  62. //
  63. func OperationLogAdd(ctx context.Context, req *apis.OperationLogAddRequest) (reply *apis.OperationLogAddReply, err error) {
  64. reply = &apis.OperationLogAddReply{}
  65. // 捕获各个task中的异常并返回给调用者
  66. defer func() {
  67. if r := recover(); r != nil {
  68. err = fmt.Errorf("%+v", r)
  69. e := &status.Status{}
  70. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  71. logger.Error("err",
  72. zap.String("system_err", err.Error()),
  73. zap.Stack("stacktrace"))
  74. }
  75. }
  76. }()
  77. if err = checkOperationLogAddParam(req); err != nil {
  78. return nil, err
  79. }
  80. err = OperationLogAddHandle(req, database.DB())
  81. return reply, err
  82. }