优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -27,16 +27,16 @@ type OrderBy struct {
|
||||
Order OrderEnum `p:"order"` //排序方式
|
||||
}
|
||||
type MongoBaseDO struct {
|
||||
Id bson.ObjectID `bson:"_id,omitempty" json:"id"` // MongoDB 默认 ID
|
||||
Creator interface{} `bson:"creator,omitempty" json:"creator"`
|
||||
CreatedAt time.Time `bson:"created_at,omitempty" json:"createdAt"`
|
||||
Updater interface{} `bson:"updater,omitempty" json:"updater"`
|
||||
UpdatedAt time.Time `bson:"updated_at,omitempty" json:"updatedAt"`
|
||||
TenantId interface{} `bson:"tenant_id" json:"tenantId" default:"1"` // 租户ID
|
||||
IsDeleted bool `bson:"is_deleted" json:"isDeleted" default:"false"`
|
||||
Id *bson.ObjectID `bson:"_id,omitempty" json:"id"` // MongoDB 默认 ID
|
||||
Creator interface{} `bson:"creator,omitempty" json:"creator"`
|
||||
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt"`
|
||||
Updater interface{} `bson:"updater,omitempty" json:"updater"`
|
||||
UpdatedAt *time.Time `bson:"updatedAt,omitempty" json:"updatedAt"`
|
||||
TenantId interface{} `bson:"tenantId" json:"tenantId" default:"1"` // 租户ID
|
||||
IsDeleted bool `bson:"isDeleted" json:"isDeleted" default:"false"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
UserName interface{} `bson:"user_name" json:"userName"` // MongoDB 默认 ID
|
||||
TenantId interface{} `bson:"tenant_id" json:"tenantId"` // 租户ID
|
||||
UserName interface{} `bson:"userName" json:"userName"` // MongoDB 默认 ID
|
||||
TenantId interface{} `bson:"tenantId" json:"tenantId"` // 租户ID
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ func (d *log) List(ctx context.Context, filter *dto.ListLogsReq, sortFields ...s
|
||||
}
|
||||
|
||||
var logs []*entity.OperationLog
|
||||
err = mongo.DB().Find(ctx, bsonFilter, &logs, consts.OperationLogCollection, nil, nil)
|
||||
_, err = mongo.DB().Find(ctx, bsonFilter, &logs, consts.OperationLogCollection, nil, nil)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -12,6 +11,7 @@ import (
|
||||
"gitee.com/red-future---jilin-g/common/beans"
|
||||
"gitee.com/red-future---jilin-g/common/log/model/dto"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/os/grpool"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/redis"
|
||||
"gitee.com/red-future---jilin-g/common/utils"
|
||||
@@ -174,8 +174,11 @@ func reconnect() error {
|
||||
return gerror.New("MongoDB重连失败,已达到最大重试次数")
|
||||
}
|
||||
|
||||
var logPool *grpool.Pool
|
||||
|
||||
// init 初始化MongoDB连接
|
||||
func init() {
|
||||
logPool = grpool.New(10)
|
||||
// 按需初始化:没有配置 mongo.address 则跳过
|
||||
mongoAddr = g.Cfg().MustGet(context.Background(), "mongo.address").String()
|
||||
if mongoAddr == "" {
|
||||
@@ -225,10 +228,10 @@ func close() {
|
||||
const PageSize = 20
|
||||
|
||||
// Find 查询多条记录
|
||||
func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, collection string, page *beans.Page, orderBy []beans.OrderBy) (err error) {
|
||||
if err = utils.ValidStructPtr(result); err != nil {
|
||||
return
|
||||
}
|
||||
func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, collection string, page *beans.Page, orderBy []beans.OrderBy) (total int64, err error) {
|
||||
//if err = utils.ValidStructPtr(result); err != nil {
|
||||
// return
|
||||
//}
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -237,12 +240,6 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
filterKey := fmt.Sprintf("%+v", filter)
|
||||
optionsKey := fmt.Sprintf("%+v%+v", page, orderBy)
|
||||
redisKey := fmt.Sprintf(redis.List, user.TenantId, collection, filterKey, optionsKey)
|
||||
resultValue := reflect.ValueOf(result)
|
||||
if resultValue.IsNil() || resultValue.IsZero() {
|
||||
return errors.New("result不能为空")
|
||||
}
|
||||
resultValue = resultValue.Elem()
|
||||
listField := resultValue.FieldByName("List")
|
||||
if m.Cache {
|
||||
var resultStr *gvar.Var
|
||||
resultStr, err = redis.RedisClient.Get(ctx, redisKey)
|
||||
@@ -250,7 +247,10 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
return
|
||||
}
|
||||
if !resultStr.IsEmpty() {
|
||||
resultStr.Structs(listField.Addr().Interface())
|
||||
if err = resultStr.Structs(result); err != nil {
|
||||
return
|
||||
}
|
||||
total = int64(len(resultStr.Array()))
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -261,19 +261,20 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
skip := int64(0)
|
||||
if page != nil {
|
||||
limit = page.PageSize
|
||||
skip = (page.PageNum - 1) * limit
|
||||
if skip <= 0 {
|
||||
if limit == -1 {
|
||||
skip = 0
|
||||
} else {
|
||||
skip = (page.PageNum - 1) * limit
|
||||
}
|
||||
}
|
||||
total, err := m.Count(ctx, filter, collection)
|
||||
if err != nil || total == 0 {
|
||||
return
|
||||
}
|
||||
utils.SetValue(ctx, result, "Total", total)
|
||||
opt := options.Find().SetSkip(skip)
|
||||
if limit != -1 {
|
||||
opt.SetLimit(limit)
|
||||
} else {
|
||||
total, err = m.Count(ctx, filter, collection)
|
||||
if err != nil || total == 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
if orderBy == nil {
|
||||
opt.SetSort(bson.M{"createdAt": -1})
|
||||
@@ -293,25 +294,17 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if limit == -1 {
|
||||
total = int64(cur.RemainingBatchLength())
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
|
||||
// 先解码到[]bson.M,再转换到目标类型,处理datetime到string的转换
|
||||
var docs []bson.M
|
||||
if err = cur.All(ctx, &docs); err != nil {
|
||||
return
|
||||
}
|
||||
for _, v := range docs {
|
||||
v["id"] = v["_id"].(bson.ObjectID).Hex()
|
||||
delete(v, "_id")
|
||||
}
|
||||
// 使用gconv转换,处理类型转换
|
||||
if err = gconv.Structs(docs, listField.Addr().Interface()); err != nil {
|
||||
if err = cur.All(ctx, result); err != nil {
|
||||
return
|
||||
}
|
||||
if m.Cache {
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, docs, int64(time.Hour))
|
||||
err = redis.RedisClient.SetEX(ctx, redisKey, result, int64(time.Hour))
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -408,16 +401,17 @@ func (m *MongoDB) Delete(ctx context.Context, filter bson.M, collection string,
|
||||
return
|
||||
}
|
||||
filter["tenantId"] = user.TenantId
|
||||
var rows []interface{}
|
||||
if err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := db.Collection(collection).DeleteMany(ctx, filter, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
count = r.DeletedCount
|
||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||
//写日志
|
||||
var rows []interface{}
|
||||
if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
||||
logRedisKey := fmt.Sprintf("log:%s", serverName)
|
||||
if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{
|
||||
@@ -450,17 +444,18 @@ func (m *MongoDB) Update(ctx context.Context, filter bson.M, update bson.M, coll
|
||||
}
|
||||
setDoc["updatedAt"] = gtime.Now().Time
|
||||
update = bson.M{"$set": setDoc}
|
||||
var rows []interface{}
|
||||
if err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
result, err = db.Collection(collection).UpdateMany(ctx, filter, update, opts...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = m.CleanRedis(ctx, filter, user.TenantId, collection)
|
||||
//写日志
|
||||
serverName := g.Cfg().MustGet(ctx, "server.name").String()
|
||||
logRedisKey := fmt.Sprintf("log:%s", serverName)
|
||||
var rows []interface{}
|
||||
if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
if _, err = redis.AddToStream(ctx, logRedisKey, &dto.RecordCreateLogReq{
|
||||
ServiceName: serverName,
|
||||
Collection: collection,
|
||||
@@ -636,7 +631,7 @@ func (m *MongoDB) Insert(ctx context.Context, documents []interface{}, collectio
|
||||
rows = append(rows, doc)
|
||||
} else {
|
||||
filter := bson.M{"_id": bson.M{"$in": ids}}
|
||||
if err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
if _, err = m.Find(ctx, filter, &rows, collection, nil, nil); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user