68 lines
1.5 KiB
Go
68 lines
1.5 KiB
Go
package exporter
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
)
|
|
|
|
// Registry 导出数据源注册中心。
|
|
type Registry struct {
|
|
sources map[string]DataSource
|
|
}
|
|
|
|
// NewRegistry 创建导出数据源注册中心。
|
|
func NewRegistry(sources ...DataSource) *Registry {
|
|
m := make(map[string]DataSource, len(sources))
|
|
for _, source := range sources {
|
|
if source == nil {
|
|
continue
|
|
}
|
|
m[source.Scene()] = source
|
|
}
|
|
return &Registry{sources: m}
|
|
}
|
|
|
|
// NewDefaultRegistry 创建默认导出数据源注册中心。
|
|
func NewDefaultRegistry(db *gorm.DB) *Registry {
|
|
return NewRegistry(
|
|
NewDeviceDataSource(db),
|
|
NewIotCardDataSource(db),
|
|
NewOrderDataSource(db),
|
|
)
|
|
}
|
|
|
|
// Get 获取指定场景数据源。
|
|
func (r *Registry) Get(scene string) (DataSource, bool) {
|
|
source, ok := r.sources[scene]
|
|
return source, ok
|
|
}
|
|
|
|
// IsSupported 判断场景是否支持。
|
|
func (r *Registry) IsSupported(scene string) bool {
|
|
_, ok := r.sources[scene]
|
|
return ok
|
|
}
|
|
|
|
// Scenes 返回当前支持的场景列表。
|
|
func (r *Registry) Scenes() []string {
|
|
result := make([]string, 0, len(r.sources))
|
|
for scene := range r.sources {
|
|
result = append(result, scene)
|
|
}
|
|
sort.Strings(result)
|
|
return result
|
|
}
|
|
|
|
// IsSupportedScene 判断是否为受支持的场景。
|
|
func IsSupportedScene(scene string) bool {
|
|
switch scene {
|
|
case constants.ExportTaskSceneDevice, constants.ExportTaskSceneIotCard, constants.ExportTaskSceneOrder:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|