🎨 优化定时任务,优化定时发布文章功能

This commit is contained in:
2025-09-13 00:44:52 +08:00
parent f9b37fb1aa
commit a9b6816064
6 changed files with 85 additions and 4 deletions

View File

@@ -1,12 +1,14 @@
package task
import (
"git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/user"
"gorm.io/gorm"
)
// CheckVip 检查用户VIP是否过期
func CheckVip(db *gorm.DB) error {
global.GVA_LOG.Info("开始检查用户VIP是否过期...")
var users []user.User
// 根据当前时间和vip_expire_time对比 查看是否到过期时间
db.Where("vip_expire_time < ? AND vip_expire_time IS NOT NULL", gorm.Expr("NOW()")).Find(&users)

24
task/clearOrder.go Normal file
View File

@@ -0,0 +1,24 @@
package task
import (
"git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/app"
"go.uber.org/zap"
"gorm.io/gorm"
"time"
)
// ClearExpiredOrders 定时清理超时订单5分钟过期
func ClearExpiredOrders(db *gorm.DB) error {
global.GVA_LOG.Info("开始清理超时订单...")
fiveMinutesAgo := time.Now().Add(-5 * time.Minute)
result := db.Model(&app.Order{}).
Where("status = 1 AND created_at < ?", fiveMinutesAgo).
Updates(map[string]interface{}{"status": 3})
if result.Error != nil {
global.GVA_LOG.Error("清理超时订单失败", zap.Error(result.Error))
return result.Error
}
global.GVA_LOG.Info("清理超时订单成功", zap.Int64("处理订单数", result.RowsAffected))
return nil
}

24
task/publishArticcles.go Normal file
View File

@@ -0,0 +1,24 @@
package task
import (
"git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/article"
"go.uber.org/zap"
"gorm.io/gorm"
"time"
)
// PublishArticles 定时发布文章
func PublishArticles(db *gorm.DB) error {
global.GVA_LOG.Info("开始执行定时发布文章任务")
now := time.Now()
result := db.Model(&article.Article{}).
Where("publish_time <= ? AND status != 1", now).
Updates(map[string]interface{}{"status": 1})
if result.Error != nil {
global.GVA_LOG.Error("定时发布文章失败", zap.Error(result.Error))
return result.Error
}
global.GVA_LOG.Info("定时发布文章成功", zap.Int64("处理文章数", result.RowsAffected))
return nil
}