zqbao 9 月之前
父节点
当前提交
8f16716ff0
共有 1 个文件被更改,包括 170 次插入117 次删除
  1. 170 117
      scheduler/task.go

+ 170 - 117
scheduler/task.go

@@ -1,62 +1,49 @@
 package scheduler
 
 import (
+	"context"
 	"eta/eta_mini_crm/models"
 	"eta/eta_mini_crm/utils"
 	"fmt"
 	"time"
 
-	"github.com/rdlucklib/rdluck_tools/paging"
-	"github.com/robfig/cron/v3"
+	"github.com/beego/beego/v2/task"
 )
 
 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())
-	}
+	// tk1 := task.NewTask("CheckUserPermission", "0 0 1 * * *", CheckUserTime)
+	// task.AddTask("CheckUserPermission", tk1)
 	// 每天凌晨1点检测, 修改用户状态
-	_, err = c.AddFunc("0 0 1 * * *", ModifyUser)
-	if err != nil {
-		utils.ApiLog.Info("cron ModifyUser task err: %s", err.Error())
-	}
-	c.Start()
+	tk2 := task.NewTask("ModifyUserStatus", "0 0 1 * * *", ModifyUser)
+	task.AddTask("ModifyUserStatus", tk2)
+	task.StartTask()
+	defer task.StopTask()
 }
 
-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 CheckUserTime(ctx context.Context) (err error) {
+// 	defer func() {
+// 		if err != nil {
+// 			utils.ApiLog.Warn("userPermission 定时任务出错,Err: %s", err.Error())
+// 		}
+// 	}()
+// 	// err = CheckUserPermissionTime()
+// 	return
+// }
 
-func ModifyUser() {
-	retryCount := 10
-	var err error
+func ModifyUser(ctx context.Context) (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
-		}
-	}
+	curTime := time.Now()
+	var pars []interface{}
+	condition := ` AND valid_end_time<? AND status=? `
+	pars = append(pars, curTime, 2)
+	err = models.UpdateUserStatus(condition, pars)
+	return
 }
 
 func ModifyUserStatus() (err error) {
@@ -70,85 +57,151 @@ func ModifyUserStatus() (err error) {
 	return
 }
 
-func CheckUserPermissionTime() (err error) {
-	var condition string
-	var pars []interface{}
+// func CheckUserPermissionTime() (err error) {
+// 	var condition string
+// 	var pars7 []interface{}
+// 	var pars15 []interface{}
+// 	var pars30 []interface{}
+// 	var pars60 []interface{}
 
-	var userTotal int
-	pageSize := 500
+// 	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
-}
+// 	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)
+
+// 	condition7 := ` AND valid_end_time<? `
+// 	pars7 = append(pars7, time7)
+// 	condition15 := `valid_end_time>? AND valid_end_time<?`
+
+// 	models.GetUserCount(condition7, pars7)
+
+// 	// 批量查询到期的用户
+// 	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
+// }
+
+// func GetSendUser(condition string, pars []interface{}, messagetType int) (user []*models.UserView, err error) {
+// 	total, err := models.GetUserCount(condition, pars)
+// 	if err != nil {
+// 		utils.ApiLog.Warn("获取用户列表失败 task err: %s", err.Error())
+// 	}
+// 	if total == 0 {
+// 		return
+// 	}
+// 	// 获得待发送用户的列表
+// 	userList, err := models.GetUserList(condition, pars, 0, total)
+// 	if err != nil {
+// 		utils.ApiLog.Warn("获取用户列表失败 task err: %s", err.Error())
+// 		return
+// 	}
+
+// 	// 获得待发送系统用户的列表
+// 	sysUserIds, err := models.GetSysUserIdList()
+// 	if err != nil {
+// 		utils.ApiLog.Warn("userPermission task err: %s", err.Error())
+// 	}
+
+// 	messageList, err := models.GetSysMessageReportByCondition(condition, pars)
+
+// 	layout := "【%s】到期%d天提醒,请及时跟进"
+// 	messageList := []models.SysMessageReport{}
+// 	for _, v := range sysUserIds {
+// 		count, err := models.GetSysMessageReportCount(v, messagetType)
+// 		if err != nil {
+// 			continue
+// 		}
+// 		if count > 0 {
+// 			continue
+// 		}
+// 		user, err := models.GetUserById(v)
+// 		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
+// }