feat(distribution): 分销站点消息中心与钱包提现多租户独立分支隔离
- 新增 000070 数据库迁移:支持 chat_conversations、wallet_accounts、wallet_ledger、withdrawal_requests 按 distribution_site_id 隔离 - 消息中心:会话列表、未读数统计、站点专属客服及订单群聊与主站实现双向完全隔离 - 资金账户与提现:独立按 (user_id, distribution_site_id) 建账,分销订单自动结算至对应分站钱包,提现与流水相互独立 - 前台界面:Axios 自动透传 X-Distribution-Site-Code,钱包、提现与消息页面呈现分站专属标牌与独立说明
This commit is contained in:
@@ -15,6 +15,7 @@ type conversationRow struct {
|
||||
ID uint64
|
||||
OrderID *uint64
|
||||
ListingID *uint64
|
||||
DistributionSiteID *uint64
|
||||
LatestOrderID *uint64
|
||||
LatestOrderNo string
|
||||
LatestOrderStatus string
|
||||
@@ -40,7 +41,7 @@ type conversationRow struct {
|
||||
|
||||
func (r *Repository) conversationQuery(ctx context.Context, principal Principal) *gorm.DB {
|
||||
return r.db.WithContext(ctx).Table("chat_conversations AS c").
|
||||
Select(`c.id, c.order_id, c.listing_id, c.type, c.support_scene, c.title, c.status, c.last_message_id,
|
||||
Select(`c.id, c.order_id, c.listing_id, c.distribution_site_id, c.type, c.support_scene, c.title, c.status, c.last_message_id,
|
||||
c.last_message_preview, c.last_message_at, c.created_at, c.updated_at, cp.role,
|
||||
(
|
||||
SELECT COUNT(1)
|
||||
@@ -54,17 +55,37 @@ func (r *Repository) conversationQuery(ctx context.Context, principal Principal)
|
||||
Where("cp.participant_type = ? AND cp.participant_id = ?", principal.Type, principal.ID)
|
||||
}
|
||||
|
||||
func (r *Repository) CountUnreadMessages(ctx context.Context, principal Principal) (int64, error) {
|
||||
var total int64
|
||||
err := r.db.WithContext(ctx).Table("chat_messages AS cm").
|
||||
func (r *Repository) CountUnreadMessages(ctx context.Context, principal Principal, siteCode ...string) (int64, error) {
|
||||
var code string
|
||||
if len(siteCode) > 0 {
|
||||
code = siteCode[0]
|
||||
}
|
||||
site, err := r.resolveSite(ctx, code)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
query := r.db.WithContext(ctx).Table("chat_messages AS cm").
|
||||
Joins("JOIN chat_participants AS cp ON cp.conversation_id = cm.conversation_id").
|
||||
Joins("JOIN chat_conversations AS c ON c.id = cm.conversation_id").
|
||||
Where("cp.participant_type = ? AND cp.participant_id = ?", principal.Type, principal.ID).
|
||||
Where("c.status = ?", "active").
|
||||
Where("NOT (cm.sender_type = ? AND cm.sender_id = ?)", principal.Type, principal.ID).
|
||||
Where("cm.sender_type <> ?", "system").
|
||||
Where("(cp.last_read_at IS NULL OR cm.created_at > cp.last_read_at)").
|
||||
Count(&total).Error
|
||||
Where("(cp.last_read_at IS NULL OR cm.created_at > cp.last_read_at)")
|
||||
|
||||
if principal.Type == "user" {
|
||||
if site != nil {
|
||||
siteFilter := "(c.distribution_site_id = ? OR (c.order_id IS NOT NULL AND c.order_id IN (SELECT ro.id FROM rental_orders ro WHERE ro.distribution_site_id = ?)))"
|
||||
query = query.Where(siteFilter, site.ID, site.ID)
|
||||
} else {
|
||||
mainFilter := "(c.distribution_site_id IS NULL OR c.distribution_site_id = 0) AND (c.order_id IS NULL OR c.order_id NOT IN (SELECT ro.id FROM rental_orders ro WHERE ro.distribution_site_id IS NOT NULL AND ro.distribution_site_id > 0))"
|
||||
query = query.Where(mainFilter)
|
||||
}
|
||||
}
|
||||
|
||||
var total int64
|
||||
err = query.Count(&total).Error
|
||||
return total, err
|
||||
}
|
||||
|
||||
@@ -315,6 +336,7 @@ func (row conversationRow) toDTO(participants []ParticipantDTO) ConversationDTO
|
||||
ID: row.ID,
|
||||
OrderID: row.OrderID,
|
||||
ListingID: row.ListingID,
|
||||
DistributionSiteID: row.DistributionSiteID,
|
||||
LatestOrderID: row.LatestOrderID,
|
||||
LatestOrderNo: row.LatestOrderNo,
|
||||
LatestOrderStatus: row.LatestOrderStatus,
|
||||
|
||||
Reference in New Issue
Block a user