优化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

57
consts/ad_format_type.go Normal file
View File

@@ -0,0 +1,57 @@
package consts
// AdFormatType 广告格式类型枚举
type AdFormatType string
const (
AdFormatTypeBanner AdFormatType = "banner" // 横幅广告
AdFormatTypeVideo AdFormatType = "video" // 视频广告
AdFormatTypeNative AdFormatType = "native" // 原生广告
AdFormatTypeInterstitial AdFormatType = "interstitial" // 插屏广告
)
// GetAllAdFormatTypes 获取所有广告格式类型
func GetAllAdFormatTypes() []AdFormatType {
return []AdFormatType{
AdFormatTypeBanner,
AdFormatTypeVideo,
AdFormatTypeNative,
AdFormatTypeInterstitial,
}
}
type AdFormatTypeKeyValue struct {
Key AdFormatType
Value string
}
var (
AdFormatTypeBannerKeyValue = AdFormatTypeKeyValue{Key: AdFormatTypeBanner, Value: "横幅广告"}
AdFormatTypeVideoKeyValue = AdFormatTypeKeyValue{Key: AdFormatTypeVideo, Value: "视频广告"}
AdFormatTypeNativeKeyValue = AdFormatTypeKeyValue{Key: AdFormatTypeNative, Value: "原生广告"}
AdFormatTypeInterstitialKeyValue = AdFormatTypeKeyValue{Key: AdFormatTypeInterstitial, Value: "插屏广告"}
)
func GetAllAdFormatTypeKeyValue() []AdFormatTypeKeyValue {
return []AdFormatTypeKeyValue{
AdFormatTypeBannerKeyValue,
AdFormatTypeVideoKeyValue,
AdFormatTypeNativeKeyValue,
AdFormatTypeInterstitialKeyValue,
}
}
var adFormatTypeValueMap = map[AdFormatType]string{
AdFormatTypeBanner: AdFormatTypeBannerKeyValue.Value,
AdFormatTypeVideo: AdFormatTypeVideoKeyValue.Value,
AdFormatTypeNative: AdFormatTypeNativeKeyValue.Value,
AdFormatTypeInterstitial: AdFormatTypeInterstitialKeyValue.Value,
}
func GetAdFormatTypeValueByKey(key AdFormatType) (value string) {
value, exists := adFormatTypeValueMap[key]
if !exists {
value = "未知广告格式"
}
return
}

View File

@@ -0,0 +1,52 @@
package consts
// AdSourceHealth 广告源健康状态枚举
type AdSourceHealth string
const (
AdSourceHealthHealthy AdSourceHealth = "healthy" // 健康
AdSourceHealthDegraded AdSourceHealth = "degraded" // 降级
AdSourceHealthUnhealthy AdSourceHealth = "unhealthy" // 不健康
)
// GetAllAdSourceHealths 获取所有广告源健康状态
func GetAllAdSourceHealths() []AdSourceHealth {
return []AdSourceHealth{
AdSourceHealthHealthy,
AdSourceHealthDegraded,
AdSourceHealthUnhealthy,
}
}
type AdSourceHealthKeyValue struct {
Key AdSourceHealth
Value string
}
var (
AdSourceHealthHealthyKeyValue = AdSourceHealthKeyValue{Key: AdSourceHealthHealthy, Value: "健康"}
AdSourceHealthDegradedKeyValue = AdSourceHealthKeyValue{Key: AdSourceHealthDegraded, Value: "降级"}
AdSourceHealthUnhealthyKeyValue = AdSourceHealthKeyValue{Key: AdSourceHealthUnhealthy, Value: "不健康"}
)
func GetAllAdSourceHealthKeyValue() []AdSourceHealthKeyValue {
return []AdSourceHealthKeyValue{
AdSourceHealthHealthyKeyValue,
AdSourceHealthDegradedKeyValue,
AdSourceHealthUnhealthyKeyValue,
}
}
var adSourceHealthValueMap = map[AdSourceHealth]string{
AdSourceHealthHealthy: AdSourceHealthHealthyKeyValue.Value,
AdSourceHealthDegraded: AdSourceHealthDegradedKeyValue.Value,
AdSourceHealthUnhealthy: AdSourceHealthUnhealthyKeyValue.Value,
}
func GetAdSourceHealthValueByKey(key AdSourceHealth) (value string) {
value, exists := adSourceHealthValueMap[key]
if !exists {
value = "未知健康状态"
}
return
}

View File

