优化mongo,封装count逻辑,处理objectId

This commit is contained in:
2026-01-08 11:07:58 +08:00
parent 65c80ae56f
commit e85c8453de
34 changed files with 753 additions and 446 deletions

View File

@@ -5,10 +5,10 @@ import (
"cid/model/dto"
"cid/model/entity"
"context"
"time"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/frame/g"
"go.mongodb.org/mongo-driver/v2/bson"
)
var AdPosition = new(adPosition)
@@ -17,39 +17,108 @@ type adPosition struct{}
// Add 添加广告位
func (s *adPosition) Add(ctx context.Context, req *dto.AddAdPositionReq) (res *dto.AddAdPositionRes, err error) {
adPosition := &entity.AdPosition{}
if err = gconv.Struct(req, adPosition); err != nil {
ids, err := dao.AdPosition.Insert(ctx, req)
if err != nil {
return
}
// 设置基础字段
now := time.Now()
adPosition.CreatedAt = now
adPosition.UpdatedAt = now
adPosition.IsDeleted = false
if err = dao.AdPosition.Insert(ctx, adPosition); err != nil {
return
}
res = &dto.AddAdPositionRes{Id: adPosition.Id.Hex()}
res = &dto.AddAdPositionRes{Id: ids[0].(*bson.ObjectID)}
return
}
// Update 更新广告位
func (s *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (err error) {
// 更新修改时间不需要设置DAO层会处理
return dao.AdPosition.Update(ctx, req)
func (s *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) error {
// 转换ID
id, err := bson.ObjectIDFromHex(req.Id)
if err != nil {
return gerror.Wrap(err, "无效的ID格式")
}
// 先获取原始广告位信息
originalAdPosition, err := dao.AdPosition.GetOne(ctx, &id)
if err != nil {
return gerror.Wrap(err, "获取原始广告位信息失败")
}
// 修改字段
if !g.IsEmpty(req.Name) {
originalAdPosition.Name = req.Name
}
if !g.IsEmpty(req.Description) {
originalAdPosition.Description = req.Description
}
if !g.IsEmpty(req.PositionCode) {
originalAdPosition.PositionCode = req.PositionCode
}
if !g.IsEmpty(req.AdFormat) {
originalAdPosition.AdFormat = req.AdFormat
}
if req.Width != nil {
originalAdPosition.Width = int64(*req.Width)
}
if req.Height != nil {
originalAdPosition.Height = int64(*req.Height)
}
if !g.IsEmpty(req.Page) {
originalAdPosition.Page = req.Page
}
if !g.IsEmpty(req.Section) {
originalAdPosition.Section = req.Section
}
if !g.IsEmpty(req.Location) {
originalAdPosition.Location = req.Location
}
if req.MaxAds != nil {
originalAdPosition.MaxAds = *req.MaxAds
}
if req.RefreshInterval != nil {
originalAdPosition.RefreshInterval = *req.RefreshInterval
}
if req.IsLazyLoad != nil {
originalAdPosition.IsLazyLoad = *req.IsLazyLoad
}
if !g.IsEmpty(req.PricingModel) {
originalAdPosition.PricingModel = req.PricingModel
}
if req.BasePrice != nil {
originalAdPosition.BasePrice = *req.BasePrice
}
if req.FloorPrice != nil {
originalAdPosition.FloorPrice = *req.FloorPrice
}
if !g.IsEmpty(req.PriceUnit) {
originalAdPosition.PriceUnit = req.PriceUnit
}
if req.DisplayRules != nil {
originalAdPosition.DisplayRules = req.DisplayRules
}
if req.Status != nil {
originalAdPosition.Status = *req.Status
}
if req.IsExclusive != nil {
originalAdPosition.IsExclusive = *req.IsExclusive
}
return dao.AdPosition.Update(ctx, &id, originalAdPosition)
}
// UpdateStatus 更新广告位状态
func (s *adPosition) UpdateStatus(ctx context.Context, req *dto.UpdateAdPositionStatusReq) (err error) {
return dao.AdPosition.UpdateStatus(ctx, req.Id, req.Status)
func (s *adPosition) UpdateStatus(ctx context.Context, req *dto.UpdateAdPositionStatusReq) error {
id, err := bson.ObjectIDFromHex(req.Id)
if err != nil {
return gerror.Wrap(err, "无效的ID格式")
}
return dao.AdPosition.UpdateStatus(ctx, &id, req.Status)
}
// GetOne 获取广告位详情
func (s *adPosition) GetOne(ctx context.Context, req *dto.GetAdPositionReq) (res *dto.GetAdPositionRes, err error) {
adPosition, err := dao.AdPosition.GetOne(ctx, req.Id)
id, err := bson.ObjectIDFromHex(req.Id)
if err != nil {
return nil, gerror.Wrap(err, "无效的ID格式")
}
adPosition, err := dao.AdPosition.GetOne(ctx, &id)
if err != nil {
return
}
@@ -74,11 +143,6 @@ func (s *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (res
return
}
// GetByCode 根据编码获取广告位
func (s *adPosition) GetByCode(ctx context.Context, code string) (adPosition *entity.AdPosition, err error) {
return dao.AdPosition.GetByCode(ctx, code)
}
// GetAvailableAdPositions 获取可用的广告位列表
func (s *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entity.AdPosition, err error) {
return dao.AdPosition.GetAvailableAdPositions(ctx)
@@ -86,21 +150,6 @@ func (s *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entit
// MatchAd 匹配广告
func (s *adPosition) MatchAd(ctx context.Context, positionCode string, userInfo map[string]interface{}) (ad *entity.Advertisement, err error) {
// 获取广告位信息
adPosition, err := dao.AdPosition.GetByCode(ctx, positionCode)
if err != nil {
return
}
// 检查广告位状态
if adPosition.Status != "启用" {
return nil, gerror.New("广告位未启用")
}
// 获取符合条件的广告列表
// 这里简化处理,实际项目中应该根据广告定向条件匹配广告
// 可以使用MongoDB的聚合管道实现复杂匹配逻辑
// 返回匹配的广告
// 这里返回第一个广告作为示例
ad = &entity.Advertisement{

View File

@@ -10,24 +10,23 @@ import (
"github.com/gogf/gf/v2/errors/gerror"
)
var (
AdSource = adSourceService{}
)
type adSource struct{}
type adSourceService struct{}
// AdSource 广告源服务
var AdSource = new(adSource)
// GetAvailableSources 获取可用的广告源列表
func (s *adSourceService) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
func (s *adSource) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
return dao.AdSource.GetAvailableSources(ctx)
}
// GetSourcesByProvider 根据提供商获取广告源
func (s *adSourceService) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
func (s *adSource) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
return dao.AdSource.GetSourcesByProvider(ctx, provider)
}
// CreateAdSource 创建广告源
func (s *adSourceService) CreateAdSource(ctx context.Context, req *dto.CreateAdSourceReq) (id string, err error) {
func (s *adSource) CreateAdSource(ctx context.Context, req *dto.CreateAdSourceReq) (id string, err error) {
// 检查广告源名称是否已存在
existingSource, err := dao.AdSource.GetByName(ctx, req.Name)
if err != nil {
@@ -54,7 +53,7 @@ func (s *adSourceService) CreateAdSource(ctx context.Context, req *dto.CreateAdS
}
// UpdateAdSource 更新广告源
func (s *adSourceService) UpdateAdSource(ctx context.Context, id string, req *dto.UpdateAdSourceReq) (affected int64, err error) {
func (s *adSource) UpdateAdSource(ctx context.Context, id string, req *dto.UpdateAdSourceReq) (affected int64, err error) {
// 检查广告源是否存在
existingSource, err := dao.AdSource.GetByID(ctx, id)
@@ -89,7 +88,7 @@ func (s *adSourceService) UpdateAdSource(ctx context.Context, id string, req *dt
}
// DeleteAdSource 删除广告源
func (s *adSourceService) DeleteAdSource(ctx context.Context, id string) (affected int64, err error) {
func (s *adSource) DeleteAdSource(ctx context.Context, id string) (affected int64, err error) {
// 检查广告源是否存在
existingSource, err := dao.AdSource.GetByID(ctx, id)
if err != nil {
@@ -103,6 +102,6 @@ func (s *adSourceService) DeleteAdSource(ctx context.Context, id string) (affect
}
// GetAdSourceByID 根据ID获取广告源
func (s *adSourceService) GetAdSourceByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
func (s *adSource) GetAdSourceByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
return dao.AdSource.GetByID(ctx, id)
}

View File

@@ -5,7 +5,6 @@ import (
"cid/model/dto"
"cid/model/entity"
"context"
"time"
"github.com/gogf/gf/v2/util/gconv"
)
@@ -21,15 +20,10 @@ func (s *advertisement) Add(ctx context.Context, req *dto.AddAdvertisementReq) (
return
}
// 设置基础字段
now := time.Now()
advertisement.CreatedAt = now
advertisement.UpdatedAt = now
advertisement.IsDeleted = false
// 设置初始状态
advertisement.Status = "待审核"
// 注意CreatedAt、UpdatedAt、TenantId、IsDeleted等字段由common/mongo的Insert方法自动设置
if err = dao.Advertisement.Insert(ctx, advertisement); err != nil {
return
}

View File

@@ -2,7 +2,6 @@ package service
import (
"context"
"time"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gconv"
@@ -23,15 +22,10 @@ func (s *advertiser) Add(ctx context.Context, req *dto.AddAdvertiserReq) (res *d
return
}
// 设置基础字段
now := time.Now()
advertiser.CreatedAt = now
advertiser.UpdatedAt = now
advertiser.IsDeleted = false
// 设置初始状态
advertiser.Status = "待审核"
// 注意CreatedAt、UpdatedAt、TenantId、IsDeleted等字段由common/mongo的Insert方法自动设置
if err = dao.Advertiser.Insert(ctx, advertiser); err != nil {
return
}

View File

@@ -12,14 +12,13 @@ import (
"github.com/gogf/gf/v2/errors/gerror"
)
var (
Application = applicationService{}
)
type application struct{}
type applicationService struct{}
// Application 应用服务
var Application = new(application)
// CreateApplication 创建应用
func (s *applicationService) CreateApplication(ctx context.Context, req *dto.CreateApplicationReq) (id string, err error) {
func (s *application) CreateApplication(ctx context.Context, req *dto.CreateApplicationReq) (id string, err error) {
// 检查应用名称是否已存在
existingApp, err := dao.Application.GetByName(ctx, req.Name)
if err != nil {
@@ -57,7 +56,7 @@ func (s *applicationService) CreateApplication(ctx context.Context, req *dto.Cre
}
// UpdateApplication 更新应用
func (s *applicationService) UpdateApplication(ctx context.Context, id string, req *dto.UpdateApplicationReq) (affected int64, err error) {
func (s *application) UpdateApplication(ctx context.Context, id string, req *dto.UpdateApplicationReq) (affected int64, err error) {
// 检查应用是否存在
existingApp, err := dao.Application.GetByID(ctx, id)
if err != nil {
@@ -117,7 +116,7 @@ func (s *applicationService) UpdateApplication(ctx context.Context, id string, r
}
// GetApplicationsByTenant 获取租户下的应用列表
func (s *applicationService) GetApplicationsByTenant(ctx context.Context, tenantID string, platform, status string, page, size int) (list []*entity.Application, total int64, err error) {
func (s *application) GetApplicationsByTenant(ctx context.Context, tenantID string, platform, status string, page, size int) (list []*entity.Application, total int64, err error) {
// 调用DAO的GetByTenantID方法获取租户下的所有应用
apps, err := dao.Application.GetByTenantID(ctx, tenantID)
if err != nil {
@@ -150,17 +149,17 @@ func (s *applicationService) GetApplicationsByTenant(ctx context.Context, tenant
}
// GetApplicationByKey 根据API密钥获取应用
func (s *applicationService) GetApplicationByKey(ctx context.Context, appKey string) (application *entity.Application, err error) {
func (s *application) GetApplicationByKey(ctx context.Context, appKey string) (application *entity.Application, err error) {
return dao.Application.GetByAPIKey(ctx, appKey)
}
// GetApplicationByID 根据ID获取应用
func (s *applicationService) GetApplicationByID(ctx context.Context, id string) (application *entity.Application, err error) {
func (s *application) GetApplicationByID(ctx context.Context, id string) (application *entity.Application, err error) {
return dao.Application.GetByID(ctx, id)
}
// DeleteApplication 删除应用
func (s *applicationService) DeleteApplication(ctx context.Context, id string) (affected int64, err error) {
func (s *application) DeleteApplication(ctx context.Context, id string) (affected int64, err error) {
err = dao.Application.Delete(ctx, id)
if err != nil {
return 0, err
@@ -169,7 +168,7 @@ func (s *applicationService) DeleteApplication(ctx context.Context, id string) (
}
// ValidateApplication 验证应用权限
func (s *applicationService) ValidateApplication(ctx context.Context, appKey, appSecret string) (application *entity.Application, err error) {
func (s *application) ValidateApplication(ctx context.Context, appKey, appSecret string) (application *entity.Application, err error) {
app, err := dao.Application.GetByAPIKey(ctx, appKey)
if err != nil {
return nil, err
@@ -188,7 +187,7 @@ func (s *applicationService) ValidateApplication(ctx context.Context, appKey, ap
}
// generateAPIKeys 生成API密钥
func (s *applicationService) generateAPIKeys() (appKey, appSecret string, err error) {
func (s *application) generateAPIKeys() (appKey, appSecret string, err error) {
// 生成32位随机字符串作为AppKey
keyBytes := make([]byte, 16)
if _, err := rand.Read(keyBytes); err != nil {
@@ -207,7 +206,7 @@ func (s *applicationService) generateAPIKeys() (appKey, appSecret string, err er
}
// ResetAPIKeys 重置API密钥
func (s *applicationService) ResetAPIKeys(ctx context.Context, id string) (appKey, appSecret string, err error) {
func (s *application) ResetAPIKeys(ctx context.Context, id string) (appKey, appSecret string, err error) {
// 检查应用是否存在
existingApp, err := dao.Application.GetByID(ctx, id)
if err != nil {

View File

@@ -10,11 +10,10 @@ import (
"github.com/gogf/gf/v2/frame/g"
)
var (
RateLimit = rateLimitService{}
)
type rateLimit struct{}
type rateLimitService struct{}
// RateLimit 限流服务
var RateLimit = new(rateLimit)
// TenantRateLimitConfig 租户限流配置
type TenantRateLimitConfig struct {
@@ -25,7 +24,7 @@ type TenantRateLimitConfig struct {
}
// CheckTenantRequestLimit 检查租户请求次数限制
func (s *rateLimitService) CheckTenantRequestLimit(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (bool, error) {
func (s *rateLimit) CheckTenantRequestLimit(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (bool, error) {
if config == nil {
// 使用默认配置
config = s.getDefaultTenantRateLimitConfig(tenantID)
@@ -72,7 +71,7 @@ func (s *rateLimitService) CheckTenantRequestLimit(ctx context.Context, tenantID
}
// GetDefaultTenantRateLimitConfig 获取默认的租户限流配置
func (s *rateLimitService) getDefaultTenantRateLimitConfig(tenantID int64) *TenantRateLimitConfig {
func (s *rateLimit) getDefaultTenantRateLimitConfig(tenantID int64) *TenantRateLimitConfig {
// 从配置文件中读取限流参数
ctx := context.Background()
@@ -105,14 +104,14 @@ func (s *rateLimitService) getDefaultTenantRateLimitConfig(tenantID int64) *Tena
}
// SetTenantRateLimitConfig 设置租户限流配置
func (s *rateLimitService) SetTenantRateLimitConfig(ctx context.Context, config *TenantRateLimitConfig) error {
func (s *rateLimit) SetTenantRateLimitConfig(ctx context.Context, config *TenantRateLimitConfig) error {
// 注意实际使用的是config.yml中的全局配置此方法仅用于兼容旧API
// 实际限流参数请修改config.yml中的tenantRateLimit部分
return nil
}
// GetTenantCurrentUsage 获取租户当前请求使用情况
func (s *rateLimitService) GetTenantCurrentUsage(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (current int64, max int64, err error) {
func (s *rateLimit) GetTenantCurrentUsage(ctx context.Context, tenantID int64, config *TenantRateLimitConfig) (current int64, max int64, err error) {
if config == nil {
config = s.getDefaultTenantRateLimitConfig(tenantID)
}

View File

@@ -12,14 +12,13 @@ import (
"github.com/gogf/gf/v2/frame/g"
)
var (
Strategy = strategyService{}
)
type strategy struct{}
type strategyService struct{}
// Strategy 策略服务
var Strategy = new(strategy)
// CreateStrategy 创建策略
func (s *strategyService) CreateStrategy(ctx context.Context, req *dto.CreateStrategyReq) (id int64, err error) {
func (s *strategy) CreateStrategy(ctx context.Context, req *dto.CreateStrategyReq) (id int64, err error) {
// 检查策略名称是否已存在
existingStrategy, err := dao.Strategy.GetByName(ctx, req.Name)
if err != nil {
@@ -63,7 +62,7 @@ func (s *strategyService) CreateStrategy(ctx context.Context, req *dto.CreateStr
}
// UpdateStrategy 更新策略
func (s *strategyService) UpdateStrategy(ctx context.Context, req *dto.UpdateStrategyReq) (affected int64, err error) {
func (s *strategy) UpdateStrategy(ctx context.Context, req *dto.UpdateStrategyReq) (affected int64, err error) {
// 检查策略是否存在
existingStrategy, err := dao.Strategy.GetByID(ctx, strconv.FormatInt(req.Id, 10))
if err != nil {
@@ -112,7 +111,7 @@ func (s *strategyService) UpdateStrategy(ctx context.Context, req *dto.UpdateStr
}
// DeleteStrategy 删除策略
func (s *strategyService) DeleteStrategy(ctx context.Context, id int64) (affected int64, err error) {
func (s *strategy) DeleteStrategy(ctx context.Context, id int64) (affected int64, err error) {
// 检查策略是否存在
existingStrategy, err := dao.Strategy.GetByID(ctx, strconv.FormatInt(id, 10))
if err != nil {
@@ -126,7 +125,7 @@ func (s *strategyService) DeleteStrategy(ctx context.Context, id int64) (affecte
}
// GetStrategyByID 根据ID获取策略
func (s *strategyService) GetStrategyByID(ctx context.Context, id int64) (strategy *dto.StrategyRes, err error) {
func (s *strategy) GetStrategyByID(ctx context.Context, id int64) (strategy *dto.StrategyRes, err error) {
entity, err := dao.Strategy.GetByID(ctx, strconv.FormatInt(id, 10))
if err != nil {
return nil, err
@@ -169,7 +168,7 @@ func (s *strategyService) GetStrategyByID(ctx context.Context, id int64) (strate
}
// GetStrategyList 获取策略列表
func (s *strategyService) GetStrategyList(ctx context.Context, req *dto.GetStrategyListReq) (res *dto.GetStrategyListRes, err error) {
func (s *strategy) GetStrategyList(ctx context.Context, req *dto.GetStrategyListReq) (res *dto.GetStrategyListRes, err error) {
list, total, err := dao.Strategy.GetList(ctx, req.Page, req.Size, req.TenantLevel, req.Status)
if err != nil {
return nil, err
@@ -220,6 +219,6 @@ func (s *strategyService) GetStrategyList(ctx context.Context, req *dto.GetStrat
}
// GetStrategyByTenantLevel 根据租户级别获取策略
func (s *strategyService) GetStrategyByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
func (s *strategy) GetStrategyByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
return dao.Strategy.GetByTenantLevel(ctx, tenantLevel)
}