2025-12-06 09:10:24 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-09 16:10:45 +08:00
|
|
|
|
"cid/model/dto"
|
|
|
|
|
|
"cid/model/entity"
|
2025-12-06 09:10:24 +08:00
|
|
|
|
"context"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
2026-02-24 17:17:10 +08:00
|
|
|
|
"gitea.com/red-future/common/db/mongo"
|
2025-12-06 09:10:24 +08:00
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
var Advertisement = &advertisement{}
|
2025-12-29 15:27:00 +08:00
|
|
|
|
|
|
|
|
|
|
type advertisement struct {
|
|
|
|
|
|
}
|
2025-12-06 09:10:24 +08:00
|
|
|
|
|
|
|
|
|
|
// Insert 插入广告
|
|
|
|
|
|
func (d *advertisement) Insert(ctx context.Context, advertisement *entity.Advertisement) (err error) {
|
|
|
|
|
|
// 获取stream消息
|
|
|
|
|
|
redis := g.Redis()
|
|
|
|
|
|
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "advertisement_stream", "$")
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
_, err = mongo.DB().Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update 更新广告
|
|
|
|
|
|
func (d *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisementReq) (err error) {
|
|
|
|
|
|
objectId, err := bson.ObjectIDFromHex(req.Id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter := bson.M{"_id": objectId}
|
|
|
|
|
|
|
|
|
|
|
|
// 构建动态更新字段
|
|
|
|
|
|
updateFields := bson.M{}
|
|
|
|
|
|
|
|
|
|
|
|
// 广告基本信息
|
|
|
|
|
|
if !g.IsEmpty(req.Title) {
|
|
|
|
|
|
updateFields["title"] = req.Title
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.Description) {
|
|
|
|
|
|
updateFields["description"] = req.Description
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AdvertiserId) {
|
|
|
|
|
|
updateFields["advertiserId"] = req.AdvertiserId
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AdPositionId) {
|
|
|
|
|
|
updateFields["adPositionId"] = req.AdPositionId
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AdType) {
|
|
|
|
|
|
updateFields["adType"] = req.AdType
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AdFormat) {
|
|
|
|
|
|
updateFields["adFormat"] = req.AdFormat
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.MaterialUrl) {
|
|
|
|
|
|
updateFields["materialUrl"] = req.MaterialUrl
|
|
|
|
|
|
}
|
2025-12-09 13:32:43 +08:00
|
|
|
|
if !g.IsEmpty(req.TargetUrl) {
|
|
|
|
|
|
updateFields["targetUrl"] = req.TargetUrl
|
2025-12-06 09:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 投放设置
|
|
|
|
|
|
if req.StartDate != nil {
|
|
|
|
|
|
updateFields["startDate"] = *req.StartDate
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.EndDate != nil {
|
|
|
|
|
|
updateFields["endDate"] = *req.EndDate
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.Budget != nil {
|
|
|
|
|
|
updateFields["budget"] = *req.Budget
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.DailyBudget != nil {
|
|
|
|
|
|
updateFields["dailyBudget"] = *req.DailyBudget
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.BidAmount != nil {
|
|
|
|
|
|
updateFields["bidAmount"] = *req.BidAmount
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.BillingType) {
|
|
|
|
|
|
updateFields["billingType"] = req.BillingType
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 投放条件
|
|
|
|
|
|
if req.Targeting != nil {
|
|
|
|
|
|
updateFields["targeting"] = req.Targeting
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 状态信息
|
|
|
|
|
|
if req.Status != nil {
|
|
|
|
|
|
updateFields["status"] = *req.Status
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.AuditStatus != nil {
|
|
|
|
|
|
updateFields["auditStatus"] = *req.AuditStatus
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.AuditReason != nil {
|
|
|
|
|
|
updateFields["auditReason"] = *req.AuditReason
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(updateFields) > 0 {
|
|
|
|
|
|
update := bson.M{"$set": updateFields}
|
2025-12-30 10:55:35 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateStatus 更新广告状态
|
|
|
|
|
|
func (d *advertisement) 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}}
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Audit 审核广告
|
|
|
|
|
|
func (d *advertisement) Audit(ctx context.Context, id, auditStatus, auditReason string) (err error) {
|
|
|
|
|
|
objectId, err := bson.ObjectIDFromHex(id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter := bson.M{"_id": objectId}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前用户ID(实际项目中应从上下文获取)
|
|
|
|
|
|
auditBy := "system"
|
|
|
|
|
|
auditTime := time.Now().Unix()
|
|
|
|
|
|
|
|
|
|
|
|
update := bson.M{
|
|
|
|
|
|
"$set": bson.M{
|
|
|
|
|
|
"auditStatus": auditStatus,
|
|
|
|
|
|
"auditReason": auditReason,
|
|
|
|
|
|
"auditTime": auditTime,
|
|
|
|
|
|
"auditBy": auditBy,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateStatistics 更新广告统计数据
|
|
|
|
|
|
func (d *advertisement) 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}
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetOne 获取单个广告
|
|
|
|
|
|
func (d *advertisement) GetOne(ctx context.Context, id string) (advertisement *entity.Advertisement, err error) {
|
|
|
|
|
|
objectId, err := bson.ObjectIDFromHex(id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
filter := bson.M{"_id": objectId}
|
|
|
|
|
|
|
|
|
|
|
|
advertisement = &entity.Advertisement{}
|
2025-12-30 10:55:35 +08:00
|
|
|
|
err = mongo.DB().FindOne(ctx, filter, advertisement, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
|
|
|
|
func (d *advertisement) buildListFilter(req *dto.ListAdvertisementReq) bson.M {
|
|
|
|
|
|
filter := bson.M{}
|
|
|
|
|
|
|
|
|
|
|
|
if !g.IsEmpty(req.AdvertiserId) {
|
|
|
|
|
|
filter["advertiserId"] = req.AdvertiserId
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AdPositionId) {
|
|
|
|
|
|
filter["adPositionId"] = req.AdPositionId
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AdType) {
|
|
|
|
|
|
filter["adType"] = req.AdType
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.Status) {
|
|
|
|
|
|
filter["status"] = req.Status
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.AuditStatus) {
|
|
|
|
|
|
filter["auditStatus"] = req.AuditStatus
|
|
|
|
|
|
}
|
|
|
|
|
|
if !g.IsEmpty(req.Title) {
|
|
|
|
|
|
filter["title"] = bson.M{"$regex": req.Title, "$options": "i"}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理日期范围
|
|
|
|
|
|
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 *advertisement) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
total, err = mongo.DB().Count(ctx, filter, entity.AdvertisementCollection)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// List 获取广告列表
|
|
|
|
|
|
func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq) (list []*entity.Advertisement, total int64, err error) {
|
|
|
|
|
|
// 构建查询过滤条件
|
|
|
|
|
|
filter := d.buildListFilter(req)
|
|
|
|
|
|
|
|
|
|
|
|
// 检查总数
|
|
|
|
|
|
total, err = d.checkTotalCount(ctx, filter)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-08 11:07:58 +08:00
|
|
|
|
// 使用common/mongo的Find方法,自动处理分页、租户等
|
|
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, req.Page, nil)
|
2025-12-06 09:10:24 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|