fix: 修复缺少渠道表时商务列表失败并补充接口实测

This commit is contained in:
Yvan
2026-11-03 18:10:59 +08:00
parent 794001dfbe
commit 43f0ec1b15
8 changed files with 224 additions and 13 deletions
+33 -4
View File
@@ -71,6 +71,9 @@ func applyUserScope(db *gorm.DB, scope business.UserScope, columnPrefix string)
return db.Where("1=0")
}
id, path, parent := columnPrefix+"id", columnPrefix+"id_path", columnPrefix+"parent_id"
if scope.OnlySelf {
return db.Where(id+" = ?", scope.SelfID)
}
query := "(" + path + " = ? OR " + path + " LIKE ? OR " + parent + " = ?)"
args := []any{scope.DescendantPath, scope.DescendantPath + ",%", scope.SelfID}
if scope.IncludeSelf {
@@ -222,12 +225,16 @@ func (r *accountRepo) AuthorityForLevel(ctx context.Context, level int) (uint, e
return ids[0], nil
}
func (r *accountRepo) DirectCounts(ctx context.Context, ids []uint) (map[uint][2]int64, error) {
result := map[uint][2]int64{}
func (r *accountRepo) DirectCounts(ctx context.Context, ids []uint) (map[uint]business.AccountCounts, error) {
result := map[uint]business.AccountCounts{}
if len(ids) == 0 {
return result, nil
}
db := r.data.DB().WithContext(ctx)
for _, id := range ids {
zero := int64(0)
result[id] = business.AccountCounts{PromoteCount: &zero}
}
var binds []struct {
UserID uint
Total int64
@@ -239,7 +246,28 @@ func (r *accountRepo) DirectCounts(ctx context.Context, ids []uint) (map[uint][2
return result, nil
}
for _, b := range binds {
result[b.UserID] = [2]int64{0, b.Total}
zero := int64(0)
result[b.UserID] = business.AccountCounts{PromoteCount: &zero, BindCount: b.Total}
}
// 系统数据演练库尚未包含渠道表时,基础资料与已通过绑定数仍可使用。
tables, err := db.Migrator().GetTables()
if err != nil {
return nil, business.ErrStorage
}
available := false
for _, table := range tables {
if table == "tab_promote" {
available = true
break
}
}
if !available {
for _, b := range binds {
value := result[b.UserID]
value.PromoteCount = nil
result[b.UserID] = value
}
return result, nil
}
var counts []struct {
UserID uint
@@ -250,7 +278,8 @@ func (r *accountRepo) DirectCounts(ctx context.Context, ids []uint) (map[uint][2
}
for _, c := range counts {
value := result[c.UserID]
value[0] = c.Total
count := c.Total
value.PromoteCount = &count
result[c.UserID] = value
}
return result, nil
+1 -1
View File
@@ -95,7 +95,7 @@ func TestAccountListScopesAndCrossPlatformCounts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if counts[13] != [2]int64{3, 2} || counts[14] != [2]int64{1, 1} || counts[99] != [2]int64{} {
if counts[13].PromoteCount == nil || *counts[13].PromoteCount != 3 || counts[13].BindCount != 2 || counts[14].PromoteCount == nil || *counts[14].PromoteCount != 1 || counts[14].BindCount != 1 || counts[99].PromoteCount == nil || *counts[99].PromoteCount != 0 {
t.Fatalf("渠道数或绑定数口径不一致: %+v", counts)
}
rows, total, err = r.List(context.Background(), business.UserScope{}, business.ListFilter(business.AccountFilter{}))