This commit is contained in:
toom1996
2026-09-07 00:01:48 +08:00
parent b049104a0b
commit 74ba700598
31 changed files with 1113 additions and 606 deletions

View File

@ -2,6 +2,8 @@ package repository
import (
"context"
"errors"
"time"
"fashionapi/internal/dto"
"fashionapi/internal/model"
@ -25,6 +27,18 @@ type BrandRepository interface {
// 供 Hot 等已知 id 列表的场景使用,避免借道 List 的 featured 旗标复用查询。
ListByIDs(ctx context.Context, ids []uint32) ([]model.Brand, error)
// FindByID 按数字主键取单个品牌;不存在返回 ErrNotFound。
FindByID(ctx context.Context, id uint32) (*model.Brand, error)
// Create 插入品牌,返回自增主键。
Create(ctx context.Context, b *model.Brand) (uint32, error)
// Update 按数字主键更新品牌中英文名。
Update(ctx context.Context, id uint32, nameEn, nameCn string) error
// CrawlBrands 返回所有可被爬虫抓取的品牌(is_deleted=0 且 name_en 非空)。
// 供爬虫取任务接口使用:不限制"是否已有走秀档案",因为爬虫本就要去补全尚无档案的品牌。
// brandID>0 时仅返回该品牌(对应爬虫 --brand 单品牌调试)。
CrawlBrands(ctx context.Context, brandID uint32) ([]model.Brand, error)
// PopularWithCover 返回"热门品牌"及其代表走秀的封面/标题/文章 ID。
// 规则:先按品牌总图片数取前 limit 个热门品牌,再为每个品牌取最新一篇走秀。
// 返回的是仓储层裸数据(数字主键),对外编码由 service 层负责。
@ -176,6 +190,55 @@ func (r *brandRepository) ListByIDs(ctx context.Context, ids []uint32) ([]model.
return items, nil
}
// FindByID 按数字主键取单个品牌;不存在返回 ErrNotFound。
func (r *brandRepository) FindByID(ctx context.Context, id uint32) (*model.Brand, error) {
var b model.Brand
err := r.db.WithContext(ctx).
Where("id = ? AND is_deleted = 0", id).
First(&b).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
}
return nil, err
}
return &b, nil
}
// Create 插入品牌,返回自增主键。
func (r *brandRepository) Create(ctx context.Context, b *model.Brand) (uint32, error) {
if err := r.db.WithContext(ctx).Create(b).Error; err != nil {
return 0, err
}
return b.ID, nil
}
// Update 按数字主键更新品牌中英文名(同时刷新 updated_at)。
func (r *brandRepository) Update(ctx context.Context, id uint32, nameEn, nameCn string) error {
return r.db.WithContext(ctx).
Model(&model.Brand{}).
Where("id = ? AND is_deleted = 0", id).
Updates(map[string]any{
"name_en": nameEn,
"name_cn": nameCn,
"updated_at": uint32(time.Now().Unix()),
}).Error
}
// CrawlBrands 返回所有可被爬虫抓取的品牌:is_deleted=0 且 name_en 非空(无英文名则无法拼 vogue 抓取 URL)。
// 不限制"是否已有走秀档案"——爬虫的职责正是补全尚无档案的品牌。
func (r *brandRepository) CrawlBrands(ctx context.Context, brandID uint32) ([]model.Brand, error) {
q := r.db.WithContext(ctx).Where("is_deleted = 0 AND name_en <> ''")
if brandID > 0 {
q = q.Where("id = ?", brandID)
}
var items []model.Brand
if err := q.Order("id ASC").Find(&items).Error; err != nil {
return nil, err
}
return items, nil
}
// PopularWithCover 返回"热门品牌"及其最新走秀的封面与标题。
//
// 规则: