refactor: 将数据库从MongoDB迁移至PostgreSQL
This commit is contained in:
@@ -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