message.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. xtime "time"
  7. "go-common/app/job/main/vip/model"
  8. "go-common/library/log"
  9. "go-common/library/time"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. maxSendMsgCount = 200
  14. willExpiredDay = 7
  15. hadExpiredDays = 14
  16. willExpiredMsg = "您还有%d天大会员即将到期,请尽快续期,享受更多特权!"
  17. willExpiredTitle = "大会员即将到期提醒"
  18. hadExpiredMsg = "很抱歉的通知您,您的大会员已过期,请尽快续期享受更多特权!"
  19. hadExpiredTitle = "大会员过期提醒"
  20. vipWillExpiredMsgCode = "10_1_2"
  21. vipHadExpiredMsgCode = "10_1_3"
  22. systemNotify = 4
  23. )
  24. func (s *Service) hadExpiredMsgJob() {
  25. defer func() {
  26. if x := recover(); x != nil {
  27. log.Error("service.hadExpiredMsgJob panic(%v)", x)
  28. go s.hadExpiredMsgJob()
  29. log.Info("service.hadExpiredMsgJob recover")
  30. }
  31. }()
  32. log.Info("start send had expire msg job ...................")
  33. var (
  34. err error
  35. mids []int64
  36. )
  37. now := xtime.Now()
  38. startTime := now.AddDate(0, 0, -hadExpiredDays-1)
  39. endTime := now.AddDate(0, 0, -hadExpiredDays)
  40. if mids, err = s.willExpireUser(time.Time(startTime.Unix()), time.Time(endTime.Unix()), model.VipStatusOverTime); err != nil {
  41. log.Error("will expire user(startTime:%v endTime:%v status:%v) error(%+v)", startTime, endTime, model.VipStatusOverTime, err)
  42. return
  43. }
  44. log.Info("send startTime(%v) endDate(%v) mids(%v)", startTime.Format("2006-01-02"), endTime.Format("2006-01-02"), mids)
  45. if err = s.batchSendMsg(mids, hadExpiredMsg, hadExpiredTitle, vipHadExpiredMsgCode, systemNotify); err != nil {
  46. log.Error("batch send msg error(%+v)", err)
  47. return
  48. }
  49. log.Info("end send had expire msg job..........................")
  50. }
  51. func (s *Service) willExpiredMsgJob() {
  52. defer func() {
  53. if x := recover(); x != nil {
  54. log.Error("service.hadExpiredMsgJob panic(%v)", x)
  55. go s.willExpiredMsgJob()
  56. log.Info("service.hadExpiredMsgJob recover")
  57. }
  58. }()
  59. log.Info("start send will expire msg job............................")
  60. var (
  61. err error
  62. mids []int64
  63. )
  64. now := xtime.Now()
  65. startTime := now.AddDate(0, 0, willExpiredDay)
  66. endTime := now.AddDate(0, 0, willExpiredDay+1)
  67. if mids, err = s.willExpireUser(time.Time(startTime.Unix()), time.Time(endTime.Unix()), model.VipStatusNotOverTime); err != nil {
  68. log.Error("will expire user(startTime:%v endTime:%v status:%v) error(%+v)", startTime, endTime, model.VipStatusNotOverTime, err)
  69. return
  70. }
  71. log.Info("send startTime(%v) endDate(%v) mids(%v)", startTime.Format("2006-01-02"), endTime.Format("2006-01-02"), mids)
  72. if err = s.batchSendMsg(mids, fmt.Sprintf(willExpiredMsg, willExpiredDay), willExpiredTitle, vipWillExpiredMsgCode, systemNotify); err != nil {
  73. log.Error("batch send msg error(%+v)", err)
  74. return
  75. }
  76. log.Info("end send will expire msg job............................")
  77. }
  78. func (s *Service) willExpireUser(startTime time.Time, endTime time.Time, status int) (mids []int64, err error) {
  79. var (
  80. maxID int
  81. size = 10000
  82. )
  83. if maxID, err = s.dao.SelMaxID(context.TODO()); err != nil {
  84. err = errors.WithStack(err)
  85. return
  86. }
  87. page := maxID / size
  88. if maxID%size != 0 {
  89. page++
  90. }
  91. for i := 0; i < page; i++ {
  92. startID := i * size
  93. endID := (i + 1) * size
  94. var tempMid []int64
  95. if tempMid, err = s.dao.SelVipUserInfos(context.TODO(), startID, endID, startTime, endTime, status); err != nil {
  96. err = errors.WithStack(err)
  97. return
  98. }
  99. mids = append(mids, tempMid...)
  100. }
  101. return
  102. }
  103. func (s *Service) batchSendMsg(mids []int64, content string, title string, ms string, dataType int) (err error) {
  104. if len(mids) <= maxSendMsgCount && len(mids) >= 1 {
  105. var midsStr = ""
  106. for _, v := range mids {
  107. midsStr += fmt.Sprintf(",%v", v)
  108. }
  109. if err = s.dao.SendMultipMsg(context.TODO(), midsStr, content, title, ms, dataType); err != nil {
  110. err = errors.WithStack(err)
  111. return
  112. }
  113. } else if len(mids) > maxSendMsgCount {
  114. page := len(mids) / maxSendMsgCount
  115. if len(mids)%maxSendMsgCount != 0 {
  116. page++
  117. }
  118. for i := 0; i < page; i++ {
  119. start := i * maxSendMsgCount
  120. end := (i + 1) * maxSendMsgCount
  121. if len(mids) < end {
  122. end = len(mids)
  123. }
  124. tempMids := mids[start:end]
  125. var midsStr []string
  126. for _, v := range tempMids {
  127. midsStr = append(midsStr, fmt.Sprintf("%v", v))
  128. }
  129. if err = s.dao.SendMultipMsg(context.TODO(), strings.Join(midsStr, ","), content, title, ms, dataType); err != nil {
  130. err = errors.WithStack(err)
  131. continue
  132. }
  133. }
  134. }
  135. return
  136. }