From a9b681606428aee6b5c06e7b920f941836895887 Mon Sep 17 00:00:00 2001 From: Echo <1711788888@qq.com> Date: Sat, 13 Sep 2025 00:44:52 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=EF=BC=8C=E4=BC=98=E5=8C=96=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=96=87=E7=AB=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- initialize/timer.go | 27 +++++++++++++++++++++++++-- model/article/article.go | 6 ++++-- service/article/article.go | 6 ++++++ task/checkVip.go | 2 ++ task/clearOrder.go | 24 ++++++++++++++++++++++++ task/publishArticcles.go | 24 ++++++++++++++++++++++++ 6 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 task/clearOrder.go create mode 100644 task/publishArticcles.go diff --git a/initialize/timer.go b/initialize/timer.go index 13b6b6e..2b90a6d 100644 --- a/initialize/timer.go +++ b/initialize/timer.go @@ -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) + } }() } diff --git a/model/article/article.go b/model/article/article.go index 6e48e34..8a8f945 100644 --- a/model/article/article.go +++ b/model/article/article.go @@ -2,6 +2,7 @@ package article import ( "git.echol.cn/loser/lckt/global" + "time" ) type Article struct { @@ -15,8 +16,9 @@ type Article struct { Price int64 `json:"price" gorm:"comment:文章价格(单位为分)"` IsFree *int `json:"isFree" gorm:"comment:是否免费;default:0"` // 是否免费 0-否 1-是 // 分类ID - CategoryId int `json:"categoryId" gorm:"comment:分类ID"` - Status int `json:"status" gorm:"comment:状态 1-已发布 2-待审核 3-审核不通过;default:2"` // 状态 0-草稿 1-已发布 2-待审核 3-审核不通过 + 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 文章表 diff --git a/service/article/article.go b/service/article/article.go index f26595b..de3cb96 100644 --- a/service/article/article.go +++ b/service/article/article.go @@ -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.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, }) } diff --git a/task/checkVip.go b/task/checkVip.go index c179985..b41ca51 100644 --- a/task/checkVip.go +++ b/task/checkVip.go @@ -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) diff --git a/task/clearOrder.go b/task/clearOrder.go new file mode 100644 index 0000000..44cd5fd --- /dev/null +++ b/task/clearOrder.go @@ -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 +} diff --git a/task/publishArticcles.go b/task/publishArticcles.go new file mode 100644 index 0000000..1310b7f --- /dev/null +++ b/task/publishArticcles.go @@ -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 +}