127 lines
3.5 KiB
Go
127 lines
3.5 KiB
Go
package dao
|
||
|
||
import (
|
||
"cid/model/dto"
|
||
"cid/model/entity"
|
||
"context"
|
||
|
||
"gitea.com/red-future/common/beans"
|
||
"gitea.com/red-future/common/db/mongo"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
"go.mongodb.org/mongo-driver/v2/bson"
|
||
)
|
||
|
||
var AdPosition = &adPosition{}
|
||
|
||
type adPosition struct {
|
||
}
|
||
|
||
// 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
|
||
}
|
||
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, entity.AdPositionCollection)
|
||
return
|
||
}
|
||
|
||
// Update 更新广告位
|
||
func (d *adPosition) Update(ctx context.Context, id *bson.ObjectID, updateData *entity.AdPosition) (err error) {
|
||
filter := bson.M{"_id": id}
|
||
|
||
if !g.IsEmpty(updateData) {
|
||
bsonm, err := mongo.EntityToBSONM(updateData)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
update := bson.M{"$set": bsonm}
|
||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
|
||
}
|
||
return
|
||
}
|
||
|
||
// UpdateStatus 更新广告位状态
|
||
func (d *adPosition) UpdateStatus(ctx context.Context, id *bson.ObjectID, status string) (err error) {
|
||
filter := bson.M{"_id": id}
|
||
update := bson.M{"$set": bson.M{"status": status}}
|
||
|
||
_, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
|
||
return
|
||
}
|
||
|
||
// GetOne 获取单个广告位
|
||
func (d *adPosition) GetOne(ctx context.Context, id *bson.ObjectID) (adPosition *entity.AdPosition, err error) {
|
||
filter := bson.M{"_id": id}
|
||
|
||
adPosition = &entity.AdPosition{}
|
||
err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
|
||
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)
|
||
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)
|
||
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)
|
||
return
|
||
}
|