list.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "github.com/jaryhe/gopkgs/database"
  13. "github.com/jaryhe/gopkgs/logger"
  14. "go.uber.org/zap"
  15. "google.golang.org/grpc/status"
  16. "time"
  17. )
  18. //
  19. func ManagementLogList(ctx context.Context, req *pb_v1.ManagementLogListRequest) (reply *pb_v1.ManagementLogListReply, err error) {
  20. reply = &pb_v1.ManagementLogListReply{}
  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. if req.PageSize == 0 {
  38. req.PageSize = 10
  39. }
  40. if req.Page == 0 {
  41. req.Page = 1
  42. }
  43. p := dbmodel.NewTLog(dbname)
  44. where := map[string]interface{}{}
  45. or := map[string]interface{}{}
  46. if req.Action != "" {
  47. where["action"] = req.Action
  48. }
  49. if req.Module != "" {
  50. where["module"] = req.Module
  51. }
  52. if req.Username != "" {
  53. where["username like"] = "%"+req.Username+"%"
  54. }
  55. if req.Start > 0 {
  56. where["created_at >="] = time.Unix(req.Start, 0).Format("2006-01-02 15:04:05")
  57. }
  58. if req.End > 0 {
  59. where["created_at <"] = time.Unix(req.End, 0).Format("2006-01-02 15:04:05")
  60. }
  61. if req.Filter != "" {
  62. or["target like"] = "%"+req.Filter+"%"
  63. or["origin like"] = "%"+req.Filter+"%"
  64. }
  65. total, err := p.Count(database.DB(), where, or)
  66. if err != nil {
  67. return nil, errors.DataBaseError
  68. }
  69. reply.Total = total
  70. reply.Page = req.Page
  71. if total == 0 {
  72. return reply, nil
  73. }
  74. list, err := p.List(database.DB(), where, or, req.PageSize, (req.Page-1)*req.PageSize)
  75. if err != nil {
  76. return nil, errors.DataBaseError
  77. }
  78. reply.List = make([]*pb_v1.ManagementLogItem, len(list))
  79. for i, v := range list {
  80. reply.List[i] = &pb_v1.ManagementLogItem{
  81. Id:v.ID,
  82. Action:v.Action,
  83. Module:v.Module,
  84. Username:v.Username,
  85. Uid:v.Uid,
  86. Origin:v.Origin,
  87. Target:v.Target,
  88. CreatedAt:v.CreatedAt.Format("2006-01-02 15:04:05"),
  89. }
  90. }
  91. return reply, nil
  92. }