logReport.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package dao
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/admin/main/reply/model"
  6. "go-common/library/database/elastic"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "strings"
  10. "time"
  11. )
  12. const (
  13. // log api
  14. _logURL = "/x/admin/search/log"
  15. )
  16. // ReportLogData report log data
  17. type ReportLogData struct {
  18. Sort string `json:"sort"`
  19. Order string `json:"order"`
  20. Page model.Page `json:"page"`
  21. Result []*ReportLogResult `json:"result"`
  22. }
  23. // ReportLogResult ReportLogResult
  24. type ReportLogResult struct {
  25. AdminName string `json:"uname"`
  26. AdminID int64 `json:"uid"`
  27. Business int64 `json:"business"`
  28. Type int32 `json:"type"`
  29. Oid int64 `json:"oid"`
  30. OidStr string `json:"oid_str"`
  31. Action string `json:"action"`
  32. Ctime string `json:"ctime"`
  33. Index0 int64 `json:"int_0"`
  34. Index1 int64 `json:"int_1"`
  35. Index2 int64 `json:"int_2"`
  36. Content string `json:"extra_data"`
  37. }
  38. // ReportLog get notice info.
  39. func (d *Dao) ReportLog(c context.Context, sp model.LogSearchParam) (data *ReportLogData, err error) {
  40. var (
  41. // log_audit评论的索引是按时间分区的,最早的数据是2018年的,所以这里最早时间是写死的2018年
  42. stime = time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
  43. etime = time.Now()
  44. )
  45. if sp.CtimeFrom != "" {
  46. if stime, err = time.Parse(model.DateFormat, sp.CtimeFrom); err != nil {
  47. log.Error("time.Parse(%v) error", sp.CtimeFrom)
  48. stime = time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
  49. }
  50. }
  51. if sp.CtimeTo != "" {
  52. if etime, err = time.Parse(model.DateFormat, sp.CtimeTo); err != nil {
  53. log.Error("time.Parse(%v) error", sp.CtimeTo)
  54. etime = time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
  55. }
  56. }
  57. action := strings.Split(sp.Action, ",")
  58. r := d.es.NewRequest("log_audit").IndexByTime("log_audit_41", elastic.IndexTypeYear, stime, etime).WhereIn("action", action).
  59. WhereRange("ctime", stime.Format(model.DateFormat), etime.Format(model.DateFormat), elastic.RangeScopeLcRc)
  60. if sp.Pn <= 0 {
  61. sp.Pn = 1
  62. }
  63. if sp.Ps <= 0 {
  64. sp.Ps = 20
  65. }
  66. if sp.Order == "" {
  67. sp.Order = "ctime"
  68. }
  69. if sp.Sort == "" {
  70. sp.Sort = "desc"
  71. }
  72. r = r.Order(sp.Order, sp.Sort).Pn(int(sp.Pn)).Ps(int(sp.Ps))
  73. if sp.Oid > 0 {
  74. r = r.WhereEq("oid", strconv.FormatInt(sp.Oid, 10))
  75. }
  76. if sp.Mid > 0 {
  77. r = r.WhereEq("int_0", sp.Mid)
  78. }
  79. if sp.Type > 0 {
  80. r = r.WhereEq("type", sp.Type)
  81. }
  82. if sp.Other > 0 {
  83. r = r.WhereEq("int_1", sp.Other)
  84. }
  85. log.Warn(r.Params())
  86. err = r.Scan(c, &data)
  87. if err != nil {
  88. log.Error("r.Scan(%v) error(%v)", c, err)
  89. return
  90. }
  91. if data == nil {
  92. err = ecode.ServerErr
  93. log.Error("log_audit error")
  94. return
  95. }
  96. return
  97. }