@@ -1,10 +1,10 @@
package service
import (
"cidservice /dao"
"cidservice /model/dto"
"cidservice /model/entity"
"cidservice /model/types"
"cid/dao"
"cid/model/dto"
"cid/model/entity"
"cid/model/types"
"context"
"encoding/json"
"fmt"
@@ -19,10 +19,10 @@ import (
)
var (
CID = cidService { }
CID = cid { }
)
type cidService struct { }
type cid struct { }
// AdMatchingStrategy 广告匹配策略
type AdMatchingStrategy struct {
@@ -34,7 +34,7 @@ type AdMatchingStrategy struct {
}
// getMatchingStrategy 获取匹配策略
func ( s * cidService ) getMatchingStrategy ( ctx context . Context , tenantLevel string ) ( * AdMatchingStrategy , error ) {
func ( s * cid ) getMatchingStrategy ( ctx context . Context , tenantLevel string ) ( * AdMatchingStrategy , error ) {
// 从数据库获取策略
strategyEntity , err := Strategy . GetStrategyByTenantLevel ( ctx , tenantLevel )
if err != nil {
@@ -72,7 +72,7 @@ func (s *cidService) getMatchingStrategy(ctx context.Context, tenantLevel string
}
// GenerateCID 生成CID广告
func ( s * cidService ) GenerateCID ( ctx context . Context , req * dto . GenerateCIDReq ) ( res * dto . GenerateCIDRes , err error ) {
func ( s * cid ) GenerateCID ( ctx context . Context , req * dto . GenerateCIDReq ) ( res * dto . GenerateCIDRes , err error ) {
// 获取当前用户信息
userInfo , err := utils . GetUserInfo ( ctx )
if err != nil {
@@ -123,7 +123,7 @@ func (s *cidService) GenerateCID(ctx context.Context, req *dto.GenerateCIDReq) (
}
// getTenantByUser 根据用户获取租户信息
func ( s * cidService ) getTenantByUser ( ctx context . Context , userId int64 ) ( * types . Tenant , error ) {
func ( s * cid ) getTenantByUser ( ctx context . Context , userId int64 ) ( * types . Tenant , error ) {
// 通过common模块获取用户信息, 包含租户ID
userInfo , err := utils . GetUserInfo ( ctx )
if err != nil {
@@ -164,7 +164,7 @@ func (s *cidService) getTenantByUser(ctx context.Context, userId int64) (*types.
}
// matchAds 根据策略匹配广告
func ( s * cidService ) matchAds ( ctx context . Context , req * dto . GenerateCIDReq , strategy * AdMatchingStrategy ) ( [ ] * dto . AdInfo , error ) {
func ( s * cid ) matchAds ( ctx context . Context , req * dto . GenerateCIDReq , strategy * AdMatchingStrategy ) ( [ ] * dto . AdInfo , error ) {
var matchedAds [ ] * dto . AdInfo
// 根据策略权重从不同源获取广告
@@ -202,7 +202,7 @@ func (s *cidService) matchAds(ctx context.Context, req *dto.GenerateCIDReq, stra
}
// getAdsFromSource 从指定广告源获取广告
func ( s * cidService ) getAdsFromSource ( ctx context . Context , source string , req * dto . GenerateCIDReq , strategy * AdMatchingStrategy , weight int ) ( [ ] * dto . AdInfo , error ) {
func ( s * cid ) getAdsFromSource ( ctx context . Context , source string , req * dto . GenerateCIDReq , strategy * AdMatchingStrategy , weight int ) ( [ ] * dto . AdInfo , error ) {
switch source {
case "self" :
return s . getSelfServiceAds ( ctx , req , weight )
@@ -216,7 +216,7 @@ func (s *cidService) getAdsFromSource(ctx context.Context, source string, req *d
}
// getSelfServiceAds 获取自营广告
func ( s * cidService ) getSelfServiceAds ( ctx context . Context , req * dto . GenerateCIDReq , count int ) ( [ ] * dto . AdInfo , error ) {
func ( s * cid ) getSelfServiceAds ( ctx context . Context , req * dto . GenerateCIDReq , count int ) ( [ ] * dto . AdInfo , error ) {
// 这里应该从数据库查询自营广告
// 暂时返回模拟数据
ads := make ( [ ] * dto . AdInfo , 0 )
@@ -236,7 +236,7 @@ func (s *cidService) getSelfServiceAds(ctx context.Context, req *dto.GenerateCID
}
// getGoogleAds 获取Google广告
func ( s * cidService ) getGoogleAds ( ctx context . Context , req * dto . GenerateCIDReq , count int ) ( [ ] * dto . AdInfo , error ) {
func ( s * cid ) getGoogleAds ( ctx context . Context , req * dto . GenerateCIDReq , count int ) ( [ ] * dto . AdInfo , error ) {
// 这里应该调用Google Ads API
// 暂时返回模拟数据
ads := make ( [ ] * dto . AdInfo , 0 )
@@ -256,7 +256,7 @@ func (s *cidService) getGoogleAds(ctx context.Context, req *dto.GenerateCIDReq,
}
// getFacebookAds 获取Facebook广告
func ( s * cidService ) getFacebookAds ( ctx context . Context , req * dto . GenerateCIDReq , count int ) ( [ ] * dto . AdInfo , error ) {
func ( s * cid ) getFacebookAds ( ctx context . Context , req * dto . GenerateCIDReq , count int ) ( [ ] * dto . AdInfo , error ) {
// 这里应该调用Facebook Ads API
// 暂时返回模拟数据
ads := make ( [ ] * dto . AdInfo , 0 )
@@ -276,14 +276,14 @@ func (s *cidService) getFacebookAds(ctx context.Context, req *dto.GenerateCIDReq
}
// generateUniqueCID 生成唯一CID
func ( s * cidService ) generateUniqueCID ( ) string {
func ( s * cid ) generateUniqueCID ( ) string {
timestamp := time . Now ( ) . Unix ( )
random := rand . Intn ( 8999 ) + 1000
return fmt . Sprintf ( "CID_%d_%d" , timestamp , random )
}
// recordCIDRequest 记录CID请求
func ( s * cidService ) recordCIDRequest ( ctx context . Context , req * dto . GenerateCIDReq , tenant * types . Tenant , ads [ ] * dto . AdInfo ) {
func ( s * cid ) recordCIDRequest ( ctx context . Context , req * dto . GenerateCIDReq , tenant * types . Tenant , ads [ ] * dto . AdInfo ) {
// 转换dto.AdInfo到entity.Ad
var entityAds [ ] entity . Ad
for _ , ad := range ads {
@@ -312,7 +312,7 @@ func (s *cidService) recordCIDRequest(ctx context.Context, req *dto.GenerateCIDR
}
// GetCIDHistory 获取CID请求历史
func ( s * cidService ) GetCIDHistory ( ctx context . Context , userId int64 , page , size int ) ( res * dto . GetCIDHistoryRes , err error ) {
func ( s * cid ) GetCIDHistory ( ctx context . Context , userId int64 , page , size int ) ( res * dto . GetCIDHistoryRes , err error ) {
history , total , err := dao . CIDRequest . GetHistory ( ctx , userId , page , size )
if err != nil {
return nil , err