优化mongo,封装count逻辑,处理objectId
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user