task.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package scheduler
  2. import (
  3. "eta/eta_mini_crm/models"
  4. "eta/eta_mini_crm/utils"
  5. "fmt"
  6. "time"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "github.com/robfig/cron/v3"
  9. )
  10. func InitJob() {
  11. fmt.Println("消息推送任务开启。。。")
  12. c := cron.New(cron.WithSeconds())
  13. // 每天凌晨1点检测, 发送消息
  14. _, err := c.AddFunc("0 0 1 * * *", CheckUserTime)
  15. if err != nil {
  16. utils.ApiLog.Info("cron CheckUserTime task err: %s", err.Error())
  17. }
  18. // 每天凌晨1点检测, 修改用户状态
  19. _, err = c.AddFunc("0 0 1 * * *", ModifyUser)
  20. if err != nil {
  21. utils.ApiLog.Info("cron ModifyUser task err: %s", err.Error())
  22. }
  23. c.Start()
  24. }
  25. func CheckUserTime() {
  26. retryCount := 10
  27. var err error
  28. defer func() {
  29. if err != nil {
  30. utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
  31. }
  32. }()
  33. for i := 0; i <= retryCount; i++ {
  34. err = CheckUserPermissionTime()
  35. if err == nil {
  36. break
  37. }
  38. }
  39. }
  40. func ModifyUser() {
  41. retryCount := 10
  42. var err error
  43. defer func() {
  44. if err != nil {
  45. utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
  46. }
  47. }()
  48. for i := 0; i <= retryCount; i++ {
  49. err = ModifyUserStatus()
  50. if err == nil {
  51. break
  52. }
  53. }
  54. }
  55. func ModifyUserStatus() (err error) {
  56. curTime := time.Now()
  57. condition := ` AND valid_end_time<?`
  58. var pars []interface{}
  59. pars = append(pars, curTime)
  60. condition += ` AND status=? `
  61. pars = append(pars, 2)
  62. err = models.UpdateUserStatus(condition, pars)
  63. return
  64. }
  65. func CheckUserPermissionTime() (err error) {
  66. var condition string
  67. var pars []interface{}
  68. var userTotal int
  69. pageSize := 500
  70. curTime := time.Now()
  71. time7 := time.Now().AddDate(0, 0, 7)
  72. time15 := time.Now().AddDate(0, 0, 15)
  73. time30 := time.Now().AddDate(0, 0, 30)
  74. time60 := time.Now().AddDate(0, 0, 60)
  75. // 批量查询到期的用户
  76. messageMap := make(map[int]int)
  77. userTotal, err = models.GetUserCount(condition, pars)
  78. if err != nil {
  79. utils.ApiLog.Warn("userPermission task err: %s", err.Error())
  80. }
  81. pageCount := paging.PageCount(userTotal, pageSize)
  82. for i := 1; i <= pageCount; i++ {
  83. startPage := utils.StartIndex(i, pageSize)
  84. userList, err := models.GetUserList(condition, pars, startPage, pageSize)
  85. if err != nil {
  86. utils.ApiLog.Warn("userPermission task err: %s", err.Error())
  87. }
  88. for _, user := range userList {
  89. if user.Status != 2 {
  90. continue
  91. }
  92. if time60.After(user.ValidEndTime) {
  93. messageMap[user.UserId] = 60
  94. }
  95. if time30.After(user.ValidEndTime) {
  96. messageMap[user.UserId] = 30
  97. }
  98. if time15.After(user.ValidEndTime) {
  99. messageMap[user.UserId] = 15
  100. }
  101. if time7.After(user.ValidEndTime) {
  102. messageMap[user.UserId] = 7
  103. }
  104. }
  105. }
  106. // 把消息发送到系统用户中
  107. sysUserIds, err := models.GetSysUserIdList()
  108. if err != nil {
  109. utils.ApiLog.Warn("userPermission task err: %s", err.Error())
  110. }
  111. layout := "【%s】到期%d天提醒,请及时跟进"
  112. messageList := []models.SysMessageReport{}
  113. for k, v := range messageMap {
  114. count, err := models.GetSysMessageReportCount(k, v)
  115. if err != nil {
  116. continue
  117. }
  118. if count > 0 {
  119. continue
  120. }
  121. user, err := models.GetUserById(k)
  122. if err != nil {
  123. continue
  124. }
  125. for _, sysUserId := range sysUserIds {
  126. messageList = append(messageList, models.SysMessageReport{
  127. UserId: k,
  128. ReceiveSysUserId: sysUserId,
  129. MessageType: v,
  130. IsRead: false,
  131. CreateTime: curTime,
  132. ModifyTime: curTime,
  133. Content: fmt.Sprintf(layout, user.RealName, v),
  134. })
  135. }
  136. err = models.InsertMultiSysMessageReport(messageList)
  137. if err != nil {
  138. continue
  139. }
  140. }
  141. return
  142. }