This commit is contained in:
孟帅
2024-03-07 20:08:56 +08:00
parent 6dd8cbadad
commit 0fbc1ad47c
246 changed files with 9441 additions and 2293 deletions

View File

@@ -0,0 +1,61 @@
// Package dict
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package dict
import (
"context"
"errors"
"fmt"
"hash/fnv"
"hotgo/internal/model"
"strconv"
)
const (
BuiltinId int64 = -1 // 内置字典ID
EnumsId int64 = -2 // 枚举字典ID
FuncId int64 = -3 // 方法字典ID
)
var NotExistKeyError = errors.New("not exist key")
// GetOptions 获取内置选项
func GetOptions(ctx context.Context, key string) (opts []*model.Option, err error) {
opts = GetEnumsOptions(key)
if opts != nil {
return
}
return GetFuncOptions(ctx, key)
}
// GetOptionsById 通过类型ID获取内置选项
func GetOptionsById(ctx context.Context, id int64) (opts []*model.Option, err error) {
for _, v := range GetAllEnums() {
if v.Id == id {
return v.Opts, nil
}
}
for _, v := range GetAllFunc() {
if v.Id == id {
return LoadFuncOptions(ctx, v)
}
}
err = NotExistKeyError
return
}
// GenIdHash 生成字典id
func GenIdHash(str string, t int64) int64 {
prefix := 10000 * t
h := fnv.New32a()
h.Write([]byte("dict" + str))
idStr := fmt.Sprintf("%d%d", prefix, int64(h.Sum32()))
id, _ := strconv.ParseInt(idStr, 10, 64)
return id
}

View File

@@ -0,0 +1,106 @@
// Package dict
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package dict
import (
"github.com/gogf/gf/v2/util/gconv"
"hash/fnv"
"hotgo/internal/model"
)
// GenDefaultOption 生成默认表格回显样式
func GenDefaultOption(key interface{}, label string) *model.Option {
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: "default",
}
}
func GenSuccessOption(key interface{}, label string) *model.Option {
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: "success",
}
}
func GenWarningOption(key interface{}, label string) *model.Option {
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: "warning",
}
}
func GenErrorOption(key interface{}, label string) *model.Option {
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: "error",
}
}
func GenInfoOption(key interface{}, label string) *model.Option {
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: "info",
}
}
// GenCustomOption 生成自定义表格回显样式
func GenCustomOption(key interface{}, label string, custom string) *model.Option {
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: custom,
}
}
// GenHashOption 根据不同label以hash算法生成表格回显样式
func GenHashOption(key interface{}, label string) *model.Option {
strings := []string{"default", "primary", "info", "success", "warning", "error"}
hash := fnv.New32()
tag := "default"
if _, err := hash.Write(gconv.Bytes(label)); err == nil {
index := int(hash.Sum32()) % len(strings)
tag = strings[index]
}
return &model.Option{
Key: key,
Label: label,
Value: key,
ListClass: tag,
}
}
// GetOptionLabel 通过key找到label
func GetOptionLabel(ses []*model.Option, key interface{}) string {
for _, v := range ses {
if gconv.String(v.Key) == gconv.String(key) {
return v.Label
}
}
return `Unknown`
}
// HasOptionKey 是否存在指定key
func HasOptionKey(ses []*model.Option, key interface{}) bool {
for _, v := range ses {
if gconv.String(v.Key) == gconv.String(key) {
return true
}
}
return false
}

View File

