management_log_list.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package log
  4. import (
  5. "context"
  6. "git.getensh.com/common/gopkgs/database"
  7. "property-log/model"
  8. "property-log/pb/v1"
  9. "time"
  10. "property-log/errors"
  11. )
  12. func ManagementLog(ctx context.Context, req *v1.ManagementLogRequest) (reply *v1.ManagementLogReply, err error) {
  13. p := &model.TManagementLog{}
  14. if req.Page == 0 {
  15. req.Page = 1
  16. }
  17. if req.PageSize == 0 {
  18. req.PageSize = 10
  19. }
  20. reply = &v1.ManagementLogReply{}
  21. reply.Page = req.PageSize
  22. where := map[string]interface{}{}
  23. if req.Start > 0 {
  24. where["created_at >="] = time.Unix(req.Start, 0)
  25. }
  26. if req.End > 0 {
  27. where["created_at <"] = time.Unix(req.End, 0)
  28. }
  29. reply.Total, err = p.Count(database.DB(), where, nil)
  30. if err != nil {
  31. return nil, errors.DataBaseError
  32. }
  33. if reply.Total == 0 {
  34. return reply, nil
  35. }
  36. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  37. if err != nil {
  38. return nil, errors.DataBaseError
  39. }
  40. reply.List = make([]*v1.ManagementLogItem, len(list))
  41. for i, v := range list {
  42. reply.List[i] = &v1.ManagementLogItem{
  43. Uid:v.Uid,
  44. Username:v.Username,
  45. Origin:v.Origin,
  46. Target:v.Target,
  47. Action:v.Action,
  48. Module:v.Module,
  49. CreatedAt:v.CreatedAt.Format("2006-01-02 15:04:05"),
  50. }
  51. }
  52. return reply, nil
  53. }