feat(distribution): 站内信与通知体系多租户独立分支隔离
- 新增 000071 数据库迁移:notifications 表支持 distribution_site_id 并自动回填历史分销订单通知 - 通知自动归属:在事务内根据订单或申诉自动关联当前分销站点 ID - 隔离查询与已读控制:列表、未读统计与批量已读均严格按站点隔离,防止主站与分站交叉泄漏 - 前台界面联动:消息中心通知 Tab 及移动端/PC端站内信页面展示分站专属标识与精准过滤
This commit is contained in:
@@ -2,6 +2,8 @@ package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -14,26 +16,63 @@ type Repository struct {
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
UserID uint64
|
||||
Type string
|
||||
Title string
|
||||
Content string
|
||||
BizType string
|
||||
BizID *uint64
|
||||
UserID uint64
|
||||
Type string
|
||||
Title string
|
||||
Content string
|
||||
BizType string
|
||||
BizID *uint64
|
||||
DistributionSiteID *uint64
|
||||
}
|
||||
|
||||
func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (r *Repository) resolveSite(ctx context.Context, siteCode string) (uint64, bool, error) {
|
||||
siteCode = strings.TrimSpace(siteCode)
|
||||
if siteCode == "" {
|
||||
return 0, false, nil
|
||||
}
|
||||
var site model.DistributionSite
|
||||
if err := r.db.WithContext(ctx).Where("site_code = ? AND status = 'active'", siteCode).First(&site).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, false, nil
|
||||
}
|
||||
return 0, false, err
|
||||
}
|
||||
return site.ID, true, nil
|
||||
}
|
||||
|
||||
func (r *Repository) applySiteFilter(db *gorm.DB, siteID uint64, isSite bool) *gorm.DB {
|
||||
if isSite && siteID > 0 {
|
||||
return db.Where("distribution_site_id = ?", siteID)
|
||||
}
|
||||
return db.Where("distribution_site_id IS NULL OR distribution_site_id = 0")
|
||||
}
|
||||
|
||||
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
siteID, isSite, err := r.resolveSite(ctx, code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
var total int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []model.Notification
|
||||
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
||||
dataQuery := r.db.WithContext(ctx).Where("user_id = ?", userID)
|
||||
dataQuery = r.applySiteFilter(dataQuery, siteID, isSite)
|
||||
if err := dataQuery.Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]NotificationDTO, 0, len(rows))
|
||||
@@ -43,19 +82,29 @@ func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int
|
||||
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64) error {
|
||||
func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64, siteCode ...string) error {
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
siteID, isSite, err := r.resolveSite(ctx, code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
result := r.db.WithContext(ctx).Model(&model.Notification{}).
|
||||
Where("id = ? AND user_id = ?", id, userID).
|
||||
Update("read_at", now)
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
result := query.Update("read_at", now)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
checkQuery := r.db.WithContext(ctx).Model(&model.Notification{}).Where("id = ? AND user_id = ?", id, userID)
|
||||
checkQuery = r.applySiteFilter(checkQuery, siteID, isSite)
|
||||
var total int64
|
||||
if err := r.db.WithContext(ctx).Model(&model.Notification{}).
|
||||
Where("id = ? AND user_id = ?", id, userID).
|
||||
Count(&total).Error; err != nil {
|
||||
if err := checkQuery.Count(&total).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if total == 0 {
|
||||
@@ -65,19 +114,39 @@ func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) UnreadCount(ctx context.Context, userID uint64) (int64, error) {
|
||||
func (r *Repository) UnreadCount(ctx context.Context, userID uint64, siteCode ...string) (int64, error) {
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
siteID, isSite, err := r.resolveSite(ctx, code)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
var total int64
|
||||
err := r.db.WithContext(ctx).Model(&model.Notification{}).
|
||||
Where("user_id = ? AND read_at IS NULL", userID).
|
||||
Count(&total).Error
|
||||
err = query.Count(&total).Error
|
||||
return total, err
|
||||
}
|
||||
|
||||
func (r *Repository) MarkAllRead(ctx context.Context, userID uint64) (int64, error) {
|
||||
func (r *Repository) MarkAllRead(ctx context.Context, userID uint64, siteCode ...string) (int64, error) {
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
siteID, isSite, err := r.resolveSite(ctx, code)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
result := r.db.WithContext(ctx).Model(&model.Notification{}).
|
||||
Where("user_id = ? AND read_at IS NULL", userID).
|
||||
Update("read_at", now)
|
||||
query := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ? AND read_at IS NULL", userID)
|
||||
query = r.applySiteFilter(query, siteID, isSite)
|
||||
|
||||
result := query.Update("read_at", now)
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
@@ -86,13 +155,30 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
if entry.UserID == 0 || entry.Title == "" {
|
||||
continue
|
||||
}
|
||||
siteID := entry.DistributionSiteID
|
||||
if siteID == nil {
|
||||
if (entry.BizType == "order" || entry.BizType == "order_handoff") && entry.BizID != nil && *entry.BizID > 0 {
|
||||
var orderSiteID *uint64
|
||||
row := tx.Table("rental_orders").Where("id = ?", *entry.BizID).Select("distribution_site_id").Row()
|
||||
if err := row.Scan(&orderSiteID); err == nil && orderSiteID != nil {
|
||||
siteID = orderSiteID
|
||||
}
|
||||
} else if entry.BizType == "dispute" && entry.BizID != nil && *entry.BizID > 0 {
|
||||
var orderSiteID *uint64
|
||||
row := tx.Table("order_disputes d").Joins("JOIN rental_orders o ON o.id = d.order_id").Where("d.id = ?", *entry.BizID).Select("o.distribution_site_id").Row()
|
||||
if err := row.Scan(&orderSiteID); err == nil && orderSiteID != nil {
|
||||
siteID = orderSiteID
|
||||
}
|
||||
}
|
||||
}
|
||||
row := model.Notification{
|
||||
UserID: entry.UserID,
|
||||
Type: entry.Type,
|
||||
Title: entry.Title,
|
||||
Content: entry.Content,
|
||||
BizType: entry.BizType,
|
||||
BizID: entry.BizID,
|
||||
UserID: entry.UserID,
|
||||
Type: entry.Type,
|
||||
Title: entry.Title,
|
||||
Content: entry.Content,
|
||||
BizType: entry.BizType,
|
||||
BizID: entry.BizID,
|
||||
DistributionSiteID: siteID,
|
||||
}
|
||||
if row.Type == "" {
|
||||
row.Type = "system"
|
||||
@@ -106,14 +192,16 @@ func Append(tx *gorm.DB, entries ...Entry) error {
|
||||
|
||||
func toDTO(row model.Notification) NotificationDTO {
|
||||
return NotificationDTO{
|
||||
ID: row.ID,
|
||||
UserID: row.UserID,
|
||||
Type: row.Type,
|
||||
Title: row.Title,
|
||||
Content: row.Content,
|
||||
BizType: row.BizType,
|
||||
BizID: row.BizID,
|
||||
ReadAt: row.ReadAt,
|
||||
CreatedAt: row.CreatedAt,
|
||||
ID: row.ID,
|
||||
UserID: row.UserID,
|
||||
Type: row.Type,
|
||||
Title: row.Title,
|
||||
Content: row.Content,
|
||||
BizType: row.BizType,
|
||||
BizID: row.BizID,
|
||||
DistributionSiteID: row.DistributionSiteID,
|
||||
ReadAt: row.ReadAt,
|
||||
CreatedAt: row.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user