134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
package services
|
|
|
|
import (
|
|
"tyapi-server/internal/domains/article/entities"
|
|
)
|
|
|
|
// AnnouncementService 公告领域服务
|
|
// 处理公告相关的业务逻辑,包括验证、状态管理等
|
|
type AnnouncementService struct{}
|
|
|
|
// NewAnnouncementService 创建公告领域服务
|
|
func NewAnnouncementService() *AnnouncementService {
|
|
return &AnnouncementService{}
|
|
}
|
|
|
|
// ValidateAnnouncement 验证公告
|
|
// 检查公告是否符合业务规则
|
|
func (s *AnnouncementService) ValidateAnnouncement(announcement *entities.Announcement) error {
|
|
// 1. 基础验证
|
|
if err := announcement.Validate(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 2. 业务规则验证
|
|
// 标题不能包含敏感词
|
|
if s.containsSensitiveWords(announcement.Title) {
|
|
return entities.NewValidationError("公告标题包含敏感词")
|
|
}
|
|
|
|
// 内容不能包含敏感词
|
|
if s.containsSensitiveWords(announcement.Content) {
|
|
return entities.NewValidationError("公告内容包含敏感词")
|
|
}
|
|
|
|
// 标题长度验证
|
|
if len(announcement.Title) > 200 {
|
|
return entities.NewValidationError("公告标题不能超过200个字符")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CanPublish 检查是否可以发布
|
|
func (s *AnnouncementService) CanPublish(announcement *entities.Announcement) error {
|
|
if announcement.Status == entities.AnnouncementStatusPublished {
|
|
return entities.NewValidationError("公告已经是发布状态")
|
|
}
|
|
|
|
if announcement.Status == entities.AnnouncementStatusArchived {
|
|
return entities.NewValidationError("已归档的公告不能发布")
|
|
}
|
|
|
|
// 检查必填字段
|
|
if announcement.Title == "" {
|
|
return entities.NewValidationError("公告标题不能为空")
|
|
}
|
|
if announcement.Content == "" {
|
|
return entities.NewValidationError("公告内容不能为空")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CanEdit 检查是否可以编辑
|
|
func (s *AnnouncementService) CanEdit(announcement *entities.Announcement) error {
|
|
if announcement.Status == entities.AnnouncementStatusPublished {
|
|
return entities.NewValidationError("已发布的公告不能编辑,请先撤回")
|
|
}
|
|
|
|
if announcement.Status == entities.AnnouncementStatusArchived {
|
|
return entities.NewValidationError("已归档的公告不能编辑")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CanArchive 检查是否可以归档
|
|
func (s *AnnouncementService) CanArchive(announcement *entities.Announcement) error {
|
|
if announcement.Status != entities.AnnouncementStatusPublished {
|
|
return entities.NewValidationError("只有已发布的公告才能归档")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CanWithdraw 检查是否可以撤回
|
|
func (s *AnnouncementService) CanWithdraw(announcement *entities.Announcement) error {
|
|
if announcement.Status != entities.AnnouncementStatusPublished {
|
|
return entities.NewValidationError("只有已发布的公告才能撤回")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CanSchedulePublish 检查是否可以定时发布
|
|
func (s *AnnouncementService) CanSchedulePublish(announcement *entities.Announcement, scheduledTime interface{}) error {
|
|
if announcement.Status == entities.AnnouncementStatusPublished {
|
|
return entities.NewValidationError("已发布的公告不能设置定时发布")
|
|
}
|
|
|
|
if announcement.Status == entities.AnnouncementStatusArchived {
|
|
return entities.NewValidationError("已归档的公告不能设置定时发布")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// containsSensitiveWords 检查是否包含敏感词
|
|
func (s *AnnouncementService) containsSensitiveWords(text string) bool {
|
|
// TODO: 实现敏感词检查逻辑
|
|
// 这里可以集成敏感词库或调用外部服务
|
|
sensitiveWords := []string{
|
|
"敏感词1",
|
|
"敏感词2",
|
|
"敏感词3",
|
|
}
|
|
|
|
for _, word := range sensitiveWords {
|
|
if len(word) > 0 && len(text) > 0 {
|
|
// 简单的字符串包含检查
|
|
// 实际项目中应该使用更复杂的算法
|
|
if len(text) >= len(word) {
|
|
for i := 0; i <= len(text)-len(word); i++ {
|
|
if text[i:i+len(word)] == word {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|