优化mongo,封装count逻辑,处理objectId

This commit is contained in:
2026-01-08 11:07:58 +08:00
parent 65c80ae56f
commit e85c8453de
34 changed files with 753 additions and 446 deletions

View File

@@ -5,12 +5,11 @@ import (
"cid/model/entity"
"context"
"gitee.com/red-future---jilin-g/common/http"
"gitee.com/red-future---jilin-g/common/beans"
"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{}
@@ -19,154 +18,52 @@ 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)
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
}
_, err = mongo.DB().Insert(ctx, []interface{}{adPosition}, entity.AdPositionCollection)
ids, err = mongo.DB().Insert(ctx, []interface{}{&result}, 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}
func (d *adPosition) Update(ctx context.Context, id *bson.ObjectID, updateData *entity.AdPosition) (err error) {
filter := bson.M{"_id": id}
// 构建动态更新字段
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}
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, status string) (err error) {
objectId, err := bson.ObjectIDFromHex(id)
if err != nil {
return
}
filter := bson.M{"_id": objectId}
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
}
// 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}
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
}
// 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)
// 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
}
@@ -206,42 +103,13 @@ func (d *adPosition) buildListFilter(req *dto.ListAdPositionReq) bson.M {
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)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, req.Page, nil)
return
}
@@ -251,8 +119,8 @@ func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entit
"status": "启用", // 只返回启用的广告位
}
opts := options.Find().SetSort(bson.M{"createdAt": -1})
err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, opts)
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, page, nil)
return
}

View File

@@ -3,11 +3,12 @@ package dao
import (
"context"
"cid/consts"
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var AdSource = &adSourceDao{}
@@ -17,27 +18,29 @@ type adSourceDao struct {
// GetByName 根据名称获取广告源
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, consts.AdSourceCollection)
return
}
// GetAvailableSources 获取可用的广告源
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1}))
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, consts.AdSourceCollection, page, nil)
return
}
// GetSourcesByProvider 根据提供商获取广告源
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1}))
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, consts.AdSourceCollection, page, nil)
return
}
// Create 创建广告源
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, "ad_sources")
ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, consts.AdSourceCollection)
if err != nil {
return "", err
}
@@ -49,7 +52,7 @@ func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id
// Update 更新广告源
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
result, err := mongo.DB().Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, "ad_sources")
result, err := mongo.DB().Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, consts.AdSourceCollection)
if err != nil {
return 0, err
}
@@ -58,7 +61,7 @@ func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (af
// Delete 删除广告源
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "ad_sources")
count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, consts.AdSourceCollection)
if err != nil {
return 0, err
}
@@ -67,13 +70,13 @@ func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, er
// GetByID 根据ID获取广告源
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, consts.AdSourceCollection)
return
}
// UpdateFields 更新广告源部分字段
func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) {
result, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "ad_sources")
result, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, consts.AdSourceCollection)
if err != nil {
return 0, err
}

View File

@@ -6,12 +6,10 @@ import (
"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{}
@@ -231,24 +229,7 @@ func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq)
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.AdvertisementCollection, opts)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, req.Page, nil)
return
}

View File

@@ -6,12 +6,10 @@ import (
"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 Advertiser = &advertiser{}
@@ -276,24 +274,7 @@ func (d *advertiser) List(ctx context.Context, req *dto.ListAdvertiserReq) (list
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.AdvertiserCollection, opts)
// 使用common/mongo的Find方法自动处理分页、租户等
total, err = mongo.DB().Find(ctx, filter, &list, entity.AdvertiserCollection, req.Page, nil)
return
}

View File

@@ -5,9 +5,9 @@ import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// applicationDao 应用DAO
@@ -38,8 +38,10 @@ func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Applic
// GetByTenantID 根据租户ID获取应用列表
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
var apps []*entity.Application
err := mongo.DB().Find(ctx,
bson.M{"tenantId": tenantID}, &apps, "application")
// 使用空的Page参数获取所有数据
page := &beans.Page{PageNum: 1, PageSize: -1} // -1表示不分页
_, err := mongo.DB().Find(ctx,
bson.M{"tenantId": tenantID}, &apps, "application", page, nil)
return apps, err
}
@@ -76,11 +78,9 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
return nil, 0, err
}
offset := (page - 1) * pageSize
err = mongo.DB().Find(ctx, filter, &apps, "application",
options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(pageSize)))
// 使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(pageSize)}
total, err = mongo.DB().Find(ctx, filter, &apps, "application", pageBean, nil)
if err != nil {
return nil, 0, err
}

View File

@@ -5,9 +5,9 @@ import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var CIDRequest = &cidRequestDao{}
@@ -31,19 +31,9 @@ func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest)
func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, size int) (list []*entity.CidRequest, total int64, err error) {
filter := bson.M{"userId": userId}
// 获取总数
total, err = mongo.DB().Count(ctx, filter, entity.CidRequestCollection)
if err != nil {
return
}
// 分页查询
offset := (page - 1) * size
err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection,
options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))
// 分页查询使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
total, err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection, pageBean, nil)
return
}

View File

@@ -5,9 +5,9 @@ import (
"cid/model/entity"
"gitee.com/red-future---jilin-g/common/beans"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var Strategy = &strategyDao{}
@@ -29,8 +29,7 @@ func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.
// GetByTenantLevel 根据租户级别获取策略
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1}))
err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies")
return
}
@@ -82,12 +81,8 @@ func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel,
return
}
// 分页查询
offset := (page - 1) * size
err = mongo.DB().Find(ctx, filter, &list, "strategies",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))
// 分页查询使用common/mongo的Find方法自动处理分页、租户等
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
total, err = mongo.DB().Find(ctx, filter, &list, "strategies", pageBean, nil)
return
}