blackhouse.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package block
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. model "go-common/app/job/main/member/model/block"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/queue/databus"
  11. "github.com/pkg/errors"
  12. )
  13. func (s *Service) creditExpireHandler(c context.Context) {
  14. if s.conf.BlockProperty.CreditExpireCheckLimit <= 0 {
  15. log.Error("conf.Conf.Property.creditExpireCheckLimit [%d] <= 0", s.conf.BlockProperty.CreditExpireCheckLimit)
  16. return
  17. }
  18. var (
  19. mids = make([]int64, s.conf.BlockProperty.CreditExpireCheckLimit)
  20. startID int64
  21. err error
  22. )
  23. for len(mids) >= s.conf.BlockProperty.CreditExpireCheckLimit {
  24. log.Info("black house expire handle startID (%d)", startID)
  25. if startID, mids, err = s.dao.UserStatusList(c, model.BlockStatusCredit, startID, s.conf.BlockProperty.CreditExpireCheckLimit); err != nil {
  26. log.Error("%+v", err)
  27. return
  28. }
  29. for _, mid := range mids {
  30. log.Info("Start handle black house mid (%d)", mid)
  31. var ok bool
  32. if ok, err = s.creditExpireCheck(c, mid); err != nil {
  33. log.Error("%+v", err)
  34. continue
  35. }
  36. if ok {
  37. log.Info("Start remove black house mid (%d)", mid)
  38. if err = s.creditExpireRemove(c, mid); err != nil {
  39. log.Error("error: %+v, mid: %d", err, mid)
  40. }
  41. }
  42. }
  43. }
  44. }
  45. func (s *Service) creditExpireCheck(c context.Context, mid int64) (ok bool, err error) {
  46. var (
  47. his *model.DBHistory
  48. ex *model.DBExtra
  49. )
  50. if his, err = s.dao.UserLastHistory(c, mid); err != nil {
  51. return
  52. }
  53. if his == nil {
  54. return
  55. }
  56. log.Info("Credit check his (%+v)", his)
  57. if his.Action != model.BlockActionLimit {
  58. return
  59. }
  60. if ex, err = s.dao.UserExtra(c, mid); err != nil {
  61. return
  62. }
  63. if ex == nil {
  64. return
  65. }
  66. log.Info("Credit check extra (%+v)", his)
  67. if ex.ActionTime.Before(his.StartTime) {
  68. return
  69. }
  70. if his.StartTime.Add(time.Duration(his.Duration) * time.Second).After(time.Now()) {
  71. return
  72. }
  73. ok = true
  74. return
  75. }
  76. func (s *Service) creditExpireRemove(c context.Context, mid int64) (err error) {
  77. var (
  78. db = &model.DBHistory{
  79. MID: mid,
  80. AdminID: -1,
  81. AdminName: "sys",
  82. Source: model.BlockSourceRemove,
  83. Area: model.BlockAreaNone,
  84. Reason: "小黑屋自动解封",
  85. Comment: "小黑屋自动解封",
  86. Action: model.BlockActionSelfRemove,
  87. StartTime: time.Now(),
  88. Duration: 0,
  89. Notify: false,
  90. }
  91. tx *xsql.Tx
  92. )
  93. if tx, err = s.dao.BeginTX(c); err != nil {
  94. return
  95. }
  96. if err = s.dao.TxInsertHistory(c, tx, db); err != nil {
  97. tx.Rollback()
  98. return
  99. }
  100. count, err := s.dao.TxUpsertUser(c, tx, mid, model.BlockStatusFalse)
  101. if err != nil || count == 0 {
  102. tx.Rollback()
  103. return
  104. }
  105. if err = tx.Commit(); err != nil {
  106. err = errors.WithStack(err)
  107. }
  108. s.mission(func() {
  109. if err := s.notifyRemoveMSG(context.TODO(), []int64{mid}); err != nil {
  110. log.Error("%+v", err)
  111. }
  112. })
  113. s.cache.Save(func() {
  114. if err := s.dao.DeleteUserCache(context.TODO(), mid); err != nil {
  115. log.Error("%+v", err)
  116. }
  117. if databusErr := s.dao.AccountNotify(context.TODO(), mid); databusErr != nil {
  118. log.Error("%+v", databusErr)
  119. }
  120. })
  121. return
  122. }
  123. func (s *Service) notifyRemoveMSG(c context.Context, mids []int64) (err error) {
  124. code, title, content := s.MSGRemoveInfo()
  125. if err = s.dao.SendSysMsg(c, code, mids, title, content, ""); err != nil {
  126. return
  127. }
  128. return
  129. }
  130. // databus
  131. func (s *Service) creditsubproc() {
  132. defer func() {
  133. if x := recover(); x != nil {
  134. log.Error("%+v", errors.WithStack(fmt.Errorf("s.creditsubproc panic(%v)", x)))
  135. go s.creditsubproc()
  136. log.Info("s.creditsubproc recover")
  137. }
  138. }()
  139. var (
  140. msg *databus.Message
  141. eventMSG *model.CreditAnswerMSG
  142. err error
  143. msgChan = s.creditSub.Messages()
  144. c = context.TODO()
  145. )
  146. for msg = range msgChan {
  147. if err = msg.Commit(); err != nil {
  148. log.Error("msg.Commit error(%v)", err)
  149. }
  150. eventMSG = &model.CreditAnswerMSG{}
  151. if err = json.Unmarshal([]byte(msg.Value), eventMSG); err != nil {
  152. log.Error("%+v", errors.WithStack(err))
  153. continue
  154. }
  155. if err = s.handleCreditAnswerMSG(c, eventMSG); err != nil {
  156. log.Error("%+v", err)
  157. continue
  158. }
  159. log.Info("s.handleCreditAnswerMSG(%v) msg", eventMSG)
  160. }
  161. log.Info("creditsubproc end")
  162. }
  163. func (s *Service) handleCreditAnswerMSG(c context.Context, msg *model.CreditAnswerMSG) (err error) {
  164. if msg.MID <= 0 {
  165. return
  166. }
  167. var (
  168. extra = &model.DBExtra{
  169. MID: msg.MID,
  170. CreditAnswerFlag: true,
  171. ActionTime: msg.MTime.Time(),
  172. }
  173. checkFlag bool
  174. )
  175. if err = s.dao.InsertExtra(c, extra); err != nil {
  176. return
  177. }
  178. // 及时检查解封
  179. log.Info("Start check black house mid (%d) from answer", extra.MID)
  180. if checkFlag, err = s.creditExpireCheck(c, extra.MID); err != nil {
  181. return
  182. }
  183. if checkFlag {
  184. log.Info("Start remove black house mid (%d)", extra.MID)
  185. if err = s.creditExpireRemove(c, extra.MID); err != nil {
  186. log.Error("error: %+v, mid: %d", err, extra.MID)
  187. return
  188. }
  189. }
  190. return
  191. }