@@ -0,0 +1,57 @@
package consts
// AdSourceProvider 广告源提供商枚举
type AdSourceProvider string
const (
AdSourceProviderGoogle AdSourceProvider = "google" // Google
AdSourceProviderBaidu AdSourceProvider = "baidu" // 百度
AdSourceProviderTencent AdSourceProvider = "tencent" // 腾讯
AdSourceProviderSelf AdSourceProvider = "self" // 自营
)
// GetAllAdSourceProviders 获取所有广告源提供商
func GetAllAdSourceProviders() []AdSourceProvider {
return []AdSourceProvider{
AdSourceProviderGoogle,
AdSourceProviderBaidu,
AdSourceProviderTencent,
AdSourceProviderSelf,
}
}
type AdSourceProviderKeyValue struct {
Key AdSourceProvider
Value string
}
var (
AdSourceProviderGoogleKeyValue = AdSourceProviderKeyValue{Key: AdSourceProviderGoogle, Value: "Google"}
AdSourceProviderBaiduKeyValue = AdSourceProviderKeyValue{Key: AdSourceProviderBaidu, Value: "百度"}
AdSourceProviderTencentKeyValue = AdSourceProviderKeyValue{Key: AdSourceProviderTencent, Value: "腾讯"}
AdSourceProviderSelfKeyValue = AdSourceProviderKeyValue{Key: AdSourceProviderSelf, Value: "自营"}
)
func GetAllAdSourceProviderKeyValue() []AdSourceProviderKeyValue {
return []AdSourceProviderKeyValue{
AdSourceProviderGoogleKeyValue,
AdSourceProviderBaiduKeyValue,
AdSourceProviderTencentKeyValue,
AdSourceProviderSelfKeyValue,
}
}
var adSourceProviderValueMap = map[AdSourceProvider]string{
AdSourceProviderGoogle: AdSourceProviderGoogleKeyValue.Value,
AdSourceProviderBaidu: AdSourceProviderBaiduKeyValue.Value,
AdSourceProviderTencent: AdSourceProviderTencentKeyValue.Value,
AdSourceProviderSelf: AdSourceProviderSelfKeyValue.Value,
}
func GetAdSourceProviderValueByKey(key AdSourceProvider) (value string) {
value, exists := adSourceProviderValueMap[key]
if !exists {
value = "未知提供商"
}
return
}

View File

@@ -0,0 +1,52 @@
package consts
// AdSourceStatus 广告源状态枚举
type AdSourceStatus string
const (
AdSourceStatusActive AdSourceStatus = "active" // 活跃
AdSourceStatusInactive AdSourceStatus = "inactive" // 非活跃
AdSourceStatusMaintenance AdSourceStatus = "maintenance" // 维护中
)
// GetAllAdSourceStatuses 获取所有广告源状态
func GetAllAdSourceStatuses() []AdSourceStatus {
return []AdSourceStatus{
AdSourceStatusActive,
AdSourceStatusInactive,
AdSourceStatusMaintenance,
}
}
type AdSourceStatusKeyValue struct {
Key AdSourceStatus
Value string
}
var (
AdSourceStatusActiveKeyValue = AdSourceStatusKeyValue{Key: AdSourceStatusActive, Value: "活跃"}
AdSourceStatusInactiveKeyValue = AdSourceStatusKeyValue{Key: AdSourceStatusInactive, Value: "非活跃"}
AdSourceStatusMaintenanceKeyValue = AdSourceStatusKeyValue{Key: AdSourceStatusMaintenance, Value: "维护中"}
)
func GetAllAdSourceStatusKeyValue() []AdSourceStatusKeyValue {
return []AdSourceStatusKeyValue{
AdSourceStatusActiveKeyValue,
AdSourceStatusInactiveKeyValue,
AdSourceStatusMaintenanceKeyValue,
}
}
var adSourceStatusValueMap = map[AdSourceStatus]string{
AdSourceStatusActive: AdSourceStatusActiveKeyValue.Value,
AdSourceStatusInactive: AdSourceStatusInactiveKeyValue.Value,
AdSourceStatusMaintenance: AdSourceStatusMaintenanceKeyValue.Value,
}
func GetAdSourceStatusValueByKey(key AdSourceStatus) (value string) {
value, exists := adSourceStatusValueMap[key]
if !exists {
value = "未知状态"
}
return
}

52
consts/ad_source_type.go Normal file
View File

@@ -0,0 +1,52 @@
package consts
// AdSourceType 广告源类型枚举
type AdSourceType string
const (
AdSourceTypeSelf AdSourceType = "self" // 自营
AdSourceTypeThirdParty AdSourceType = "third_party" // 第三方
AdSourceTypeExchange AdSourceType = "exchange" // 广告交易平台
)
// GetAllAdSourceTypes 获取所有广告源类型
func GetAllAdSourceTypes() []AdSourceType {
return []AdSourceType{
AdSourceTypeSelf,
AdSourceTypeThirdParty,
AdSourceTypeExchange,
}
}
type AdSourceTypeKeyValue struct {
Key AdSourceType
Value string
}
var (
AdSourceTypeSelfKeyValue = AdSourceTypeKeyValue{Key: AdSourceTypeSelf, Value: "自营"}
AdSourceTypeThirdPartyKeyValue = AdSourceTypeKeyValue{Key: AdSourceTypeThirdParty, Value: "第三方"}
AdSourceTypeExchangeKeyValue = AdSourceTypeKeyValue{Key: AdSourceTypeExchange, Value: "广告交易平台"}
)
func GetAllAdSourceTypeKeyValue() []AdSourceTypeKeyValue {
return []AdSourceTypeKeyValue{
AdSourceTypeSelfKeyValue,
AdSourceTypeThirdPartyKeyValue,
AdSourceTypeExchangeKeyValue,
}
}
var adSourceTypeValueMap = map[AdSourceType]string{
AdSourceTypeSelf: AdSourceTypeSelfKeyValue.Value,
AdSourceTypeThirdParty: AdSourceTypeThirdPartyKeyValue.Value,
AdSourceTypeExchange: AdSourceTypeExchangeKeyValue.Value,
}
func GetAdSourceTypeValueByKey(key AdSourceType) (value string) {
value, exists := adSourceTypeValueMap[key]
if !exists {
value = "未知类型"
}
return
}

