block.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/spy/conf"
  8. "go-common/app/job/main/spy/model"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. func (s *Service) lastBlockNo(seconds int64) int64 {
  13. return time.Now().Unix()/seconds - 1 //cron -1 get the last hours block mids
  14. }
  15. //BlockTask block task.
  16. func (s *Service) BlockTask(c context.Context) {
  17. var (
  18. blockNo = s.lastBlockNo(s.c.Property.Block.CycleTimes)
  19. )
  20. v, ok := s.Config(model.AutoBlock)
  21. if !ok {
  22. log.Error("Verfiy get config error(%s,%v)", model.AutoBlock, s.spyConfig)
  23. return
  24. }
  25. if v.(int8) != model.AutoBlockOpen {
  26. log.Info("autoBlock Close(%s)", blockNo)
  27. return
  28. }
  29. mids, _ := s.blockUsers(c, blockNo)
  30. if len(mids) == 0 {
  31. log.Info("s.blockUsers len is zero")
  32. return
  33. }
  34. if err := s.dao.SetBlockCache(c, mids); err != nil {
  35. log.Error("s.dao.SetBlockCache(%v) error(%v)", mids, err)
  36. return
  37. }
  38. }
  39. func (s *Service) blockUsers(c context.Context, blockNo int64) (mids []int64, err error) {
  40. v, ok := s.Config(model.LimitBlockCount)
  41. if !ok {
  42. log.Error("blockUsers get config error(%v)", s.spyConfig)
  43. return
  44. }
  45. if mids, err = s.dao.BlockMidCache(c, blockNo, v.(int64)); err != nil {
  46. log.Error("s.dao.BlockMidCache(%s, %d) error(%v)", blockNo, v.(int64), err)
  47. return
  48. }
  49. return
  50. }
  51. func (s *Service) blockByMid(c context.Context, mid int64) (err error) {
  52. ui, ok := s.canBlock(c, mid)
  53. if !ok {
  54. log.Info("s.canBlock user had block(%d)", mid)
  55. return
  56. }
  57. reason, remake := s.blockReason(c, mid)
  58. if err = s.block(c, mid, ui, reason, remake); err != nil {
  59. log.Error("s.block(%d,%v,%s) err(%v)", mid, ui, reason, err)
  60. }
  61. return
  62. }
  63. func (s *Service) block(c context.Context, mid int64, ui *model.UserInfo, reason string, remake string) (err error) {
  64. var (
  65. tx *sql.Tx
  66. )
  67. ui.State = model.StateBlock
  68. if tx, err = s.dao.BeginTran(c); err != nil {
  69. log.Error("s.dao.BeginTran() err(%v)", err)
  70. return
  71. }
  72. defer func() {
  73. if err != nil {
  74. if err1 := tx.Rollback(); err1 != nil {
  75. log.Error("tx.Rollback() error(%v)", err1)
  76. }
  77. return
  78. }
  79. if err = tx.Commit(); err != nil {
  80. log.Error("tx.Commit() error(%v)", err)
  81. }
  82. }()
  83. if err = s.dao.BlockAccount(c, ui.Mid, reason); err != nil {
  84. log.Error("s.dao.BlockAccount(%d) error(%v)", ui.Mid, err)
  85. return
  86. }
  87. var ueh = &model.UserEventHistory{
  88. Mid: mid,
  89. EventID: conf.Conf.Property.BlockEvent,
  90. Score: ui.Score,
  91. BaseScore: ui.BaseScore,
  92. EventScore: ui.EventScore,
  93. Remark: remake,
  94. Reason: "自动封禁",
  95. }
  96. if err = s.dao.TxAddEventHistory(c, tx, ueh); err != nil {
  97. log.Error("s.dao.TxAddEventHistory(%+v) error(%v)", ueh, err)
  98. return
  99. }
  100. if err = s.dao.TxAddPunishment(c, tx, ui.Mid, model.PunishmentTypeBlock,
  101. reason, s.lastBlockNo(s.c.Property.Block.CycleTimes)); err != nil {
  102. log.Error("s.dao.TxAddPunishment(%d,%s) error(%v)", ui.Mid, reason, err)
  103. return
  104. }
  105. // update user state.
  106. if err = s.dao.TxUpdateUserState(c, tx, ui); err != nil {
  107. log.Error("s.dao.TxUpdateUserState(%v) error(%v)", ui, err)
  108. return
  109. }
  110. s.promBlockInfo.Incr("actual_block_count")
  111. return
  112. }
  113. func (s *Service) canBlock(c context.Context, mid int64) (ui *model.UserInfo, ok bool) {
  114. var (
  115. err error
  116. )
  117. ui, err = s.dao.UserInfo(c, mid)
  118. if err != nil {
  119. log.Error("s.UserInfo(%d) err(%v)", mid, err)
  120. return
  121. }
  122. v, b := s.Config(model.LessBlockScore)
  123. if !b {
  124. log.Error("scoreLessHandler get config error(%s,%v)", model.LessBlockScore, s.spyConfig)
  125. return
  126. }
  127. // if blocked already , return
  128. if ui.State == model.StateBlock || ui.Score > v.(int8) {
  129. log.Info("canBlock not block(%v)", ui)
  130. return
  131. }
  132. ok = true
  133. return
  134. }
  135. func (s *Service) blockReason(c context.Context, mid int64) (reason string, remake string) {
  136. var (
  137. err error
  138. hs []*model.UserEventHistory
  139. buf bytes.Buffer
  140. )
  141. if hs, err = s.dao.HistoryList(c, mid, model.BlockReasonSize); err != nil || len(hs) == 0 {
  142. log.Error("s.dao.HistoryList(%d) err(%v)", mid, err)
  143. return
  144. }
  145. m := make(map[string]int)
  146. for i, v := range hs {
  147. if i == 0 {
  148. remake = v.Remark
  149. }
  150. if m[v.Reason] == 0 {
  151. m[v.Reason] = 1
  152. } else {
  153. m[v.Reason] = m[v.Reason] + 1
  154. }
  155. }
  156. for k, v := range m {
  157. buf.WriteString(k)
  158. buf.WriteString("x")
  159. buf.WriteString(fmt.Sprintf("%d ", v))
  160. }
  161. reason = buf.String()
  162. return
  163. }