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

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

@@ -24,8 +24,7 @@ func Timer() {
fmt.Println("add timer error:", err)
}
// 其他定时任务定在这里 参考上方使用方法
// 定时清理过期VIP用户
_, err = global.GVA_Timer.AddTaskByFunc("ClearVip", "@daily", func() {
err2 := task.CheckVip(global.GVA_DB)
if err2 != nil {
@@ -35,5 +34,29 @@ func Timer() {
if err != nil {
fmt.Println("add timer error:", err)
}
// 定时发布文章
fmt.Println("注册定时任务: PublishArticles")
_, err = global.GVA_Timer.AddTaskByFunc("PublishArticles", "0 0/1 * * * ?", func() {
fmt.Println("执行定时任务: PublishArticles")
err3 := task.PublishArticles(global.GVA_DB)
if err3 != nil {
fmt.Println("定时发布文章失败:", err3)
}
}, "定时发布文章任务,每分钟检查一次", option...)
if err != nil {
fmt.Println("add timer error:", err)
}
// 清理超时订单
_, err = global.GVA_Timer.AddTaskByFunc("ClearExpiredOrders", "0 0/5 * * * ?", func() {
err4 := task.ClearExpiredOrders(global.GVA_DB)
if err4 != nil {
fmt.Println("清理超时订单失败:", err4)
}
}, "定时清理超时订单任务每5分钟检查一次", option...)
if err != nil {
fmt.Println("add timer error:", err)
}
}()
}

View File

@@ -2,6 +2,7 @@ package article
import (
"git.echol.cn/loser/lckt/global"
"time"
)
type Article struct {
@@ -17,6 +18,7 @@ type Article struct {
// 分类ID
CategoryId int `json:"categoryId" gorm:"comment:分类ID"`
Status int `json:"status" gorm:"comment:状态 1-已发布 2-待审核 3-审核不通过;default:2"` // 状态 0-草稿 1-已发布 2-待审核 3-审核不通过
PublishTime *time.Time `json:"publishTime" gorm:"comment:定时发布时间"`
}
// TableName 文章表

View File

@@ -10,6 +10,7 @@ import (
"git.echol.cn/loser/lckt/model/user"
"go.uber.org/zap"
"strings"
"time"
)
type ArticleService struct{}
@@ -217,6 +218,10 @@ func (s ArticleService) BulkUpload(p request.BulkUpload) (err error) {
}
content := "<p><img src=" + a + " alt=\"" + a + "\" data-href=\"\" style=\"width: 100%;height: auto;\"/></p>"
// 将 p.PublishTime转为time.time类型
loc, _ := time.LoadLocation("Asia/Shanghai")
publishTime, _ := time.ParseInLocation("2006-01-02 15:04:05", p.PublishTime, loc)
articles = append(articles, article.Article{
Title: p.Title,
Desc: p.Desc,
@@ -227,6 +232,7 @@ func (s ArticleService) BulkUpload(p request.BulkUpload) (err error) {
Content: content,
IsFree: p.IsFree,
Price: int64(p.Price),
PublishTime: &publishTime,
})
}

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
}