52
consts/auth_type.go Normal file
View File

@@ -0,0 +1,52 @@
package consts
// AuthType 认证类型枚举
type AuthType string
const (
AuthTypeAPIKey AuthType = "api_key" // API密钥
AuthTypeOAuth AuthType = "oauth" // OAuth
AuthTypeBasic AuthType = "basic" // Basic认证
)
// GetAllAuthTypes 获取所有认证类型
func GetAllAuthTypes() []AuthType {
return []AuthType{
AuthTypeAPIKey,
AuthTypeOAuth,
AuthTypeBasic,
}
}
type AuthTypeKeyValue struct {
Key AuthType
Value string
}
var (
AuthTypeAPIKeyKeyValue = AuthTypeKeyValue{Key: AuthTypeAPIKey, Value: "API密钥"}
AuthTypeOAuthKeyValue = AuthTypeKeyValue{Key: AuthTypeOAuth, Value: "OAuth"}
AuthTypeBasicKeyValue = AuthTypeKeyValue{Key: AuthTypeBasic, Value: "Basic认证"}
)
func GetAllAuthTypeKeyValue() []AuthTypeKeyValue {
return []AuthTypeKeyValue{
AuthTypeAPIKeyKeyValue,
AuthTypeOAuthKeyValue,
AuthTypeBasicKeyValue,
}
}
var authTypeValueMap = map[AuthType]string{
AuthTypeAPIKey: AuthTypeAPIKeyKeyValue.Value,
AuthTypeOAuth: AuthTypeOAuthKeyValue.Value,
AuthTypeBasic: AuthTypeBasicKeyValue.Value,
}
func GetAuthTypeValueByKey(key AuthType) (value string) {
value, exists := authTypeValueMap[key]
if !exists {
value = "未知认证类型"
}
return
}

57
consts/bidding_type.go Normal file
View File

@@ -0,0 +1,57 @@
package consts
// BiddingType 竞价类型枚举
type BiddingType string
const (
BiddingTypeCPM BiddingType = "cpm" // 千次展示成本
BiddingTypeCPC BiddingType = "cpc" // 每次点击成本
BiddingTypeCPA BiddingType = "cpa" // 每次行动成本
BiddingTypeRTB BiddingType = "rtb" // 实时竞价
)
// GetAllBiddingTypes 获取所有竞价类型
func GetAllBiddingTypes() []BiddingType {
return []BiddingType{
BiddingTypeCPM,
BiddingTypeCPC,
BiddingTypeCPA,
BiddingTypeRTB,
}
}
type BiddingTypeKeyValue struct {
Key BiddingType
Value string
}
var (
BiddingTypeCPMKeyValue = BiddingTypeKeyValue{Key: BiddingTypeCPM, Value: "千次展示成本"}
BiddingTypeCPCKeypValue = BiddingTypeKeyValue{Key: BiddingTypeCPC, Value: "每次点击成本"}
BiddingTypeCPAKeyValue = BiddingTypeKeyValue{Key: BiddingTypeCPA, Value: "每次行动成本"}
BiddingTypeRTBKeyValue = BiddingTypeKeyValue{Key: BiddingTypeRTB, Value: "实时竞价"}
)
func GetAllBiddingTypeKeyValue() []BiddingTypeKeyValue {
return []BiddingTypeKeyValue{
BiddingTypeCPMKeyValue,
BiddingTypeCPCKeypValue,
BiddingTypeCPAKeyValue,
BiddingTypeRTBKeyValue,
}
}
var biddingTypeValueMap = map[BiddingType]string{
BiddingTypeCPM: BiddingTypeCPMKeyValue.Value,
BiddingTypeCPC: BiddingTypeCPCKeypValue.Value,
BiddingTypeCPA: BiddingTypeCPAKeyValue.Value,
BiddingTypeRTB: BiddingTypeRTBKeyValue.Value,
}
func GetBiddingTypeValueByKey(key BiddingType) (value string) {
value, exists := biddingTypeValueMap[key]
if !exists {
value = "未知竞价类型"
}
return
}

57
consts/billing_model.go Normal file
View File

