From 1d0661ae4029890cdd738035a912d97527b26642 Mon Sep 17 00:00:00 2001 From: ayflying Date: Wed, 2 Apr 2025 12:12:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BC=BA=E5=B0=91=E7=9A=84?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/random.go | 32 ++++++++++++++++++++++++++++++++ tools/redis.go | 2 +- tools/time.go | 7 +++++++ tools/tools.go | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/tools/random.go b/tools/random.go index a67afee..fb81fd7 100644 --- a/tools/random.go +++ b/tools/random.go @@ -1,6 +1,8 @@ package tools import ( + "github.com/gogf/gf/v2/util/gconv" + "github.com/gogf/gf/v2/util/grand" "math/rand" "time" ) @@ -50,3 +52,33 @@ func (m *randMod) RandomAll(data map[int]int, n int) []int { } return result } + +// RandByArrInt 根据传入的 interface 切片中的整数值按权重随机返回一个索引 +// 参数 s: 一个包含整数的 interface 切片,切片中的每个元素代表一个权重 +// 返回值: 随机选中的元素的索引 +func RandByArrInt(s []interface{}) int { + // 初始化总权重为 0 + sv := 0 + // 遍历切片,累加每个元素的权重 + for i := range s { + sv += gconv.Int(s[i]) + } + if sv < 1 { + return 0 + } + + // 使用 grand.Intn 生成一个 0 到总权重之间的随机数 + r := grand.Intn(sv) + // 初始化当前累加的权重为 0 + var all int + // 再次遍历切片,累加权重 + for i := range s { + all += gconv.Int(s[i]) + // 如果当前累加的权重大于随机数,则返回当前索引 + if all > r { + return i + } + } + // 如果没有找到符合条件的索引,返回 0 + return 0 +} diff --git a/tools/redis.go b/tools/redis.go index 8cd7ca2..2d773c5 100644 --- a/tools/redis.go +++ b/tools/redis.go @@ -14,7 +14,7 @@ type redis struct { } func (r *redis) Load() { - g.Log().Debugf(gctx.New(), "初始化工具类") + g.Log().Debugf(gctx.New(), "初始化redis工具类") if Redis == nil { Redis = &redis{} } diff --git a/tools/time.go b/tools/time.go index ec87623..388fd57 100644 --- a/tools/time.go +++ b/tools/time.go @@ -121,3 +121,10 @@ func (m *timeMod) GetDailyTimeList(time1 time.Time, time2 time.Time) (timeList [ } return } + +// ExcelTime2Time excel时间转时间 (12/10/24 02:03转为时间) +func (m *timeMod) ExcelTime2Time(excelTime string) time.Time { + layout := "01/02/06 15:04" // 月/日/年(最后两位) 小时:分钟 (24小时制) + timeNew, _ := time.ParseInLocation(layout, excelTime, time.Local) + return timeNew +} diff --git a/tools/tools.go b/tools/tools.go index 309d449..ab740ad 100644 --- a/tools/tools.go +++ b/tools/tools.go @@ -1,6 +1,7 @@ package tools import ( + "encoding/json" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gtime" @@ -30,7 +31,7 @@ type tools struct { } func init() { - g.Log().Debugf(gctx.New(), "初始化工具类") + g.Log().Debugf(gctx.New(), "初始化tools工具类") names := []toolsInterface{ Tools, @@ -253,3 +254,33 @@ func (m *tools) ItemsMerge(_items ...[][]int64) [][]int64 { } return items } + +// ProcessingMap 处理map +// 该函数用于递归处理一个键为字符串、值为int64的map。 +// 如果键是一个JSON字符串,会尝试将其解析为一个新的map,并递归处理这个新的map。 +// 最终返回一个处理后的map,其中所有键都是非JSON字符串。 +func (m *tools) ProcessingMap(data map[string]int64) map[string]int64 { + // 创建一个临时map,用于存储处理后的键值对 + var temp = make(map[string]int64) + // 遍历输入的map + for k, v := range data { + // 创建一个新的map,用于存储解析后的JSON数据 + data_k := make(map[string]int64) + // 尝试将键解析为JSON数据 + err := json.Unmarshal([]byte(k), &data_k) + // 如果解析成功 + if err == nil { + // 递归处理解析后的map + data_kmap := m.ProcessingMap(data_k) + // 返回处理后的map + // 如果解析失败,直接将原键值对添加到临时map中 + // 将递归处理后的结果合并到临时map中 + for k_k, k_v := range data_kmap { + temp[k_k] = k_v + } + } else { + temp[k] = v + } + } + return temp +}