search.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package search
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "go-common/app/interface/main/creative/model/search"
  7. "go-common/library/database/elastic"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. var (
  12. orderMap = map[string]string{
  13. "senddate": "pubdate", //发布时间
  14. "click": "click", //点击数
  15. "scores": "review", //评论
  16. "stow": "favorite", //收藏
  17. "dm_count": "dm_count", //弹幕
  18. }
  19. applyStateMap = map[string]string{
  20. "pending": "pending",
  21. "processed": "processed",
  22. "neglected": "neglected",
  23. }
  24. )
  25. // ArchivesES search archives by es.
  26. func (d *Dao) ArchivesES(c context.Context, mid int64, tid int16, keyword, order, class, ip string, pn, ps, coop int) (sres *search.Result, err error) {
  27. r := d.es.NewRequest("creative_archive_staff").Fields(
  28. "id",
  29. "pid",
  30. "typeid",
  31. "title",
  32. "state",
  33. "cover",
  34. "description",
  35. "duration",
  36. "pubdate",
  37. )
  38. r.Index("creative_archive").Pn(pn).Ps(ps).OrderScoreFirst(false)
  39. if mid > 0 && coop == 0 {
  40. cmbup := &elastic.Combo{}
  41. cmbup.ComboEQ([]map[string]interface{}{
  42. {"mid": mid},
  43. })
  44. r.WhereCombo(cmbup.MinEQ(1))
  45. } else if mid > 0 && coop == 1 {
  46. cmbup := &elastic.Combo{}
  47. cmbup.ComboEQ([]map[string]interface{}{
  48. {"mid": mid},
  49. {"staff_mid": mid},
  50. })
  51. r.WhereCombo(cmbup.MinEQ(1))
  52. }
  53. if keyword != "" { //筛选稿件标题或者描述
  54. r.WhereLike([]string{"title", "description"}, []string{keyword}, true, "low")
  55. }
  56. if tid > 0 {
  57. r.WhereEq("pid", tid)
  58. }
  59. if class != "" {
  60. if len(strings.Split(class, ",")) == 1 { //如果筛选全部则不传参数
  61. r.WhereEq("state", class) //state: is_pubing,pubed,not_pubed(全部) pubed (已通过) not_pubed(未通过) is_pubing(进行中)
  62. }
  63. }
  64. if order != "" {
  65. if o, ok := orderMap[order]; ok {
  66. r.Order(o, "desc")
  67. }
  68. } else {
  69. r.Order("pubdate", "desc") //默认按发布时间倒序排
  70. }
  71. log.Info("ArchivesES params(%s)", r.Params())
  72. var res = &search.ArcResult{}
  73. if err = r.Scan(c, res); err != nil {
  74. log.Error("ArchivesES r.Scan error(%v)", err)
  75. err = ecode.CreativeSearchErr
  76. return
  77. }
  78. sres = &search.Result{}
  79. sres.Page.Pn = res.Page.Num
  80. sres.Page.Ps = res.Page.Size
  81. sres.Page.Count = res.Page.Total
  82. if res.Result.PList != nil {
  83. sres.Class = &search.ClassCount{ //获取按稿件状态计数
  84. Pubed: res.Result.PList.Pubed.Count,
  85. NotPubed: res.Result.PList.NotPubed.Count,
  86. Pubing: res.Result.PList.IsPubing.Count,
  87. }
  88. }
  89. tcs := make(map[int16]*search.TypeCount)
  90. for _, v := range res.Result.TList { //获取按一级分区稿件计数
  91. if v != nil {
  92. key, err := strconv.ParseInt(v.Key, 10, 16)
  93. if err != nil {
  94. log.Error("strconv.ParseInt(%s)|error(%v)", v.Key, err)
  95. return nil, err
  96. }
  97. tid = int16(key)
  98. tc := &search.TypeCount{
  99. Tid: tid,
  100. Count: int64(v.Count),
  101. }
  102. tcs[tid] = tc
  103. }
  104. }
  105. sres.Type = tcs
  106. for _, v := range res.Result.Vlist {
  107. if v != nil {
  108. sres.Aids = append(sres.Aids, v.ID)
  109. }
  110. }
  111. return
  112. }
  113. // ArchivesStaffES search staff applies by es.
  114. func (d *Dao) ArchivesStaffES(c context.Context, mid int64, tid int16, keyword, state string, pn, ps int) (sres *search.StaffApplyResult, err error) {
  115. r := d.es.NewRequest("creative_archive_apply").Fields(
  116. "id",
  117. "pid",
  118. "typeid",
  119. "title",
  120. "state",
  121. "cover",
  122. "description",
  123. "duration",
  124. "pubdate",
  125. )
  126. r.Index("creative_archive").Pn(pn).Ps(ps).OrderScoreFirst(false)
  127. if mid > 0 {
  128. r.WhereEq("apply_staff.apply_staff_mid", mid)
  129. }
  130. if state != "" {
  131. if o, ok := applyStateMap[state]; ok {
  132. r.WhereEq("apply_staff.deal_state", o)
  133. }
  134. } else {
  135. r.WhereEq("apply_staff.deal_state", "pending")
  136. }
  137. if keyword != "" { //筛选稿件标题或者描述
  138. r.WhereLike([]string{"title", "description"}, []string{keyword}, true, "low")
  139. }
  140. if tid > 0 {
  141. r.WhereEq("pid", tid)
  142. }
  143. log.Info("ArchivesStaffES params(%s)", r.Params())
  144. var res = &search.ApplyResult{}
  145. if err = r.Scan(c, res); err != nil {
  146. log.Error("ArchivesStaffES r.Scan error(%v)", err)
  147. err = ecode.CreativeSearchErr
  148. return
  149. }
  150. sres = &search.StaffApplyResult{}
  151. sres.Page.Pn = res.Page.Num
  152. sres.Page.Ps = res.Page.Size
  153. sres.Page.Count = res.Page.Total
  154. //tlist
  155. if res.Result.ApplyPList != nil {
  156. sres.StateCount = &search.ApplyStateCount{ //获取按稿件状态计数
  157. Pending: res.Result.ApplyPList.Pending.Count,
  158. Processed: res.Result.ApplyPList.Processed.Count,
  159. Neglected: res.Result.ApplyPList.Neglected.Count,
  160. }
  161. }
  162. // vlist
  163. tcs := make(map[int16]*search.TypeCount)
  164. for _, v := range res.Result.TList { //获取按一级分区稿件计数
  165. if v != nil {
  166. key, err := strconv.ParseInt(v.Key, 10, 16)
  167. if err != nil {
  168. log.Error("strconv.ParseInt(%s)|error(%v)", v.Key, err)
  169. return nil, err
  170. }
  171. tid = int16(key)
  172. tc := &search.TypeCount{
  173. Tid: tid,
  174. Count: int64(v.Count),
  175. }
  176. tcs[tid] = tc
  177. }
  178. }
  179. sres.Type = tcs
  180. for _, v := range res.Result.Vlist {
  181. if v != nil {
  182. sres.Aids = append(sres.Aids, v.ID)
  183. }
  184. }
  185. return
  186. }