review.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package model
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "go-common/library/log"
  7. )
  8. // ReviewConf 复审配置
  9. type ReviewConf struct {
  10. ID int64 `json:"id" form:"id"`
  11. Types []int64 `json:"types" form:"types,split"` // 分区
  12. mtp map[int16]struct{}
  13. UpFroms []int64 `json:"upfroms" form:"upfroms,split"` // 投稿来源
  14. muf map[int8]struct{}
  15. UpGroups []int64 `json:"upgroups" form:"upgroups,split"` // 用户组
  16. mug map[int8]struct{}
  17. Uids []int64 `json:"uids" form:"uids,split"` // 指定uid
  18. Unames []string `json:"unames"` // 指定uid
  19. muid map[int64]struct{}
  20. FansLow int64 `json:"fanslow" form:"fanslow"` // 粉丝数最低值
  21. FansHigh int64 `json:"fanshigh" form:"fanshigh"` // 粉丝数最高
  22. Bt FormatTime `json:"bt" form:"bt"`
  23. Et FormatTime `json:"et" form:"et"`
  24. State int8 `json:"state" form:"state"`
  25. UID int64 `json:"uid"`
  26. Uname string `json:"uname"`
  27. Desc string `json:"desc" form:"desc"`
  28. Mt FormatTime `json:"mt"`
  29. }
  30. // Refresh refresh
  31. func (r *ReviewConf) Refresh() {
  32. mtp := make(map[int16]struct{})
  33. muf := make(map[int8]struct{})
  34. mug := make(map[int8]struct{})
  35. muid := make(map[int64]struct{})
  36. for _, tp := range r.Types {
  37. mtp[int16(tp)] = struct{}{}
  38. }
  39. for _, uf := range r.UpFroms {
  40. muf[int8(uf)] = struct{}{}
  41. }
  42. for _, ug := range r.UpGroups {
  43. mug[int8(ug)] = struct{}{}
  44. }
  45. for _, uid := range r.Uids {
  46. muid[uid] = struct{}{}
  47. }
  48. r.mtp = mtp
  49. r.muf = muf
  50. r.mug = mug
  51. r.muid = muid
  52. }
  53. // SubmitForm form
  54. type SubmitForm struct {
  55. Status int16 `json:"status" form:"status"`
  56. ID int64 `json:"id" form:"id"`
  57. CID int64 `json:"cid" form:"cid"`
  58. AID int64 `json:"aid" form:"aid"`
  59. MID int64 `json:"mid" form:"mid"`
  60. Eptitle string `json:"eptitle,omitempty" form:"eptitle"`
  61. Description string `json:"description,omitempty" form:"description"`
  62. Note string `json:"note,omitempty" form:"note"`
  63. ReasonID int64 `json:"reason_id,omitempty" form:"reason_id"`
  64. Reason string `json:"reason,omitempty" form:"reason"`
  65. TID int64 `json:"tid,omitempty" form:"tid"`
  66. Norank int32 `json:"norank" form:"norank"`
  67. Noindex int32 `json:"noindex" form:"noindex"`
  68. PushBlog int32 `json:"push_blog" form:"push_blog"`
  69. NoRecommend int32 `json:"norecommend" form:"norecommend"`
  70. Nosearch int32 `json:"nosearch" form:"nosearch"`
  71. OverseaBlock int32 `json:"oversea_block" form:"oversea_block"`
  72. Encoding int8 `json:"encoding" form:"encoding"`
  73. TaskID int64 `json:"task_id" form:"task_id"`
  74. UID int64 `json:"uid" form:"uid"`
  75. Uname string `json:"uname" form:"uname"`
  76. }
  77. // ReviewCache 快速判断配置项是否命中
  78. type ReviewCache struct {
  79. MRC map[int64]*ReviewConf
  80. Mux sync.RWMutex
  81. }
  82. // NewRC 复审配置
  83. func NewRC() *ReviewCache {
  84. rc := &ReviewCache{}
  85. rc.MRC = make(map[int64]*ReviewConf)
  86. return rc
  87. }
  88. // Check 检查配置是否命中
  89. func (rc *ReviewCache) Check(c context.Context, opt *TaskPriority, uid int64) bool {
  90. rc.Mux.RLock()
  91. defer rc.Mux.RUnlock()
  92. if len(rc.MRC) == 0 {
  93. log.Info("ReviewCache empty")
  94. return false
  95. }
  96. log.Info("ReviewCache opt(%+v) uid(%d),", opt, uid)
  97. for id, item := range rc.MRC {
  98. log.Info("ReviewCache config(%+v)", item)
  99. if item.State != 0 {
  100. continue
  101. }
  102. bt := item.Bt.TimeValue()
  103. et := item.Et.TimeValue()
  104. if bt.After(time.Now()) || (!et.IsZero() && et.Before(time.Now())) {
  105. continue
  106. }
  107. if len(item.mtp) > 0 {
  108. if _, ok := item.mtp[opt.TypeID]; !ok {
  109. continue
  110. }
  111. }
  112. if len(item.muf) > 0 {
  113. if _, ok := item.muf[opt.UpFrom]; !ok {
  114. continue
  115. }
  116. }
  117. if len(item.mug) > 0 {
  118. var hit bool
  119. for _, ug := range opt.UpGroups {
  120. if _, ok := item.mug[ug]; ok {
  121. hit = true
  122. break
  123. }
  124. }
  125. if !hit {
  126. continue
  127. }
  128. }
  129. if len(item.muid) > 0 {
  130. if _, ok := item.muid[uid]; !ok {
  131. continue
  132. }
  133. }
  134. if item.FansHigh > 0 {
  135. if opt.Fans < item.FansLow || opt.Fans > item.FansHigh {
  136. continue
  137. }
  138. }
  139. log.Info("ReviewCache task(%d) hit config(%d)", opt.TaskID, id)
  140. return true
  141. }
  142. return false
  143. }