@@ -0,0 +1,57 @@
package consts
// BillingModel 计费模式枚举
type BillingModel string
const (
BillingModelCPM BillingModel = "cpm" // 千次展示成本
BillingModelCPC BillingModel = "cpc" // 每次点击成本
BillingModelCPA BillingModel = "cpa" // 每次行动成本
BillingModelRevShare BillingModel = "rev_share" // 收入分成
)
// GetAllBillingModels 获取所有计费模式
func GetAllBillingModels() []BillingModel {
return []BillingModel{
BillingModelCPM,
BillingModelCPC,
BillingModelCPA,
BillingModelRevShare,
}
}
type BillingModelKeyValue struct {
Key BillingModel
Value string
}
var (
BillingModelCPMKeyValue = BillingModelKeyValue{Key: BillingModelCPM, Value: "千次展示成本"}
BillingModelCPCKeypValue = BillingModelKeyValue{Key: BillingModelCPC, Value: "每次点击成本"}
BillingModelCPAKeyValue = BillingModelKeyValue{Key: BillingModelCPA, Value: "每次行动成本"}
BillingModelRevShareKeyValue = BillingModelKeyValue{Key: BillingModelRevShare, Value: "收入分成"}
)
func GetAllBillingModelKeyValue() []BillingModelKeyValue {
return []BillingModelKeyValue{
BillingModelCPMKeyValue,
BillingModelCPCKeypValue,
BillingModelCPAKeyValue,
BillingModelRevShareKeyValue,
}
}
var billingModelValueMap = map[BillingModel]string{
BillingModelCPM: BillingModelCPMKeyValue.Value,
BillingModelCPC: BillingModelCPCKeypValue.Value,
BillingModelCPA: BillingModelCPAKeyValue.Value,
BillingModelRevShare: BillingModelRevShareKeyValue.Value,
}
func GetBillingModelValueByKey(key BillingModel) (value string) {
value, exists := billingModelValueMap[key]
if !exists {
value = "未知计费模式"
}
return
}

18
consts/collections.go Normal file
View File

@@ -0,0 +1,18 @@
package consts
// MongoDB集合名称常量
const (
AdPositionCollection = "ad_position" // 广告位集合
AdSourceCollection = "ad_source" // 广告源集合
AdvertisementCollection = "advertisement" // 广告集合
AdvertiserCollection = "advertiser" // 广告主集合
ApplicationCollection = "application" // 应用集合
CidRequestCollection = "cid_request" // CID请求集合
StrategyCollection = "strategy" // 策略集合
AdCreativeCollection = "ad_creative" // 广告创意集合
AdPlatformCollection = "ad_platform" // 广告平台集合
AdTypeCollection = "ad_type" // 广告类型集合
AppPlatformConfigCollection = "app_platform_config" // 应用平台配置集合
PlatformDeliveryRuleCollection = "platform_delivery_rule" // 平台投放规则集合
TargetingCollection = "targeting" // 定向规则集合
)

19
consts/config.go Normal file
View File

@@ -0,0 +1,19 @@
package consts
// 默认配置值
const (
DefaultTimeout = 5000 // 默认超时时间(毫秒)
DefaultRetryCount = 3 // 默认重试次数
DefaultPriority = 1 // 默认优先级
)
// 错误消息
const (
ErrAdSourceNotFound = "广告源不存在"
ErrAdSourceNameExists = "广告源名称已存在"
ErrAdSourceCodeExists = "广告源编码已存在"
ErrAdSourceInactive = "广告源非活跃状态"
ErrAdSourceUnhealthy = "广告源健康状态异常"
ErrRateLimitExceeded = "请求频率超限"
ErrInvalidConfiguration = "配置参数无效"
)

View File

@@ -1,86 +1,16 @@
package consts
// 广告源状态
const (
AdSourceStatusActive = "active" // 活跃
AdSourceStatusInactive = "inactive" // 非活跃
AdSourceStatusMaintenance = "maintenance" // 维护中
)
// 注意:以下枚举常量已迁移到单独的枚举文件中,请使用新的枚举类型:
// - AdSourceStatus -> consts.AdSourceStatus (ad_source_status.go)
// - AdSourceHealth -> consts.AdSourceHealth (ad_source_health.go)
// - AdSourceType -> consts.AdSourceType (ad_source_type.go)
// - AdSourceProvider -> consts.AdSourceProvider (ad_source_provider.go)
// - AuthType -> consts.AuthType (auth_type.go)
// - BiddingType -> consts.BiddingType (bidding_type.go)
// - AdFormatType -> consts.AdFormatType (ad_format_type.go)
// - BillingModel -> consts.BillingModel (billing_model.go)
// - PaymentTerms -> consts.PaymentTerms (payment_terms.go)
// 广告源健康状态
const (
AdSourceHealthHealthy = "healthy" // 健康
AdSourceHealthDegraded = "degraded" // 降级
AdSourceHealthUnhealthy = "unhealthy" // 不健康
)
// 广告源类型
const (
AdSourceTypeSelf = "self" // 自营
AdSourceTypeThirdParty = "third_party" // 第三方
AdSourceTypeExchange = "exchange" // 广告交易平台
)
// 广告源提供商
const (
AdSourceProviderGoogle = "google" // Google
AdSourceProviderBaidu = "baidu" // 百度
AdSourceProviderTencent = "tencent" // 腾讯
AdSourceProviderSelf = "self" // 自营
)
// 认证类型
const (
AuthTypeAPIKey = "api_key" // API密钥
AuthTypeOAuth = "oauth" // OAuth
AuthTypeBasic = "basic" // Basic认证
)
// 竞价类型
const (
BiddingTypeCPM = "cpm" // 千次展示成本
BiddingTypeCPC = "cpc" // 每次点击成本
BiddingTypeCPA = "cpa" // 每次行动成本
BiddingTypeRTB = "rtb" // 实时竞价
)
// 广告格式类型
const (
AdFormatTypeBanner = "banner" // 横幅广告
AdFormatTypeVideo = "video" // 视频广告
AdFormatTypeNative = "native" // 原生广告
AdFormatTypeInterstitial = "interstitial" // 插屏广告
)
// 计费模式
const (
BillingModelCPM = "cpm" // 千次展示成本
BillingModelCPC = "cpc" // 每次点击成本
BillingModelCPA = "cpa" // 每次行动成本
BillingModelRevShare = "rev_share" // 收入分成
)
// 支付条款
const (
PaymentTermsNet30 = "net_30" // 30天
PaymentTermsNet60 = "net_60" // 60天
PaymentTermsNet90 = "net_90" // 90天
)
// 默认配置值
const (
DefaultTimeout = 5000 // 默认超时时间(毫秒)
DefaultRetryCount = 3 // 默认重试次数
DefaultPriority = 1 // 默认优先级
)
// 错误消息
const (
ErrAdSourceNotFound = "广告源不存在"
ErrAdSourceNameExists = "广告源名称已存在"
ErrAdSourceCodeExists = "广告源编码已存在"
ErrAdSourceInactive = "广告源非活跃状态"
ErrAdSourceUnhealthy = "广告源健康状态异常"
ErrRateLimitExceeded = "请求频率超限"
ErrInvalidConfiguration = "配置参数无效"
)
// 配置值常量已迁移到 config.go 文件中
// 错误消息常量已迁移到 config.go 文件中
// MongoDB集合名称常量已迁移到 collections.go 文件中

