87 lines
3.1 KiB
Go
87 lines
3.1 KiB
Go
// Package catalog 目录库服务(方案第一阶段 — 目录库独立查询层)。
|
||
//
|
||
// 方案定位:目录库负责存储 MA、Hash、片库文件 ID、省内内容编码等映射关系,
|
||
// 为标识查询接口提供统一入口。本包在 chain.Client 之上封装独立查询服务,
|
||
// 支持按 MA 码、Hash、省级编码、片库文件 ID 四种维度查询标识信息。
|
||
//
|
||
// 架构说明:
|
||
// - MVP 阶段委托 chain.Client 获取数据(零数据冗余,单一真相源)
|
||
// - 生产阶段可替换为独立 PostgreSQL 存储(通过 CatalogStore 接口注入)
|
||
// - 对外接口不变,业务层零改动
|
||
package catalog
|
||
|
||
import (
|
||
"github.com/tcs-iptv/tcs/internal/chain"
|
||
"github.com/tcs-iptv/tcs/internal/model"
|
||
)
|
||
|
||
// Catalog 目录库查询服务。
|
||
// 提供四种维度的标识信息查询:CC码、Hash、省级编码、片库文件ID。
|
||
type Catalog struct {
|
||
client chain.Client
|
||
store CatalogStore // 可选独立存储(为 nil 时委托 chain.Client)
|
||
}
|
||
|
||
// CatalogStore 目录库独立存储接口(生产阶段可替换为 PG 实现)。
|
||
type CatalogStore interface {
|
||
FindByHash(fileHash string) (model.ContentQueryResult, error)
|
||
FindByProvincialCode(code string) (model.ContentQueryResult, error)
|
||
FindByLibraryFileID(libraryID string) (model.ContentQueryResult, error)
|
||
}
|
||
|
||
// New 创建目录库服务(委托 chain.Client)。
|
||
func New(c chain.Client) *Catalog {
|
||
return &Catalog{client: c}
|
||
}
|
||
|
||
// NewWithStore 创建目录库服务(使用独立存储)。
|
||
func NewWithStore(c chain.Client, s CatalogStore) *Catalog {
|
||
return &Catalog{client: c, store: s}
|
||
}
|
||
|
||
// QueryByMA 根据CC码查询标识信息及映射关系。
|
||
func (cat *Catalog) QueryByMA(ccCode string) (model.ContentQueryResult, error) {
|
||
c, err := cat.client.QueryContent(ccCode)
|
||
if err != nil {
|
||
return model.ContentQueryResult{Found: false}, err
|
||
}
|
||
mr, _ := cat.client.QueryMappings(ccCode)
|
||
eps, _ := cat.client.ListEpisodes(ccCode)
|
||
bindings := eps
|
||
return model.ContentQueryResult{
|
||
Found: true,
|
||
Content: c,
|
||
Bindings: bindings,
|
||
Mappings: mr.Mappings,
|
||
}, nil
|
||
}
|
||
|
||
// QueryByHash 根据内容哈希反查标识信息。
|
||
func (cat *Catalog) QueryByHash(fileHash string) (model.ContentQueryResult, error) {
|
||
if cat.store != nil {
|
||
return cat.store.FindByHash(fileHash)
|
||
}
|
||
return cat.client.QueryByHash(fileHash)
|
||
}
|
||
|
||
// QueryByProvincialCode 根据省级内容编码反查标识信息。
|
||
func (cat *Catalog) QueryByProvincialCode(code string) (model.ContentQueryResult, error) {
|
||
if cat.store != nil {
|
||
return cat.store.FindByProvincialCode(code)
|
||
}
|
||
return cat.client.QueryByProvincialCode(code)
|
||
}
|
||
|
||
// QueryByLibraryFileID 根据片库文件 ID 反查标识信息。
|
||
func (cat *Catalog) QueryByLibraryFileID(libraryID string) (model.ContentQueryResult, error) {
|
||
if cat.store != nil {
|
||
return cat.store.FindByLibraryFileID(libraryID)
|
||
}
|
||
return cat.client.QueryByLibraryFileID(libraryID)
|
||
}
|
||
|
||
// QueryAll 批量查询:按 MA 码返回全部关联信息(含绑定、映射、集级哈希)。
|
||
func (cat *Catalog) QueryAll(ccCode string) (model.ContentQueryResult, error) {
|
||
return cat.QueryByMA(ccCode)
|
||
}
|