refactor: 将数据库从MongoDB迁移至PostgreSQL
This commit is contained in:
@@ -3,13 +3,14 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"oss/consts"
|
||||
"oss/dao"
|
||||
"oss/model/dto"
|
||||
"oss/model/entity"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
|
||||
"gitea.com/red-future/common/minio"
|
||||
"gitea.com/red-future/common/redis"
|
||||
"gitea.com/red-future/common/utils"
|
||||
@@ -51,18 +52,18 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
|
||||
getByTenantIdReq := &dto.GetByTenantIdReq{
|
||||
TenantId: user.TenantId,
|
||||
}
|
||||
tenantOssTotalRes, err := TenantOssTotal.GetOneByTenantId(ctx, getByTenantIdReq)
|
||||
tenantOssTotalRes, err := dao.TenantOssTotal.GetOneByTenantId(ctx, getByTenantIdReq)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "查询数据库-获取租户存储容量总数失败: %v", err)
|
||||
return err
|
||||
}
|
||||
if tenantOssTotalRes == nil || tenantOssTotalRes.TenantOssTotal == nil || g.IsEmpty(tenantOssTotalRes.Id) || tenantOssTotalRes.Id.IsZero() {
|
||||
if tenantOssTotalRes == nil || g.IsEmpty(tenantOssTotalRes.Id) {
|
||||
// 数据库中没有该租户的记录,创建默认配置
|
||||
tenantOssTotalEntity.TenantId = user.TenantId
|
||||
tenantOssTotalEntity.UsedOssSize = 0
|
||||
tenantOssTotalEntity.TotalOssSize = g.Cfg().MustGet(ctx, "oss.capacitySize").Int() * 1024 * 1024
|
||||
} else {
|
||||
tenantOssTotalEntity = tenantOssTotalRes.TenantOssTotal
|
||||
tenantOssTotalEntity = tenantOssTotalRes
|
||||
}
|
||||
} else {
|
||||
// 反序列化-redis获取租户存储容量总数
|
||||
@@ -104,11 +105,12 @@ func (f *file) UploadFile(ctx context.Context, req *dto.UploadFileReq) (res *dto
|
||||
return nil, err
|
||||
}
|
||||
// 插入数据库
|
||||
ossEntity := &entity.File{
|
||||
ossEntity := &dto.UploadFile{
|
||||
TenantId: tenantId,
|
||||
FileURL: fileURL,
|
||||
FileSize: fileSize,
|
||||
}
|
||||
if err = dao.File.Insert(ctx, ossEntity); err != nil {
|
||||
if _, err = dao.File.Insert(ctx, ossEntity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 返回图片url
|
||||
|
||||
@@ -2,12 +2,14 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitea.com/red-future/common/redis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"oss/consts"
|
||||
"oss/dao"
|
||||
"oss/model/dto"
|
||||
|
||||
"gitea.com/red-future/common/beans"
|
||||
"gitea.com/red-future/common/redis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type tenantOssTotal struct{}
|
||||
@@ -15,17 +17,10 @@ type tenantOssTotal struct{}
|
||||
// TenantOssTotal 存储文件服务
|
||||
var TenantOssTotal = new(tenantOssTotal)
|
||||
|
||||
func (s *tenantOssTotal) GetOneByTenantId(ctx context.Context, req *dto.GetByTenantIdReq) (res *dto.GetByTenantIdRes, err error) {
|
||||
e, err := dao.TenantOssTotal.GetOneByTenantId(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.GetByTenantIdRes{
|
||||
TenantOssTotal: e,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *tenantOssTotal) UpdateUsedOssSize(ctx context.Context) (err error) {
|
||||
ctx = context.WithValue(ctx, "user", &beans.User{
|
||||
UserName: "admin",
|
||||
})
|
||||
// 使用 Keys 取出所有key
|
||||
keys, err := redis.RedisClient().Keys(ctx, consts.OssTotalKey)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user