mongo.go重构

This commit is contained in:
2025-12-30 10:55:35 +08:00
parent ec2cc8e995
commit b48613bca1
9 changed files with 65 additions and 102 deletions

View File

@@ -13,16 +13,9 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
) )
var AdPosition = &adPosition{ var AdPosition = &adPosition{}
NoCache: true,
}
type adPosition struct { type adPosition struct {
NoCache bool
}
func (d *adPosition) SetNoCache() {
AdPosition.NoCache = true
} }
// Insert 插入广告位 // Insert 插入广告位
@@ -36,7 +29,7 @@ func (d *adPosition) Insert(ctx context.Context, adPosition *entity.AdPosition)
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg) g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
} }
_, err = mongo.Insert(ctx, []interface{}{adPosition}, entity.AdPositionCollection) _, err = mongo.DB().Insert(ctx, []interface{}{adPosition}, entity.AdPositionCollection)
return return
} }
@@ -124,7 +117,7 @@ func (d *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (
if len(updateFields) > 0 { if len(updateFields) > 0 {
update := bson.M{"$set": updateFields} update := bson.M{"$set": updateFields}
_, err = mongo.Update(ctx, filter, update, entity.AdPositionCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
} }
return return
} }
@@ -138,7 +131,7 @@ func (d *adPosition) UpdateStatus(ctx context.Context, id, status string) (err e
filter := bson.M{"_id": objectId} filter := bson.M{"_id": objectId}
update := bson.M{"$set": bson.M{"status": status}} update := bson.M{"$set": bson.M{"status": status}}
_, err = mongo.Update(ctx, filter, update, entity.AdPositionCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
return return
} }
@@ -151,7 +144,7 @@ func (d *adPosition) UpdateStatistics(ctx context.Context, id string, stats map[
filter := bson.M{"_id": objectId} filter := bson.M{"_id": objectId}
update := bson.M{"$set": stats} update := bson.M{"$set": stats}
_, err = mongo.Update(ctx, filter, update, entity.AdPositionCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdPositionCollection)
return return
} }
@@ -164,7 +157,7 @@ func (d *adPosition) GetOne(ctx context.Context, id string) (adPosition *entity.
filter := bson.M{"_id": objectId} filter := bson.M{"_id": objectId}
adPosition = &entity.AdPosition{} adPosition = &entity.AdPosition{}
err = mongo.FindOne(ctx, d.NoCache, filter, adPosition, entity.AdPositionCollection) err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
return return
} }
@@ -173,7 +166,7 @@ func (d *adPosition) GetByCode(ctx context.Context, code string) (adPosition *en
filter := bson.M{"positionCode": code} filter := bson.M{"positionCode": code}
adPosition = &entity.AdPosition{} adPosition = &entity.AdPosition{}
err = mongo.FindOne(ctx, d.NoCache, filter, adPosition, entity.AdPositionCollection) err = mongo.DB().FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
return return
} }
@@ -215,7 +208,7 @@ func (d *adPosition) buildListFilter(req *dto.ListAdPositionReq) bson.M {
// checkTotalCount 检查总数 // checkTotalCount 检查总数
func (d *adPosition) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) { func (d *adPosition) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
total, err = mongo.Count(ctx, d.NoCache, filter, entity.AdPositionCollection) total, err = mongo.DB().Count(ctx, filter, entity.AdPositionCollection)
return return
} }
@@ -248,7 +241,7 @@ func (d *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (list
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort) opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.AdPositionCollection, opts) err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, opts)
return return
} }
@@ -260,6 +253,6 @@ func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entit
opts := options.Find().SetSort(bson.M{"createdAt": -1}) opts := options.Find().SetSort(bson.M{"createdAt": -1})
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.AdPositionCollection, opts) err = mongo.DB().Find(ctx, filter, &list, entity.AdPositionCollection, opts)
return return
} }

View File

