add timed
This commit is contained in:
@@ -35,6 +35,7 @@ type Article struct {
|
||||
IsFeatured bool `gorm:"default:false" json:"is_featured" comment:"是否推荐"`
|
||||
PublishedAt *time.Time `json:"published_at" comment:"发布时间"`
|
||||
ScheduledAt *time.Time `json:"scheduled_at" comment:"定时发布时间"`
|
||||
TaskID string `gorm:"type:varchar(100)" json:"task_id" comment:"定时任务ID"`
|
||||
|
||||
// 统计信息
|
||||
ViewCount int `gorm:"default:0" json:"view_count" comment:"阅读量"`
|
||||
@@ -119,7 +120,7 @@ func (a *Article) Publish() error {
|
||||
}
|
||||
|
||||
// SchedulePublish 定时发布文章
|
||||
func (a *Article) SchedulePublish(scheduledTime time.Time) error {
|
||||
func (a *Article) SchedulePublish(scheduledTime time.Time, taskID string) error {
|
||||
if a.Status == ArticleStatusPublished {
|
||||
return NewValidationError("文章已经是发布状态")
|
||||
}
|
||||
@@ -130,6 +131,35 @@ func (a *Article) SchedulePublish(scheduledTime time.Time) error {
|
||||
|
||||
a.Status = ArticleStatusDraft // 保持草稿状态,等待定时发布
|
||||
a.ScheduledAt = &scheduledTime
|
||||
a.TaskID = taskID
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateSchedulePublish 更新定时发布时间
|
||||
func (a *Article) UpdateSchedulePublish(scheduledTime time.Time, taskID string) error {
|
||||
if a.Status == ArticleStatusPublished {
|
||||
return NewValidationError("文章已经是发布状态")
|
||||
}
|
||||
|
||||
if scheduledTime.Before(time.Now()) {
|
||||
return NewValidationError("定时发布时间不能早于当前时间")
|
||||
}
|
||||
|
||||
a.ScheduledAt = &scheduledTime
|
||||
a.TaskID = taskID
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CancelSchedulePublish 取消定时发布
|
||||
func (a *Article) CancelSchedulePublish() error {
|
||||
if a.Status == ArticleStatusPublished {
|
||||
return NewValidationError("文章已经是发布状态")
|
||||
}
|
||||
|
||||
a.ScheduledAt = nil
|
||||
a.TaskID = ""
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user