company_log_list.go 1.4 KB

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