feat(tenant): 隔离客服通知文件与后台任务
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -16,6 +17,7 @@ type Repository struct {
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
TenantID uint64
|
||||
UserID uint64
|
||||
Type string
|
||||
Title string
|
||||
@@ -62,6 +64,7 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
var total int64
|
||||
@@ -71,6 +74,7 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []model.Notification
|
||||
dataQuery := r.db.WithContext(ctx).Where("user_id = ?", userID)
|
||||
dataQuery = tenantFilter(ctx, dataQuery)
|
||||
dataQuery = r.applySiteFilter(dataQuery, siteID, isSite)
|
||||
if err := dataQuery.Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
@@ -94,6 +98,7 @@ func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64, sit
|
||||
|
||||
now := time.Now()
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
result := query.Update("read_at", now)
|
||||
@@ -102,6 +107,7 @@ func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64, sit
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
checkQuery := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
||||
checkQuery = tenantFilter(ctx, checkQuery)
|
||||
checkQuery = r.applySiteFilter(checkQuery, siteID, isSite)
|
||||
var total int64
|
||||
if err := checkQuery.Count(&total).Error; err != nil {
|
||||
@@ -125,6 +131,7 @@ func (r *Repository) UnreadCount(ctx context.Context, userID uint64, siteCode ..
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
var total int64
|
||||
@@ -144,6 +151,7 @@ func (r *Repository) MarkAllRead(ctx context.Context, userID uint64, siteCode ..
|
||||
|
||||
now := time.Now()
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
||||
query = tenantFilter(ctx, query)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
result := query.Update("read_at", now)
|
||||
@@ -155,6 +163,13 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
if entry.UserID == 0 || entry.Title == "" {
|
||||
continue
|
||||
}
|
||||
tenantID := entry.TenantID
|
||||
if tenantID == 0 {
|
||||
var user model.User
|
||||
if err := tx.Select("tenant_id").First(&user, entry.UserID).Error; err == nil {
|
||||
tenantID = user.TenantID
|
||||
}
|
||||
}
|
||||
siteID := entry.DistributionSiteID
|
||||
if siteID == nil {
|
||||
if (entry.BizType == "order" || entry.BizType == "order_handoff") && entry.BizID != nil && *entry.BizID > 0 {
|
||||
@@ -174,6 +189,7 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
}
|
||||
}
|
||||
row := model.Notification{
|
||||
TenantID: tenantID,
|
||||
UserID: entry.UserID,
|
||||
Type: entry.Type,
|
||||
Title: entry.Title,
|
||||
@@ -195,6 +211,7 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
func toDTO(row model.Notification) NotificationDTO {
|
||||
return NotificationDTO{
|
||||
ID: row.ID,
|
||||
TenantID: row.TenantID,
|
||||
UserID: row.UserID,
|
||||
Type: row.Type,
|
||||
Title: row.Title,
|
||||
@@ -206,3 +223,10 @@ func toDTO(row model.Notification) NotificationDTO {
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func tenantFilter(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