system_log_list.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 SystemLog(ctx context.Context, req *v1.SystemLogRequest) (reply *v1.SystemLogReply, err error) {
  13. p := &model.TCompanyLog{}
  14. if req.Page == 0 {
  15. req.Page = 1
  16. }
  17. if req.PageSize == 0 {
  18. req.PageSize = 10
  19. }
  20. reply = &v1.SystemLogReply{}
  21. reply.Page = req.PageSize
  22. where := map[string]interface{}{}
  23. if req.Cid > 0 {
  24. where["cid"] = req.Cid
  25. }
  26. if req.GardenId > 0 {
  27. where["garden_id"] = req.GardenId
  28. }
  29. if req.Start > 0 {
  30. where["created_at >="] = time.Unix(req.Start, 0)
  31. }
  32. if req.End > 0 {
  33. where["created_at <"] = time.Unix(req.End, 0)
  34. }
  35. reply.Total, err = p.Count(database.DB(), where, nil)
  36. if err != nil {
  37. return nil, errors.DataBaseError
  38. }
  39. if reply.Total == 0 {
  40. return reply, nil
  41. }
  42. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  43. if err != nil {
  44. return nil, errors.DataBaseError
  45. }
  46. reply.List = make([]*v1.SystemLogItem, len(list))
  47. for i, v := range list {
  48. reply.List[i] = &v1.SystemLogItem{
  49. Uid:v.Uid,
  50. Username:v.Username,
  51. Cid:v.Cid,
  52. Origin:v.Origin,
  53. Target:v.Target,
  54. Action:v.Action,
  55. Module:v.Module,
  56. CreatedAt:v.CreatedAt.Format("2006-01-02 15:04:05"),
  57. }
  58. }
  59. return reply, nil
  60. }