dao.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package search
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/main/videoup/conf"
  8. "go-common/app/admin/main/videoup/model/archive"
  9. "go-common/app/admin/main/videoup/model/search"
  10. "go-common/library/database/elastic"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. bm "go-common/library/net/http/blademaster"
  14. "go-common/library/xstr"
  15. )
  16. const (
  17. _searchURL = "/x/admin/search/log"
  18. )
  19. // Dao is search dao
  20. type Dao struct {
  21. c *bm.ClientConfig
  22. httpClient *bm.Client
  23. URI string
  24. es *elastic.Config
  25. }
  26. var (
  27. d *Dao
  28. )
  29. // New new search dao
  30. func New(c *conf.Config) *Dao {
  31. return &Dao{
  32. c: c.HTTPClient.Read,
  33. httpClient: bm.NewClient(c.HTTPClient.Read),
  34. URI: c.Host.MngSearch + _searchURL,
  35. es: &elastic.Config{
  36. Host: c.Host.Manager,
  37. HTTPClient: c.HTTPClient.Search,
  38. },
  39. }
  40. }
  41. // OutTime 退出时间,es的group by查询,最大1000条
  42. func (d *Dao) OutTime(c context.Context, ids []int64) (mcases map[int64][]interface{}, err error) {
  43. mcases = make(map[int64][]interface{})
  44. params := url.Values{}
  45. params.Set("appid", "log_audit_group")
  46. params.Set("group", "uid")
  47. params.Set("uid", xstr.JoinInts(ids))
  48. params.Set("business", strconv.Itoa(archive.LogClientConsumer))
  49. params.Set("action", strconv.Itoa(int(archive.ActionHandsOFF)))
  50. params.Set("ps", strconv.Itoa(len(ids)))
  51. res := &archive.SearchLogResult{}
  52. if err = d.httpClient.Get(c, d.URI, "", params, &res); err != nil {
  53. log.Error("log_audit_group d.httpClient.Get error(%v)", err)
  54. return
  55. }
  56. if res.Code != ecode.OK.Code() {
  57. log.Error("log_audit_group ecode:%v", res.Code)
  58. return
  59. }
  60. for _, item := range res.Data.Result {
  61. mcases[item.UID] = []interface{}{item.Ctime}
  62. }
  63. log.Info("log_audit_group get: %s params:%s ret:%v", d.URI, params.Encode(), res)
  64. return
  65. }
  66. // InQuitList 登入登出日志
  67. func (d *Dao) InQuitList(c context.Context, uids []int64, bt, et string) (l []*archive.InQuit, err error) {
  68. params := url.Values{}
  69. params.Set("appid", "log_audit")
  70. params.Set("business", strconv.Itoa(archive.LogClientConsumer))
  71. if len(uids) > 0 {
  72. params.Set("uid", xstr.JoinInts(uids))
  73. }
  74. if len(bt) > 0 && len(et) > 0 {
  75. params.Set("ctime_from", bt)
  76. params.Set("ctime_to", et)
  77. }
  78. params.Set("order", "ctime")
  79. params.Set("sort", "desc")
  80. params.Set("ps", "10000")
  81. res := &archive.SearchLogResult{}
  82. if err = d.httpClient.Get(c, d.URI, "", params, res); err != nil {
  83. log.Error("InQuitList d.httpClient.Get error(%v)", err)
  84. return
  85. }
  86. if res.Code != ecode.OK.Code() {
  87. log.Error("InQuitList ecode:%v", res.Code)
  88. return
  89. }
  90. mapHelp := make(map[int64]*archive.InQuit)
  91. for i := len(res.Data.Result) - 1; i >= 0; i-- {
  92. item := res.Data.Result[i]
  93. if item.Action == "0" {
  94. ctime, _ := time.Parse(archive.TimeFormatSec, item.Ctime)
  95. iqlog := &archive.InQuit{
  96. Date: ctime.Format("2006-01-02"),
  97. UID: item.UID,
  98. Uname: item.Uname,
  99. InTime: ctime.Format("15:04:05"),
  100. }
  101. mapHelp[item.UID] = iqlog
  102. l = append([]*archive.InQuit{iqlog}, l[:]...)
  103. }
  104. if item.Action == "1" {
  105. if iqlog, ok := mapHelp[item.UID]; ok {
  106. ctime, _ := time.Parse(archive.TimeFormatSec, item.Ctime)
  107. if date := ctime.Format("2006-01-02"); date == iqlog.Date {
  108. iqlog.OutTime = ctime.Format("15:04:05")
  109. } else {
  110. iqlog.OutTime = ctime.Format(archive.TimeFormatSec)
  111. }
  112. }
  113. }
  114. }
  115. return
  116. }
  117. // SearchCopyright search video copyright
  118. func (d *Dao) SearchCopyright(c context.Context, kw string) (result *search.CopyrightResultData, err error) {
  119. var (
  120. ps = 30 //copyright不需要翻页,产品(计晓峰)说返回30条数据就可以
  121. )
  122. if kw == "" {
  123. return
  124. }
  125. es := elastic.NewElastic(d.es)
  126. eReq := es.NewRequest("copyright")
  127. eReq.Ps(ps)
  128. eReq.Index("copyright")
  129. eReq.WhereLike([]string{"name", "oname", "aka_names"}, []string{kw}, true, elastic.LikeLevelLow)
  130. log.Info("SearchCopyright(%s)", eReq.Params())
  131. if err = eReq.Scan(c, &result); err != nil {
  132. log.Error("s.SearchCopyright(%s) error(%v)", kw, err)
  133. return
  134. }
  135. if result == nil {
  136. result = &search.CopyrightResultData{}
  137. }
  138. if result.Result == nil {
  139. result.Result = []*search.Copyright{}
  140. }
  141. return
  142. }