@@ -4,46 +4,40 @@ import (
"context" "context"
"cid/model/entity" "cid/model/entity"
"gitee.com/red-future---jilin-g/common/mongo" "gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
) )
var AdSource = &adSourceDao{ var AdSource = &adSourceDao{}
NoCache: true,
}
type adSourceDao struct { type adSourceDao struct {
NoCache bool
}
func (d *adSourceDao) SetNoCache() {
AdSource.NoCache = true
} }
// GetByName 根据名称获取广告源 // GetByName 根据名称获取广告源
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) { func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
err = mongo.FindOne(ctx, d.NoCache, bson.M{"name": name}, &adSource, "ad_sources") err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
return return
} }
// GetAvailableSources 获取可用的广告源 // GetAvailableSources 获取可用的广告源
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) { func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
err = mongo.Find(ctx, d.NoCache, bson.M{"status": "active"}, &list, "ad_sources", err = mongo.DB().Find(ctx, bson.M{"status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1})) options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1}))
return return
} }
// GetSourcesByProvider 根据提供商获取广告源 // GetSourcesByProvider 根据提供商获取广告源
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) { func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
err = mongo.Find(ctx, d.NoCache, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources", err = mongo.DB().Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1})) options.Find().SetSort(bson.M{"priority": -1}))
return return
} }
// Create 创建广告源 // Create 创建广告源
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) { func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
ids, err := mongo.Insert(ctx, []interface{}{adSource}, "ad_sources") ids, err := mongo.DB().Insert(ctx, []interface{}{adSource}, "ad_sources")
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -55,7 +49,7 @@ func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id
// Update 更新广告源 // Update 更新广告源
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) { func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
result, err := mongo.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}, "ad_sources")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -64,7 +58,7 @@ func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (af
// Delete 删除广告源 // Delete 删除广告源
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) { func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
count, err := mongo.Delete(ctx, bson.M{"_id": id}, "ad_sources") count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "ad_sources")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -73,13 +67,13 @@ func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, er
// GetByID 根据ID获取广告源 // GetByID 根据ID获取广告源
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) { func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
err = mongo.FindOne(ctx, d.NoCache, bson.M{"_id": id}, &adSource, "ad_sources") err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
return return
} }
// UpdateFields 更新广告源部分字段 // UpdateFields 更新广告源部分字段
func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) { func (d *adSourceDao) UpdateFields(ctx context.Context, id string, data *entity.AdSource) (affected int64, err error) {
result, err := mongo.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}, "ad_sources")
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@@ -14,16 +14,9 @@ import (
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
) )
var Advertisement = &advertisement{ var Advertisement = &advertisement{}
NoCache: true,
}
type advertisement struct { type advertisement struct {
NoCache bool
}
func (d *advertisement) SetNoCache() {
Advertisement.NoCache = true
} }
// Insert 插入广告 // Insert 插入广告
@@ -37,7 +30,7 @@ func (d *advertisement) Insert(ctx context.Context, advertisement *entity.Advert
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg) g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
} }
_, err = mongo.Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection) _, err = mongo.DB().Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection)
return return
} }
@@ -116,7 +109,7 @@ func (d *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisement
if len(updateFields) > 0 { if len(updateFields) > 0 {
update := bson.M{"$set": updateFields} update := bson.M{"$set": updateFields}
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
} }
return return
} }
@@ -130,7 +123,7 @@ func (d *advertisement) UpdateStatus(ctx context.Context, id, status string) (er
filter := bson.M{"_id": objectId} filter := bson.M{"_id": objectId}
update := bson.M{"$set": bson.M{"status": status}} update := bson.M{"$set": bson.M{"status": status}}
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
return return
} }
@@ -155,7 +148,7 @@ func (d *advertisement) Audit(ctx context.Context, id, auditStatus, auditReason
}, },
} }
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
return return
} }
@@ -168,7 +161,7 @@ func (d *advertisement) UpdateStatistics(ctx context.Context, id string, stats m
filter := bson.M{"_id": objectId} filter := bson.M{"_id": objectId}
update := bson.M{"$set": stats} update := bson.M{"$set": stats}
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection) _, err = mongo.DB().Update(ctx, filter, update, entity.AdvertisementCollection)
return return
} }
@@ -181,7 +174,7 @@ func (d *advertisement) GetOne(ctx context.Context, id string) (advertisement *e
filter := bson.M{"_id": objectId} filter := bson.M{"_id": objectId}
advertisement = &entity.Advertisement{} advertisement = &entity.Advertisement{}
err = mongo.FindOne(ctx, d.NoCache, filter, advertisement, entity.AdvertisementCollection) err = mongo.DB().FindOne(ctx, filter, advertisement, entity.AdvertisementCollection)
return return
} }
@@ -223,7 +216,7 @@ func (d *advertisement) buildListFilter(req *dto.ListAdvertisementReq) bson.M {
// checkTotalCount 检查总数 // checkTotalCount 检查总数
func (d *advertisement) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) { func (d *advertisement) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
total, err = mongo.Count(ctx, d.NoCache, filter, entity.AdvertisementCollection) total, err = mongo.DB().Count(ctx, filter, entity.AdvertisementCollection)
return return
} }
@@ -256,6 +249,6 @@ func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq)
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort) opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.AdvertisementCollection, opts) err = mongo.DB().Find(ctx, filter, &list, entity.AdvertisementCollection, opts)
return return
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"cid/model/entity" "cid/model/entity"
"gitee.com/red-future---jilin-g/common/mongo" "gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
@@ -11,20 +12,13 @@ import (
// applicationDao 应用DAO // applicationDao 应用DAO
type applicationDao struct { type applicationDao struct {
NoCache bool
} }
var Application = &applicationDao{ var Application = &applicationDao{}
NoCache: true,
}
func (d *applicationDao) SetNoCache() {
Application.NoCache = true
}
// Create 创建应用 // Create 创建应用
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) { func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
ids, err := mongo.Insert(ctx, []interface{}{app}, "application") ids, err := mongo.DB().Insert(ctx, []interface{}{app}, "application")
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -37,14 +31,14 @@ func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (s
// GetByID 根据ID获取应用 // GetByID 根据ID获取应用
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) { func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
var app *entity.Application var app *entity.Application
err := mongo.FindOne(ctx, d.NoCache, bson.M{"_id": id}, &app, "application") err := mongo.DB().FindOne(ctx, bson.M{"_id": id}, &app, "application")
return app, err return app, err
} }
// GetByTenantID 根据租户ID获取应用列表 // GetByTenantID 根据租户ID获取应用列表
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) { func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
var apps []*entity.Application var apps []*entity.Application
err := mongo.Find(ctx, d.NoCache, err := mongo.DB().Find(ctx,
bson.M{"tenantId": tenantID}, &apps, "application") bson.M{"tenantId": tenantID}, &apps, "application")
return apps, err return apps, err
} }
@@ -52,20 +46,20 @@ func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]
// GetByAPIKey 根据API密钥获取应用 // GetByAPIKey 根据API密钥获取应用
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) { func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
var app *entity.Application var app *entity.Application
err := mongo.FindOne(ctx, d.NoCache, err := mongo.DB().FindOne(ctx,
bson.M{"appKey": apiKey}, &app, "application") bson.M{"appKey": apiKey}, &app, "application")
return app, err return app, err
} }
// Update 更新应用 // Update 更新应用
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error { func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
_, err := mongo.Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application") _, err := mongo.DB().Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
return err return err
} }
// Delete 删除应用 // Delete 删除应用
func (d *applicationDao) Delete(ctx context.Context, id string) error { func (d *applicationDao) Delete(ctx context.Context, id string) error {
_, err := mongo.Delete(ctx, bson.M{"_id": id}, "application") _, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "application")
return err return err
} }
@@ -77,13 +71,13 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
} }
var apps []*entity.Application var apps []*entity.Application
total, err := mongo.Count(ctx, d.NoCache, filter, "application") total, err := mongo.DB().Count(ctx, filter, "application")
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
offset := (page - 1) * pageSize offset := (page - 1) * pageSize
err = mongo.Find(ctx, d.NoCache, filter, &apps, "application", err = mongo.DB().Find(ctx, filter, &apps, "application",
options.Find().SetSort(bson.M{"createdAt": -1}). options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)). SetSkip(int64(offset)).
SetLimit(int64(pageSize))) SetLimit(int64(pageSize)))
@@ -97,12 +91,12 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
// GetByName 根据名称获取应用 // GetByName 根据名称获取应用
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) { func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
var app *entity.Application var app *entity.Application
err := mongo.FindOne(ctx, d.NoCache, bson.M{"name": name}, &app, "application") err := mongo.DB().FindOne(ctx, bson.M{"name": name}, &app, "application")
return app, err return app, err
} }
// UpdateFields 更新应用部分字段 // UpdateFields 更新应用部分字段
func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error { func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error {
_, err := mongo.Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application") _, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
return err return err
} }

