123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package scheduler
- import (
- "eta/eta_mini_crm/models"
- "eta/eta_mini_crm/utils"
- "fmt"
- "time"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/robfig/cron/v3"
- )
- func InitJob() {
- fmt.Println("消息推送任务开启。。。")
- c := cron.New(cron.WithSeconds())
- // 每天凌晨1点检测, 发送消息
- _, err := c.AddFunc("0 0 1 * * *", CheckUserTime)
- if err != nil {
- utils.ApiLog.Info("cron CheckUserTime task err: %s", err.Error())
- }
- // 每天凌晨1点检测, 修改用户状态
- _, err = c.AddFunc("0 0 1 * * *", ModifyUser)
- if err != nil {
- utils.ApiLog.Info("cron ModifyUser task err: %s", err.Error())
- }
- c.Start()
- }
- func CheckUserTime() {
- retryCount := 10
- var err error
- defer func() {
- if err != nil {
- utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
- }
- }()
- for i := 0; i <= retryCount; i++ {
- err = CheckUserPermissionTime()
- if err == nil {
- break
- }
- }
- }
- func ModifyUser() {
- retryCount := 10
- var err error
- defer func() {
- if err != nil {
- utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
- }
- }()
- for i := 0; i <= retryCount; i++ {
- err = ModifyUserStatus()
- if err == nil {
- break
- }
- }
- }
- func ModifyUserStatus() (err error) {
- curTime := time.Now()
- condition := ` AND valid_end_time<?`
- var pars []interface{}
- pars = append(pars, curTime)
- condition += ` AND status=? `
- pars = append(pars, 2)
- err = models.UpdateUserStatus(condition, pars)
- return
- }
- func CheckUserPermissionTime() (err error) {
- var condition string
- var pars []interface{}
- var userTotal int
- pageSize := 500
- curTime := time.Now()
- time7 := time.Now().AddDate(0, 0, 7)
- time15 := time.Now().AddDate(0, 0, 15)
- time30 := time.Now().AddDate(0, 0, 30)
- time60 := time.Now().AddDate(0, 0, 60)
- // 批量查询到期的用户
- messageMap := make(map[int]int)
- userTotal, err = models.GetUserCount(condition, pars)
- if err != nil {
- utils.ApiLog.Warn("userPermission task err: %s", err.Error())
- }
- pageCount := paging.PageCount(userTotal, pageSize)
- for i := 1; i <= pageCount; i++ {
- startPage := utils.StartIndex(i, pageSize)
- userList, err := models.GetUserList(condition, pars, startPage, pageSize)
- if err != nil {
- utils.ApiLog.Warn("userPermission task err: %s", err.Error())
- }
- for _, user := range userList {
- if user.Status != 2 {
- continue
- }
- if time60.After(user.ValidEndTime) {
- messageMap[user.UserId] = 60
- }
- if time30.After(user.ValidEndTime) {
- messageMap[user.UserId] = 30
- }
- if time15.After(user.ValidEndTime) {
- messageMap[user.UserId] = 15
- }
- if time7.After(user.ValidEndTime) {
- messageMap[user.UserId] = 7
- }
- }
- }
- // 把消息发送到系统用户中
- sysUserIds, err := models.GetSysUserIdList()
- if err != nil {
- utils.ApiLog.Warn("userPermission task err: %s", err.Error())
- }
- layout := "【%s】到期%d天提醒,请及时跟进"
- messageList := []models.SysMessageReport{}
- for k, v := range messageMap {
- count, err := models.GetSysMessageReportCount(k, v)
- if err != nil {
- continue
- }
- if count > 0 {
- continue
- }
- user, err := models.GetUserById(k)
- if err != nil {
- continue
- }
- for _, sysUserId := range sysUserIds {
- messageList = append(messageList, models.SysMessageReport{
- UserId: k,
- ReceiveSysUserId: sysUserId,
- MessageType: v,
- IsRead: false,
- CreateTime: curTime,
- ModifyTime: curTime,
- Content: fmt.Sprintf(layout, user.RealName, v),
- })
- }
- err = models.InsertMultiSysMessageReport(messageList)
- if err != nil {
- continue
- }
- }
- return
- }
|