2025-11-25 11:51:16 +08:00
|
|
|
|
package mongo
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2025-11-25 13:47:20 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2025-11-25 11:51:16 +08:00
|
|
|
|
"gitee.com/red-future---jilin-g/common/utils"
|
|
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
2025-12-02 11:27:35 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2025-11-25 11:51:16 +08:00
|
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2025-12-02 11:27:35 +08:00
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2025-11-25 13:47:20 +08:00
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
2025-11-25 11:51:16 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var db = new(mongo.Database)
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
|
defer cancel()
|
2025-11-28 14:01:08 +08:00
|
|
|
|
mongoAddr := g.Cfg().MustGet(context.Background(), "mongo.address").String()
|
2025-11-25 13:47:20 +08:00
|
|
|
|
opt := options.Client().ApplyURI(mongoAddr)
|
|
|
|
|
|
client, err := mongo.Connect(opt)
|
2025-11-25 11:51:16 +08:00
|
|
|
|
if err != nil {
|
2025-11-25 13:47:20 +08:00
|
|
|
|
glog.Error(ctx, "mongodb连接失败", err)
|
2025-11-25 11:51:16 +08:00
|
|
|
|
}
|
2025-11-25 13:47:20 +08:00
|
|
|
|
// 从连接串中解析数据库名
|
2025-11-25 11:51:16 +08:00
|
|
|
|
dbName := gstr.SubStr(mongoAddr, strings.LastIndex(mongoAddr, "/")+1, len(mongoAddr))
|
2025-11-25 13:47:20 +08:00
|
|
|
|
// 如果连接串带有参数(如 ?retryWrites=true),需要去掉参数部分
|
|
|
|
|
|
if strings.Contains(dbName, "?") {
|
|
|
|
|
|
dbName = gstr.SubStr(dbName, 0, strings.Index(dbName, "?"))
|
|
|
|
|
|
}
|
2025-11-25 11:51:16 +08:00
|
|
|
|
db = client.Database(dbName)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Find 查询多条记录
|
2025-11-25 14:19:52 +08:00
|
|
|
|
func Find(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOptions]) (err error) {
|
2025-11-25 11:51:16 +08:00
|
|
|
|
if err = utils.ValidStructPtr(result); err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-02 11:27:35 +08:00
|
|
|
|
filter["isDeleted"] = false
|
|
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter["tenantId"] = user.TenantId
|
2025-11-25 11:51:16 +08:00
|
|
|
|
cur, err := db.Collection(collection).Find(ctx, filter, opts...)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err = cur.All(ctx, result)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FindOne 查询1条记录
|
2025-11-25 14:19:52 +08:00
|
|
|
|
func FindOne(ctx context.Context, filter bson.M, result interface{}, collection string, opts ...options.Lister[options.FindOneOptions]) (err error) {
|
2025-12-02 11:27:35 +08:00
|
|
|
|
if len(filter) == 0 {
|
|
|
|
|
|
err = gerror.New("缺少查询条件")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-25 11:51:16 +08:00
|
|
|
|
if err = utils.ValidStructPtr(result); err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-02 11:27:35 +08:00
|
|
|
|
filter["isDeleted"] = false
|
|
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter["tenantId"] = user.TenantId
|
2025-11-25 11:51:16 +08:00
|
|
|
|
cur := db.Collection(collection).FindOne(ctx, filter, opts...)
|
|
|
|
|
|
err = cur.Decode(result)
|
|
|
|
|
|
if err == mongo.ErrNoDocuments {
|
|
|
|
|
|
err = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Delete 删除记录
|
2025-11-25 14:19:52 +08:00
|
|
|
|
func Delete(ctx context.Context, filter bson.M, collection string, opts ...options.Lister[options.DeleteManyOptions]) (count int64, err error) {
|
2025-11-28 17:13:16 +08:00
|
|
|
|
if len(filter) == 0 {
|
|
|
|
|
|
err = gerror.New("缺少查询条件")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-02 11:27:35 +08:00
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter["tenantId"] = user.TenantId
|
2025-11-25 11:51:16 +08:00
|
|
|
|
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
count = r.DeletedCount
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update 修改记录
|
2025-12-02 15:56:57 +08:00
|
|
|
|
func Update(ctx context.Context, filter bson.M, update bson.M, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.UpdateResult, err error) {
|
2025-11-28 17:13:16 +08:00
|
|
|
|
if len(filter) == 0 {
|
|
|
|
|
|
err = gerror.New("缺少查询条件")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-12-02 11:27:35 +08:00
|
|
|
|
filter["isDeleted"] = false
|
|
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter["tenantId"] = user.TenantId
|
2025-12-02 15:56:57 +08:00
|
|
|
|
setDoc := update["$set"].(bson.M)
|
|
|
|
|
|
setDoc["updater"] = user.UserName
|
|
|
|
|
|
setDoc["updatedAt"] = gtime.Now().Time
|
|
|
|
|
|
update = bson.M{"$set": setDoc}
|
|
|
|
|
|
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
|
2025-11-25 11:51:16 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-25 13:47:20 +08:00
|
|
|
|
// Insert 插入多条记录
|
2025-11-25 14:19:52 +08:00
|
|
|
|
func Insert(ctx context.Context, documents []interface{}, collection string, opts ...options.Lister[options.InsertManyOptions]) (ids []interface{}, err error) {
|
2025-12-02 11:27:35 +08:00
|
|
|
|
user, err := utils.GetUserInfo(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
docs := make([]interface{}, 0, len(documents))
|
|
|
|
|
|
for _, document := range documents {
|
|
|
|
|
|
doc := gconv.Map(document)
|
|
|
|
|
|
delete(doc, "id")
|
|
|
|
|
|
doc["creator"] = user.UserName
|
|
|
|
|
|
doc["createdAt"] = gtime.Now().Time
|
|
|
|
|
|
doc["updater"] = user.UserName
|
|
|
|
|
|
doc["updatedAt"] = gtime.Now().Time
|
|
|
|
|
|
doc["tenantId"] = user.TenantId
|
|
|
|
|
|
doc["isDeleted"] = false
|
|
|
|
|
|
docs = append(docs, doc)
|
|
|
|
|
|
}
|
|
|
|
|
|
r, err := db.Collection(collection).InsertMany(ctx, docs, opts...)
|
2025-11-25 11:51:16 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
ids = r.InsertedIDs
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-11-25 16:37:29 +08:00
|
|
|
|
|
2025-11-28 14:01:08 +08:00
|
|
|
|
// Count 查询总数
|
2025-11-25 16:37:29 +08:00
|
|
|
|
func Count(ctx context.Context, filter bson.M, collection string) (count int64, err error) {
|
2025-12-02 11:27:35 +08:00
|
|
|
|
filter["isDeleted"] = false
|
2025-11-25 16:37:29 +08:00
|
|
|
|
// 调用驱动的 CountDocuments,在数据库端执行的
|
|
|
|
|
|
count, err = db.Collection(collection).CountDocuments(ctx, filter)
|
|
|
|
|
|
return
|
2025-11-28 14:01:08 +08:00
|
|
|
|
}
|