View File

@@ -4,26 +4,20 @@ import (
"context" "context"
"cid/model/entity" "cid/model/entity"
"gitee.com/red-future---jilin-g/common/mongo" "gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
) )
var CIDRequest = &cidRequestDao{ var CIDRequest = &cidRequestDao{}
NoCache: true,
}
type cidRequestDao struct { type cidRequestDao struct {
NoCache bool
}
func (d *cidRequestDao) SetNoCache() {
CIDRequest.NoCache = true
} }
// Create 创建CID请求记录 // Create 创建CID请求记录
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) { func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) {
ids, err := mongo.Insert(ctx, []interface{}{request}, entity.CidRequestCollection) ids, err := mongo.DB().Insert(ctx, []interface{}{request}, entity.CidRequestCollection)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -38,14 +32,14 @@ func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, siz
filter := bson.M{"userId": userId} filter := bson.M{"userId": userId}
// 获取总数 // 获取总数
total, err = mongo.Count(ctx, d.NoCache, filter, entity.CidRequestCollection) total, err = mongo.DB().Count(ctx, filter, entity.CidRequestCollection)
if err != nil { if err != nil {
return return
} }
// 分页查询 // 分页查询
offset := (page - 1) * size offset := (page - 1) * size
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.CidRequestCollection, err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection,
options.Find().SetSort(bson.M{"createdAt": -1}). options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)). SetSkip(int64(offset)).
SetLimit(int64(size))) SetLimit(int64(size)))
@@ -58,14 +52,14 @@ func (d *cidRequestDao) GetStatistics(ctx context.Context, userId string) (stats
stats = make(map[string]interface{}) stats = make(map[string]interface{})
// 总请求数 // 总请求数
totalRequests, err := mongo.Count(ctx, d.NoCache, bson.M{"userId": userId}, entity.CidRequestCollection) totalRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId}, entity.CidRequestCollection)
if err != nil { if err != nil {
return nil, err return nil, err
} }
stats["total_requests"] = totalRequests stats["total_requests"] = totalRequests
// 成功请求数 // 成功请求数
successfulRequests, err := mongo.Count(ctx, d.NoCache, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection) successfulRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -4,45 +4,39 @@ import (
"context" "context"
"cid/model/entity" "cid/model/entity"
"gitee.com/red-future---jilin-g/common/mongo" "gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson" "go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options" "go.mongodb.org/mongo-driver/v2/mongo/options"
) )
var Strategy = &strategyDao{ var Strategy = &strategyDao{}
NoCache: true,
}
type strategyDao struct { type strategyDao struct {
NoCache bool
}
func (d *strategyDao) SetNoCache() {
Strategy.NoCache = true
} }
// GetByName 根据名称获取策略 // GetByName 根据名称获取策略
func (d *strategyDao) GetByName(ctx context.Context, name string) (strategy *entity.Strategy, err error) { func (d *strategyDao) GetByName(ctx context.Context, name string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, d.NoCache, bson.M{"name": name}, &strategy, "strategies") err = mongo.DB().FindOne(ctx, bson.M{"name": name}, &strategy, "strategies")
return return
} }
// GetByID 根据ID获取策略 // GetByID 根据ID获取策略
func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.Strategy, err error) { func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, d.NoCache, bson.M{"_id": id}, &strategy, "strategies") err = mongo.DB().FindOne(ctx, bson.M{"_id": id}, &strategy, "strategies")
return return
} }
// GetByTenantLevel 根据租户级别获取策略 // GetByTenantLevel 根据租户级别获取策略
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) { func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, d.NoCache, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies", err = mongo.DB().FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1})) options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1}))
return return
} }
// Create 创建策略 // Create 创建策略
func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id string, err error) { func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id string, err error) {
ids, err := mongo.Insert(ctx, []interface{}{strategy}, "strategies") ids, err := mongo.DB().Insert(ctx, []interface{}{strategy}, "strategies")
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -54,7 +48,7 @@ func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id
// Update 更新策略 // Update 更新策略
func (d *strategyDao) Update(ctx context.Context, strategy *entity.Strategy) (affected int64, err error) { func (d *strategyDao) Update(ctx context.Context, strategy *entity.Strategy) (affected int64, err error) {
result, err := mongo.Update(ctx, bson.M{"_id": strategy.Id}, bson.M{"$set": strategy}, "strategies") result, err := mongo.DB().Update(ctx, bson.M{"_id": strategy.Id}, bson.M{"$set": strategy}, "strategies")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -63,7 +57,7 @@ func (d *strategyDao) Update(ctx context.Context, strategy *entity.Strategy) (af
// Delete 删除策略 // Delete 删除策略
func (d *strategyDao) Delete(ctx context.Context, id string) (affected int64, err error) { func (d *strategyDao) Delete(ctx context.Context, id string) (affected int64, err error) {
count, err := mongo.Delete(ctx, bson.M{"_id": id}, "strategies") count, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "strategies")
if err != nil { if err != nil {
return 0, err return 0, err
} }
@@ -83,14 +77,14 @@ func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel,
} }
// 获取总数 // 获取总数
total, err = mongo.Count(ctx, d.NoCache, filter, "strategies") total, err = mongo.DB().Count(ctx, filter, "strategies")
if err != nil { if err != nil {
return return
} }
// 分页查询 // 分页查询
offset := (page - 1) * size offset := (page - 1) * size
err = mongo.Find(ctx, d.NoCache, filter, &list, "strategies", err = mongo.DB().Find(ctx, filter, &list, "strategies",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}). options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}).
SetSkip(int64(offset)). SetSkip(int64(offset)).
SetLimit(int64(size))) SetLimit(int64(size)))

