limit.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package block
  2. import (
  3. "context"
  4. "time"
  5. model "go-common/app/job/main/member/model/block"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. func (s *Service) limitExpireHandler(c context.Context) {
  11. if s.conf.BlockProperty.LimitExpireCheckLimit <= 0 {
  12. log.Error("conf.Conf.Property.LimitExpireCheckLimit [%d] <= 0", s.conf.BlockProperty.LimitExpireCheckLimit)
  13. return
  14. }
  15. var (
  16. mids = make([]int64, s.conf.BlockProperty.LimitExpireCheckLimit)
  17. startID int64
  18. err error
  19. )
  20. for len(mids) >= s.conf.BlockProperty.LimitExpireCheckLimit {
  21. log.Info("limit expire handle startID (%d)", startID)
  22. if startID, mids, err = s.dao.UserStatusList(c, model.BlockStatusLimit, startID, s.conf.BlockProperty.LimitExpireCheckLimit); err != nil {
  23. log.Error("%+v", err)
  24. return
  25. }
  26. for _, mid := range mids {
  27. var ok bool
  28. if ok, err = s.limitExpireCheck(c, mid); err != nil {
  29. log.Error("%+v", err)
  30. continue
  31. }
  32. if ok {
  33. if err = s.limitExpireRemove(c, mid); err != nil {
  34. log.Error("error: %+v, mid: %d", err, mid)
  35. continue
  36. }
  37. }
  38. time.Sleep(time.Millisecond * 100)
  39. }
  40. }
  41. }
  42. func (s *Service) limitExpireCheck(c context.Context, mid int64) (ok bool, err error) {
  43. var (
  44. his *model.DBHistory
  45. )
  46. if his, err = s.dao.UserLastHistory(c, mid); err != nil {
  47. return
  48. }
  49. if his == nil {
  50. return
  51. }
  52. if his.Action != model.BlockActionLimit {
  53. return
  54. }
  55. if his.StartTime.Add(time.Duration(his.Duration) * time.Second).After(time.Now()) {
  56. return
  57. }
  58. ok = true
  59. return
  60. }
  61. func (s *Service) limitExpireRemove(c context.Context, mid int64) (err error) {
  62. var (
  63. _reason, _comment = "系统自动解封", "系统自动解封"
  64. stime = time.Now()
  65. db = &model.DBHistory{
  66. MID: mid,
  67. AdminID: model.BlockJOBManagerID,
  68. AdminName: model.BlockJOBManagerName,
  69. Source: model.BlockSourceRemove,
  70. Area: model.BlockAreaNone,
  71. Reason: _reason,
  72. Comment: _comment,
  73. Action: model.BlockActionSelfRemove,
  74. StartTime: stime,
  75. Duration: 0,
  76. Notify: false,
  77. }
  78. tx *xsql.Tx
  79. )
  80. if tx, err = s.dao.BeginTX(c); err != nil {
  81. return
  82. }
  83. if err = s.dao.TxInsertHistory(c, tx, db); err != nil {
  84. tx.Rollback()
  85. return
  86. }
  87. count, err := s.dao.TxUpsertUser(c, tx, mid, model.BlockStatusFalse)
  88. if err != nil || count == 0 {
  89. tx.Rollback()
  90. return
  91. }
  92. if err = tx.Commit(); err != nil {
  93. err = errors.WithStack(err)
  94. }
  95. s.mission(func() {
  96. if err := s.notifyRemoveMSG(context.TODO(), []int64{mid}); err != nil {
  97. log.Error("%+v", err)
  98. }
  99. })
  100. s.mission(func() {
  101. s.AddAuditLog(context.TODO(), model.BlockActionSelfRemove, model.BlockJOBManagerID, model.BlockJOBManagerName, []int64{mid}, 0, model.BlockSourceRemove, model.BlockAreaNone, _reason, _comment, false, stime)
  102. })
  103. s.cache.Save(func() {
  104. if err := s.dao.DeleteUserCache(context.TODO(), mid); err != nil {
  105. log.Error("%+v", err)
  106. }
  107. if databusErr := s.dao.AccountNotify(context.TODO(), mid); databusErr != nil {
  108. log.Error("%+v", databusErr)
  109. }
  110. })
  111. return
  112. }