refactor(publish): 公开读改走只读视图,后台仍读基表

This commit is contained in:
toom1996
2026-09-23 10:07:43 +08:00
parent 83823a8d49
commit f5144038ab
8 changed files with 260 additions and 23 deletions

View File

@ -71,12 +71,12 @@ func NewBrandRepository(db *gorm.DB) BrandRepository {
// 排序与索引统一基于 name_en(拉丁规范名);name_en 为空的纯中文品牌归入 OTHER 桶。
const latinPrefixRegexp = "^[A-Za-z]"
// hasArticlesSubQuery 子查询:所有"至少有一篇走秀档案"的品牌 id。
// hasArticlesSubQuery 子查询:所有「至少有一篇已发布走秀档案」的品牌 id。
// 库中存在 2 万+ 无档案品牌,索引必须靠它收敛。
// 读公开视图而非基表:否则只有待审走秀的品牌会提前出现在前台的 A-Z 索引里。
func (r *brandRepository) hasArticlesSubQuery() *gorm.DB {
return r.db.Model(&model.BrandRunway{}).
Select("DISTINCT brand_id").
Where("is_deleted = 0")
return r.db.Table(publicRunways).
Select("DISTINCT brand_id")
}
// brandFilterScope 把品牌查询条件编译为 GORM Scope。
@ -157,10 +157,10 @@ func (r *brandRepository) FeaturedIDs(ctx context.Context, metric string, limit
var agg []struct {
BrandID uint32 `gorm:"column:brand_id"`
}
// 读公开视图:热门榜只能由已发布走秀构成,避免把待审/驳回内容的图片数计入热度。
err := r.db.WithContext(ctx).
Model(&model.BrandRunway{}).
Table(publicRunways).
Select("brand_id, COALESCE(SUM(image_count),0) AS images, COUNT(*) AS shows").
Where("is_deleted = 0").
Group("brand_id").
Order(orderCol + " DESC").
Limit(limit).
@ -281,7 +281,7 @@ WITH latest_per_brand AS (
END DESC,
id DESC
) AS rn
FROM brand_runways
FROM public_brand_runways brand_runways
WHERE is_deleted = 0 AND brand_id IN (?)
)
SELECT id, brand_id, cover, title_en, title_cn