task.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package scheduler
  2. import (
  3. "context"
  4. "eta/eta_mini_crm/models"
  5. "eta/eta_mini_crm/utils"
  6. "fmt"
  7. "time"
  8. "github.com/beego/beego/v2/task"
  9. )
  10. func InitJob() {
  11. fmt.Println("消息推送任务开启。。。")
  12. // 每天凌晨12点10分检测, 发送消息
  13. tk1 := task.NewTask("SendReminderMsg", "0 10 0 * * *", SendReminderMsg)
  14. task.AddTask("发送消息提醒", tk1)
  15. // 每天凌晨12点检测, 修改用户状态
  16. tk2 := task.NewTask("ModifyUserStatus", "5 0 0 * * *", ModifyUserStatus)
  17. task.AddTask("定时修改用户状态", tk2)
  18. task.StartTask()
  19. }
  20. func ModifyUserStatus(ctx context.Context) (err error) {
  21. defer func() {
  22. if err := recover(); err != nil {
  23. utils.ApiLog.Warn("发送提醒消息,定时任务出错,Err: %s", err)
  24. }
  25. }()
  26. curTime := time.Now()
  27. var pars []interface{}
  28. condition := ` AND valid_end_time<?`
  29. pars = append(pars, curTime)
  30. condition += ` AND status=? `
  31. pars = append(pars, 2)
  32. userIds, err := models.GetUserIdListByCondition(condition, pars)
  33. if err != nil {
  34. return
  35. }
  36. err = models.UpdateUserStatus(condition, pars)
  37. if err != nil {
  38. return
  39. }
  40. userRecordList := make([]*models.UserChangeRecord, 0)
  41. for _, v := range userIds {
  42. record := &models.UserChangeRecord{}
  43. record.UserId = v
  44. record.SysUserId = 0
  45. record.Content = "有效期到期禁用用户"
  46. record.CreateTime = time.Now()
  47. userRecordList = append(userRecordList, record)
  48. }
  49. err = models.UserChangeRecordMultiInsert(userRecordList)
  50. if err != nil {
  51. return
  52. }
  53. return
  54. }
  55. func SendReminderMsg(ctx context.Context) (err error) {
  56. defer func() {
  57. if err := recover(); err != nil {
  58. utils.ApiLog.Warn("发送提醒消息,定时任务出错,Err: %s", err)
  59. }
  60. }()
  61. var userPars7 []interface{}
  62. var userPars15 []interface{}
  63. var userPars30 []interface{}
  64. var userPars60 []interface{}
  65. var msgPars7 []interface{}
  66. var msgPars15 []interface{}
  67. var msgPars30 []interface{}
  68. var msgPars60 []interface{}
  69. userTime7 := time.Now().AddDate(0, 0, 7)
  70. userTime15 := time.Now().AddDate(0, 0, 15)
  71. userTime30 := time.Now().AddDate(0, 0, 30)
  72. userTime60 := time.Now().AddDate(0, 0, 60)
  73. msgTime7 := time.Now().AddDate(0, 0, -7)
  74. msgTime15 := time.Now().AddDate(0, 0, -15)
  75. msgTime30 := time.Now().AddDate(0, 0, -30)
  76. msgTime60 := time.Now().AddDate(0, 0, -60)
  77. userCondition1 := ` AND valid_end_time<? `
  78. userCondition2 := ` AND valid_end_time<? AND valid_end_time>? `
  79. msgCondition := ` AND create_time>? `
  80. userPars7 = append(userPars7, userTime7)
  81. userPars15 = append(userPars15, userTime15, userTime7)
  82. userPars30 = append(userPars30, userTime30, userTime15)
  83. userPars60 = append(userPars60, userTime60, userTime30)
  84. msgPars7 = append(msgPars7, msgTime7)
  85. msgPars15 = append(msgPars15, msgTime15)
  86. msgPars30 = append(msgPars30, msgTime30)
  87. msgPars60 = append(msgPars60, msgTime60)
  88. err = SendMsgToSysUser(userCondition1, msgCondition, userPars7, msgPars7, 7)
  89. if err != nil {
  90. utils.ApiLog.Warn("发送提醒消息,定时任务出错,Err: %s", err.Error())
  91. }
  92. err = SendMsgToSysUser(userCondition2, msgCondition, userPars15, msgPars15, 15)
  93. if err != nil {
  94. utils.ApiLog.Warn("发送提醒消息,定时任务出错,Err: %s", err.Error())
  95. }
  96. err = SendMsgToSysUser(userCondition2, msgCondition, userPars30, msgPars30, 30)
  97. if err != nil {
  98. utils.ApiLog.Warn("发送提醒消息,定时任务出错,Err: %s", err.Error())
  99. }
  100. err = SendMsgToSysUser(userCondition2, msgCondition, userPars60, msgPars60, 60)
  101. if err != nil {
  102. utils.ApiLog.Warn("发送提醒消息,定时任务出错,Err: %s", err.Error())
  103. }
  104. return
  105. }
  106. func SendMsgToSysUser(userCondition, msgCondition string, userPars, msgPars []interface{}, messagetType int) (err error) {
  107. total, err := models.GetUserCount(userCondition, userPars)
  108. if err != nil {
  109. utils.ApiLog.Warn("获取用户列表失败 task err: %s", err.Error())
  110. }
  111. if total == 0 {
  112. return
  113. }
  114. // 获得待发送用户的列表
  115. userList, err := models.GetUserList(userCondition, userPars, 0, total)
  116. if err != nil {
  117. return
  118. }
  119. // 获得待通知系统用户的列表
  120. sysUserIds, err := models.GetSysUserIdList()
  121. if err != nil {
  122. return
  123. }
  124. messageList, err := models.GetSysMessageReportByCondition(msgCondition, msgPars)
  125. if err != nil {
  126. return
  127. }
  128. layout := "【%s】到期%d天提醒,请及时跟进"
  129. messageMap := make(map[int]map[int]struct{})
  130. for _, v := range messageList {
  131. if mv, ok := messageMap[v.ReceiveSysUserId]; ok {
  132. mv[v.UserId] = struct{}{}
  133. } else {
  134. messageMap[v.ReceiveSysUserId] = make(map[int]struct{})
  135. messageMap[v.ReceiveSysUserId][v.UserId] = struct{}{}
  136. }
  137. }
  138. sendMsg := make([]*models.SysMessageReport, 0)
  139. for _, v := range sysUserIds {
  140. if userMap, ok := messageMap[v]; ok {
  141. for _, u := range userList {
  142. if _, ok := userMap[u.UserId]; !ok {
  143. sendMsg = append(sendMsg, &models.SysMessageReport{
  144. UserId: u.UserId,
  145. ReceiveSysUserId: v,
  146. MessageType: messagetType,
  147. IsRead: false,
  148. CreateTime: time.Now(),
  149. ModifyTime: time.Now(),
  150. Content: fmt.Sprintf(layout, u.RealName, messagetType),
  151. })
  152. }
  153. }
  154. } else {
  155. for _, u := range userList {
  156. sendMsg = append(sendMsg, &models.SysMessageReport{
  157. UserId: u.UserId,
  158. ReceiveSysUserId: v,
  159. MessageType: messagetType,
  160. IsRead: false,
  161. CreateTime: time.Now(),
  162. ModifyTime: time.Now(),
  163. Content: fmt.Sprintf(layout, u.RealName, messagetType),
  164. })
  165. }
  166. }
  167. }
  168. if len(sendMsg) > 0 {
  169. err = models.InsertMultiSysMessageReport(sendMsg)
  170. }
  171. return
  172. }