search.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/member/model"
  10. "go-common/library/database/elastic"
  11. "go-common/library/log"
  12. "github.com/pkg/errors"
  13. )
  14. var (
  15. _numPattern = regexp.MustCompile(`^\d+$`)
  16. )
  17. func allnum(s string) bool {
  18. return _numPattern.MatchString(s)
  19. }
  20. // SearchMember is.
  21. func (d *Dao) SearchMember(ctx context.Context, arg *model.ArgList) (*model.SearchMemberResult, error) {
  22. r := d.es.NewRequest("member_user").
  23. Fields("mid", "name").
  24. Index("user_base").
  25. Order("mid", elastic.OrderAsc).
  26. Ps(int(arg.PS)).
  27. Pn(int(arg.PN))
  28. if arg.Mid != 0 {
  29. r.WhereEq("mid", arg.Mid)
  30. }
  31. if arg.Keyword != "" {
  32. fields := []string{"name"}
  33. if allnum(arg.Keyword) {
  34. fields = append(fields, "mid")
  35. }
  36. r.WhereLike(fields, []string{arg.Keyword}, true, elastic.LikeLevelLow)
  37. }
  38. result := &model.SearchMemberResult{}
  39. if err := r.Scan(ctx, &result); err != nil {
  40. return nil, errors.WithStack(err)
  41. }
  42. return result, nil
  43. }
  44. // SearchLog is.
  45. func (d *Dao) SearchLog(ctx context.Context, uid, oid int64, uname, action string) (*model.SearchLogResult, error) {
  46. nowYear := time.Now().Year()
  47. index := []string{
  48. fmt.Sprintf("log_audit_121_%d", nowYear),
  49. fmt.Sprintf("log_audit_121_%d", nowYear-1),
  50. }
  51. r := d.es.NewRequest("log_audit").
  52. Index(index...).
  53. Fields("uid", "uname", "oid", "type", "action", "str_0", "str_1", "str_2", "int_0", "int_1", "int_2", "ctime", "extra_data").
  54. Order("ctime", elastic.OrderDesc)
  55. // 默认查询的是第一页,每夜10条记录
  56. if uid > 0 {
  57. r.WhereEq("uid", strconv.FormatInt(uid, 10))
  58. }
  59. if oid > 0 {
  60. r.WhereEq("oid", strconv.FormatInt(oid, 10))
  61. }
  62. if uname != "" {
  63. r.WhereEq("uname", uname)
  64. }
  65. if action != "" {
  66. r.WhereIn("action", strings.Split(action, ","))
  67. }
  68. result := &model.SearchLogResult{}
  69. if err := r.Scan(ctx, result); err != nil {
  70. log.Error("Failed to SearchLog: Scan params(%s) error(%+v)", r.Params(), err)
  71. return nil, err
  72. }
  73. return result, nil
  74. }
  75. // SearchFaceCheckRes is.
  76. func (d *Dao) SearchFaceCheckRes(ctx context.Context, fileName string) (*model.SearchLogResult, error) {
  77. index := fmt.Sprintf("log_audit_161_%s", time.Now().Format("2006_01"))
  78. r := d.es.NewRequest("log_audit").
  79. Index(index).
  80. Fields("uid", "uname", "oid", "type", "action", "str_0", "str_1", "str_2", "int_0", "int_1", "int_2", "ctime", "extra_data").
  81. WhereEq("str_1", "face").
  82. WhereEq("str_2", fileName).
  83. Order("ctime", elastic.OrderDesc).
  84. Pn(1).
  85. Ps(1)
  86. result := &model.SearchLogResult{}
  87. if err := r.Scan(ctx, result); err != nil {
  88. log.Error("Failed to SearchFaceCheckRes: Scan params(%s) error(%+v)", r.Params(), err)
  89. return nil, err
  90. }
  91. return result, nil
  92. }
  93. // SearchUserAuditLog is.
  94. func (d *Dao) SearchUserAuditLog(ctx context.Context, mid int64) (*model.SearchLogResult, error) {
  95. nowYear := time.Now().Year()
  96. index := []string{
  97. fmt.Sprintf("log_audit_121_%d", nowYear),
  98. fmt.Sprintf("log_audit_121_%d", nowYear-1),
  99. }
  100. r := d.es.NewRequest("log_audit").
  101. Index(index...).
  102. Fields("uid", "uname", "oid", "type", "action", "str_0", "str_1", "str_2", "int_0", "int_1", "int_2", "ctime", "extra_data").
  103. WhereEq("action", "base_audit").
  104. WhereEq("oid", strconv.FormatInt(mid, 10)).
  105. Order("ctime", elastic.OrderDesc).
  106. Pn(1).
  107. Ps(5)
  108. result := &model.SearchLogResult{}
  109. if err := r.Scan(ctx, result); err != nil {
  110. log.Error("Failed to SearchUserAuditLog: Scan params(%s) error(%+v)", r.Params(), err)
  111. return nil, err
  112. }
  113. return result, nil
  114. }
  115. // SearchUserPropertyReview is.
  116. func (d *Dao) SearchUserPropertyReview(ctx context.Context, mid int64, property []int, state []int, isMonitor, isDesc bool, operator, stime, etime string, pn, ps int) (*model.SearchUserPropertyReviewResult, error) {
  117. order := elastic.OrderAsc
  118. if isDesc {
  119. order = elastic.OrderDesc
  120. }
  121. monitor := 0
  122. if isMonitor {
  123. monitor = 1
  124. }
  125. r := d.es.NewRequest("user_property_review").
  126. Fields("id").
  127. Index("user_property_review").
  128. WhereEq("is_monitor", monitor).
  129. WhereRange("ctime", stime, etime, elastic.RangeScopeLcRc).
  130. Order("id", order).
  131. Ps(ps).
  132. Pn(pn)
  133. if mid > 0 {
  134. r.WhereEq("mid", mid)
  135. }
  136. if operator != "" {
  137. r.WhereEq("operator", operator)
  138. }
  139. if len(property) > 0 {
  140. r.WhereIn("property", property)
  141. }
  142. if len(state) > 0 {
  143. r.WhereIn("state", state)
  144. }
  145. result := &model.SearchUserPropertyReviewResult{}
  146. if err := r.Scan(ctx, result); err != nil {
  147. return nil, errors.WithStack(err)
  148. }
  149. return result, nil
  150. }