blackhouse.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/block/conf"
  8. "go-common/app/job/main/block/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/queue/databus"
  12. "github.com/pkg/errors"
  13. )
  14. func (s *Service) creditExpireHandler(c context.Context) {
  15. if conf.Conf.Property.CreditExpireCheckLimit <= 0 {
  16. log.Error("conf.Conf.Property.creditExpireCheckLimit [%d] <= 0", conf.Conf.Property.CreditExpireCheckLimit)
  17. return
  18. }
  19. var (
  20. mids = make([]int64, conf.Conf.Property.CreditExpireCheckLimit)
  21. startID int64
  22. err error
  23. )
  24. for len(mids) >= conf.Conf.Property.CreditExpireCheckLimit {
  25. log.Info("black house expire handle startID (%d)", startID)
  26. if startID, mids, err = s.dao.UserStatusList(c, model.BlockStatusCredit, startID, conf.Conf.Property.CreditExpireCheckLimit); err != nil {
  27. log.Error("%+v", err)
  28. return
  29. }
  30. for _, mid := range mids {
  31. log.Info("Start handle black house mid (%d)", mid)
  32. var ok bool
  33. if ok, err = s.creditExpireCheck(c, mid); err != nil {
  34. log.Error("%+v", err)
  35. continue
  36. }
  37. if ok {
  38. log.Info("Start remove black house mid (%d)", mid)
  39. if err = s.creditExpireRemove(c, mid); err != nil {
  40. log.Error("%+v", err)
  41. }
  42. }
  43. }
  44. }
  45. }
  46. func (s *Service) creditExpireCheck(c context.Context, mid int64) (ok bool, err error) {
  47. var (
  48. his *model.DBHistory
  49. ex *model.DBExtra
  50. )
  51. if his, err = s.dao.UserLastHistory(c, mid); err != nil {
  52. return
  53. }
  54. if his == nil {
  55. return
  56. }
  57. log.Info("Credit check his (%+v)", his)
  58. if his.Action != model.BlockActionLimit || his.Source != model.BlockSourceCredit {
  59. return
  60. }
  61. if ex, err = s.dao.UserExtra(c, mid); err != nil {
  62. return
  63. }
  64. if ex == nil {
  65. return
  66. }
  67. log.Info("Credit check extra (%+v)", his)
  68. if ex.ActionTime.Before(his.StartTime) {
  69. return
  70. }
  71. if his.StartTime.Add(time.Duration(his.Duration) * time.Second).After(time.Now()) {
  72. return
  73. }
  74. ok = true
  75. return
  76. }
  77. func (s *Service) creditExpireRemove(c context.Context, mid int64) (err error) {
  78. var (
  79. db = &model.DBHistory{
  80. MID: mid,
  81. AdminID: -1,
  82. AdminName: "sys",
  83. Source: model.BlockSourceRemove,
  84. Area: model.BlockAreaNone,
  85. Reason: "小黑屋自动解封",
  86. Comment: "小黑屋自动解封",
  87. Action: model.BlockActionSelfRemove,
  88. StartTime: time.Now(),
  89. Duration: 0,
  90. Notify: false,
  91. }
  92. tx *xsql.Tx
  93. )
  94. if tx, err = s.dao.BeginTX(c); err != nil {
  95. return
  96. }
  97. if err = s.dao.TxInsertHistory(c, tx, db); err != nil {
  98. tx.Rollback()
  99. return
  100. }
  101. if err = s.dao.TxUpsertUser(c, tx, mid, model.BlockStatusFalse); err != nil {
  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.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. return
  187. }
  188. }
  189. return
  190. }