fix(chat): 分销付款改为进入账号群,不再另建订单群
主站、分站和分站之间都只用发布时的账号群,二维码仍跟这个群走。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -28,6 +28,57 @@ func (r *Repository) resolveSite(ctx context.Context, siteCode string) (*model.D
|
|||||||
return &site, nil
|
return &site, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// userSiteConversationFilter 限制用户在当前站点能看到的会话。
|
||||||
|
// 账号群只有一个:本站发布的群按站点归属显示;跨站成交时,租客在下单的站点也能看到这个群。
|
||||||
|
func userSiteConversationFilter(siteID uint64, userID uint64) (string, []any) {
|
||||||
|
if siteID > 0 {
|
||||||
|
filter := `(
|
||||||
|
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 = ?
|
||||||
|
AND ro.distribution_mode != 'two_party_distributor_receive'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
c.type = ?
|
||||||
|
AND c.listing_id IN (
|
||||||
|
SELECT ro.listing_id FROM rental_orders ro
|
||||||
|
WHERE ro.distribution_site_id = ?
|
||||||
|
AND ro.renter_id = ?
|
||||||
|
AND ro.distribution_mode != 'two_party_distributor_receive'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)`
|
||||||
|
return filter, []any{siteID, siteID, ConversationTypeListingGroup, siteID, userID}
|
||||||
|
}
|
||||||
|
filter := `(
|
||||||
|
(
|
||||||
|
(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
|
||||||
|
AND ro.distribution_mode != 'two_party_distributor_receive'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
OR (
|
||||||
|
c.type = ?
|
||||||
|
AND c.listing_id IN (
|
||||||
|
SELECT ro.listing_id FROM rental_orders ro
|
||||||
|
WHERE ro.renter_id = ?
|
||||||
|
AND ro.distribution_mode = 'two_party_distributor_receive'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)`
|
||||||
|
return filter, []any{ConversationTypeListingGroup, userID}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Repository) ListConversations(ctx context.Context, principal Principal, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
func (r *Repository) ListConversations(ctx context.Context, principal Principal, page, pageSize int, siteCode ...string) (*PaginatedResult, error) {
|
||||||
page, pageSize = normalizePagination(page, pageSize)
|
page, pageSize = normalizePagination(page, pageSize)
|
||||||
db := r.db.WithContext(ctx)
|
db := r.db.WithContext(ctx)
|
||||||
@@ -50,15 +101,13 @@ func (r *Repository) ListConversations(ctx context.Context, principal Principal,
|
|||||||
Where("c.status = ?", "active")
|
Where("c.status = ?", "active")
|
||||||
|
|
||||||
if principal.Type == "user" {
|
if principal.Type == "user" {
|
||||||
|
siteID := uint64(0)
|
||||||
if site != nil {
|
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 = ? AND ro.distribution_mode != 'two_party_distributor_receive')))"
|
siteID = site.ID
|
||||||
countDB = countDB.Where(siteFilter, site.ID, site.ID)
|
|
||||||
dataQuery = dataQuery.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 AND ro.distribution_mode != 'two_party_distributor_receive'))"
|
|
||||||
countDB = countDB.Where(mainFilter)
|
|
||||||
dataQuery = dataQuery.Where(mainFilter)
|
|
||||||
}
|
}
|
||||||
|
filter, args := userSiteConversationFilter(siteID, principal.ID)
|
||||||
|
countDB = countDB.Where(filter, args...)
|
||||||
|
dataQuery = dataQuery.Where(filter, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
@@ -159,22 +208,6 @@ func (r *Repository) FindOrderConversation(ctx context.Context, userID uint64, o
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if order.DistributionSiteID != nil && *order.DistributionSiteID > 0 {
|
|
||||||
var row conversationRow
|
|
||||||
err := r.conversationQuery(ctx, principal).Where("c.order_id = ?", orderID).First(&row).Error
|
|
||||||
if err == nil {
|
|
||||||
participants, err := r.participants(ctx, row.ID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
dto := row.toDTO(participants)
|
|
||||||
return &dto, nil
|
|
||||||
}
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if order.ListingID > 0 {
|
if order.ListingID > 0 {
|
||||||
var row conversationRow
|
var row conversationRow
|
||||||
err := r.conversationQuery(ctx, principal).
|
err := r.conversationQuery(ctx, principal).
|
||||||
|
|||||||
@@ -90,6 +90,81 @@ func TestFindOrderConversationPrefersListingGroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFindOrderConversationDistributionPrefersListingGroup(t *testing.T) {
|
||||||
|
db := setupConversationTestDB(t)
|
||||||
|
repo := NewRepository(db, nil, nil)
|
||||||
|
now := time.Date(2026, 6, 18, 12, 0, 0, 0, time.UTC)
|
||||||
|
site := model.DistributionSite{
|
||||||
|
SiteCode: "site_listing_group",
|
||||||
|
SiteName: "账号群分销",
|
||||||
|
Domain: "listing-group.hfb.com",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
if err := db.Create(&site).Error; err != nil {
|
||||||
|
t.Fatalf("创建分销站点失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
owner := model.User{Phone: "13800001005", Nickname: "号主"}
|
||||||
|
renter := model.User{Phone: "13800001006", Nickname: "租客"}
|
||||||
|
if err := db.Create(&owner).Error; err != nil {
|
||||||
|
t.Fatalf("创建号主失败: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&renter).Error; err != nil {
|
||||||
|
t.Fatalf("创建租客失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
order := model.RentalOrder{
|
||||||
|
OrderNo: "ORDER-DIST-LISTING-GROUP",
|
||||||
|
ListingID: 103,
|
||||||
|
AccountID: 203,
|
||||||
|
OwnerID: owner.ID,
|
||||||
|
RenterID: renter.ID,
|
||||||
|
Status: "renting",
|
||||||
|
DistributionSiteID: &site.ID,
|
||||||
|
}
|
||||||
|
if err := db.Create(&order).Error; err != nil {
|
||||||
|
t.Fatalf("创建订单失败: %v", err)
|
||||||
|
}
|
||||||
|
orderConv := model.ChatConversation{
|
||||||
|
OrderID: &order.ID,
|
||||||
|
DistributionSiteID: &site.ID,
|
||||||
|
Type: ConversationTypeOrderGroup,
|
||||||
|
Title: "订单群聊",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
listingConv := model.ChatConversation{
|
||||||
|
ListingID: &order.ListingID,
|
||||||
|
DistributionSiteID: &site.ID,
|
||||||
|
Type: ConversationTypeListingGroup,
|
||||||
|
Title: "账号群",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
if err := db.Create(&orderConv).Error; err != nil {
|
||||||
|
t.Fatalf("创建订单群失败: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&listingConv).Error; err != nil {
|
||||||
|
t.Fatalf("创建账号群失败: %v", err)
|
||||||
|
}
|
||||||
|
participants := []model.ChatParticipant{
|
||||||
|
{ConversationID: orderConv.ID, ParticipantType: "user", ParticipantID: order.RenterID, Role: "renter", JoinedAt: now},
|
||||||
|
{ConversationID: listingConv.ID, ParticipantType: "user", ParticipantID: order.RenterID, Role: "renter", JoinedAt: now},
|
||||||
|
}
|
||||||
|
if err := db.Create(&participants).Error; err != nil {
|
||||||
|
t.Fatalf("创建成员失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dto, err := repo.FindOrderConversation(t.Context(), order.RenterID, order.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("查询订单会话失败: %v", err)
|
||||||
|
}
|
||||||
|
if dto.ID != listingConv.ID {
|
||||||
|
t.Fatalf("会话 ID = %d, want 账号群 %d", dto.ID, listingConv.ID)
|
||||||
|
}
|
||||||
|
if dto.Type != ConversationTypeListingGroup {
|
||||||
|
t.Fatalf("会话类型 = %q, want %q", dto.Type, ConversationTypeListingGroup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFindOrderConversationFallsBackToOrderGroup(t *testing.T) {
|
func TestFindOrderConversationFallsBackToOrderGroup(t *testing.T) {
|
||||||
db := setupConversationTestDB(t)
|
db := setupConversationTestDB(t)
|
||||||
repo := NewRepository(db, nil, nil)
|
repo := NewRepository(db, nil, nil)
|
||||||
@@ -147,6 +222,120 @@ func TestFindOrderConversationFallsBackToOrderGroup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestListingGroupVisibleToCrossSiteRenter(t *testing.T) {
|
||||||
|
db := setupConversationTestDB(t)
|
||||||
|
repo := NewRepository(db, nil, nil)
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
renter := model.User{Phone: "13900003001", Nickname: "跨站租客"}
|
||||||
|
if err := db.Create(&renter).Error; err != nil {
|
||||||
|
t.Fatalf("创建租客失败: %v", err)
|
||||||
|
}
|
||||||
|
buySite := model.DistributionSite{
|
||||||
|
SiteCode: "site_buyer",
|
||||||
|
SiteName: "买家分站",
|
||||||
|
Domain: "buyer.hfb.com",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
listingSite := model.DistributionSite{
|
||||||
|
SiteCode: "site_listing",
|
||||||
|
SiteName: "挂货分站",
|
||||||
|
Domain: "listing.hfb.com",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
if err := db.Create(&buySite).Error; err != nil {
|
||||||
|
t.Fatalf("创建买家分站失败: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&listingSite).Error; err != nil {
|
||||||
|
t.Fatalf("创建挂货分站失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mainListingID := uint64(301)
|
||||||
|
distListingID := uint64(302)
|
||||||
|
mainGroup := model.ChatConversation{
|
||||||
|
ListingID: &mainListingID,
|
||||||
|
Type: ConversationTypeListingGroup,
|
||||||
|
Title: "主站账号群",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
distGroup := model.ChatConversation{
|
||||||
|
ListingID: &distListingID,
|
||||||
|
DistributionSiteID: &listingSite.ID,
|
||||||
|
Type: ConversationTypeListingGroup,
|
||||||
|
Title: "分站账号群",
|
||||||
|
Status: "active",
|
||||||
|
}
|
||||||
|
if err := db.Create(&mainGroup).Error; err != nil {
|
||||||
|
t.Fatalf("创建主站账号群失败: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&distGroup).Error; err != nil {
|
||||||
|
t.Fatalf("创建分站账号群失败: %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Create(&[]model.ChatParticipant{
|
||||||
|
{ConversationID: mainGroup.ID, ParticipantType: "user", ParticipantID: renter.ID, Role: "renter", JoinedAt: now},
|
||||||
|
{ConversationID: distGroup.ID, ParticipantType: "user", ParticipantID: renter.ID, Role: "renter", JoinedAt: now},
|
||||||
|
}).Error; err != nil {
|
||||||
|
t.Fatalf("创建成员失败: %v", err)
|
||||||
|
}
|
||||||
|
orders := []model.RentalOrder{
|
||||||
|
{
|
||||||
|
OrderNo: "ORDER-PLATFORM-RECEIVE",
|
||||||
|
ListingID: mainListingID,
|
||||||
|
RenterID: renter.ID,
|
||||||
|
Status: "renting",
|
||||||
|
DistributionSiteID: &buySite.ID,
|
||||||
|
DistributionMode: "two_party_platform_receive",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
OrderNo: "ORDER-DISTRIBUTOR-RECEIVE",
|
||||||
|
ListingID: distListingID,
|
||||||
|
RenterID: renter.ID,
|
||||||
|
Status: "renting",
|
||||||
|
DistributionSiteID: &listingSite.ID,
|
||||||
|
DistributionMode: "two_party_distributor_receive",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
if err := db.Create(&orders).Error; err != nil {
|
||||||
|
t.Fatalf("创建订单失败: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
principal := Principal{Type: "user", ID: renter.ID}
|
||||||
|
buyList, err := repo.ListConversations(t.Context(), principal, 1, 20, buySite.SiteCode)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("查询买家分站会话失败: %v", err)
|
||||||
|
}
|
||||||
|
if !conversationListHas(buyList, mainGroup.ID) {
|
||||||
|
t.Fatalf("买家分站应看到主站账号群 %d", mainGroup.ID)
|
||||||
|
}
|
||||||
|
if conversationListHas(buyList, distGroup.ID) {
|
||||||
|
t.Fatalf("买家分站不应看到其他分站账号群 %d", distGroup.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
mainList, err := repo.ListConversations(t.Context(), principal, 1, 20)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("查询主站会话失败: %v", err)
|
||||||
|
}
|
||||||
|
if !conversationListHas(mainList, mainGroup.ID) {
|
||||||
|
t.Fatalf("主站应看到主站账号群 %d", mainGroup.ID)
|
||||||
|
}
|
||||||
|
if !conversationListHas(mainList, distGroup.ID) {
|
||||||
|
t.Fatalf("主站买家应看到分站收号的账号群 %d", distGroup.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func conversationListHas(result *PaginatedResult, id uint64) bool {
|
||||||
|
items, ok := result.Items.([]ConversationDTO)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, item := range items {
|
||||||
|
if item.ID == id {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func TestChatTenantIsolation(t *testing.T) {
|
func TestChatTenantIsolation(t *testing.T) {
|
||||||
db := setupConversationTestDB(t)
|
db := setupConversationTestDB(t)
|
||||||
repo := NewRepository(db, nil, nil)
|
repo := NewRepository(db, nil, nil)
|
||||||
|
|||||||
@@ -75,13 +75,12 @@ func (r *Repository) CountUnreadMessages(ctx context.Context, principal Principa
|
|||||||
Where("(cp.last_read_at IS NULL OR cm.created_at > cp.last_read_at)")
|
Where("(cp.last_read_at IS NULL OR cm.created_at > cp.last_read_at)")
|
||||||
|
|
||||||
if principal.Type == "user" {
|
if principal.Type == "user" {
|
||||||
|
siteID := uint64(0)
|
||||||
if site != nil {
|
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 = ? AND ro.distribution_mode != 'two_party_distributor_receive')))"
|
siteID = 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 AND ro.distribution_mode != 'two_party_distributor_receive'))"
|
|
||||||
query = query.Where(mainFilter)
|
|
||||||
}
|
}
|
||||||
|
filter, args := userSiteConversationFilter(siteID, principal.ID)
|
||||||
|
query = query.Where(filter, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
var total int64
|
var total int64
|
||||||
|
|||||||
@@ -358,25 +358,17 @@ func (r *Repository) ConfirmPaidFromChannelTx(tx *gorm.DB, orderID uint64) (uint
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分销订单创建独立分站群聊;主站订单拉租客进发布群
|
// 付款后只拉租客进账号群。主站和分销都不再另建订单群,二维码仍是发布时分到这个群的那一张。
|
||||||
conversationID := uint64(0)
|
conversationID := uint64(0)
|
||||||
if order.DistributionSiteID != nil && *order.DistributionSiteID > 0 {
|
if err := chat.AddRenterToListingConversation(tx, listing.ID, order.RenterID, order.OrderNo); err != nil {
|
||||||
orderConv, err := chat.EnsureOrderConversation(tx, *order)
|
return 0, err
|
||||||
if err != nil {
|
}
|
||||||
return 0, err
|
var listingConv model.ChatConversation
|
||||||
}
|
err = tx.Where("listing_id = ? AND type = ?", listing.ID, chat.ConversationTypeListingGroup).First(&listingConv).Error
|
||||||
if orderConv != nil {
|
if err == nil {
|
||||||
conversationID = orderConv.ID
|
conversationID = listingConv.ID
|
||||||
}
|
} else if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
} else {
|
return 0, err
|
||||||
if err := chat.AddRenterToListingConversation(tx, listing.ID, order.RenterID, order.OrderNo); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
// 查询发布群ID用于返回
|
|
||||||
var listingConv model.ChatConversation
|
|
||||||
if err := tx.Where("listing_id = ?", listing.ID).First(&listingConv).Error; err == nil {
|
|
||||||
conversationID = listingConv.ID
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if isPlatformHandoffOrder(*order) {
|
if isPlatformHandoffOrder(*order) {
|
||||||
|
|||||||
Reference in New Issue
Block a user