迁移公告,邮件

This commit is contained in:
2026-08-13 22:04:27 +08:00
parent 21c264fcd7
commit 9044bafdff
18 changed files with 946 additions and 28 deletions
+100
View File
@@ -0,0 +1,100 @@
package biz
import (
"context"
"encoding/json"
"errors"
"time"
)
type Announcement struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
Title string
Content string
UserID *uint
Attachments json.RawMessage
}
type AnnouncementFilter struct {
Page, PageSize int
StartCreatedAt, EndCreatedAt *time.Time
}
type AnnouncementRepo interface {
Create(context.Context, *Announcement) error
Delete(context.Context, uint) error
DeleteByIDs(context.Context, []uint) error
Update(context.Context, *Announcement) error
Find(context.Context, uint) (*Announcement, error)
List(context.Context, AnnouncementFilter) ([]*Announcement, int64, error)
UserOptions(context.Context) ([]UserOption, error)
}
type UserOption struct {
Label string
Value uint
}
type AnnouncementUsecase struct{ repo AnnouncementRepo }
func NewAnnouncementUsecase(repo AnnouncementRepo) *AnnouncementUsecase {
return &AnnouncementUsecase{repo: repo}
}
func validateAnnouncement(item *Announcement) error {
if item == nil || item.Title == "" {
return errors.New("公告标题不能为空")
}
if len(item.Attachments) == 0 || !json.Valid(item.Attachments) {
item.Attachments = json.RawMessage("[]")
}
return nil
}
func (uc *AnnouncementUsecase) Create(ctx context.Context, item *Announcement) error {
if err := validateAnnouncement(item); err != nil {
return err
}
return uc.repo.Create(ctx, item)
}
func (uc *AnnouncementUsecase) Update(ctx context.Context, item *Announcement) error {
if item == nil || item.ID == 0 {
return errors.New("公告ID不能为空")
}
if err := validateAnnouncement(item); err != nil {
return err
}
return uc.repo.Update(ctx, item)
}
func (uc *AnnouncementUsecase) Delete(ctx context.Context, id uint) error {
if id == 0 {
return errors.New("公告ID不能为空")
}
return uc.repo.Delete(ctx, id)
}
func (uc *AnnouncementUsecase) DeleteByIDs(ctx context.Context, ids []uint) error {
if len(ids) == 0 {
return errors.New("公告ID不能为空")
}
return uc.repo.DeleteByIDs(ctx, ids)
}
func (uc *AnnouncementUsecase) Find(ctx context.Context, id uint) (*Announcement, error) {
if id == 0 {
return nil, errors.New("公告ID不能为空")
}
return uc.repo.Find(ctx, id)
}
func (uc *AnnouncementUsecase) List(ctx context.Context, filter AnnouncementFilter) ([]*Announcement, int64, error) {
return uc.repo.List(ctx, filter)
}
func (uc *AnnouncementUsecase) UserOptions(ctx context.Context) ([]UserOption, error) {
return uc.repo.UserOptions(ctx)
}
+1 -1
View File
@@ -3,4 +3,4 @@ package biz
import "github.com/google/wire"
// ProviderSet is biz providers.
var ProviderSet = wire.NewSet(NewAdminUsecase, NewSystemUsecase, NewAccessUsecase, NewSettingsUsecase, NewVersionUsecase, NewExportUsecase, NewAuditUsecase, NewTaskUsecase, NewMediaUsecase)
var ProviderSet = wire.NewSet(NewAdminUsecase, NewSystemUsecase, NewAccessUsecase, NewSettingsUsecase, NewVersionUsecase, NewExportUsecase, NewAuditUsecase, NewTaskUsecase, NewMediaUsecase, NewAnnouncementUsecase, NewEmailUsecase)
+55
View File
@@ -0,0 +1,55 @@
package biz
import (
"context"
"errors"
"strings"
)
type EmailRepo interface {
Send(context.Context, []string, string, string) error
DefaultRecipients() []string
Enabled() bool
}
type EmailUsecase struct{ repo EmailRepo }
func NewEmailUsecase(repo EmailRepo) *EmailUsecase { return &EmailUsecase{repo: repo} }
func splitRecipients(value string) []string {
parts := strings.Split(value, ",")
result := make([]string, 0, len(parts))
for _, part := range parts {
if recipient := strings.TrimSpace(part); recipient != "" {
result = append(result, recipient)
}
}
return result
}
func (uc *EmailUsecase) Send(ctx context.Context, to, subject, body string) error {
recipients := splitRecipients(to)
if len(recipients) == 0 || subject == "" {
return errors.New("收件人和邮件标题不能为空")
}
return uc.repo.Send(ctx, recipients, subject, body)
}
func (uc *EmailUsecase) Test(ctx context.Context) error {
recipients := uc.repo.DefaultRecipients()
if len(recipients) == 0 {
return errors.New("未配置测试邮件收件人")
}
return uc.repo.Send(ctx, recipients, "test", "test")
}
func (uc *EmailUsecase) Alert(ctx context.Context, subject, body string) error {
if !uc.repo.Enabled() {
return nil
}
recipients := uc.repo.DefaultRecipients()
if len(recipients) == 0 {
return nil
}
return uc.repo.Send(ctx, recipients, subject, body)
}