feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -4,6 +4,7 @@ import "time"
|
||||
|
||||
type AnnouncementDTO struct {
|
||||
ID uint64 `json:"id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
DistributionSiteID uint64 `json:"distribution_site_id"`
|
||||
DistributionSiteName string `json:"distribution_site_name,omitempty"`
|
||||
Title string `json:"title"`
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -49,6 +50,7 @@ func (r *Repository) ResolvePublicSiteScope(ctx context.Context, siteCode string
|
||||
func (r *Repository) List(ctx context.Context, query AnnouncementListQuery) (*PaginatedResult, error) {
|
||||
var total int64
|
||||
tx := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("status = ?", "published")
|
||||
tx = tenantScope(ctx, tx)
|
||||
|
||||
if len(query.AllowedSiteIDs) > 0 {
|
||||
tx = tx.Where("distribution_site_id IN ?", query.AllowedSiteIDs)
|
||||
@@ -91,6 +93,7 @@ func (r *Repository) List(ctx context.Context, query AnnouncementListQuery) (*Pa
|
||||
func (r *Repository) PublicPopup(ctx context.Context, allowedSiteIDs []uint64) (*AnnouncementDTO, error) {
|
||||
query := r.db.WithContext(ctx).
|
||||
Where("status = ? AND is_popup = ?", "published", true)
|
||||
query = tenantScope(ctx, query)
|
||||
if len(allowedSiteIDs) > 0 {
|
||||
query = query.Where("distribution_site_id IN ?", allowedSiteIDs)
|
||||
}
|
||||
@@ -114,6 +117,7 @@ func (r *Repository) GetByID(ctx context.Context, id uint64, allowedSiteIDs []ui
|
||||
var announcement model.Announcement
|
||||
db := r.db.WithContext(ctx)
|
||||
query := db.Where("id = ? AND status = ?", id, "published")
|
||||
query = tenantScope(ctx, query)
|
||||
if len(allowedSiteIDs) > 0 {
|
||||
query = query.Where("distribution_site_id IN ?", allowedSiteIDs)
|
||||
}
|
||||
@@ -163,6 +167,9 @@ func (r *Repository) adminListBase(ctx context.Context, query AnnouncementListQu
|
||||
base := r.db.WithContext(ctx).Table("announcements AS a").
|
||||
Select("a.id, a.distribution_site_id, COALESCE(s.site_name, '') AS distribution_site_name, a.title, a.content, a.category, a.priority, a.is_pinned, a.is_important, a.is_popup, a.view_count, a.status, a.published_at, a.created_at, a.updated_at").
|
||||
Joins("LEFT JOIN distribution_sites AS s ON s.id = a.distribution_site_id")
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
base = base.Where("a.tenant_id = ?", tenantID)
|
||||
}
|
||||
|
||||
if query.Status != "" {
|
||||
base = base.Where("a.status = ?", query.Status)
|
||||
@@ -193,6 +200,7 @@ func (r *Repository) AdminGetByID(ctx context.Context, id uint64) (*Announcement
|
||||
// Create 创建公告
|
||||
func (r *Repository) Create(ctx context.Context, req CreateAnnouncementRequest, createdBy uint64) (*AnnouncementDTO, error) {
|
||||
announcement := model.Announcement{
|
||||
TenantID: tenancy.IDFromContext(ctx),
|
||||
DistributionSiteID: req.DistributionSiteID,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
@@ -231,7 +239,11 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateAnnounceme
|
||||
updates["is_important"] = req.IsImportant
|
||||
updates["is_popup"] = req.IsPopup
|
||||
|
||||
if err := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
||||
query := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", id)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
if err := query.Updates(updates).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -241,8 +253,11 @@ func (r *Repository) Update(ctx context.Context, id uint64, req UpdateAnnounceme
|
||||
// Publish 发布公告
|
||||
func (r *Repository) Publish(ctx context.Context, id uint64) error {
|
||||
now := time.Now()
|
||||
return r.db.WithContext(ctx).Model(&model.Announcement{}).
|
||||
Where("id = ?", id).
|
||||
query := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", id)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.
|
||||
Updates(map[string]interface{}{
|
||||
"status": "published",
|
||||
"published_at": now,
|
||||
@@ -251,16 +266,22 @@ func (r *Repository) Publish(ctx context.Context, id uint64) error {
|
||||
|
||||
// Archive 归档公告
|
||||
func (r *Repository) Archive(ctx context.Context, id uint64) error {
|
||||
return r.db.WithContext(ctx).Model(&model.Announcement{}).
|
||||
Where("id = ?", id).
|
||||
query := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", id)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.
|
||||
Update("status", "archived").Error
|
||||
}
|
||||
|
||||
// Unarchive 取消归档公告,恢复为已发布状态
|
||||
func (r *Repository) Unarchive(ctx context.Context, id uint64) error {
|
||||
now := time.Now()
|
||||
return r.db.WithContext(ctx).Model(&model.Announcement{}).
|
||||
Where("id = ?", id).
|
||||
query := r.db.WithContext(ctx).Model(&model.Announcement{}).Where("id = ?", id)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.
|
||||
Updates(map[string]interface{}{
|
||||
"status": "published",
|
||||
"published_at": gorm.Expr("COALESCE(published_at, ?)", now),
|
||||
@@ -269,12 +290,17 @@ func (r *Repository) Unarchive(ctx context.Context, id uint64) error {
|
||||
|
||||
// Delete 删除公告
|
||||
func (r *Repository) Delete(ctx context.Context, id uint64) error {
|
||||
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&model.Announcement{}).Error
|
||||
query := r.db.WithContext(ctx).Where("id = ?", id)
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
query = query.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return query.Delete(&model.Announcement{}).Error
|
||||
}
|
||||
|
||||
func toAnnouncementDTO(a model.Announcement) AnnouncementDTO {
|
||||
return AnnouncementDTO{
|
||||
ID: a.ID,
|
||||
TenantID: a.TenantID,
|
||||
DistributionSiteID: a.DistributionSiteID,
|
||||
Title: a.Title,
|
||||
Content: a.Content,
|
||||
@@ -290,3 +316,10 @@ func toAnnouncementDTO(a model.Announcement) AnnouncementDTO {
|
||||
UpdatedAt: a.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func tenantScope(ctx context.Context, db *gorm.DB) *gorm.DB {
|
||||
if tenantID := tenancy.IDFromContext(ctx); tenantID > 0 {
|
||||
return db.Where("tenant_id = ?", tenantID)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user