parent
0aba7d60a1
commit
57399066c3
@ -1,4 +1,4 @@
|
|||||||
package mgotool
|
package mtool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -0,0 +1,103 @@
|
|||||||
|
package mgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/mgo/mtool"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/pagination"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewUserMongo(db *mongo.Database) (relation.UserModelInterface, error) {
|
||||||
|
return &UserMgo{
|
||||||
|
coll: db.Collection("user"),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserMgo struct {
|
||||||
|
coll *mongo.Collection
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) Create(ctx context.Context, users []*relation.UserModel) error {
|
||||||
|
return mtool.InsertMany(ctx, u.coll, users)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) UpdateByMap(ctx context.Context, userID string, args map[string]any) (err error) {
|
||||||
|
if len(args) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return mtool.UpdateOne(ctx, u.coll, bson.M{"user_id": userID}, bson.M{"$set": args}, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
||||||
|
return mtool.Find[*relation.UserModel](ctx, u.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
||||||
|
return mtool.FindOne[*relation.UserModel](ctx, u.coll, bson.M{"user_id": userID})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) Page(ctx context.Context, pagination pagination.Pagination) (count int64, users []*relation.UserModel, err error) {
|
||||||
|
return mtool.FindPage[*relation.UserModel](ctx, u.coll, bson.M{}, pagination)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) GetAllUserID(ctx context.Context, pagination pagination.Pagination) (int64, []string, error) {
|
||||||
|
return mtool.FindPage[string](ctx, u.coll, bson.M{}, pagination, options.Find().SetProjection(bson.M{"user_id": 1}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) Exist(ctx context.Context, userID string) (exist bool, err error) {
|
||||||
|
return mtool.Exist(ctx, u.coll, bson.M{"user_id": userID})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
||||||
|
return mtool.FindOne[int](ctx, u.coll, bson.M{"user_id": userID}, options.FindOne().SetProjection(bson.M{"global_recv_msg_opt": 1}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
||||||
|
if before == nil {
|
||||||
|
return mtool.Count(ctx, u.coll, bson.M{})
|
||||||
|
}
|
||||||
|
return mtool.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *UserMgo) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
||||||
|
pipeline := bson.A{
|
||||||
|
bson.M{
|
||||||
|
"$match": bson.M{
|
||||||
|
"create_time": bson.M{
|
||||||
|
"$gte": start,
|
||||||
|
"$lt": end,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
bson.M{
|
||||||
|
"$group": bson.M{
|
||||||
|
"_id": bson.M{
|
||||||
|
"$dateToString": bson.M{
|
||||||
|
"format": "%Y-%m-%d",
|
||||||
|
"date": "$create_time",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"count": bson.M{
|
||||||
|
"$sum": 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
type Item struct {
|
||||||
|
Date string `bson:"_id"`
|
||||||
|
Count int64 `bson:"count"`
|
||||||
|
}
|
||||||
|
items, err := mtool.Aggregate[Item](ctx, u.coll, pipeline)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
res := make(map[string]int64, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
res[item.Date] = item.Count
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
}
|
@ -1,71 +0,0 @@
|
|||||||
package newmgo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/newmgo/mgotool"
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/pagination"
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
|
||||||
"go.mongodb.org/mongo-driver/mongo/options"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewUserMongo(db *mongo.Database) (relation.UserModelInterface, error) {
|
|
||||||
return &UserMgo{
|
|
||||||
coll: db.Collection("user"),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserMgo struct {
|
|
||||||
coll *mongo.Collection
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) Create(ctx context.Context, users []*relation.UserModel) error {
|
|
||||||
return mgotool.InsertMany(ctx, u.coll, users)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) UpdateByMap(ctx context.Context, userID string, args map[string]any) (err error) {
|
|
||||||
if len(args) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return mgotool.UpdateOne(ctx, u.coll, bson.M{"user_id": userID}, bson.M{"$set": args}, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
|
||||||
return mgotool.Find[*relation.UserModel](ctx, u.coll, bson.M{"user_id": bson.M{"$in": userIDs}})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
|
||||||
return mgotool.FindOne[*relation.UserModel](ctx, u.coll, bson.M{"user_id": userID})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) Page(ctx context.Context, pagination pagination.Pagination) (count int64, users []*relation.UserModel, err error) {
|
|
||||||
return mgotool.FindPage[*relation.UserModel](ctx, u.coll, bson.M{}, pagination)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) GetAllUserID(ctx context.Context, pagination pagination.Pagination) (int64, []string, error) {
|
|
||||||
return mgotool.FindPage[string](ctx, u.coll, bson.M{}, pagination, options.Find().SetProjection(bson.M{"user_id": 1}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) Exist(ctx context.Context, userID string) (exist bool, err error) {
|
|
||||||
return mgotool.Exist(ctx, u.coll, bson.M{"user_id": userID})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
|
||||||
return mgotool.FindOne[int](ctx, u.coll, bson.M{"user_id": userID}, options.FindOne().SetProjection(bson.M{"global_recv_msg_opt": 1}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
|
||||||
return mgotool.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserMgo) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
|
||||||
//type Temp struct {
|
|
||||||
// CreateTime time.Time `bson:"create_time"`
|
|
||||||
// Number int64 `bson:"number"`
|
|
||||||
//}
|
|
||||||
//mgotool.Find(ctx, u.coll, bson.M{"create_time": bson.M{"$gte": start, "$lt": end}}, options.Find().SetProjection(bson.M{"create_time": 1}))
|
|
||||||
panic("implement me")
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
@ -1,111 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/errs"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/ormutil"
|
|
||||||
//
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type BlackGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewBlackGorm(db *gorm.DB) relation.BlackModelInterface {
|
|
||||||
// return &BlackGorm{NewMetaDB(db, &relation.BlackModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
|
||||||
// return utils.Wrap(b.db(ctx).Create(&blacks).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
|
||||||
// return utils.Wrap(b.db(ctx).Delete(blacks).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) UpdateByMap(
|
|
||||||
// ctx context.Context,
|
|
||||||
// ownerUserID, blockUserID string,
|
|
||||||
// args map[string]any,
|
|
||||||
//) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// b.db(ctx).Where("block_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Updates(args).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) Update(ctx context.Context, blacks []*relation.BlackModel) (err error) {
|
|
||||||
// return utils.Wrap(b.db(ctx).Updates(&blacks).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) Find(
|
|
||||||
// ctx context.Context,
|
|
||||||
// blacks []*relation.BlackModel,
|
|
||||||
//) (blackList []*relation.BlackModel, err error) {
|
|
||||||
// var where [][]any
|
|
||||||
// for _, black := range blacks {
|
|
||||||
// where = append(where, []any{black.OwnerUserID, black.BlockUserID})
|
|
||||||
// }
|
|
||||||
// return blackList, utils.Wrap(
|
|
||||||
// b.db(ctx).Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.BlackModel, err error) {
|
|
||||||
// black = &relation.BlackModel{}
|
|
||||||
// return black, utils.Wrap(
|
|
||||||
// b.db(ctx).Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) FindOwnerBlacks(
|
|
||||||
// ctx context.Context,
|
|
||||||
// ownerUserID string,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
//) (blacks []*relation.BlackModel, total int64, err error) {
|
|
||||||
// err = b.db(ctx).Count(&total).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, 0, utils.Wrap(err, "")
|
|
||||||
// }
|
|
||||||
// totalUint32, blacks, err := ormutil.GormPage[relation.BlackModel](
|
|
||||||
// b.db(ctx).Where("owner_user_id = ?", ownerUserID),
|
|
||||||
// pageNumber,
|
|
||||||
// showNumber,
|
|
||||||
// )
|
|
||||||
// total = int64(totalUint32)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) FindBlackUserIDs(ctx context.Context, ownerUserID string) (blackUserIDs []string, err error) {
|
|
||||||
// return blackUserIDs, utils.Wrap(
|
|
||||||
// b.db(ctx).Where("owner_user_id = ?", ownerUserID).Pluck("block_user_id", &blackUserIDs).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (b *BlackGorm) FindOwnerBlackInfos(ctx context.Context, ownerUserID string, userIDs []string) (blacks []*relation.BlackModel, err error) {
|
|
||||||
// return blacks, errs.Wrap(b.db(ctx).Where("owner_user_id = ? and block_user_id in ?", ownerUserID, userIDs).Find(&blacks).Error)
|
|
||||||
//}
|
|
@ -1,64 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//
|
|
||||||
//import (
|
|
||||||
// "github.com/golang/protobuf/jsonpb"
|
|
||||||
// "github.com/jinzhu/copier"
|
|
||||||
// "google.golang.org/protobuf/proto"
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/protocol/constant"
|
|
||||||
// pbmsg "github.com/OpenIMSDK/protocol/msg"
|
|
||||||
// sdkws "github.com/OpenIMSDK/protocol/sdkws"
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type ChatLogGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewChatLogGorm(db *gorm.DB) relation.ChatLogModelInterface {
|
|
||||||
// return &ChatLogGorm{NewMetaDB(db, &relation.ChatLogModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error {
|
|
||||||
// chatLog := new(relation.ChatLogModel)
|
|
||||||
// copier.Copy(chatLog, msg.MsgData)
|
|
||||||
// switch msg.MsgData.SessionType {
|
|
||||||
// case constant.GroupChatType, constant.SuperGroupChatType:
|
|
||||||
// chatLog.RecvID = msg.MsgData.GroupID
|
|
||||||
// case constant.SingleChatType:
|
|
||||||
// chatLog.RecvID = msg.MsgData.RecvID
|
|
||||||
// }
|
|
||||||
// if msg.MsgData.ContentType >= constant.NotificationBegin && msg.MsgData.ContentType <= constant.NotificationEnd {
|
|
||||||
// var tips sdkws.TipsComm
|
|
||||||
// _ = proto.Unmarshal(msg.MsgData.Content, &tips)
|
|
||||||
// marshaler := jsonpb.Marshaler{
|
|
||||||
// OrigName: true,
|
|
||||||
// EnumsAsInts: false,
|
|
||||||
// EmitDefaults: false,
|
|
||||||
// }
|
|
||||||
// chatLog.Content, _ = marshaler.MarshalToString(&tips)
|
|
||||||
// } else {
|
|
||||||
// chatLog.Content = string(msg.MsgData.Content)
|
|
||||||
// }
|
|
||||||
// chatLog.CreateTime = utils.UnixMillSecondToTime(msg.MsgData.CreateTime)
|
|
||||||
// chatLog.SendTime = utils.UnixMillSecondToTime(msg.MsgData.SendTime)
|
|
||||||
// return c.DB.Create(chatLog).Error
|
|
||||||
//}
|
|
@ -1,244 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/errs"
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/protocol/constant"
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type ConversationGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewConversationGorm(db *gorm.DB) relation.ConversationModelInterface {
|
|
||||||
// return &ConversationGorm{NewMetaDB(db, &relation.ConversationModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) NewTx(tx any) relation.ConversationModelInterface {
|
|
||||||
// return &ConversationGorm{NewMetaDB(tx.(*gorm.DB), &relation.ConversationModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) Create(ctx context.Context, conversations []*relation.ConversationModel) (err error) {
|
|
||||||
// return utils.Wrap(c.db(ctx).Create(&conversations).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) Delete(ctx context.Context, groupIDs []string) (err error) {
|
|
||||||
// return utils.Wrap(c.db(ctx).Where("group_id in (?)", groupIDs).Delete(&relation.ConversationModel{}).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) UpdateByMap(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userIDList []string,
|
|
||||||
// conversationID string,
|
|
||||||
// args map[string]any,
|
|
||||||
//) (rows int64, err error) {
|
|
||||||
// result := c.db(ctx).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args)
|
|
||||||
// return result.RowsAffected, utils.Wrap(result.Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) Update(ctx context.Context, conversation *relation.ConversationModel) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where("owner_user_id = ? and conversation_id = ?", conversation.OwnerUserID, conversation.ConversationID).
|
|
||||||
// Updates(conversation).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) Find(
|
|
||||||
// ctx context.Context,
|
|
||||||
// ownerUserID string,
|
|
||||||
// conversationIDs []string,
|
|
||||||
//) (conversations []*relation.ConversationModel, err error) {
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where("owner_user_id=? and conversation_id IN (?)", ownerUserID, conversationIDs).
|
|
||||||
// Find(&conversations).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return conversations, err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) Take(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userID, conversationID string,
|
|
||||||
//) (conversation *relation.ConversationModel, err error) {
|
|
||||||
// cc := &relation.ConversationModel{}
|
|
||||||
// return cc, utils.Wrap(
|
|
||||||
// c.db(ctx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) FindUserID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userIDs []string,
|
|
||||||
// conversationIDs []string,
|
|
||||||
//) (existUserID []string, err error) {
|
|
||||||
// return existUserID, utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where(" owner_user_id IN (?) and conversation_id in (?)", userIDs, conversationIDs).
|
|
||||||
// Pluck("owner_user_id", &existUserID).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) FindConversationID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userID string,
|
|
||||||
// conversationIDList []string,
|
|
||||||
//) (existConversationID []string, err error) {
|
|
||||||
// return existConversationID, utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where(" conversation_id IN (?) and owner_user_id=?", conversationIDList, userID).
|
|
||||||
// Pluck("conversation_id", &existConversationID).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) FindUserIDAllConversationID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userID string,
|
|
||||||
//) (conversationIDList []string, err error) {
|
|
||||||
// return conversationIDList, utils.Wrap(
|
|
||||||
// c.db(ctx).Where("owner_user_id=?", userID).Pluck("conversation_id", &conversationIDList).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) FindUserIDAllConversations(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userID string,
|
|
||||||
//) (conversations []*relation.ConversationModel, err error) {
|
|
||||||
// return conversations, utils.Wrap(c.db(ctx).Where("owner_user_id=?", userID).Find(&conversations).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) FindRecvMsgNotNotifyUserIDs(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
//) (userIDs []string, err error) {
|
|
||||||
// return userIDs, utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where("group_id = ? and recv_msg_opt = ?", groupID, constant.ReceiveNotNotifyMessage).
|
|
||||||
// Pluck("owner_user_id", &userIDs).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) FindSuperGroupRecvMsgNotNotifyUserIDs(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
//) (userIDs []string, err error) {
|
|
||||||
// return userIDs, utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where("group_id = ? and recv_msg_opt = ? and conversation_type = ?", groupID, constant.ReceiveNotNotifyMessage, constant.SuperGroupChatType).
|
|
||||||
// Pluck("owner_user_id", &userIDs).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetUserRecvMsgOpt(
|
|
||||||
// ctx context.Context,
|
|
||||||
// ownerUserID, conversationID string,
|
|
||||||
//) (opt int, err error) {
|
|
||||||
// var conversation relation.ConversationModel
|
|
||||||
// return int(
|
|
||||||
// conversation.RecvMsgOpt,
|
|
||||||
// ), utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where("conversation_id = ? And owner_user_id = ?", conversationID, ownerUserID).
|
|
||||||
// Select("recv_msg_opt").
|
|
||||||
// Find(&conversation).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetAllConversationIDs(ctx context.Context) (conversationIDs []string, err error) {
|
|
||||||
// return conversationIDs, utils.Wrap(
|
|
||||||
// c.db(ctx).Distinct("conversation_id").Pluck("conversation_id", &conversationIDs).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetAllConversationIDsNumber(ctx context.Context) (int64, error) {
|
|
||||||
// var num int64
|
|
||||||
// err := c.db(ctx).Select("COUNT(DISTINCT conversation_id)").Model(&relation.ConversationModel{}).Count(&num).Error
|
|
||||||
// return num, errs.Wrap(err)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) PageConversationIDs(ctx context.Context, pageNumber, showNumber int32) (conversationIDs []string, err error) {
|
|
||||||
// err = c.db(ctx).Distinct("conversation_id").Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("conversation_id", &conversationIDs).Error
|
|
||||||
// err = errs.Wrap(err)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetConversationsByConversationID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// conversationIDs []string,
|
|
||||||
//) (conversations []*relation.ConversationModel, err error) {
|
|
||||||
// return conversations, utils.Wrap(
|
|
||||||
// c.db(ctx).Where("conversation_id IN (?)", conversationIDs).Find(&conversations).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetConversationIDsNeedDestruct(
|
|
||||||
// ctx context.Context,
|
|
||||||
//) (conversations []*relation.ConversationModel, err error) {
|
|
||||||
// return conversations, utils.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Where("is_msg_destruct = 1 && msg_destruct_time != 0 && (UNIX_TIMESTAMP(NOW()) > (msg_destruct_time + UNIX_TIMESTAMP(latest_msg_destruct_time)) || latest_msg_destruct_time is NULL)").
|
|
||||||
// Find(&conversations).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID string, conversationID string) (int32, error) {
|
|
||||||
// var recvMsgOpt int32
|
|
||||||
// return recvMsgOpt, errs.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Model(&relation.ConversationModel{}).
|
|
||||||
// Where("conversation_id = ? and owner_user_id in ?", conversationID, userID).
|
|
||||||
// Pluck("recv_msg_opt", &recvMsgOpt).
|
|
||||||
// Error,
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (c *ConversationGorm) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) {
|
|
||||||
// var userIDs []string
|
|
||||||
// return userIDs, errs.Wrap(
|
|
||||||
// c.db(ctx).
|
|
||||||
// Model(&relation.ConversationModel{}).
|
|
||||||
// Where("conversation_id = ? and recv_msg_opt <> ?", conversationID, constant.ReceiveMessage).
|
|
||||||
// Pluck("owner_user_id", &userIDs).Error,
|
|
||||||
// )
|
|
||||||
//}
|
|
@ -1,15 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation // import "github.com/openimsdk/open-im-server/v3/pkg/common/db/relation"
|
|
@ -1,154 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
// type FriendRequestGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func NewFriendRequestGorm(db *gorm.DB) relation.FriendRequestModelInterface {
|
|
||||||
// return &FriendRequestGorm{NewMetaDB(db, &relation.FriendRequestModel{})}
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (f *FriendRequestGorm) NewTx(tx any) relation.FriendRequestModelInterface {
|
|
||||||
// return &FriendRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.FriendRequestModel{})}
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 插入多条记录.
|
|
||||||
// func (f *FriendRequestGorm) Create(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) {
|
|
||||||
// return utils.Wrap(f.db(ctx).Create(&friendRequests).Error, "")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 删除记录.
|
|
||||||
// func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID string) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// f.db(ctx).
|
|
||||||
// Where("from_user_id = ? AND to_user_id = ?", fromUserID, toUserID).
|
|
||||||
// Delete(&relation.FriendRequestModel{}).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 更新零值.
|
|
||||||
// func (f *FriendRequestGorm) UpdateByMap(
|
|
||||||
// ctx context.Context,
|
|
||||||
// fromUserID string,
|
|
||||||
// toUserID string,
|
|
||||||
// args map[string]any,
|
|
||||||
// ) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// f.db(ctx).
|
|
||||||
// Model(&relation.FriendRequestModel{}).
|
|
||||||
// Where("from_user_id = ? AND to_user_id =?", fromUserID, toUserID).
|
|
||||||
// Updates(args).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 更新记录 (非零值).
|
|
||||||
// func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
|
|
||||||
// fr2 := *friendRequest
|
|
||||||
// fr2.FromUserID = ""
|
|
||||||
// fr2.ToUserID = ""
|
|
||||||
// return utils.Wrap(
|
|
||||||
// f.db(ctx).
|
|
||||||
// Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).
|
|
||||||
// Updates(fr2).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 获取来指定用户的好友申请 未找到 不返回错误.
|
|
||||||
// func (f *FriendRequestGorm) Find(
|
|
||||||
// ctx context.Context,
|
|
||||||
// fromUserID, toUserID string,
|
|
||||||
// ) (friendRequest *relation.FriendRequestModel, err error) {
|
|
||||||
// friendRequest = &relation.FriendRequestModel{}
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return friendRequest, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (f *FriendRequestGorm) Take(
|
|
||||||
// ctx context.Context,
|
|
||||||
// fromUserID, toUserID string,
|
|
||||||
// ) (friendRequest *relation.FriendRequestModel, err error) {
|
|
||||||
// friendRequest = &relation.FriendRequestModel{}
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return friendRequest, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 获取toUserID收到的好友申请列表.
|
|
||||||
// func (f *FriendRequestGorm) FindToUserID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// toUserID string,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
// ) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
|
|
||||||
// err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("to_user_id = ? ", toUserID).Count(&total).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, 0, utils.Wrap(err, "")
|
|
||||||
// }
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// f.db(ctx).
|
|
||||||
// Where("to_user_id = ? ", toUserID).
|
|
||||||
// Limit(int(showNumber)).
|
|
||||||
// Offset(int(pageNumber-1)*int(showNumber)).
|
|
||||||
// Find(&friendRequests).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 获取fromUserID发出去的好友申请列表.
|
|
||||||
// func (f *FriendRequestGorm) FindFromUserID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// fromUserID string,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
// ) (friendRequests []*relation.FriendRequestModel, total int64, err error) {
|
|
||||||
// err = f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? ", fromUserID).Count(&total).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, 0, utils.Wrap(err, "")
|
|
||||||
// }
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// f.db(ctx).
|
|
||||||
// Where("from_user_id = ? ", fromUserID).
|
|
||||||
// Limit(int(showNumber)).
|
|
||||||
// Offset(int(pageNumber-1)*int(showNumber)).
|
|
||||||
// Find(&friendRequests).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) {
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// f.db(ctx).
|
|
||||||
// Where("(from_user_id = ? AND to_user_id = ?) OR (from_user_id = ? AND to_user_id = ?)", fromUserID, toUserID, toUserID, fromUserID).
|
|
||||||
// Find(&friends).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return
|
|
||||||
// }
|
|
@ -1,198 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
//
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/protocol/constant"
|
|
||||||
// "github.com/OpenIMSDK/tools/ormutil"
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//var _ relation.GroupMemberModelInterface = (*GroupMemberGorm)(nil)
|
|
||||||
//
|
|
||||||
//type GroupMemberGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewGroupMemberDB(db *gorm.DB) relation.GroupMemberModelInterface {
|
|
||||||
// return &GroupMemberGorm{NewMetaDB(db, &relation.GroupMemberModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) NewTx(tx any) relation.GroupMemberModelInterface {
|
|
||||||
// return &GroupMemberGorm{NewMetaDB(tx.(*gorm.DB), &relation.GroupMemberModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel) (err error) {
|
|
||||||
// return utils.Wrap(g.db(ctx).Create(&groupMemberList).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// g.db(ctx).Where("group_id = ? and user_id in (?)", groupID, userIDs).Delete(&relation.GroupMemberModel{}).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string) (err error) {
|
|
||||||
// return utils.Wrap(g.db(ctx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any) (err error) {
|
|
||||||
// return utils.Wrap(g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Updates(data).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) UpdateRoleLevel(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
// userID string,
|
|
||||||
// roleLevel int32,
|
|
||||||
//) (rowsAffected int64, err error) {
|
|
||||||
// db := g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
|
||||||
// "role_level": roleLevel,
|
|
||||||
// })
|
|
||||||
// return db.RowsAffected, utils.Wrap(db.Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) Find(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupIDs []string,
|
|
||||||
// userIDs []string,
|
|
||||||
// roleLevels []int32,
|
|
||||||
//) (groupMembers []*relation.GroupMemberModel, err error) {
|
|
||||||
// db := g.db(ctx)
|
|
||||||
// if len(groupIDs) > 0 {
|
|
||||||
// db = db.Where("group_id in (?)", groupIDs)
|
|
||||||
// }
|
|
||||||
// if len(userIDs) > 0 {
|
|
||||||
// db = db.Where("user_id in (?)", userIDs)
|
|
||||||
// }
|
|
||||||
// if len(roleLevels) > 0 {
|
|
||||||
// db = db.Where("role_level in (?)", roleLevels)
|
|
||||||
// }
|
|
||||||
// return groupMembers, utils.Wrap(db.Find(&groupMembers).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) Take(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
// userID string,
|
|
||||||
//) (groupMember *relation.GroupMemberModel, err error) {
|
|
||||||
// groupMember = &relation.GroupMemberModel{}
|
|
||||||
// return groupMember, utils.Wrap(
|
|
||||||
// g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) TakeOwner(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
//) (groupMember *relation.GroupMemberModel, err error) {
|
|
||||||
// groupMember = &relation.GroupMemberModel{}
|
|
||||||
// return groupMember, utils.Wrap(
|
|
||||||
// g.db(ctx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) SearchMember(
|
|
||||||
// ctx context.Context,
|
|
||||||
// keyword string,
|
|
||||||
// groupIDs []string,
|
|
||||||
// userIDs []string,
|
|
||||||
// roleLevels []int32,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
//) (total uint32, groupList []*relation.GroupMemberModel, err error) {
|
|
||||||
// db := g.db(ctx)
|
|
||||||
// ormutil.GormIn(&db, "group_id", groupIDs)
|
|
||||||
// ormutil.GormIn(&db, "user_id", userIDs)
|
|
||||||
// ormutil.GormIn(&db, "role_level", roleLevels)
|
|
||||||
// return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) MapGroupMemberNum(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupIDs []string,
|
|
||||||
//) (count map[string]uint32, err error) {
|
|
||||||
// return ormutil.MapCount(g.db(ctx).Where("group_id in (?)", groupIDs), "group_id")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) FindJoinUserID(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupIDs []string,
|
|
||||||
//) (groupUsers map[string][]string, err error) {
|
|
||||||
// var groupMembers []*relation.GroupMemberModel
|
|
||||||
// if err := g.db(ctx).Select("group_id, user_id").Where("group_id in (?)", groupIDs).Find(&groupMembers).Error; err != nil {
|
|
||||||
// return nil, utils.Wrap(err, "")
|
|
||||||
// }
|
|
||||||
// groupUsers = make(map[string][]string)
|
|
||||||
// for _, item := range groupMembers {
|
|
||||||
// v, ok := groupUsers[item.GroupID]
|
|
||||||
// if !ok {
|
|
||||||
// groupUsers[item.GroupID] = []string{item.UserID}
|
|
||||||
// } else {
|
|
||||||
// groupUsers[item.GroupID] = append(v, item.UserID)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return groupUsers, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) FindMemberUserID(ctx context.Context, groupID string) (userIDs []string, err error) {
|
|
||||||
// return userIDs, utils.Wrap(g.db(ctx).Where("group_id = ?", groupID).Pluck("user_id", &userIDs).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) FindUserJoinedGroupID(ctx context.Context, userID string) (groupIDs []string, err error) {
|
|
||||||
// return groupIDs, utils.Wrap(g.db(ctx).Where("user_id = ?", userID).Pluck("group_id", &groupIDs).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) TakeGroupMemberNum(ctx context.Context, groupID string) (count int64, err error) {
|
|
||||||
// return count, utils.Wrap(g.db(ctx).Where("group_id = ?", groupID).Count(&count).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) FindUsersJoinedGroupID(ctx context.Context, userIDs []string) (map[string][]string, error) {
|
|
||||||
// var groupMembers []*relation.GroupMemberModel
|
|
||||||
// err := g.db(ctx).Select("group_id, user_id").Where("user_id IN (?)", userIDs).Find(&groupMembers).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// result := make(map[string][]string)
|
|
||||||
// for _, groupMember := range groupMembers {
|
|
||||||
// v, ok := result[groupMember.UserID]
|
|
||||||
// if !ok {
|
|
||||||
// result[groupMember.UserID] = []string{groupMember.GroupID}
|
|
||||||
// } else {
|
|
||||||
// result[groupMember.UserID] = append(v, groupMember.GroupID)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return result, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupMemberGorm) FindUserManagedGroupID(ctx context.Context, userID string) (groupIDs []string, err error) {
|
|
||||||
// return groupIDs, utils.Wrap(
|
|
||||||
// g.db(ctx).
|
|
||||||
// Model(&relation.GroupMemberModel{}).
|
|
||||||
// Where("user_id = ? and (role_level = ? or role_level = ?)", userID, constant.GroupOwner, constant.GroupAdmin).
|
|
||||||
// Pluck("group_id", &groupIDs).
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
//}
|
|
@ -1,106 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
// "time"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/protocol/constant"
|
|
||||||
//
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/errs"
|
|
||||||
// "github.com/OpenIMSDK/tools/ormutil"
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//var _ relation.GroupModelInterface = (*GroupGorm)(nil)
|
|
||||||
//
|
|
||||||
//type GroupGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
|
|
||||||
// return &GroupGorm{NewMetaDB(db, &relation.GroupModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) NewTx(tx any) relation.GroupModelInterface {
|
|
||||||
// return &GroupGorm{NewMetaDB(tx.(*gorm.DB), &relation.GroupModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel) (err error) {
|
|
||||||
// return utils.Wrap(g.DB.Create(&groups).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]any) (err error) {
|
|
||||||
// return utils.Wrap(g.DB.Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32) (err error) {
|
|
||||||
// return utils.Wrap(g.DB.Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) Find(ctx context.Context, groupIDs []string) (groups []*relation.GroupModel, err error) {
|
|
||||||
// return groups, utils.Wrap(g.DB.Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.GroupModel, err error) {
|
|
||||||
// group = &relation.GroupModel{}
|
|
||||||
// return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
|
|
||||||
// db := g.DB
|
|
||||||
// db = db.WithContext(ctx).Where("status!=?", constant.GroupStatusDismissed)
|
|
||||||
// return ormutil.GormSearch[relation.GroupModel](db, []string{"name"}, keyword, pageNumber, showNumber)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
|
|
||||||
// return groupIDs, utils.Wrap(g.DB.Model(&relation.GroupModel{}).Where("group_type = ? ", groupType).Pluck("group_id", &groupIDs).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
|
||||||
// db := g.db(ctx).Model(&relation.GroupModel{})
|
|
||||||
// if before != nil {
|
|
||||||
// db = db.Where("create_time < ?", before)
|
|
||||||
// }
|
|
||||||
// if err := db.Count(&count).Error; err != nil {
|
|
||||||
// return 0, err
|
|
||||||
// }
|
|
||||||
// return count, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
|
||||||
// var res []struct {
|
|
||||||
// Date time.Time `gorm:"column:date"`
|
|
||||||
// Count int64 `gorm:"column:count"`
|
|
||||||
// }
|
|
||||||
// err := g.db(ctx).Model(&relation.GroupModel{}).Select("DATE(create_time) AS date, count(1) AS count").Where("create_time >= ? and create_time < ?", start, end).Group("date").Find(&res).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// v := make(map[string]int64)
|
|
||||||
// for _, r := range res {
|
|
||||||
// v[r.Date.Format("2006-01-02")] = r.Count
|
|
||||||
// }
|
|
||||||
// return v, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupGorm) FindNotDismissedGroup(ctx context.Context, groupIDs []string) (groups []*relation.GroupModel, err error) {
|
|
||||||
// return groups, utils.Wrap(g.DB.Where("group_id in (?) and status != ?", groupIDs, constant.GroupStatusDismissed).Find(&groups).Error, "")
|
|
||||||
//}
|
|
@ -1,119 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/ormutil"
|
|
||||||
//
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type GroupRequestGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewGroupRequest(db *gorm.DB) relation.GroupRequestModelInterface {
|
|
||||||
// return &GroupRequestGorm{
|
|
||||||
// NewMetaDB(db, &relation.GroupRequestModel{}),
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) NewTx(tx any) relation.GroupRequestModelInterface {
|
|
||||||
// return &GroupRequestGorm{NewMetaDB(tx.(*gorm.DB), &relation.GroupRequestModel{})}
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel) (err error) {
|
|
||||||
// return utils.Wrap(g.DB.WithContext(ctx).Create(&groupRequests).Error, utils.GetSelfFuncName())
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) Delete(ctx context.Context, groupID string, userID string) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// g.DB.WithContext(ctx).
|
|
||||||
// Where("group_id = ? and user_id = ? ", groupID, userID).
|
|
||||||
// Delete(&relation.GroupRequestModel{}).
|
|
||||||
// Error,
|
|
||||||
// utils.GetSelfFuncName(),
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) UpdateHandler(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
// userID string,
|
|
||||||
// handledMsg string,
|
|
||||||
// handleResult int32,
|
|
||||||
//) (err error) {
|
|
||||||
// return utils.Wrap(
|
|
||||||
// g.DB.WithContext(ctx).
|
|
||||||
// Model(&relation.GroupRequestModel{}).
|
|
||||||
// Where("group_id = ? and user_id = ? ", groupID, userID).
|
|
||||||
// Updates(map[string]any{
|
|
||||||
// "handle_msg": handledMsg,
|
|
||||||
// "handle_result": handleResult,
|
|
||||||
// }).
|
|
||||||
// Error,
|
|
||||||
// utils.GetSelfFuncName(),
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) Take(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupID string,
|
|
||||||
// userID string,
|
|
||||||
//) (groupRequest *relation.GroupRequestModel, err error) {
|
|
||||||
// groupRequest = &relation.GroupRequestModel{}
|
|
||||||
// return groupRequest, utils.Wrap(
|
|
||||||
// g.DB.WithContext(ctx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error,
|
|
||||||
// utils.GetSelfFuncName(),
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) Page(
|
|
||||||
// ctx context.Context,
|
|
||||||
// userID string,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
//) (total uint32, groups []*relation.GroupRequestModel, err error) {
|
|
||||||
// return ormutil.GormSearch[relation.GroupRequestModel](
|
|
||||||
// g.DB.WithContext(ctx).Where("user_id = ?", userID),
|
|
||||||
// nil,
|
|
||||||
// "",
|
|
||||||
// pageNumber,
|
|
||||||
// showNumber,
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) PageGroup(
|
|
||||||
// ctx context.Context,
|
|
||||||
// groupIDs []string,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
//) (total uint32, groups []*relation.GroupRequestModel, err error) {
|
|
||||||
// return ormutil.GormPage[relation.GroupRequestModel](
|
|
||||||
// g.DB.WithContext(ctx).Where("group_id in ?", groupIDs),
|
|
||||||
// pageNumber,
|
|
||||||
// showNumber,
|
|
||||||
// )
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (g *GroupRequestGorm) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (total int64, groupRequests []*relation.GroupRequestModel, err error) {
|
|
||||||
// err = g.DB.WithContext(ctx).Where("group_id = ? and user_id in ?", groupID, userIDs).Find(&groupRequests).Error
|
|
||||||
// return int64(len(groupRequests)), groupRequests, utils.Wrap(err, utils.GetSelfFuncName())
|
|
||||||
//}
|
|
@ -1,49 +0,0 @@
|
|||||||
package relation
|
|
||||||
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
// "time"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/errs"
|
|
||||||
// "github.com/OpenIMSDK/tools/ormutil"
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// relationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type LogGorm struct {
|
|
||||||
// db *gorm.DB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (l *LogGorm) Create(ctx context.Context, log []*relationtb.Log) error {
|
|
||||||
// return errs.Wrap(l.db.WithContext(ctx).Create(log).Error)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (l *LogGorm) Search(ctx context.Context, keyword string, start time.Time, end time.Time, pageNumber int32, showNumber int32) (uint32, []*relationtb.Log, error) {
|
|
||||||
// db := l.db.WithContext(ctx).Where("create_time >= ?", start)
|
|
||||||
// if end.UnixMilli() != 0 {
|
|
||||||
// db = l.db.WithContext(ctx).Where("create_time <= ?", end)
|
|
||||||
// }
|
|
||||||
// db = db.Order("create_time desc")
|
|
||||||
// return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (l *LogGorm) Delete(ctx context.Context, logIDs []string, userID string) error {
|
|
||||||
// if userID == "" {
|
|
||||||
// return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Delete(&relationtb.Log{}).Error)
|
|
||||||
// }
|
|
||||||
// return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Delete(&relationtb.Log{}).Error)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (l *LogGorm) Get(ctx context.Context, logIDs []string, userID string) ([]*relationtb.Log, error) {
|
|
||||||
// var logs []*relationtb.Log
|
|
||||||
// if userID == "" {
|
|
||||||
// return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Find(&logs).Error)
|
|
||||||
// }
|
|
||||||
// return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewLogGorm(db *gorm.DB) relationtb.LogInterface {
|
|
||||||
// db.AutoMigrate(&relationtb.Log{})
|
|
||||||
// return &LogGorm{db: db}
|
|
||||||
//}
|
|
@ -1,54 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
//
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/errs"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type ObjectInfoGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewObjectInfo(db *gorm.DB) relation.ObjectInfoModelInterface {
|
|
||||||
// return &ObjectInfoGorm{
|
|
||||||
// NewMetaDB(db, &relation.ObjectModel{}),
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (o *ObjectInfoGorm) NewTx(tx any) relation.ObjectInfoModelInterface {
|
|
||||||
// return &ObjectInfoGorm{
|
|
||||||
// NewMetaDB(tx.(*gorm.DB), &relation.ObjectModel{}),
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (o *ObjectInfoGorm) SetObject(ctx context.Context, obj *relation.ObjectModel) (err error) {
|
|
||||||
// if err := o.DB.WithContext(ctx).Where("name = ?", obj.Name).FirstOrCreate(obj).Error; err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (o *ObjectInfoGorm) Take(ctx context.Context, name string) (info *relation.ObjectModel, err error) {
|
|
||||||
// info = &relation.ObjectModel{}
|
|
||||||
// return info, errs.Wrap(o.DB.WithContext(ctx).Where("name = ?", name).Take(info).Error)
|
|
||||||
//}
|
|
@ -1,138 +0,0 @@
|
|||||||
// Copyright © 2023 OpenIM. All rights reserved.
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package relation
|
|
||||||
|
|
||||||
//
|
|
||||||
//import (
|
|
||||||
// "context"
|
|
||||||
// "time"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/errs"
|
|
||||||
//
|
|
||||||
// "gorm.io/gorm"
|
|
||||||
//
|
|
||||||
// "github.com/OpenIMSDK/tools/utils"
|
|
||||||
//
|
|
||||||
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
|
||||||
//)
|
|
||||||
//
|
|
||||||
//type UserGorm struct {
|
|
||||||
// *MetaDB
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func NewUserGorm(db *gorm.DB) relation.UserModelInterface {
|
|
||||||
// //return &UserGorm{NewMetaDB(db, &relation.UserModel{})}
|
|
||||||
// return nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 插入多条.
|
|
||||||
//func (u *UserGorm) Create(ctx context.Context, users []*relation.UserModel) (err error) {
|
|
||||||
// return utils.Wrap(u.db(ctx).Create(&users).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 更新用户信息 零值.
|
|
||||||
//func (u *UserGorm) UpdateByMap(ctx context.Context, userID string, args map[string]any) (err error) {
|
|
||||||
// return utils.Wrap(u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Updates(args).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 更新多个用户信息 非零值.
|
|
||||||
//func (u *UserGorm) Update(ctx context.Context, user *relation.UserModel) (err error) {
|
|
||||||
// return utils.Wrap(u.db(ctx).Model(user).Updates(user).Error, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 获取指定用户信息 不存在,也不返回错误.
|
|
||||||
//func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
|
||||||
// err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "")
|
|
||||||
// return users, err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 获取某个用户信息 不存在,则返回错误.
|
|
||||||
//func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
|
||||||
// user = &relation.UserModel{}
|
|
||||||
// err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "")
|
|
||||||
// return user, err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 获取用户信息 不存在,不返回错误.
|
|
||||||
//func (u *UserGorm) Page(
|
|
||||||
// ctx context.Context,
|
|
||||||
// pageNumber, showNumber int32,
|
|
||||||
//) (users []*relation.UserModel, count int64, err error) {
|
|
||||||
// err = utils.Wrap(u.db(ctx).Count(&count).Error, "")
|
|
||||||
// if err != nil {
|
|
||||||
// return
|
|
||||||
// }
|
|
||||||
// err = utils.Wrap(
|
|
||||||
// u.db(ctx).
|
|
||||||
// Limit(int(showNumber)).
|
|
||||||
// Offset(int((pageNumber-1)*showNumber)).
|
|
||||||
// Find(&users).
|
|
||||||
// Order("create_time DESC").
|
|
||||||
// Error,
|
|
||||||
// "",
|
|
||||||
// )
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// 获取所有用户ID.
|
|
||||||
//func (u *UserGorm) GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) {
|
|
||||||
// if pageNumber == 0 || showNumber == 0 {
|
|
||||||
// return userIDs, errs.Wrap(u.db(ctx).Pluck("user_id", &userIDs).Error)
|
|
||||||
// } else {
|
|
||||||
// return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (u *UserGorm) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
|
||||||
// err = u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Pluck("global_recv_msg_opt", &opt).Error
|
|
||||||
// return opt, err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (u *UserGorm) CountTotal(ctx context.Context, before *time.Time) (count int64, err error) {
|
|
||||||
// db := u.db(ctx).Model(&relation.UserModel{})
|
|
||||||
// if before != nil {
|
|
||||||
// db = db.Where("create_time < ?", before)
|
|
||||||
// }
|
|
||||||
// if err := db.Count(&count).Error; err != nil {
|
|
||||||
// return 0, err
|
|
||||||
// }
|
|
||||||
// return count, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func (u *UserGorm) CountRangeEverydayTotal(
|
|
||||||
// ctx context.Context,
|
|
||||||
// start time.Time,
|
|
||||||
// end time.Time,
|
|
||||||
//) (map[string]int64, error) {
|
|
||||||
// var res []struct {
|
|
||||||
// Date time.Time `gorm:"column:date"`
|
|
||||||
// Count int64 `gorm:"column:count"`
|
|
||||||
// }
|
|
||||||
// err := u.db(ctx).
|
|
||||||
// Model(&relation.UserModel{}).
|
|
||||||
// Select("DATE(create_time) AS date, count(1) AS count").
|
|
||||||
// Where("create_time >= ? and create_time < ?", start, end).
|
|
||||||
// Group("date").
|
|
||||||
// Find(&res).
|
|
||||||
// Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// v := make(map[string]int64)
|
|
||||||
// for _, r := range res {
|
|
||||||
// v[r.Date.Format("2006-01-02")] = r.Count
|
|
||||||
// }
|
|
||||||
// return v, nil
|
|
||||||
//}
|
|
@ -0,0 +1 @@
|
|||||||
|
package internal
|
@ -0,0 +1 @@
|
|||||||
|
package internal
|
@ -0,0 +1,295 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/mgo"
|
||||||
|
mongoModel "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
|
mysqlModel "github.com/openimsdk/open-im-server/v3/tools/data-conversion/openim/mysql/v3"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/logger"
|
||||||
|
"log"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var (
|
||||||
|
mysqlUsername = "root" // mysql用户名
|
||||||
|
mysqlPassword = "openIM123" // mysql密码
|
||||||
|
mysqlAddr = "127.0.0.1:13306" // mysql地址
|
||||||
|
mysqlDatabase = "openIM_v3" // mysql数据库名字
|
||||||
|
)
|
||||||
|
|
||||||
|
var s3 = "minio" // 文件储存方式 minio, cos, oss
|
||||||
|
|
||||||
|
var (
|
||||||
|
mongoUsername = "root" // mysql用户名
|
||||||
|
mongoPassword = "openIM123" // mysql密码
|
||||||
|
mongoHosts = "127.0.0.1:13306" // mysql地址
|
||||||
|
mongoDatabase = "openIM_v3" // mysql数据库名字
|
||||||
|
)
|
||||||
|
|
||||||
|
mysqlAddr = "172.16.8.142:13306"
|
||||||
|
mongoHosts = "172.16.8.142:37017"
|
||||||
|
|
||||||
|
mysqlDSN := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysqlUsername, mysqlPassword, mysqlAddr, mysqlDatabase)
|
||||||
|
mysqlDB, err := gorm.Open(mysql.Open(mysqlDSN), &gorm.Config{Logger: logger.Discard})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("open mysql db failed", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("open mysql db success")
|
||||||
|
var mongoURI string
|
||||||
|
if mongoPassword != "" && mongoUsername != "" {
|
||||||
|
mongoURI = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
||||||
|
mongoUsername, mongoPassword, mongoHosts,
|
||||||
|
mongoDatabase, 100)
|
||||||
|
} else {
|
||||||
|
mongoURI = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin", mongoHosts, mongoDatabase, 100)
|
||||||
|
}
|
||||||
|
mongoClient, err := mongo.Connect(context.Background(), options.Client().ApplyURI(mongoURI))
|
||||||
|
if err != nil {
|
||||||
|
log.Println("open mongo db failed", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Println("open mongo db success")
|
||||||
|
mongoDB := mongoClient.Database(mongoDatabase)
|
||||||
|
|
||||||
|
c := convert{}
|
||||||
|
|
||||||
|
var tasks []func() error
|
||||||
|
tasks = append(tasks,
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewUserMongo, c.User) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewFriendMongo, c.Friend) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewFriendRequestMongo, c.FriendRequest) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewBlackMongo, c.Black) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewGroupMongo, c.Group) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewGroupMember, c.GroupMember) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewGroupRequestMgo, c.GroupRequest) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewConversationMongo, c.Conversation) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewS3Mongo, c.Object(s3)) },
|
||||||
|
func() error { return newTask(mysqlDB, mongoDB, mgo.NewLogMongo, c.Log) },
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, task := range tasks {
|
||||||
|
if err := task(); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type convert struct{}
|
||||||
|
|
||||||
|
func (convert) User(v mysqlModel.UserModel) mongoModel.UserModel {
|
||||||
|
return mongoModel.UserModel{
|
||||||
|
UserID: v.UserID,
|
||||||
|
Nickname: v.Nickname,
|
||||||
|
FaceURL: v.FaceURL,
|
||||||
|
Ex: v.Ex,
|
||||||
|
AppMangerLevel: v.AppMangerLevel,
|
||||||
|
GlobalRecvMsgOpt: v.GlobalRecvMsgOpt,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) Friend(v mysqlModel.FriendModel) mongoModel.FriendModel {
|
||||||
|
return mongoModel.FriendModel{
|
||||||
|
OwnerUserID: v.OwnerUserID,
|
||||||
|
FriendUserID: v.FriendUserID,
|
||||||
|
Remark: v.Remark,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
AddSource: v.AddSource,
|
||||||
|
OperatorUserID: v.OperatorUserID,
|
||||||
|
Ex: v.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) FriendRequest(v mysqlModel.FriendRequestModel) mongoModel.FriendRequestModel {
|
||||||
|
return mongoModel.FriendRequestModel{
|
||||||
|
FromUserID: v.FromUserID,
|
||||||
|
ToUserID: v.ToUserID,
|
||||||
|
HandleResult: v.HandleResult,
|
||||||
|
ReqMsg: v.ReqMsg,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
HandlerUserID: v.HandlerUserID,
|
||||||
|
HandleMsg: v.HandleMsg,
|
||||||
|
HandleTime: v.HandleTime,
|
||||||
|
Ex: v.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) Black(v mysqlModel.BlackModel) mongoModel.BlackModel {
|
||||||
|
return mongoModel.BlackModel{
|
||||||
|
OwnerUserID: v.OwnerUserID,
|
||||||
|
BlockUserID: v.BlockUserID,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
AddSource: v.AddSource,
|
||||||
|
OperatorUserID: v.OperatorUserID,
|
||||||
|
Ex: v.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) Group(v mysqlModel.GroupModel) mongoModel.GroupModel {
|
||||||
|
return mongoModel.GroupModel{
|
||||||
|
GroupID: v.GroupID,
|
||||||
|
GroupName: v.GroupName,
|
||||||
|
Notification: v.Notification,
|
||||||
|
Introduction: v.Introduction,
|
||||||
|
FaceURL: v.FaceURL,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
Ex: v.Ex,
|
||||||
|
Status: v.Status,
|
||||||
|
CreatorUserID: v.CreatorUserID,
|
||||||
|
GroupType: v.GroupType,
|
||||||
|
NeedVerification: v.NeedVerification,
|
||||||
|
LookMemberInfo: v.LookMemberInfo,
|
||||||
|
ApplyMemberFriend: v.ApplyMemberFriend,
|
||||||
|
NotificationUpdateTime: v.NotificationUpdateTime,
|
||||||
|
NotificationUserID: v.NotificationUserID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) GroupMember(v mysqlModel.GroupMemberModel) mongoModel.GroupMemberModel {
|
||||||
|
return mongoModel.GroupMemberModel{
|
||||||
|
GroupID: v.GroupID,
|
||||||
|
UserID: v.UserID,
|
||||||
|
Nickname: v.Nickname,
|
||||||
|
FaceURL: v.FaceURL,
|
||||||
|
RoleLevel: v.RoleLevel,
|
||||||
|
JoinTime: v.JoinTime,
|
||||||
|
JoinSource: v.JoinSource,
|
||||||
|
InviterUserID: v.InviterUserID,
|
||||||
|
OperatorUserID: v.OperatorUserID,
|
||||||
|
MuteEndTime: v.MuteEndTime,
|
||||||
|
Ex: v.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) GroupRequest(v mysqlModel.GroupRequestModel) mongoModel.GroupRequestModel {
|
||||||
|
return mongoModel.GroupRequestModel{
|
||||||
|
UserID: v.UserID,
|
||||||
|
GroupID: v.GroupID,
|
||||||
|
HandleResult: v.HandleResult,
|
||||||
|
ReqMsg: v.ReqMsg,
|
||||||
|
HandledMsg: v.HandledMsg,
|
||||||
|
ReqTime: v.ReqTime,
|
||||||
|
HandleUserID: v.HandleUserID,
|
||||||
|
HandledTime: v.HandledTime,
|
||||||
|
JoinSource: v.JoinSource,
|
||||||
|
InviterUserID: v.InviterUserID,
|
||||||
|
Ex: v.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) Conversation(v mysqlModel.ConversationModel) mongoModel.ConversationModel {
|
||||||
|
return mongoModel.ConversationModel{
|
||||||
|
OwnerUserID: v.OwnerUserID,
|
||||||
|
ConversationID: v.ConversationID,
|
||||||
|
ConversationType: v.ConversationType,
|
||||||
|
UserID: v.UserID,
|
||||||
|
GroupID: v.GroupID,
|
||||||
|
RecvMsgOpt: v.RecvMsgOpt,
|
||||||
|
IsPinned: v.IsPinned,
|
||||||
|
IsPrivateChat: v.IsPrivateChat,
|
||||||
|
BurnDuration: v.BurnDuration,
|
||||||
|
GroupAtType: v.GroupAtType,
|
||||||
|
AttachedInfo: v.AttachedInfo,
|
||||||
|
Ex: v.Ex,
|
||||||
|
MaxSeq: v.MaxSeq,
|
||||||
|
MinSeq: v.MinSeq,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
IsMsgDestruct: v.IsMsgDestruct,
|
||||||
|
MsgDestructTime: v.MsgDestructTime,
|
||||||
|
LatestMsgDestructTime: v.LatestMsgDestructTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) Object(engine string) func(v mysqlModel.ObjectModel) mongoModel.ObjectModel {
|
||||||
|
return func(v mysqlModel.ObjectModel) mongoModel.ObjectModel {
|
||||||
|
return mongoModel.ObjectModel{
|
||||||
|
Name: v.Name,
|
||||||
|
UserID: v.UserID,
|
||||||
|
Hash: v.Hash,
|
||||||
|
Engine: engine,
|
||||||
|
Key: v.Key,
|
||||||
|
Size: v.Size,
|
||||||
|
ContentType: v.ContentType,
|
||||||
|
Group: v.Cause,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (convert) Log(v mysqlModel.Log) mongoModel.LogModel {
|
||||||
|
return mongoModel.LogModel{
|
||||||
|
LogID: v.LogID,
|
||||||
|
Platform: v.Platform,
|
||||||
|
UserID: v.UserID,
|
||||||
|
CreateTime: v.CreateTime,
|
||||||
|
Url: v.Url,
|
||||||
|
FileName: v.FileName,
|
||||||
|
SystemType: v.SystemType,
|
||||||
|
Version: v.Version,
|
||||||
|
Ex: v.Ex,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTask[MYSQLTABLE interface{ TableName() string }, MONGOMODEL any, MONGOTABLE any](gormDB *gorm.DB, mongoDB *mongo.Database, mongoDBInit func(db *mongo.Database) (MONGOMODEL, error), convert func(v MYSQLTABLE) MONGOTABLE) error {
|
||||||
|
obj, err := mongoDBInit(mongoDB)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var zero MYSQLTABLE
|
||||||
|
tableName := zero.TableName()
|
||||||
|
coll, err := getColl(obj)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get mongo collection %s failed, err: %w", tableName, err)
|
||||||
|
}
|
||||||
|
var count int
|
||||||
|
defer func() {
|
||||||
|
log.Printf("convert %s %d records", tableName, count)
|
||||||
|
}()
|
||||||
|
const batch = 100
|
||||||
|
for page := 0; ; page++ {
|
||||||
|
var res []MYSQLTABLE
|
||||||
|
if err := gormDB.Find(&res).Limit(batch).Offset(page * batch).Error; err != nil {
|
||||||
|
return fmt.Errorf("find table %s failed, err: %w", tableName, err)
|
||||||
|
}
|
||||||
|
if len(res) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
temp := make([]any, len(res))
|
||||||
|
for i := range res {
|
||||||
|
temp[i] = convert(res[i])
|
||||||
|
}
|
||||||
|
if _, err := coll.InsertMany(context.Background(), temp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
count += len(res)
|
||||||
|
if len(res) < batch {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getColl(obj any) (_ *mongo.Collection, err error) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
err = fmt.Errorf("not found %+v", e)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
stu := reflect.ValueOf(obj).Elem()
|
||||||
|
typ := reflect.TypeOf(&mongo.Collection{}).String()
|
||||||
|
for i := 0; i < stu.NumField(); i++ {
|
||||||
|
field := stu.Field(i)
|
||||||
|
if field.Type().String() == typ {
|
||||||
|
return (*mongo.Collection)(field.UnsafePointer()), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/mgo"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getColl1(obj any) (_ *mongo.Collection, err error) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
err = errors.New("not found")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
stu := reflect.ValueOf(obj).Elem()
|
||||||
|
typ := reflect.TypeOf(&mongo.Collection{}).String()
|
||||||
|
for i := 0; i < stu.NumField(); i++ {
|
||||||
|
field := stu.Field(i)
|
||||||
|
if field.Type().String() == typ {
|
||||||
|
return (*mongo.Collection)(field.UnsafePointer()), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, errors.New("not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestName(t *testing.T) {
|
||||||
|
coll, err := getColl1(&mgo.GroupMgo{})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(coll)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue