From 7d2214903741d7991dd487b1ab10857f55e94a45 Mon Sep 17 00:00:00 2001 From: Leufolium Date: Wed, 17 Jul 2024 17:12:37 +0800 Subject: [PATCH] 1 --- app/mix/dao/mongo.go | 2 +- bizcommon/util/util.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index d79d0bb8..b98d5255 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -2319,7 +2319,7 @@ func (m *Mongo) CreateStreamer(ctx *gin.Context, streamer *dbstruct.Streamer) er func (m *Mongo) UpdateStreamer(ctx *gin.Context, streamer *dbstruct.Streamer) error { col := m.getColStreamer() - set := util.EntityToM(streamer) + set := util.DeepEntityToM(streamer) set["ut"] = time.Now().Unix() filter := qmgo.M{ "mid": util.DerefInt64(streamer.Mid), diff --git a/bizcommon/util/util.go b/bizcommon/util/util.go index c2ef77a3..44ee4f8f 100644 --- a/bizcommon/util/util.go +++ b/bizcommon/util/util.go @@ -35,6 +35,23 @@ func StringToMd5(s string) string { // 将实体类结构体指针转为bson.M,采用反射 func EntityToM(p any) qmgo.M { + set := qmgo.M{} + pType := reflect.TypeOf(p).Elem() + pVal := reflect.ValueOf(p).Elem() + for i := 0; i < pType.NumField(); i++ { + name := pType.Field(i).Tag.Get("bson") + if name != "" { + field := pVal.Field(i) + if name != "_id" && !field.IsNil() { + value := field.Elem().Interface() + set[name] = value + } + } + } + return set +} + +func DeepEntityToM(p any) qmgo.M { set := qmgo.M{} pType := reflect.TypeOf(p).Elem() pVal := reflect.ValueOf(p).Elem()