mongo.go重构

This commit is contained in:
2025-12-30 10:09:18 +08:00
parent 5ef3957be9
commit ec2cc8e995

View File

@@ -14,16 +14,9 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
var Advertiser = &advertiser{
NoCache: true,
}
var Advertiser = &advertiser{}
type advertiser struct {
NoCache bool
}
func (d *advertiser) SetNoCache() {
Advertiser.NoCache = true
}
// Insert 插入广告主
@@ -37,7 +30,7 @@ func (d *advertiser) Insert(ctx context.Context, advertiser *entity.Advertiser)
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
}
_, err = mongo.Insert(ctx, []interface{}{advertiser}, entity.AdvertiserCollection)
_, err = mongo.DB().Insert(ctx, []interface{}{advertiser}, entity.AdvertiserCollection)
return
}
@@ -138,7 +131,7 @@ func (d *advertiser) Update(ctx context.Context, req *dto.UpdateAdvertiserReq) (
if len(updateFields) > 0 {
update := bson.M{"$set": updateFields}
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
}
return
}
@@ -152,7 +145,7 @@ func (d *advertiser) UpdateStatus(ctx context.Context, id, status string) (err e
filter := bson.M{"_id": objectId}
update := bson.M{"$set": bson.M{"status": status}}
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
@@ -177,7 +170,7 @@ func (d *advertiser) Audit(ctx context.Context, id, auditStatus, auditReason str
},
}
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
@@ -191,7 +184,7 @@ func (d *advertiser) Recharge(ctx context.Context, id string, amount int64, rema
// 先获取当前余额
advertiser := &entity.Advertiser{}
err = mongo.FindOne(ctx, d.NoCache, filter, advertiser, entity.AdvertiserCollection)
err = mongo.DB().FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
if err != nil {
return
}
@@ -200,7 +193,7 @@ func (d *advertiser) Recharge(ctx context.Context, id string, amount int64, rema
newBalance := advertiser.AccountBalance + amount
update := bson.M{"$set": bson.M{"accountBalance": newBalance}}
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
@@ -213,7 +206,7 @@ func (d *advertiser) UpdateCreditLimit(ctx context.Context, id string, creditLim
filter := bson.M{"_id": objectId}
update := bson.M{"$set": bson.M{"creditLimit": creditLimit}}
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
_, err = mongo.DB().Update(ctx, filter, update, entity.AdvertiserCollection)
return
}
@@ -226,7 +219,7 @@ func (d *advertiser) GetOne(ctx context.Context, id string) (advertiser *entity.
filter := bson.M{"_id": objectId}
advertiser = &entity.Advertiser{}
err = mongo.FindOne(ctx, d.NoCache, filter, advertiser, entity.AdvertiserCollection)
err = mongo.DB().FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
return
}
@@ -268,7 +261,7 @@ func (d *advertiser) buildListFilter(req *dto.ListAdvertiserReq) bson.M {
// checkTotalCount 检查总数
func (d *advertiser) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
total, err = mongo.Count(ctx, d.NoCache, filter, entity.AdvertiserCollection)
total, err = mongo.DB().Count(ctx, filter, entity.AdvertiserCollection)
return
}
@@ -301,6 +294,6 @@ func (d *advertiser) List(ctx context.Context, req *dto.ListAdvertiserReq) (list
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.AdvertiserCollection, opts)
err = mongo.DB().Find(ctx, filter, &list, entity.AdvertiserCollection, opts)
return
}