初始化项目
This commit is contained in:
256
dao/advertisement_dao.go
Normal file
256
dao/advertisement_dao.go
Normal file
@@ -0,0 +1,256 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cidService/model/dto"
|
||||
"cidService/model/entity"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"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 Advertisement = &advertisement{}
|
||||
|
||||
type advertisement struct{}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection)
|
||||
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
|
||||
}
|
||||
if !g.IsEmpty(req.LinkUrl) {
|
||||
updateFields["linkUrl"] = req.LinkUrl
|
||||
}
|
||||
if !g.IsEmpty(req.LandingPageUrl) {
|
||||
updateFields["landingPageUrl"] = req.LandingPageUrl
|
||||
}
|
||||
|
||||
// 投放设置
|
||||
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}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
}
|
||||
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}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
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}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
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{}
|
||||
err = mongo.FindOne(ctx, filter, advertisement, entity.AdvertisementCollection)
|
||||
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) {
|
||||
total, err = mongo.Count(ctx, filter, entity.AdvertisementCollection)
|
||||
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
|
||||
}
|
||||
|
||||
// 分页参数处理
|
||||
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.Find(ctx, filter, &list, entity.AdvertisementCollection, opts)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user