package entities import ( "time" "github.com/google/uuid" "gorm.io/gorm" ) // AnnouncementStatus 公告状态枚举 type AnnouncementStatus string const ( AnnouncementStatusDraft AnnouncementStatus = "draft" // 草稿 AnnouncementStatusPublished AnnouncementStatus = "published" // 已发布 AnnouncementStatusArchived AnnouncementStatus = "archived" // 已归档 ) // Announcement 公告聚合根 // 用于对系统公告进行管理,支持发布、撤回、定时发布等功能 type Announcement struct { // 基础标识 ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"公告唯一标识"` Title string `gorm:"type:varchar(200);not null;index" json:"title" comment:"公告标题"` Content string `gorm:"type:text;not null" json:"content" comment:"公告内容"` Status AnnouncementStatus `gorm:"type:varchar(20);not null;default:'draft';index" json:"status" comment:"公告状态"` ScheduledAt *time.Time `gorm:"index" json:"scheduled_at" comment:"定时发布时间"` CreatedAt time.Time `gorm:"autoCreateTime;index" json:"created_at" comment:"创建时间"` UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"` DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"` } // TableName 指定表名 func (Announcement) TableName() string { return "announcements" } // BeforeCreate GORM钩子:创建前自动生成UUID func (a *Announcement) BeforeCreate(tx *gorm.DB) error { if a.ID == "" { a.ID = uuid.New().String() } return nil } // 实现 Entity 接口 - 提供统一的实体管理接口 // GetID 获取实体唯一标识 func (a *Announcement) GetID() string { return a.ID } // GetCreatedAt 获取创建时间 func (a *Announcement) GetCreatedAt() time.Time { return a.CreatedAt } // GetUpdatedAt 获取更新时间 func (a *Announcement) GetUpdatedAt() time.Time { return a.UpdatedAt } // 验证公告信息 func (a *Announcement) Validate() error { if a.Title == "" { return NewValidationError("公告标题不能为空") } if a.Content == "" { return NewValidationError("公告内容不能为空") } return nil } // 发布公告 func (a *Announcement) Publish() error { if a.Status == AnnouncementStatusPublished { return NewValidationError("公告已经是发布状态") } a.Status = AnnouncementStatusPublished now := time.Now() a.CreatedAt = now return nil } // 撤回公告 func (a *Announcement) Withdraw() error { if a.Status == AnnouncementStatusDraft { return NewValidationError("公告已经是草稿状态") } a.Status = AnnouncementStatusDraft now := time.Now() a.CreatedAt = now return nil } // 定时发布公告 func (a *Announcement) SchedulePublish(scheduledTime time.Time) error { if a.Status == AnnouncementStatusPublished { return NewValidationError("公告已经是发布状态") } a.Status = AnnouncementStatusDraft // 保持草稿状态,等待定时发布 a.ScheduledAt = &scheduledTime return nil } // 更新定时发布时间 func (a *Announcement) UpdateSchedulePublish(scheduledTime time.Time) error { if a.Status == AnnouncementStatusPublished { return NewValidationError("公告已经是发布状态") } if scheduledTime.Before(time.Now()) { return NewValidationError("定时发布时间不能早于当前时间") } a.ScheduledAt = &scheduledTime return nil } // CancelSchedulePublish 取消定时发布 func (a *Announcement) CancelSchedulePublish() error { if a.Status == AnnouncementStatusPublished { return NewValidationError("公告已经是发布状态") } a.ScheduledAt = nil return nil } // IsScheduled 判断是否已设置定时发布 func (a *Announcement) IsScheduled() bool { return a.ScheduledAt != nil && a.Status == AnnouncementStatusDraft } // GetScheduledTime 获取定时发布时间 func (a *Announcement) GetScheduledTime() *time.Time { return a.ScheduledAt }