add.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package log
  4. import (
  5. "context"
  6. "cp-organization-management/errors"
  7. dbmodel "cp-organization-management/model"
  8. pb_v1 "cp-organization-management/pb/v1"
  9. "cp-organization-management/utils"
  10. "encoding/json"
  11. "fmt"
  12. "time"
  13. "github.com/jaryhe/gopkgs/database"
  14. "github.com/jaryhe/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. //
  19. func ManagementLogAdd(ctx context.Context, req *pb_v1.ManagementLogAddRequest) (reply *pb_v1.ManagementLogAddReply, err error) {
  20. reply = &pb_v1.ManagementLogAddReply{}
  21. // 捕获各个task中的异常并返回给调用者
  22. defer func() {
  23. if r := recover(); r != nil {
  24. err = fmt.Errorf("%+v", r)
  25. e := &status.Status{}
  26. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  27. logger.Error("err",
  28. zap.String("system_err", err.Error()),
  29. zap.Stack("stacktrace"))
  30. }
  31. }
  32. }()
  33. if req.OrganizationCode == "" {
  34. return nil, errors.ParamsError
  35. }
  36. dbname := utils.GetDbName(req.OrganizationCode)
  37. p := dbmodel.NewTLog(dbname)
  38. p.Uid = req.Uid
  39. p.Username = req.Username
  40. p.Action = req.Action
  41. p.Module = req.Module
  42. p.Origin = req.Origin
  43. p.Target = req.Target
  44. p.CreatedAt = time.Unix(req.CreatedAt, 0)
  45. err = p.Insert(database.DB())
  46. if err != nil {
  47. return nil, errors.DataBaseError
  48. }
  49. return &pb_v1.ManagementLogAddReply{}, nil
  50. }