259 lines
6.4 KiB
Go
259 lines
6.4 KiB
Go
package dao
|
|
|
|
import (
|
|
"cid/model/dto"
|
|
"cid/model/entity"
|
|
"context"
|
|
|
|
"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 AdPosition = &adPosition{}
|
|
|
|
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)
|
|
}
|
|
|
|
_, err = mongo.DB().Insert(ctx, []interface{}{adPosition}, 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}
|
|
|
|
// 构建动态更新字段
|
|
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}
|
|
_, 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}
|
|
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}
|
|
|
|
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)
|
|
return
|
|
}
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
func (d *adPosition) buildListFilter(req *dto.ListAdPositionReq) bson.M {
|
|
filter := bson.M{}
|
|
|
|
if !g.IsEmpty(req.Name) {
|
|
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
|
}
|
|
if !g.IsEmpty(req.PositionCode) {
|
|
filter["positionCode"] = req.PositionCode
|
|
}
|
|
if !g.IsEmpty(req.PageName) {
|
|
filter["page"] = req.PageName
|
|
}
|
|
if !g.IsEmpty(req.Section) {
|
|
filter["section"] = req.Section
|
|
}
|
|
if !g.IsEmpty(req.Status) {
|
|
filter["status"] = req.Status
|
|
}
|
|
if !g.IsEmpty(req.AdFormat) {
|
|
filter["adFormat"] = req.AdFormat
|
|
}
|
|
|
|
// 处理日期范围
|
|
if len(req.DateRange) == 2 {
|
|
startTime := gconv.Int64(req.DateRange[0])
|
|
endTime := gconv.Int64(req.DateRange[1])
|
|
filter["createdAt"] = bson.M{
|
|
"$gte": startTime,
|
|
"$lte": endTime,
|
|
}
|
|
}
|
|
|
|
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)
|
|
return
|
|
}
|
|
|
|
// GetAvailableAdPositions 获取可用的广告位列表
|
|
func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entity.AdPosition, err error) {
|
|
filter := bson.M{
|
|
"status": "启用", // 只返回启用的广告位
|
|
}
|
|
|
|
opts := options.Find().SetSort(bson.M{"createdAt": -1})
|
|
|
|
err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, opts)
|
|
return
|
|
}
|