119 lines
3.1 KiB
Go
119 lines
3.1 KiB
Go
package service
|
||
|
||
import (
|
||
"cid/dao"
|
||
"cid/model/dto"
|
||
"cid/model/entity"
|
||
"context"
|
||
"time"
|
||
|
||
"github.com/gogf/gf/v2/errors/gerror"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
)
|
||
|
||
var AdPosition = new(adPosition)
|
||
|
||
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 {
|
||
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()}
|
||
return
|
||
}
|
||
|
||
// Update 更新广告位
|
||
func (s *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (err error) {
|
||
// 更新修改时间(不需要设置,DAO层会处理)
|
||
return dao.AdPosition.Update(ctx, req)
|
||
}
|
||
|
||
// UpdateStatus 更新广告位状态
|
||
func (s *adPosition) UpdateStatus(ctx context.Context, req *dto.UpdateAdPositionStatusReq) (err error) {
|
||
return dao.AdPosition.UpdateStatus(ctx, req.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)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
res = &dto.GetAdPositionRes{
|
||
AdPosition: adPosition,
|
||
}
|
||
return
|
||
}
|
||
|
||
// List 获取广告位列表
|
||
func (s *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (res *dto.ListAdPositionRes, err error) {
|
||
list, total, err := dao.AdPosition.List(ctx, req)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
res = &dto.ListAdPositionRes{
|
||
List: list,
|
||
Total: int(total),
|
||
}
|
||
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)
|
||
}
|
||
|
||
// 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{
|
||
Title: "示例广告",
|
||
MaterialUrl: "https://example.com/ad.jpg",
|
||
TargetUrl: "https://example.com",
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
// UpdateAdPositionStatistics 更新广告位统计
|
||
func (s *adPosition) UpdateAdPositionStatistics(ctx context.Context, id string, impressions, clicks, revenue int64) (err error) {
|
||
return
|
||
}
|