Files
cid/dao/ad_position_dao.go

127 lines
3.5 KiB
Go
Raw Normal View History

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"
2026-02-24 16:24:47 +08:00
"gitea.com/red-future/common/beans"
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 AdPosition = &adPosition{}
type adPosition struct {
}
2025-12-06 09:10:24 +08:00
// Insert 插入广告位
func (d *adPosition) Insert(ctx context.Context, req *dto.AddAdPositionReq) (ids []any, err error) {
var result entity.AdPosition
if err = gconv.Struct(req, &result); err != nil {
return
2025-12-06 09:10:24 +08:00
}
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, entity.AdPositionCollection)
2025-12-06 09:10:24 +08:00
return
}
// Update 更新广告位
func (d *adPosition) Update(ctx context.Context, id *bson.ObjectID, updateData *entity.AdPosition) (err error) {
filter := bson.M{"_id": id}
2025-12-06 09:10:24 +08:00
if !g.IsEmpty(updateData) {
bsonm, err := mongo.EntityToBSONM(updateData)
if err != nil {
return err
}
update := bson.M{"$set": bsonm}
2025-12-30 10:55:35 +08:00
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
2025-12-06 09:10:24 +08:00
}
return
}
// UpdateStatus 更新广告位状态
func (d *adPosition) UpdateStatus(ctx context.Context, id *bson.ObjectID, status string) (err error) {
filter := bson.M{"_id": id}
2025-12-06 09:10:24 +08:00
update := bson.M{"$set": bson.M{"status": status}}
2025-12-30 10:55:35 +08:00
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
2025-12-06 09:10:24 +08:00
return
}
// GetOne 获取单个广告位
func (d *adPosition) GetOne(ctx context.Context, id *bson.ObjectID) (adPosition *entity.AdPosition, err error) {
filter := bson.M{"_id": id}
2025-12-06 09:10:24 +08:00
adPosition = &entity.AdPosition{}
2025-12-30 10:55:35 +08:00
err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
2025-12-06 09:10:24 +08:00
return
}
// Delete 删除广告位
func (d *adPosition) Delete(ctx context.Context, id *bson.ObjectID) (err error) {
filter := bson.M{"_id": id}
_, err = mongo.DB().Delete(ctx, filter, entity.AdPositionCollection)
2025-12-06 09:10:24 +08:00
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
}
// List 获取广告位列表
func (d *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (list []*entity.AdPosition, total int64, err error) {
// 构建查询过滤条件
filter := d.buildListFilter(req)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, req.Page, nil)
2025-12-06 09:10:24 +08:00
return
}
// GetAvailableAdPositions 获取可用的广告位列表
func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entity.AdPosition, err error) {
filter := bson.M{
"status": "启用", // 只返回启用的广告位
}
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, page, nil)
2025-12-06 09:10:24 +08:00
return
}