2026-05-21 15:20:12 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-05-27 18:07:55 +08:00
|
|
|
"github.com/luxsin/app-api/internal/cache"
|
2026-05-21 15:20:12 +08:00
|
|
|
"github.com/luxsin/app-api/internal/model"
|
2026-05-27 18:07:55 +08:00
|
|
|
"github.com/redis/go-redis/v9"
|
2026-05-21 15:20:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ModelRepository struct {
|
2026-05-27 18:07:55 +08:00
|
|
|
db *sql.DB
|
|
|
|
|
cache *cache.ModelCache
|
2026-05-21 15:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-27 18:07:55 +08:00
|
|
|
func NewModelRepository(db *sql.DB, modelCache *cache.ModelCache) *ModelRepository {
|
|
|
|
|
return &ModelRepository{db: db, cache: modelCache}
|
2026-05-21 15:20:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *ModelRepository) List(ctx context.Context, brandName, modelName string) ([]model.Model, error) {
|
|
|
|
|
brandName = strings.TrimSpace(brandName)
|
|
|
|
|
modelName = strings.TrimSpace(modelName)
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
case brandName != "":
|
2026-05-27 18:07:55 +08:00
|
|
|
// 按品牌精确查询:优先从缓存获取
|
|
|
|
|
list, err := r.cache.GetByBrand(ctx, brandName)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return list, nil
|
|
|
|
|
}
|
|
|
|
|
// 缓存未命中或 Redis 异常,降级到数据库
|
|
|
|
|
if err != redis.Nil {
|
|
|
|
|
fmt.Printf("model cache read failed, fallback to db: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list, err = r.ListByBrandFromDB(ctx, brandName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 回写缓存
|
|
|
|
|
if cacheErr := r.cache.SetByBrand(ctx, brandName, list); cacheErr != nil {
|
|
|
|
|
fmt.Printf("model cache write failed: %v\n", cacheErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
|
|
2026-05-21 15:20:12 +08:00
|
|
|
case modelName != "":
|
2026-05-27 18:07:55 +08:00
|
|
|
// 按型号模糊查询:从全量缓存中过滤
|
|
|
|
|
list, err := r.cache.GetAll(ctx)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return filterModels(list, modelName), nil
|
|
|
|
|
}
|
|
|
|
|
// 缓存未命中或 Redis 异常,降级到数据库
|
|
|
|
|
if err != redis.Nil {
|
|
|
|
|
fmt.Printf("model cache read failed, fallback to db: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list, err = r.ListByModelFromDB(ctx, modelName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 回写全量缓存
|
|
|
|
|
if allModels, dbErr := r.ListAllFromDB(ctx); dbErr == nil {
|
|
|
|
|
if cacheErr := r.cache.SetAll(ctx, allModels); cacheErr != nil {
|
|
|
|
|
fmt.Printf("model cache write failed: %v\n", cacheErr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
|
|
2026-05-21 15:20:12 +08:00
|
|
|
default:
|
|
|
|
|
return []model.Model{}, nil
|
|
|
|
|
}
|
2026-05-27 18:07:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListAllFromDB 从数据库查询全部型号(用于预热和缓存回写)
|
|
|
|
|
func (r *ModelRepository) ListAllFromDB(ctx context.Context) ([]model.Model, error) {
|
|
|
|
|
const query = `SELECT id, brand_name, name, form, rig, source, eq_key, create_at FROM model ORDER BY name ASC`
|
2026-05-21 15:20:12 +08:00
|
|
|
|
2026-05-27 18:07:55 +08:00
|
|
|
rows, err := r.db.QueryContext(ctx, query)
|
2026-05-21 15:20:12 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query model: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
2026-05-27 18:07:55 +08:00
|
|
|
return scanModels(rows)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListByBrandFromDB 从数据库按品牌查询型号
|
|
|
|
|
func (r *ModelRepository) ListByBrandFromDB(ctx context.Context, brandName string) ([]model.Model, error) {
|
|
|
|
|
const query = `SELECT id, brand_name, name, form, rig, source, eq_key, create_at FROM model WHERE brand_name = ? ORDER BY name ASC`
|
|
|
|
|
|
|
|
|
|
rows, err := r.db.QueryContext(ctx, query, brandName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query model: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
return scanModels(rows)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListByModelFromDB 从数据库按型号名称模糊查询
|
|
|
|
|
func (r *ModelRepository) ListByModelFromDB(ctx context.Context, modelName string) ([]model.Model, error) {
|
|
|
|
|
const query = `SELECT id, brand_name, name, form, rig, source, eq_key, create_at FROM model WHERE name LIKE ? ORDER BY name ASC`
|
|
|
|
|
|
|
|
|
|
rows, err := r.db.QueryContext(ctx, query, "%"+modelName+"%")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("query model: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
return scanModels(rows)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// filterModels 在应用层做模糊过滤
|
|
|
|
|
func filterModels(list []model.Model, modelName string) []model.Model {
|
|
|
|
|
filtered := make([]model.Model, 0)
|
|
|
|
|
for _, m := range list {
|
|
|
|
|
if strings.Contains(
|
|
|
|
|
strings.ToLower(m.Name),
|
|
|
|
|
strings.ToLower(modelName),
|
|
|
|
|
) {
|
|
|
|
|
filtered = append(filtered, m)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return filtered
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func scanModels(rows *sql.Rows) ([]model.Model, error) {
|
2026-05-21 15:20:12 +08:00
|
|
|
list := make([]model.Model, 0)
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
m, err := scanModel(rows)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
list = append(list, m)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("iterate model: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return list, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func scanModel(rows *sql.Rows) (model.Model, error) {
|
|
|
|
|
var m model.Model
|
|
|
|
|
var form, rig, source, eqKey sql.NullString
|
|
|
|
|
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&m.ID,
|
|
|
|
|
&m.BrandName,
|
|
|
|
|
&m.Name,
|
|
|
|
|
&form,
|
|
|
|
|
&rig,
|
|
|
|
|
&source,
|
|
|
|
|
&eqKey,
|
|
|
|
|
&m.CreateAt,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return model.Model{}, fmt.Errorf("scan model: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m.Form = nullStringPtr(form)
|
|
|
|
|
m.Rig = nullStringPtr(rig)
|
|
|
|
|
m.Source = nullStringPtr(source)
|
|
|
|
|
m.EqKey = nullStringPtr(eqKey)
|
|
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nullStringPtr(ns sql.NullString) *string {
|
|
|
|
|
if !ns.Valid {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
s := ns.String
|
|
|
|
|
return &s
|
|
|
|
|
}
|