6
go.mod
View File

@@ -3,7 +3,7 @@ module cid
go 1.25.3 go 1.25.3
require ( require (
gitee.com/red-future---jilin-g/common v0.2.6 gitee.com/red-future---jilin-g/common v0.2.9
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.5 github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.5
github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.5 github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.5
github.com/gogf/gf/v2 v2.9.5 github.com/gogf/gf/v2 v2.9.5
@@ -11,7 +11,7 @@ require (
golang.org/x/net v0.47.0 golang.org/x/net v0.47.0
) )
//replace gitee.com/red-future---jilin-g/common v0.2.6 => ../common //replace gitee.com/red-future---jilin-g/common v0.2.9 => ../common
require ( require (
github.com/BurntSushi/toml v1.5.0 // indirect github.com/BurntSushi/toml v1.5.0 // indirect
@@ -89,5 +89,3 @@ require (
google.golang.org/protobuf v1.36.8 // indirect google.golang.org/protobuf v1.36.8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )
//replace gitee.com/red-future---jilin-g/common v0.1.9 => ../common

6
go.sum
View File

@@ -1,6 +1,8 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
gitee.com/red-future---jilin-g/common v0.2.1/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8= gitee.com/red-future---jilin-g/common v0.2.8 h1:mqe/qb6MejwV3VmnYxrO5MAHsFhvMejyfBA1993ejBg=
gitee.com/red-future---jilin-g/common v0.2.6/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8= gitee.com/red-future---jilin-g/common v0.2.8/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8=
gitee.com/red-future---jilin-g/common v0.2.9 h1:lqYUnqTQLEcvxfFBqRMpetMudQze5cQlM4fKDouu2CU=
gitee.com/red-future---jilin-g/common v0.2.9/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=

1
update.sql Normal file
View File

@@ -0,0 +1 @@
-----------2025-06-16 15:00:00--------------