search.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/dm/model"
  8. "go-common/library/database/elastic"
  9. "go-common/library/log"
  10. )
  11. // return recent two years report search index.
  12. func (d *Dao) rptSearchIndex() string {
  13. year := time.Now().Year()
  14. return fmt.Sprintf("dmreport_%d,dmreport_%d", year-1, year)
  15. }
  16. // SearchReportAid 根据mid获取用户的所有有举报弹幕的稿件
  17. func (d *Dao) SearchReportAid(c context.Context, mid int64, upOp int8, states []int8, pn, ps int64) (aids []int64, err error) {
  18. res := new(model.SearchReportAidResult)
  19. order := []map[string]string{{"arc_aid": "desc"}}
  20. req := d.es.NewRequest("dmreport").Fields("arc_aid").Index(d.rptSearchIndex())
  21. req.WhereEq("arc_mid", mid).WhereEq("up_op", upOp).WhereIn("state", states).GroupBy(elastic.EnhancedModeGroupBy, "arc_aid", order)
  22. req.Pn(int(pn)).Ps(int(ps))
  23. if err = req.Scan(c, res); err != nil {
  24. log.Error("search params(%s), err(%v)", req.Params(), err)
  25. return
  26. }
  27. if values, ok := res.Result["group_by_arc_aid"]; ok {
  28. for _, v := range values {
  29. var aid int64
  30. if aid, err = strconv.ParseInt(v.Key, 10, 64); err != nil {
  31. log.Error("strconv.ParseInt(%s) error(%v)", v.Key, err)
  32. return
  33. }
  34. aids = append(aids, aid)
  35. }
  36. }
  37. return
  38. }
  39. // SearchReport 根据up主id,稿件id获取举报弹幕列表
  40. func (d *Dao) SearchReport(c context.Context, mid, aid, pn, ps int64, upOp int8, states []int64) (res *model.SearchReportResult, err error) {
  41. req := d.es.NewRequest("dmreport")
  42. req.Fields("id", "dmid", "cid", "arc_aid", "arc_typeid", "dm_owner_uid", "dm_msg", "count", "content", "up_op", "state",
  43. "uid", "rp_time", "reason", "dm_deleted", "arc_mid", "pool_id", "model", "score", "dm_ctime", "ctime", "mtime")
  44. req.Index(d.rptSearchIndex())
  45. if aid > 0 {
  46. req.WhereEq("arc_aid", aid)
  47. }
  48. if mid > 0 {
  49. req.WhereEq("arc_mid", mid)
  50. }
  51. if len(states) > 0 {
  52. req.WhereIn("state", states)
  53. }
  54. req.WhereNot(elastic.NotTypeEq, "dm_owner_uid").WhereEq("dm_owner_uid", 0)
  55. req.WhereEq("up_op", upOp)
  56. req.Order("rp_time", "desc")
  57. req.Pn(int(pn)).Ps(int(ps))
  58. res = &model.SearchReportResult{}
  59. if err = req.Scan(c, res); err != nil {
  60. log.Error("req.Scan() search params(%s), err(%v)", req.Params(), err)
  61. }
  62. return
  63. }
  64. // UpdateSearchReport update report search index.
  65. func (d *Dao) UpdateSearchReport(c context.Context, rpts []*model.UptSearchReport) (err error) {
  66. up := d.es.NewUpdate("dmreport").Insert() // if data not exist insert,else update
  67. for _, rpt := range rpts {
  68. t, err1 := time.ParseInLocation("2006-01-02 15:04:05", rpt.Ctime, time.Local)
  69. if err1 != nil {
  70. log.Error("time.ParseInLocation(%s) error(%v)", rpt.Ctime, err1)
  71. return err1
  72. }
  73. up.AddData(fmt.Sprintf("dmreport_%d", t.Year()), rpt)
  74. }
  75. if err = up.Do(c); err != nil {
  76. log.Error("update.Do() error(%v)", err)
  77. }
  78. return
  79. }