refactor: 将数据库从MongoDB迁移至PostgreSQL
This commit is contained in:
@@ -3,18 +3,24 @@ package dao
|
||||
import (
|
||||
"context"
|
||||
"oss/consts"
|
||||
"oss/model/dto"
|
||||
"oss/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
)
|
||||
|
||||
var File = &file{}
|
||||
|
||||
type file struct {
|
||||
}
|
||||
type file struct{}
|
||||
|
||||
// Insert 插入
|
||||
func (d *file) Insert(ctx context.Context, entity *entity.File) (err error) {
|
||||
_, err = mongo.DB().Insert(ctx, []interface{}{&entity}, consts.FileCollection)
|
||||
func (d *file) Insert(ctx context.Context, req *dto.UploadFile) (res *entity.File, err error) {
|
||||
if err = gconv.Struct(req, &res); err != nil {
|
||||
return
|
||||
}
|
||||
res.Bid = guid.S()
|
||||
_, err = gfdb.DB(ctx).Model(ctx, consts.FileCollection).Insert(&res)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
commonMongo "gitea.com/red-future/common/db/mongo"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
// MongoDAO MongoDB原生查询(不需要token验证)
|
||||
var MongoDAO = &mongoDAO{}
|
||||
|
||||
type mongoDAO struct{}
|
||||
|
||||
// SaveOrUpdate 原生批量增加或修改
|
||||
func (d *mongoDAO) SaveOrUpdate(ctx context.Context, filter []bson.M, update []bson.M, collection string, opts ...options.Lister[options.UpdateManyOptions]) (result *mongo.BulkWriteResult, err error) {
|
||||
db := commonMongo.GetDB()
|
||||
if len(filter) == 0 || len(update) == 0 {
|
||||
err = gerror.New("缺少查询条件或更新数据")
|
||||
return
|
||||
}
|
||||
if len(filter) != len(update) {
|
||||
err = gerror.New("查询条件和更新数据的数量必须一致")
|
||||
return
|
||||
}
|
||||
// 构建批量操作模型
|
||||
var models []mongo.WriteModel
|
||||
for i := 0; i < len(filter); i++ {
|
||||
if g.IsEmpty(filter[i]["tenantId"]) && g.IsEmpty(gconv.Map(update[i]["$set"])["updater"]) {
|
||||
return nil, gerror.New("tenantId不能为空")
|
||||
}
|
||||
if g.IsEmpty(filter[i]["updater"]) && g.IsEmpty(gconv.Map(update[i]["$set"])["updater"]) {
|
||||
return nil, gerror.New("updater不能为空")
|
||||
}
|
||||
// 处理过滤器
|
||||
filter[i]["isDeleted"] = false
|
||||
// 处理更新数据
|
||||
if setDoc, exists := update[i]["$set"].(bson.M); exists {
|
||||
setDoc["updater"] = gconv.Map(update[i]["$set"])["updater"]
|
||||
setDoc["updatedAt"] = gtime.Now().Time
|
||||
} else {
|
||||
// 如果没有$set字段,则创建一个
|
||||
setDoc := bson.M{}
|
||||
setDoc["updater"] = gconv.Map(update[i]["$set"])["updater"]
|
||||
setDoc["updatedAt"] = gtime.Now().Time
|
||||
update[i]["$set"] = setDoc
|
||||
}
|
||||
// 创建更新操作模型
|
||||
updateModel := mongo.NewUpdateOneModel()
|
||||
updateModel.SetFilter(filter[i])
|
||||
updateModel.SetUpdate(update[i])
|
||||
updateModel.SetUpsert(true) // 默认不插入新文档
|
||||
// 处理选项参数
|
||||
if len(opts) > 0 {
|
||||
for _, opt := range opts {
|
||||
var updateOpts options.UpdateManyOptions
|
||||
optFuncs := opt.List()
|
||||
for _, fn := range optFuncs {
|
||||
fn(&updateOpts)
|
||||
}
|
||||
if updateOpts.Upsert != nil {
|
||||
updateModel.SetUpsert(*updateOpts.Upsert)
|
||||
}
|
||||
}
|
||||
}
|
||||
models = append(models, updateModel)
|
||||
}
|
||||
// 执行批量操作,无序执行提高性能
|
||||
bulkOpts := options.BulkWrite().SetOrdered(false)
|
||||
bulkResult, err := db.Collection(collection).BulkWrite(ctx, models, bulkOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 清理相关缓存
|
||||
for i := 0; i < len(filter); i++ {
|
||||
var tenantId any
|
||||
if g.IsEmpty(filter[i]["tenantId"]) {
|
||||
tenantId = filter[i]["tenantId"]
|
||||
}
|
||||
if g.IsEmpty(gconv.Map(update[i]["$set"])["tenantId"]) {
|
||||
tenantId = gconv.Map(update[i]["$set"])["tenantId"]
|
||||
}
|
||||
err = commonMongo.DB().CleanRedis(ctx, filter[i], tenantId, collection)
|
||||
if err != nil {
|
||||
glog.Warning(ctx, "清理Redis缓存失败:", err)
|
||||
}
|
||||
}
|
||||
return bulkResult, nil
|
||||
}
|
||||
@@ -6,38 +6,58 @@ import (
|
||||
"oss/model/dto"
|
||||
"oss/model/entity"
|
||||
|
||||
"gitea.com/red-future/common/db/mongo"
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"github.com/gogf/gf/v2/util/guid"
|
||||
)
|
||||
|
||||
var TenantOssTotal = &tenantOssTotal{}
|
||||
|
||||
type tenantOssTotal struct {
|
||||
}
|
||||
type tenantOssTotal struct{}
|
||||
|
||||
// SaveOrUpdate 增加或更新
|
||||
func (d *tenantOssTotal) SaveOrUpdate(ctx context.Context, updateData []*dto.UpdateUsedOssReq) (err error) {
|
||||
if !g.IsEmpty(updateData) {
|
||||
var filter, update []bson.M
|
||||
for _, v := range updateData {
|
||||
buildUpdateData, err := mongo.BuildUpdateData(ctx, v)
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.TenantOssTotalCollection).Where(gdb.Map{"tenant_id": v.TenantId})
|
||||
// 检查是否存在
|
||||
count, err := model.Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
// 更新
|
||||
_, err = model.Data(gdb.Map{
|
||||
"used_oss_size": v.UsedOssSize,
|
||||
"total_oss_size": v.TotalOssSize,
|
||||
"updater": v.Updater,
|
||||
}).Update()
|
||||
} else {
|
||||
// 插入
|
||||
_, err = gfdb.DB(ctx).Model(ctx, consts.TenantOssTotalCollection).Data(gdb.Map{
|
||||
"bid": guid.S(),
|
||||
"tenant_id": v.TenantId,
|
||||
"used_oss_size": v.UsedOssSize,
|
||||
"total_oss_size": v.TotalOssSize,
|
||||
"creator": v.Updater,
|
||||
"updater": v.Updater,
|
||||
}).Insert()
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
filter = append(filter, bson.M{"tenantId": v.TenantId})
|
||||
update = append(update, bson.M{"$set": buildUpdateData})
|
||||
}
|
||||
_, err = MongoDAO.SaveOrUpdate(ctx, filter, update, consts.TenantOssTotalCollection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *tenantOssTotal) GetOneByTenantId(ctx context.Context, req *dto.GetByTenantIdReq) (res *entity.TenantOssTotal, err error) {
|
||||
filter := bson.M{"tenantId": req.TenantId}
|
||||
err = mongo.DB().FindOne(ctx, filter, &res, consts.TenantOssTotalCollection)
|
||||
model := gfdb.DB(ctx).Model(ctx, consts.TenantOssTotalCollection).Where(entity.TenantOssCol.TenantId, req.TenantId)
|
||||
record, err := model.One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = record.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user