@@ -0,0 +1,72 @@
// Package dict
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package dict
import (
"fmt"
"hotgo/internal/model"
"sync"
)
type EnumsOption struct {
Id int64 // 字典ID由系统自动生成
Key string // 字典选项key
Label string // 字典选项标签名称
Opts []*model.Option // 数据选项
}
var (
enumsOptions = make(map[string]*EnumsOption)
eLock sync.Mutex
)
// GetAllEnums 获取所有枚举字典
func GetAllEnums() map[string]*EnumsOption {
return enumsOptions
}
// RegisterEnums 注册枚举字典选项
func RegisterEnums(key, label string, opts []*model.Option) {
eLock.Lock()
defer eLock.Unlock()
if len(key) == 0 {
panic("字典key不能为空")
}
if _, ok := enumsOptions[key]; ok {
panic(fmt.Sprintf("重复注册枚举字典选项:%v", key))
}
for _, v := range opts {
v.Type = key
}
enumsOptions[key] = &EnumsOption{
Id: GenIdHash(key, EnumsId),
Key: key,
Label: label,
Opts: opts,
}
}
// SaveEnums 更新枚举字典选项
func SaveEnums(key, label string, opts []*model.Option) {
eLock.Lock()
defer eLock.Unlock()
if _, ok := enumsOptions[key]; ok {
delete(enumsOptions, key)
}
RegisterEnums(key, label, opts)
}
// GetEnumsOptions 获取指定枚举字典的数据选项
func GetEnumsOptions(key string) []*model.Option {
enums, ok := enumsOptions[key]
if !ok {
return nil
}
return enums.Opts
}

View File

@@ -0,0 +1,131 @@
// Package dict
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package dict
import (
"context"
"fmt"
"hotgo/internal/model"
"sync"
)
// FuncDict 方法字典,实现本接口即可使用内置方法字典
type FuncDict func(ctx context.Context) (res []*model.Option, err error)
type FuncOption struct {
Id int64 // 字典ID由系统自动生成
Key string // 字典选项key
Label string // 字典选项标签名称
Fun FuncDict // 字典方法
Cache bool // 是否缓存数据选项
Opts []*model.Option // 缓存的数据选项
sync.Mutex
}
var (
funcOptions = make(map[string]*FuncOption)
fLock sync.Mutex
)
// GetAllFunc 获取所有方法字典
func GetAllFunc() map[string]*FuncOption {
return funcOptions
}
// RegisterFunc 注册方法字典选项
func RegisterFunc(key, label string, fun FuncDict, cache ...bool) {
fLock.Lock()
defer fLock.Unlock()
if len(key) == 0 {
panic("字典key不能为空")
}
if _, ok := funcOptions[key]; ok {
panic(fmt.Sprintf("重复注册方法选项:%v", key))
}
isCache := false
if len(cache) > 0 {
isCache = cache[0]
}
funcOptions[key] = &FuncOption{
Id: GenIdHash(key, FuncId),
Key: key,
Label: label,
Fun: fun,
Cache: isCache,
Opts: nil,
}
}
// SaveFunc 更新方法字典选项
func SaveFunc(key, label string, fun FuncDict, cache ...bool) {
fLock.Lock()
defer fLock.Unlock()
if _, ok := funcOptions[key]; ok {
delete(funcOptions, key)
}
RegisterFunc(key, label, fun, cache...)
}
// ClearFuncCache 清理指定方法缓存选项
func ClearFuncCache(key string) (err error) {
fun, ok := funcOptions[key]
if !ok {
err = NotExistKeyError
return
}
fun.Lock()
defer fun.Unlock()
if fun.Opts != nil {
fun.Opts = nil
}
return
}
// GetFuncOptions 获取指定方法字典的数据选项
func GetFuncOptions(ctx context.Context, key string) (res []*model.Option, err error) {
fun, ok := funcOptions[key]
if !ok {
err = NotExistKeyError
return
}
return LoadFuncOptions(ctx, fun)
}
// LoadFuncOptions 加载指定方法字典的数据选项
func LoadFuncOptions(ctx context.Context, fun *FuncOption) (res []*model.Option, err error) {
if fun.Cache && fun.Opts != nil {
res = fun.Opts
return
}
fun.Lock()
defer fun.Unlock()
if fun.Cache && fun.Opts != nil {
res = fun.Opts
return
}
res, err = fun.Fun(ctx)
if err != nil {
return nil, err
}
for k := range res {
res[k].Type = fun.Key
}
if fun.Cache {
fun.Opts = res
}
return
}