dao.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package search
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/credit/conf"
  6. "go-common/app/admin/main/credit/model"
  7. "go-common/app/admin/main/credit/model/blocked"
  8. "go-common/app/admin/main/credit/model/search"
  9. "go-common/library/database/elastic"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "github.com/pkg/errors"
  13. )
  14. // Dao .
  15. type Dao struct {
  16. elastic *elastic.Elastic
  17. }
  18. // New .
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{
  21. elastic: elastic.NewElastic(nil),
  22. }
  23. return
  24. }
  25. // Blocked get search blocked.
  26. func (d *Dao) Blocked(c context.Context, arg *blocked.ArgBlockedSearch) (ids []int64, pager *blocked.Pager, err error) {
  27. req := d.elastic.NewRequest(blocked.BusinessBlockedInfo).Index(blocked.TableBlockedInfo).Fields("id")
  28. if arg.Keyword != blocked.SearchDefaultString {
  29. req.WhereLike([]string{"origin_content"}, []string{arg.Keyword}, true, elastic.LikeLevelHigh)
  30. }
  31. if arg.UID != blocked.SearchDefaultNum {
  32. req.WhereEq("uid", arg.UID)
  33. }
  34. if arg.OPID != blocked.SearchDefaultNum {
  35. req.WhereEq("oper_id", arg.OPID)
  36. }
  37. if arg.OriginType != blocked.SearchDefaultNum {
  38. req.WhereEq("origin_type", arg.OriginType)
  39. }
  40. if arg.BlockedType != blocked.SearchDefaultNum {
  41. req.WhereEq("blocked_type", arg.BlockedType)
  42. }
  43. if arg.PublishStatus != blocked.SearchDefaultNum {
  44. req.WhereEq("publish_status", arg.PublishStatus)
  45. }
  46. req.WhereRange("punish_time", arg.Start, arg.End, elastic.RangeScopeLcRc)
  47. req.WhereEq("status", blocked.SearchDefaultStatus)
  48. req.Pn(arg.PN).Ps(arg.PS).Order(arg.Order, arg.Sort)
  49. var res *search.ReSearchData
  50. if err = req.Scan(c, &res); err != nil {
  51. err = errors.Errorf("elastic search(%s) error(%v)", req.Params(), err)
  52. return
  53. }
  54. ids, pager = pagerExtra(res)
  55. return
  56. }
  57. // Publish get search publish.
  58. func (d *Dao) Publish(c context.Context, arg *blocked.ArgPublishSearch) (ids []int64, pager *blocked.Pager, err error) {
  59. req := d.elastic.NewRequest(blocked.BusinessBlockedPublish).Index(blocked.TableBlockedPublish).Fields("id")
  60. if arg.Keyword != blocked.SearchDefaultString {
  61. req.WhereLike([]string{"title", "sub_title"}, []string{arg.Keyword}, true, elastic.LikeLevelHigh)
  62. }
  63. if arg.Type != blocked.SearchDefaultNum {
  64. req.WhereEq("ptype", arg.Type)
  65. }
  66. req.WhereRange("show_time", arg.ShowFrom, arg.ShowTo, elastic.RangeScopeLcRc)
  67. req.WhereEq("status", blocked.SearchDefaultStatus)
  68. req.Pn(arg.PN).Ps(arg.PS).Order(arg.Order, arg.Sort)
  69. var res *search.ReSearchData
  70. if err = req.Scan(c, &res); err != nil {
  71. err = errors.Errorf("elastic search(%s) error(%v)", req.Params(), err)
  72. return
  73. }
  74. ids, pager = pagerExtra(res)
  75. return
  76. }
  77. // Case get search case.
  78. func (d *Dao) Case(c context.Context, arg *blocked.ArgCaseSearch) (ids []int64, pager *blocked.Pager, err error) {
  79. req := d.elastic.NewRequest(blocked.BusinessBlockedCase).Index(blocked.TableBlockedCase).Fields("id")
  80. if arg.Keyword != blocked.SearchDefaultString {
  81. req.WhereLike([]string{"origin_content"}, []string{arg.Keyword}, true, elastic.LikeLevelHigh)
  82. }
  83. if arg.OriginType != blocked.SearchDefaultNum {
  84. req.WhereEq("origin_type", arg.OriginType)
  85. }
  86. if arg.Status != blocked.SearchDefaultNum {
  87. req.WhereEq("status", arg.Status)
  88. }
  89. if arg.CaseType != blocked.SearchDefaultNum {
  90. req.WhereEq("case_type", arg.CaseType)
  91. }
  92. if arg.UID != blocked.SearchDefaultNum {
  93. req.WhereEq("mid", arg.UID)
  94. }
  95. if arg.OPID != blocked.SearchDefaultNum {
  96. req.WhereEq("oper_id", arg.OPID)
  97. }
  98. req.WhereRange("start_time", arg.TimeFrom, arg.TimeTo, elastic.RangeScopeLcRc)
  99. req.Pn(arg.PN).Ps(arg.PS).Order(arg.Order, arg.Sort)
  100. var res *search.ReSearchData
  101. if err = req.Scan(c, &res); err != nil {
  102. err = errors.Errorf("elastic search(%s) error(%v)", req.Params(), err)
  103. return
  104. }
  105. ids, pager = pagerExtra(res)
  106. return
  107. }
  108. // Jury get search jury.
  109. func (d *Dao) Jury(c context.Context, arg *blocked.ArgJurySearch) (ids []int64, pager *blocked.Pager, err error) {
  110. req := d.elastic.NewRequest(blocked.BusinessBlockedJury).Index(blocked.TableBlockedJury).Fields("id")
  111. if arg.UID != blocked.SearchDefaultNum {
  112. req.WhereEq("mid", arg.UID)
  113. }
  114. if arg.Status != blocked.SearchDefaultNum {
  115. req.WhereEq("status", arg.Status)
  116. }
  117. if arg.Black != blocked.SearchDefaultNum {
  118. req.WhereEq("black", arg.Black)
  119. }
  120. req.WhereRange("expired", arg.ExpiredFrom, arg.ExpiredTo, elastic.RangeScopeLcRc)
  121. req.Pn(arg.PN).Ps(arg.PS).Order(arg.Order, arg.Sort)
  122. var res *search.ReSearchData
  123. if err = req.Scan(c, &res); err != nil {
  124. err = errors.Errorf("elastic search(%s) error(%v)", req.Params(), err)
  125. return
  126. }
  127. ids, pager = pagerExtra(res)
  128. return
  129. }
  130. // Opinion get search opinion.
  131. func (d *Dao) Opinion(c context.Context, arg *blocked.ArgOpinionSearch) (ids []int64, pager *blocked.Pager, err error) {
  132. req := d.elastic.NewRequest(blocked.BusinessBlockedOpinion).Index(blocked.TableBlockedOpinion).Fields("id")
  133. if arg.UID != blocked.SearchDefaultNum {
  134. req.WhereEq("mid", arg.UID)
  135. }
  136. if arg.CID != blocked.SearchDefaultNum {
  137. req.WhereEq("cid", arg.CID)
  138. }
  139. if arg.Vote != blocked.SearchDefaultNum {
  140. req.WhereEq("vote", arg.Vote)
  141. }
  142. if arg.State != blocked.SearchDefaultNum {
  143. req.WhereEq("state", arg.State)
  144. }
  145. req.Pn(arg.PN).Ps(arg.PS).Order(arg.Order, arg.Sort)
  146. var res *search.ReSearchData
  147. if err = req.Scan(c, &res); err != nil {
  148. err = errors.Errorf("elastic search(%s) error(%v)", req.Params(), err)
  149. return
  150. }
  151. ids, pager = pagerExtra(res)
  152. return
  153. }
  154. // KPIPoint get search kpi point.
  155. func (d *Dao) KPIPoint(c context.Context, arg *blocked.ArgKpiPointSearch) (ids []int64, pager *blocked.Pager, err error) {
  156. var (
  157. dayFromT, dayToT time.Time
  158. dayFrom, dayTo string
  159. )
  160. req := d.elastic.NewRequest(blocked.BusinessBlockedKpiPoint).Index(blocked.TableBlockedKpiPoint).Fields("id")
  161. if arg.UID != blocked.SearchDefaultNum {
  162. req.WhereEq("mid", arg.UID)
  163. }
  164. if arg.Start != blocked.SearchDefaultString {
  165. if dayFromT, err = time.ParseInLocation(model.TimeFormatSec, arg.Start, time.Local); err != nil {
  166. err = errors.Errorf("time.ParseInLocation(%s) error(%v)", arg.Start, err)
  167. return
  168. }
  169. dayFrom = dayFromT.Format(model.TimeFormatDay)
  170. }
  171. if arg.End != blocked.SearchDefaultString {
  172. if dayToT, err = time.ParseInLocation(model.TimeFormatSec, arg.End, time.Local); err != nil {
  173. err = errors.Errorf("time.ParseInLocation(%s) error(%v)", arg.End, err)
  174. return
  175. }
  176. dayTo = dayToT.Format(model.TimeFormatDay)
  177. }
  178. req.WhereRange("day", dayFrom, dayTo, elastic.RangeScopeLcRc)
  179. req.Pn(arg.PN).Ps(arg.PS).Order(arg.Order, arg.Sort)
  180. var res *search.ReSearchData
  181. if err = req.Scan(c, &res); err != nil {
  182. err = errors.Errorf("elastic search(%s) error(%v)", req.Params(), err)
  183. return
  184. }
  185. ids, pager = pagerExtra(res)
  186. return
  187. }
  188. // SearchUpdate update about seach data.
  189. func (d *Dao) SearchUpdate(c context.Context, appid string, table string, data []interface{}) (err error) {
  190. us := d.elastic.NewUpdate(appid)
  191. for _, v := range data {
  192. us.AddData(table, v)
  193. }
  194. if err = us.Do(c); err != nil {
  195. log.Info("appid(%s) table(%s) params(%s) ip(%s) search blocked update error(%v)", appid, table, us.Params(), metadata.String(c, metadata.RemoteIP), err)
  196. }
  197. return
  198. }
  199. func pagerExtra(res *search.ReSearchData) (ids []int64, pager *blocked.Pager) {
  200. for _, v := range res.Result {
  201. ids = append(ids, v.ID)
  202. }
  203. if res.Page != nil {
  204. pager = &blocked.Pager{
  205. Total: res.Page.Total,
  206. PN: res.Page.PN,
  207. PS: res.Page.PS,
  208. Sort: res.Sort,
  209. Order: res.Order,
  210. }
  211. }
  212. return
  213. }