52
consts/payment_terms.go Normal file
View File

@@ -0,0 +1,52 @@
package consts
// PaymentTerms 支付条款枚举
type PaymentTerms string
const (
PaymentTermsNet30 PaymentTerms = "net_30" // 30天
PaymentTermsNet60 PaymentTerms = "net_60" // 60天
PaymentTermsNet90 PaymentTerms = "net_90" // 90天
)
// GetAllPaymentTerms 获取所有支付条款
func GetAllPaymentTerms() []PaymentTerms {
return []PaymentTerms{
PaymentTermsNet30,
PaymentTermsNet60,
PaymentTermsNet90,
}
}
type PaymentTermsKeyValue struct {
Key PaymentTerms
Value string
}
var (
PaymentTermsNet30KeyValue = PaymentTermsKeyValue{Key: PaymentTermsNet30, Value: "30天"}
PaymentTermsNet60KeyValue = PaymentTermsKeyValue{Key: PaymentTermsNet60, Value: "60天"}
PaymentTermsNet90KeyValue = PaymentTermsKeyValue{Key: PaymentTermsNet90, Value: "90天"}
)
func GetAllPaymentTermsKeyValue() []PaymentTermsKeyValue {
return []PaymentTermsKeyValue{
PaymentTermsNet30KeyValue,
PaymentTermsNet60KeyValue,
PaymentTermsNet90KeyValue,
}
}
var paymentTermsValueMap = map[PaymentTerms]string{
PaymentTermsNet30: PaymentTermsNet30KeyValue.Value,
PaymentTermsNet60: PaymentTermsNet60KeyValue.Value,
PaymentTermsNet90: PaymentTermsNet90KeyValue.Value,
}
func GetPaymentTermsValueByKey(key PaymentTerms) (value string) {
value, exists := paymentTermsValueMap[key]
if !exists {
value = "未知支付条款"
}
return
}

View File

