task.go 5.7 KB

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