list.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. "xingjia-management-gateway/apis"
  13. "xingjia-management-gateway/consts"
  14. "xingjia-management-gateway/errors"
  15. dbmodel "xingjia-management-gateway/model"
  16. )
  17. func checkOperationLogListParam(req *apis.OperationLogListRequest) error {
  18. if req.Page == 0 {
  19. req.Page = 1
  20. }
  21. if req.PageSize == 0 {
  22. req.PageSize = 10
  23. }
  24. return nil
  25. }
  26. //
  27. func OperationLogList(ctx context.Context, req *apis.OperationLogListRequest) (reply *apis.OperationLogListReply, err error) {
  28. reply = &apis.OperationLogListReply{}
  29. // 捕获各个task中的异常并返回给调用者
  30. defer func() {
  31. if r := recover(); r != nil {
  32. err = fmt.Errorf("%+v", r)
  33. e := &status.Status{}
  34. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  35. logger.Error("err",
  36. zap.String("system_err", err.Error()),
  37. zap.Stack("stacktrace"))
  38. }
  39. }
  40. }()
  41. if err = checkOperationLogListParam(req); err != nil {
  42. return nil, err
  43. }
  44. where := map[string]interface{}{}
  45. if req.Start > 0 {
  46. where["operation_time >="] = req.Start
  47. }
  48. if req.End > 0 {
  49. where["operation_time <="] = req.End
  50. }
  51. p := &dbmodel.TOperationLog{}
  52. reply.Page = req.Page
  53. reply.Total, err = p.Count(database.DB(), where, nil)
  54. if err != nil {
  55. return nil, errors.DataBaseError
  56. }
  57. if reply.Total == 0 {
  58. return reply, nil
  59. }
  60. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. reply.List = make([]*apis.OperationLogItem, len(list))
  65. for i, v := range list {
  66. item := &apis.OperationLogItem{
  67. Id: v.ID,
  68. User: v.User,
  69. Uid: v.Uid,
  70. OriginContent: v.OriginContent,
  71. TargetContent: v.TargetContent,
  72. OperationTime: v.OperationTime,
  73. Operation: consts.OperationActionName[int(v.Operation)],
  74. Module: consts.OperationModuleName[int(v.Module)],
  75. }
  76. reply.List[i] = item
  77. }
  78. return reply, err
  79. }