@@ -6,7 +6,7 @@ import (
"cid/model/dto"
"cid/service"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
)
type adPosition struct{}
@@ -19,13 +19,13 @@ func (c *adPosition) Add(ctx context.Context, req *dto.AddAdPositionReq) (res *d
}
// Update 更新广告位
func (c *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (res *http.ResponseEmpty, err error) {
func (c *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (res *beans.ResponseEmpty, err error) {
err = service.AdPosition.Update(ctx, req)
return
}
// UpdateStatus 更新广告位状态
func (c *adPosition) UpdateStatus(ctx context.Context, req *dto.UpdateAdPositionStatusReq) (res *http.ResponseEmpty, err error) {
func (c *adPosition) UpdateStatus(ctx context.Context, req *dto.UpdateAdPositionStatusReq) (res *beans.ResponseEmpty, err error) {
err = service.AdPosition.UpdateStatus(ctx, req)
return
}

View File

@@ -5,7 +5,7 @@ import (
"cid/service"
"context"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
)
type advertisement struct{}
@@ -18,19 +18,19 @@ func (c *advertisement) Add(ctx context.Context, req *dto.AddAdvertisementReq) (
}
// Update 更新广告
func (c *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisementReq) (res *http.ResponseEmpty, err error) {
func (c *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisementReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertisement.Update(ctx, req)
return
}
// UpdateStatus 更新广告状态
func (c *advertisement) UpdateStatus(ctx context.Context, req *dto.UpdateAdStatusReq) (res *http.ResponseEmpty, err error) {
func (c *advertisement) UpdateStatus(ctx context.Context, req *dto.UpdateAdStatusReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertisement.UpdateStatus(ctx, req)
return
}
// Audit 审核广告
func (c *advertisement) Audit(ctx context.Context, req *dto.AuditAdvertisementReq) (res *http.ResponseEmpty, err error) {
func (c *advertisement) Audit(ctx context.Context, req *dto.AuditAdvertisementReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertisement.Audit(ctx, req)
return
}

View File

@@ -5,7 +5,7 @@ import (
"cid/service"
"context"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
)
type advertiser struct{}
@@ -18,31 +18,31 @@ func (c *advertiser) Add(ctx context.Context, req *dto.AddAdvertiserReq) (res *d
}
// Update 更新广告主
func (c *advertiser) Update(ctx context.Context, req *dto.UpdateAdvertiserReq) (res *http.ResponseEmpty, err error) {
func (c *advertiser) Update(ctx context.Context, req *dto.UpdateAdvertiserReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertiser.Update(ctx, req)
return
}
// UpdateStatus 更新广告主状态
func (c *advertiser) UpdateStatus(ctx context.Context, req *dto.UpdateAdvertiserStatusReq) (res *http.ResponseEmpty, err error) {
func (c *advertiser) UpdateStatus(ctx context.Context, req *dto.UpdateAdvertiserStatusReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertiser.UpdateStatus(ctx, req)
return
}
// Audit 审核广告主
func (c *advertiser) Audit(ctx context.Context, req *dto.AuditAdvertiserReq) (res *http.ResponseEmpty, err error) {
func (c *advertiser) Audit(ctx context.Context, req *dto.AuditAdvertiserReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertiser.Audit(ctx, req)
return
}
// Recharge 充值
func (c *advertiser) Recharge(ctx context.Context, req *dto.RechargeAdvertiserReq) (res *http.ResponseEmpty, err error) {
func (c *advertiser) Recharge(ctx context.Context, req *dto.RechargeAdvertiserReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertiser.Recharge(ctx, req)
return
}
// UpdateCreditLimit 更新授信额度
func (c *advertiser) UpdateCreditLimit(ctx context.Context, req *dto.UpdateCreditLimitReq) (res *http.ResponseEmpty, err error) {
func (c *advertiser) UpdateCreditLimit(ctx context.Context, req *dto.UpdateCreditLimitReq) (res *beans.ResponseEmpty, err error) {
err = service.Advertiser.UpdateCreditLimit(ctx, req)
return
}

View File

@@ -5,12 +5,11 @@ import (
"cid/model/entity"
"context"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var AdPosition = &adPosition{}
@@ -19,154 +18,52 @@ type adPosition struct {
}
// Insert 插入广告位
func (d *adPosition) Insert(ctx context.Context, adPosition *entity.AdPosition) (err error) {
// 获取stream消息
redis := g.Redis()
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "ad_position_stream", "$")
if err != nil {
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
} else {
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
func (d *adPosition) Insert(ctx context.Context, req *dto.AddAdPositionReq) (ids []any, err error) {
var result entity.AdPosition
if err = gconv.Struct(req, &result); err != nil {
return
}
_, err = mongo.DB().Insert(ctx, []interface{}{adPosition}, entity.AdPositionCollection)
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, entity.AdPositionCollection)
return
}
// Update 更新广告位
func (d *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (err error) {
objectId, err := bson.ObjectIDFromHex(req.Id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
func (d *adPosition) Update(ctx context.Context, id *bson.ObjectID, updateData *entity.AdPosition) (err error) {
filter := bson.M{"_id": id}
// 构建动态更新字段
updateFields := bson.M{}
// 基本信息
if !g.IsEmpty(req.Name) {
updateFields["name"] = req.Name
}
if !g.IsEmpty(req.Description) {
updateFields["description"] = req.Description
}
if !g.IsEmpty(req.PositionCode) {
updateFields["positionCode"] = req.PositionCode
}
if !g.IsEmpty(req.AdFormat) {
updateFields["adFormat"] = req.AdFormat
}
// 尺寸信息
if req.Width != nil {
updateFields["width"] = *req.Width
}
if req.Height != nil {
updateFields["height"] = *req.Height
}
// 位置信息
if !g.IsEmpty(req.Page) {
updateFields["page"] = req.Page
}
if !g.IsEmpty(req.Section) {
updateFields["section"] = req.Section
}
if !g.IsEmpty(req.Location) {
updateFields["location"] = req.Location
}
// 展示设置
if req.MaxAds != nil {
updateFields["maxAds"] = *req.MaxAds
}
if req.RefreshInterval != nil {
updateFields["refreshInterval"] = *req.RefreshInterval
}
if req.IsLazyLoad != nil {
updateFields["isLazyLoad"] = *req.IsLazyLoad
}
// 定价设置
if !g.IsEmpty(req.PricingModel) {
updateFields["pricingModel"] = req.PricingModel
}
if req.BasePrice != nil {
updateFields["basePrice"] = *req.BasePrice
}
if req.FloorPrice != nil {
updateFields["floorPrice"] = *req.FloorPrice
}
if !g.IsEmpty(req.PriceUnit) {
updateFields["priceUnit"] = req.PriceUnit
}
// 展示规则
if req.DisplayRules != nil {
updateFields["displayRules"] = req.DisplayRules
}
// 状态信息
if req.Status != nil {
updateFields["status"] = *req.Status
}
if req.IsExclusive != nil {
updateFields["isExclusive"] = *req.IsExclusive
}
if len(updateFields) > 0 {
update := bson.M{"$set": updateFields}
if !g.IsEmpty(updateData) {
bsonm, err := mongo.EntityToBSONM(updateData)
if err != nil {
return err
}
update := bson.M{"$set": bsonm}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
}
return
}
// UpdateStatus 更新广告位状态
func (d *adPosition) UpdateStatus(ctx context.Context, id, status string) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
func (d *adPosition) UpdateStatus(ctx context.Context, id *bson.ObjectID, status string) (err error) {
filter := bson.M{"_id": id}
update := bson.M{"$set": bson.M{"status": status}}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
return
}
// UpdateStatistics 更新广告位统计数据
func (d *adPosition) UpdateStatistics(ctx context.Context, id string, stats map[string]interface{}) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
update := bson.M{"$set": stats}
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
return
}
// GetOne 获取单个广告位
func (d *adPosition) GetOne(ctx context.Context, id string) (adPosition *entity.AdPosition, err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
func (d *adPosition) GetOne(ctx context.Context, id *bson.ObjectID) (adPosition *entity.AdPosition, err error) {
filter := bson.M{"_id": id}
adPosition = &entity.AdPosition{}
err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
return
}
// GetByCode 根据编码获取广告位
func (d *adPosition) GetByCode(ctx context.Context, code string) (adPosition *entity.AdPosition, err error) {
filter := bson.M{"positionCode": code}
adPosition = &entity.AdPosition{}
err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
// Delete 删除广告位
func (d *adPosition) Delete(ctx context.Context, id *bson.ObjectID) (err error) {
filter := bson.M{"_id": id}
_, err = mongo.DB().Delete(ctx, filter, entity.AdPositionCollection)
return
}
@@ -206,42 +103,13 @@ func (d *adPosition) buildListFilter(req *dto.ListAdPositionReq) bson.M {
return filter
}
// checkTotalCount 检查总数
func (d *adPosition) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
total, err = mongo.DB().Count(ctx, filter, entity.AdPositionCollection)
return
}
// List 获取广告位列表
func (d *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (list []*entity.AdPosition, total int64, err error) {
// 构建查询过滤条件
filter := d.buildListFilter(req)
// 检查总数
total, err = d.checkTotalCount(ctx, filter)
if err != nil {
return
}
// 分页参数处理
pageNum := req.PageNum
if pageNum <= 0 {
pageNum = 1
}
pageSize := req.PageSize
if pageSize <= 0 {
pageSize = http.PageSize
}
limit := int64(pageSize)
skip := int64((pageNum - 1) * pageSize)
// 排序处理
sort := bson.M{"createdAt": -1}
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, opts)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, req.Page, nil)
return
}
@@ -251,8 +119,8 @@ func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entit
"status": "启用", // 只返回启用的广告位
}
opts := options.Find().SetSort(bson.M{"createdAt": -1})
err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, opts)
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, page, nil)
return
}

View File

@@ -3,11 +3,12 @@ package dao
import (
"context"
"cid/consts"
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var AdSource = &adSourceDao{}
@@ -17,27 +18,29 @@ type adSourceDao struct {
// GetByName 根据名称获取广告源
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, consts.AdSourceCollection)
return
}
// GetAvailableSources 获取可用的广告源
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1}))
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, consts.AdSourceCollection, page, nil)
return
}
// GetSourcesByProvider 根据提供商获取广告源
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1}))
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, consts.AdSourceCollection, page, nil)
return
}
// Create 创建广告源
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, "ad_sources")
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, consts.AdSourceCollection)
if err != nil {
return "", err
}
@@ -49,7 +52,7 @@ func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id
// Update 更新广告源
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
result, err := mongo.DB().Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, "ad_sources")
result, err := mongo.DB().Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, consts.AdSourceCollection)
if err != nil {
return 0, err
}
@@ -58,7 +61,7 @@ func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (af
// Delete 删除广告源
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "ad_sources")
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, consts.AdSourceCollection)
if err != nil {
return 0, err
}
@@ -67,13 +70,13 @@ func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, er
// GetByID 根据ID获取广告源
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, consts.AdSourceCollection)
return
}
// UpdateFields 更新广告源部分字段
func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) {
result, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "ad_sources")
result, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, consts.AdSourceCollection)
if err != nil {
return 0, err
}

