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
+13 -6
View File
@@ -32,14 +32,21 @@ type Account struct {
RejectReason string
AuthorityID uint
CreatedAt, UpdatedAt time.Time
PromoteCount, BindCount int64
PromoteCount *int64
BindCount int64
}
type AccountCounts struct {
// 无渠道表时为 nil,避免将未接入数据误报为真实零渠道。
PromoteCount *int64
BindCount int64
}
// UserScope 表示商务树可见范围,与框架部门权限相互独立。
type UserScope struct {
All, Empty, IncludeSelf bool
SelfID uint
DescendantPath string
All, Empty, IncludeSelf, OnlySelf bool
SelfID uint
DescendantPath string
}
type AccountFilter struct {
@@ -84,7 +91,7 @@ type AccountRepo interface {
Create(context.Context, *Account) error
Update(context.Context, *Account) error
AuthorityForLevel(context.Context, int) (uint, error)
DirectCounts(context.Context, []uint) (map[uint][2]int64, error)
DirectCounts(context.Context, []uint) (map[uint]AccountCounts, error)
}
type AccountUsecase struct{ repo AccountRepo }
@@ -141,7 +148,7 @@ func (uc *AccountUsecase) List(ctx context.Context, filter AccountFilter, option
return nil, 0, err
}
for _, row := range rows {
row.PromoteCount, row.BindCount = counts[row.ID][0], counts[row.ID][1]
row.PromoteCount, row.BindCount = counts[row.ID].PromoteCount, counts[row.ID].BindCount
}
return rows, total, nil
}
+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{}))
+1 -1
View File
@@ -46,7 +46,7 @@ type BusinessAccountResponse struct {
AuthorityID uint `json:"authorityId"`
CreateTime string `json:"createTime"`
UpdateTime string `json:"updateTime"`
PromoteCount int64 `json:"promoteCount"`
PromoteCount *int64 `json:"promoteCount"`
BindCount int64 `json:"bindCount"`
}