vip.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "go-common/app/job/main/usersuit/model"
  7. vipmdl "go-common/app/service/main/vip/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _vipGid = 31
  12. _vipUserInfoTable = "vip_user_info"
  13. )
  14. func (s *Service) vipconsumerproc() {
  15. defer s.wg.Done()
  16. var (
  17. msgs = s.vipBinLogSub.Messages()
  18. err error
  19. c = context.TODO()
  20. )
  21. for {
  22. msg, ok := <-msgs
  23. if !ok {
  24. log.Error("s.vipBinLogSub.Message closed")
  25. return
  26. }
  27. msg.Commit()
  28. m := &model.Message{}
  29. if err = json.Unmarshal(msg.Value, m); err != nil {
  30. log.Error("json.Unmarshal(%v) error(%v)", string(msg.Value), err)
  31. continue
  32. }
  33. switch m.Table {
  34. case _vipUserInfoTable:
  35. if m.Action == "update" {
  36. s.dealUserPendantEquip(c, m.New, m.Old)
  37. }
  38. default:
  39. log.Warn("vipBinLogConsumer unknown message action(%s)", m.Table)
  40. }
  41. if err != nil {
  42. log.Error("vipBinLogMessage key(%s) value(%s) partition(%d) offset(%d) commit error(%v)", msg.Key, msg.Value, msg.Partition, msg.Offset, err)
  43. continue
  44. }
  45. log.Info("vipBinLogMessage key(%s) value(%s) partition(%d) offset(%d) commit", msg.Key, msg.Value, msg.Partition, msg.Offset)
  46. }
  47. }
  48. func (s *Service) dealUserPendantEquip(c context.Context, nwMsg []byte, oldMsg []byte) (err error) {
  49. mr := &model.VipInfoMessage{}
  50. if err = json.Unmarshal(nwMsg, mr); err != nil {
  51. log.Error("json.Unmarshal(%s) error(%v)", string(nwMsg), err)
  52. return
  53. }
  54. var (
  55. gid int64
  56. pe *model.PendantEquip
  57. )
  58. if pe, err = s.pendantDao.PendantEquipMID(c, mr.Mid); err != nil {
  59. log.Error("mid(%d) s.pendantDao.PendantEquipMID error(%v)", mr.Mid, err)
  60. return
  61. }
  62. if pe == nil || pe.Pid == 0 || pe.Expires == 0 {
  63. log.Warn("mid(%d) no equip pendant(%d) expires(%d)", mr.Mid, pe.Pid, pe.Expires)
  64. return
  65. }
  66. if gid, err = s.pendantDao.PendantEquipGidPid(c, pe.Pid); err != nil {
  67. log.Error("mid(%d) pid(%d) s.pendantDao.PendantEquipGidPid error(%v)", mr.Mid, pe.Pid, err)
  68. return
  69. }
  70. if gid != _vipGid {
  71. log.Warn("mid(%d) no equip the vip gid(%d) of pid(%d)", mr.Mid, gid, pe.Pid)
  72. return
  73. }
  74. if mr.VipStatus == vipmdl.VipStatusNotOverTime {
  75. var ts time.Time
  76. if ts, err = time.ParseInLocation(model.TimeFormatSec, mr.VipOverdueTime, time.Local); err != nil {
  77. log.Error("time.ParseInLocation(%s) error(%v)", mr.VipOverdueTime, err)
  78. return
  79. }
  80. if ts.Unix() <= pe.Expires {
  81. log.Warn("mid(%d) pendant equip_time(%d) than vipoverdue_time(%d)", mr.Mid, pe.Expires, ts.Unix())
  82. return
  83. }
  84. if _, err = s.pendantDao.UpEquipExpires(c, mr.Mid, ts.Unix()); err != nil {
  85. log.Error("s.pendantDao.UpEquipExpires(%d,%d) error(%+v)", mr.Mid, ts.Unix(), err)
  86. return
  87. }
  88. } else {
  89. if _, err = s.pendantDao.UpEquipMID(c, mr.Mid); err != nil {
  90. log.Error("s.pendantDao.UpEquipMID(%d) error(%+v)", mr.Mid, err)
  91. return
  92. }
  93. log.Warn("mid(%d) vip status is overtime", mr.Mid)
  94. }
  95. s.pendantDao.DelEquipCache(c, mr.Mid)
  96. s.addNotify(func() {
  97. s.accNotify(context.TODO(), mr.Mid, model.AccountNotifyUpdatePendant)
  98. })
  99. return
  100. }