View File

@@ -6,12 +6,10 @@ import (
"context"
"time"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/mongo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var Advertisement = &advertisement{}
@@ -231,24 +229,7 @@ func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq)
return
}
// 分页参数处理
pageNum := req.PageNum
if pageNum <= 0 {
pageNum = 1
}
pageSize := req.PageSize
if pageSize <= 0 {
pageSize = http.PageSize
}
limit := int64(pageSize)
skip := int64((pageNum - 1) * pageSize)
// 排序处理
sort := bson.M{"createdAt": -1}
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, opts)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, req.Page, nil)
return
}

View File

@@ -6,12 +6,10 @@ import (
"context"
"time"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/mongo"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var Advertiser = &advertiser{}
@@ -276,24 +274,7 @@ func (d *advertiser) List(ctx context.Context, req *dto.ListAdvertiserReq) (list
return
}
// 分页参数处理
pageNum := req.PageNum
if pageNum <= 0 {
pageNum = 1
}
pageSize := req.PageSize
if pageSize <= 0 {
pageSize = http.PageSize
}
limit := int64(pageSize)
skip := int64((pageNum - 1) * pageSize)
// 排序处理
sort := bson.M{"createdAt": -1}
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
err = mongo.DB().Find(ctx, filter, &list, entity.AdvertiserCollection, opts)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertiserCollection, req.Page, nil)
return
}

