168 lines
4.3 KiB
Go
168 lines
4.3 KiB
Go
package service
|
|
|
|
import (
|
|
"cid/dao"
|
|
"cid/model/dto"
|
|
"cid/model/entity"
|
|
"context"
|
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
var AdPosition = new(adPosition)
|
|
|
|
type adPosition struct{}
|
|
|
|
// Add 添加广告位
|
|
func (s *adPosition) Add(ctx context.Context, req *dto.AddAdPositionReq) (res *dto.AddAdPositionRes, err error) {
|
|
ids, err := dao.AdPosition.Insert(ctx, req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
res = &dto.AddAdPositionRes{Id: ids[0].(*bson.ObjectID)}
|
|
return
|
|
}
|
|
|
|
// Update 更新广告位
|
|
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) 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) {
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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) {
|
|
// 返回匹配的广告
|
|
// 这里返回第一个广告作为示例
|
|
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
|
|
}
|