View File

@@ -5,9 +5,9 @@ import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// applicationDao 应用DAO
@@ -38,8 +38,10 @@ func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Applic
// GetByTenantID 根据租户ID获取应用列表
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
var apps []*entity.Application
err := mongo.DB().Find(ctx,
bson.M{"tenantId": tenantID}, &apps, "application")
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err := mongo.DB().Find(ctx,
bson.M{"tenantId": tenantID}, &apps, "application", page, nil)
return apps, err
}
@@ -76,11 +78,9 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
return nil, 0, err
}
offset := (page - 1) * pageSize
err = mongo.DB().Find(ctx, filter, &apps, "application",
options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(pageSize)))
// 使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(pageSize)}
total, err = mongo.DB().Find(ctx, filter, &apps, "application", pageBean, nil)
if err != nil {
return nil, 0, err
}

View File

@@ -5,9 +5,9 @@ import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var CIDRequest = &cidRequestDao{}
@@ -31,19 +31,9 @@ func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest)
func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, size int) (list []*entity.CidRequest, total int64, err error) {
filter := bson.M{"userId": userId}
// 获取总数
total, err = mongo.DB().Count(ctx, filter, entity.CidRequestCollection)
if err != nil {
return
}
// 分页查询
offset := (page - 1) * size
err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection,
options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))
// 分页查询使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
total, err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection, pageBean, nil)
return
}

View File

@@ -5,9 +5,9 @@ import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var Strategy = &strategyDao{}
@@ -29,8 +29,7 @@ func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.
// GetByTenantLevel 根据租户级别获取策略
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1}))
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies")
return
}
@@ -82,12 +81,8 @@ func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel,
return
}
// 分页查询
offset := (page - 1) * size
err = mongo.DB().Find(ctx, filter, &list, "strategies",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))
// 分页查询使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
total, err = mongo.DB().Find(ctx, filter, &list, "strategies", pageBean, nil)
return
}

2
go.mod
View File

@@ -11,7 +11,7 @@ require (
golang.org/x/net v0.47.0
)
//replace gitee.com/red-future---jilin-g/common v0.2.9 => ../common
replace gitee.com/red-future---jilin-g/common => ../common
require (
github.com/BurntSushi/toml v1.5.0 // indirect

View File

@@ -3,8 +3,9 @@ package dto
import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
"github.com/gogf/gf/v2/frame/g"
"go.mongodb.org/mongo-driver/v2/bson"
)
// AddAdPositionReq 添加广告位请求
@@ -46,7 +47,7 @@ type AddAdPositionReq struct {
}
type AddAdPositionRes struct {
Id string `json:"id"`
Id *bson.ObjectID `json:"id"`
}
// UpdateAdPositionReq 更新广告位请求
@@ -102,7 +103,7 @@ type GetAdPositionRes struct {
// ListAdPositionReq 获取广告位列表请求
type ListAdPositionReq struct {
g.Meta `path:"/list" method:"get" tags:"广告位管理" summary:"获取广告位列表" dc:"分页查询广告位列表,支持多条件筛选"`
http.Page
*beans.Page
Name string `json:"name"` // 广告位名称模糊查询
PositionCode string `json:"positionCode"` // 广告位编码

View File

@@ -3,7 +3,7 @@ package dto
import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
"github.com/gogf/gf/v2/frame/g"
)
@@ -80,7 +80,7 @@ type GetAdSourceRes struct {
// ListAdSourceReq 获取广告源列表请求
type ListAdSourceReq struct {
g.Meta `path:"/getList" method:"get" tags:"广告源管理" summary:"获取广告源列表" dc:"分页查询广告源列表,支持多条件筛选"`
http.Page
*beans.Page
Name string `json:"name"` // 广告源名称模糊查询
Code string `json:"code"` // 广告源编码

View File

@@ -3,7 +3,7 @@ package dto
import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
"github.com/gogf/gf/v2/frame/g"
)
@@ -83,7 +83,7 @@ type GetAdvertisementRes struct {
// ListAdvertisementReq 获取广告列表请求
type ListAdvertisementReq struct {
g.Meta `path:"/list" method:"get" tags:"广告管理" summary:"获取广告列表" dc:"分页查询广告列表,支持多条件筛选"`
http.Page
*beans.Page
AdvertiserId string `json:"advertiserId"` // 广告主ID
AdPositionId string `json:"adPositionId"` // 广告位ID

View File

@@ -3,7 +3,7 @@ package dto
import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
"github.com/gogf/gf/v2/frame/g"
)
@@ -103,7 +103,7 @@ type GetAdvertiserRes struct {
// ListAdvertiserReq 获取广告主列表请求
type ListAdvertiserReq struct {
g.Meta `path:"/list" method:"get" tags:"广告主管理" summary:"获取广告主列表" dc:"分页查询广告主列表,支持多条件筛选"`
http.Page
*beans.Page
Name string `json:"name"` // 广告主名称模糊查询
ContactName string `json:"contactName"` // 联系人模糊查询

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)
}