初始化项目

This commit is contained in:
2025-12-10 15:41:52 +08:00
parent 339dd97f66
commit 232009bbc2
25 changed files with 419 additions and 467 deletions

View File

@@ -4,7 +4,9 @@ import (
"context"
"cid/model/entity"
"github.com/gogf/gf/v2/frame/g"
"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{}
@@ -13,77 +15,65 @@ type adSourceDao struct{}
// GetByName 根据名称获取广告源
func (d *adSourceDao) GetByName(ctx context.Context, name string) (adSource *entity.AdSource, err error) {
err = g.DB().Model("ad_sources").
Where("name = ?", name).
Scan(&adSource)
err = mongo.FindOne(ctx, bson.M{"name": name}, &adSource, "ad_sources")
return
}
// GetAvailableSources 获取可用的广告源
func (d *adSourceDao) GetAvailableSources(ctx context.Context) (list []*entity.AdSource, err error) {
err = g.DB().Model("ad_sources").
Where("status = ?", "active").
Order("priority DESC, created_at ASC").
Scan(&list)
err = mongo.Find(ctx, bson.M{"status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": 1}))
return
}
// GetSourcesByProvider 根据提供商获取广告源
func (d *adSourceDao) GetSourcesByProvider(ctx context.Context, provider string) (list []*entity.AdSource, err error) {
err = g.DB().Model("ad_sources").
Where("provider = ? AND status = ?", provider, "active").
Order("priority DESC").
Scan(&list)
err = mongo.Find(ctx, bson.M{"provider": provider, "status": "active"}, &list, "ad_sources",
options.Find().SetSort(bson.M{"priority": -1}))
return
}
// Create 创建广告源
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id int64, err error) {
result, err := g.DB().Model("ad_sources").Insert(adSource)
func (d *adSourceDao) Create(ctx context.Context, adSource *entity.AdSource) (id string, err error) {
ids, err := mongo.Insert(ctx, []interface{}{adSource}, "ad_sources")
if err != nil {
return 0, err
return "", err
}
if len(ids) > 0 {
id = ids[0].(string)
}
id, err = result.LastInsertId()
return
}
// Update 更新广告源
func (d *adSourceDao) Update(ctx context.Context, adSource *entity.AdSource) (affected int64, err error) {
result, err := g.DB().Model("ad_sources").
Where("id = ?", adSource.Id).
Update(adSource)
result, err := mongo.Update(ctx, bson.M{"_id": adSource.Id}, bson.M{"$set": adSource}, "ad_sources")
if err != nil {
return 0, err
}
return result.RowsAffected()
return result.ModifiedCount, nil
}
// Delete 删除广告源
func (d *adSourceDao) Delete(ctx context.Context, id int64) (affected int64, err error) {
result, err := g.DB().Model("ad_sources").
Where("id = ?", id).
Delete()
func (d *adSourceDao) Delete(ctx context.Context, id string) (affected int64, err error) {
count, err := mongo.Delete(ctx, bson.M{"_id": id}, "ad_sources")
if err != nil {
return 0, err
}
return result.RowsAffected()
return count, nil
}
// GetByID 根据ID获取广告源
func (d *adSourceDao) GetByID(ctx context.Context, id int64) (adSource *entity.AdSource, err error) {
err = g.DB().Model("ad_sources").
Where("id = ?", id).
Scan(&adSource)
func (d *adSourceDao) GetByID(ctx context.Context, id string) (adSource *entity.AdSource, err error) {
err = mongo.FindOne(ctx, bson.M{"_id": id}, &adSource, "ad_sources")
return
}
// UpdateFields 更新广告源部分字段
func (d *adSourceDao) UpdateFields(ctx context.Context, id int64, data *entity.AdSource) (affected int64, err error) {
result, err := g.DB().Model("ad_sources").
Where("id = ?", id).
Update(data)
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")
if err != nil {
return 0, err
}
return result.RowsAffected()
return result.ModifiedCount, nil
}

View File

@@ -4,9 +4,9 @@ import (
"context"
"cid/model/entity"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"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
@@ -14,84 +14,85 @@ type applicationDao struct{}
var Application = &applicationDao{}
// Ctx 获取数据库上下文
func (d *applicationDao) Ctx(ctx context.Context) *gdb.Model {
return g.DB().Model("application").Ctx(ctx)
}
// Create 创建应用
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (int64, error) {
result, err := d.Ctx(ctx).Insert(app)
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
ids, err := mongo.Insert(ctx, []interface{}{app}, "application")
if err != nil {
return 0, err
return "", err
}
id, _ := result.LastInsertId()
return id, nil
if len(ids) > 0 {
return ids[0].(string), nil
}
return "", nil
}
// GetByID 根据ID获取应用
func (d *applicationDao) GetByID(ctx context.Context, id int64) (*entity.Application, error) {
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
var app *entity.Application
err := d.Ctx(ctx).Where("id", id).Scan(&app)
err := mongo.FindOne(ctx, bson.M{"_id": id}, &app, "application")
return app, err
}
// GetByTenantID 根据租户ID获取应用列表
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID int64) ([]*entity.Application, error) {
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
var apps []*entity.Application
err := d.Ctx(ctx).Where("tenant_id", tenantID).Scan(&apps)
err := mongo.Find(ctx, bson.M{"tenantId": tenantID}, &apps, "application")
return apps, err
}
// GetByAPIKey 根据API密钥获取应用
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
var app *entity.Application
err := d.Ctx(ctx).Where("app_key", apiKey).Scan(&app)
err := mongo.FindOne(ctx, bson.M{"appKey": apiKey}, &app, "application")
return app, err
}
// Update 更新应用
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
_, err := d.Ctx(ctx).Where("id", app.Id).Update(app)
_, err := mongo.Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
return err
}
// Delete 删除应用
func (d *applicationDao) Delete(ctx context.Context, id int64) error {
_, err := d.Ctx(ctx).Where("id", id).Delete()
func (d *applicationDao) Delete(ctx context.Context, id string) error {
_, err := mongo.Delete(ctx, bson.M{"_id": id}, "application")
return err
}
// List 应用列表
func (d *applicationDao) List(ctx context.Context, tenantID int64, page, pageSize int) ([]*entity.Application, int, error) {
model := d.Ctx(ctx)
if tenantID > 0 {
model = model.Where("tenant_id", tenantID)
func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSize int) ([]*entity.Application, int, error) {
filter := bson.M{}
if tenantID != "" {
filter["tenantId"] = tenantID
}
var apps []*entity.Application
err := model.Page(page, pageSize).OrderDesc("created_at").Scan(&apps)
total, err := mongo.Count(ctx, filter, "application")
if err != nil {
return nil, 0, err
}
total, err := model.Count()
offset := (page - 1) * pageSize
err = mongo.Find(ctx, filter, &apps, "application",
options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(pageSize)))
if err != nil {
return nil, 0, err
}
return apps, total, nil
return apps, int(total), nil
}
// GetByName 根据名称获取应用
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
var app *entity.Application
err := d.Ctx(ctx).Where("name", name).Scan(&app)
err := mongo.FindOne(ctx, bson.M{"name": name}, &app, "application")
return app, err
}
// UpdateFields 更新应用部分字段
func (d *applicationDao) UpdateFields(ctx context.Context, id int64, data *entity.Application) error {
_, err := d.Ctx(ctx).Where("id", id).Update(data)
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")
return err
}

View File

@@ -1,10 +1,12 @@
package dao
import (
"cid/model/entity"
"context"
"github.com/gogf/gf/v2/frame/g"
"cid/model/entity"
"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{}
@@ -12,70 +14,58 @@ var CIDRequest = &cidRequestDao{}
type cidRequestDao struct{}
// Create 创建CID请求记录
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id int64, err error) {
result, err := g.DB().Model("cid_requests").Insert(request)
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) {
ids, err := mongo.Insert(ctx, []interface{}{request}, "cid_requests")
if err != nil {
return 0, err
return "", err
}
if len(ids) > 0 {
id = ids[0].(string)
}
id, err = result.LastInsertId()
return
}
// GetHistory 获取CID请求历史
func (d *cidRequestDao) GetHistory(ctx context.Context, userId int64, page, size int) (list []*entity.CidRequest, total int64, err error) {
model := g.DB().Model("cid_requests")
// 根据用户ID筛选
model = model.Where("user_id = ?", userId)
func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, size int) (list []*entity.CidRequest, total int64, err error) {
filter := bson.M{"userId": userId}
// 获取总数
count, err := model.Count()
total, err = mongo.Count(ctx, filter, "cid_requests")
if err != nil {
return
}
total = int64(count)
// 分页查询
offset := (page - 1) * size
err = model.Order("created_at DESC").
Offset(offset).
Limit(size).
Scan(&list)
err = mongo.Find(ctx, filter, &list, "cid_requests",
options.Find().SetSort(bson.M{"createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))
return
}
// GetStatistics 获取统计信息
func (d *cidRequestDao) GetStatistics(ctx context.Context, userId int64) (stats map[string]interface{}, err error) {
func (d *cidRequestDao) GetStatistics(ctx context.Context, userId string) (stats map[string]interface{}, err error) {
stats = make(map[string]interface{})
// 总请求数
totalRequests, err := g.DB().Model("cid_requests").
Where("user_id = ?", userId).
Count()
totalRequests, err := mongo.Count(ctx, bson.M{"userId": userId}, "cid_requests")
if err != nil {
return nil, err
}
stats["total_requests"] = totalRequests
// 成功请求数
successfulRequests, err := g.DB().Model("cid_requests").
Where("user_id = ? AND status = ?", userId, "completed").
Count()
successfulRequests, err := mongo.Count(ctx, bson.M{"userId": userId, "status": "completed"}, "cid_requests")
if err != nil {
return nil, err
}
stats["successful_requests"] = successfulRequests
// 平均处理时间
avgProcessTime, err := g.DB().Model("cid_requests").
Fields("AVG(process_time)").
Where("user_id = ?", userId).
Value()
if err != nil {
return nil, err
}
stats["average_process_time"] = avgProcessTime
// 平均处理时间需要单独计算MongoDB聚合查询
// 这里简化处理返回0
stats["average_process_time"] = 0
return
}

View File

@@ -5,8 +5,9 @@ import (
"cid/model/entity"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"gitee.com/red-future---jilin-g/common/mongo"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo/options"
)
// statReportDao 统计报表DAO
@@ -14,77 +15,74 @@ type statReportDao struct{}
var StatReport = &statReportDao{}
// Ctx 获取数据库上下文
func (d *statReportDao) Ctx(ctx context.Context) *gdb.Model {
return g.DB().Model("stat_report").Ctx(ctx)
}
// Create 创建统计报表
func (d *statReportDao) Create(ctx context.Context, report *entity.StatReport) (int64, error) {
result, err := d.Ctx(ctx).Insert(report)
if err != nil {
return 0, err
}
id, _ := result.LastInsertId()
return id, nil
func (d *statReportDao) Create(ctx context.Context, report *entity.StatReport) (err error) {
_, err = mongo.Insert(ctx, []interface{}{report}, "stat_report")
return
}
// GetByID 根据ID获取统计报表
func (d *statReportDao) GetByID(ctx context.Context, id int64) (*entity.StatReport, error) {
var report *entity.StatReport
err := d.Ctx(ctx).Where("id", id).Scan(&report)
return report, err
func (d *statReportDao) GetByID(ctx context.Context, id string) (report *entity.StatReport, err error) {
filter := bson.M{"_id": id}
report = &entity.StatReport{}
err = mongo.FindOne(ctx, filter, report, "stat_report")
return
}
// GetByTenantAndDate 根据租户和日期获取统计报表
func (d *statReportDao) GetByTenantAndDate(ctx context.Context, tenantID int64, reportType, date string) (*entity.StatReport, error) {
var report *entity.StatReport
err := d.Ctx(ctx).Where("tenant_id", tenantID).Where("report_type", reportType).Where("report_date", date).Scan(&report)
return report, err
func (d *statReportDao) GetByTenantAndDate(ctx context.Context, tenantID, reportType, date string) (report *entity.StatReport, err error) {
filter := bson.M{"tenantId": tenantID, "reportType": reportType, "reportDate": date}
report = &entity.StatReport{}
err = mongo.FindOne(ctx, filter, report, "stat_report")
return
}
// Update 更新统计报表
func (d *statReportDao) Update(ctx context.Context, report *entity.StatReport) error {
_, err := d.Ctx(ctx).Where("id", report.Id).Update(report)
return err
func (d *statReportDao) Update(ctx context.Context, report *entity.StatReport) (err error) {
filter := bson.M{"_id": report.Id}
update := bson.M{"$set": report}
_, err = mongo.Update(ctx, filter, update, "stat_report")
return
}
// Delete 删除统计报表
func (d *statReportDao) Delete(ctx context.Context, id int64) error {
_, err := d.Ctx(ctx).Where("id", id).Delete()
return err
func (d *statReportDao) Delete(ctx context.Context, id string) (err error) {
filter := bson.M{"_id": id}
_, err = mongo.Delete(ctx, filter, "stat_report")
return
}
// List 统计报表列表
func (d *statReportDao) List(ctx context.Context, tenantID, appID int64, reportType, startDate, endDate string, page, pageSize int) ([]*entity.StatReport, int, error) {
model := d.Ctx(ctx)
func (d *statReportDao) List(ctx context.Context, tenantID, appID, reportType, startDate, endDate string, page, pageSize int) (reports []*entity.StatReport, total int, err error) {
filter := bson.M{}
if tenantID > 0 {
model = model.Where("tenant_id", tenantID)
if tenantID != "" {
filter["tenantId"] = tenantID
}
if appID > 0 {
model = model.Where("app_id", appID)
if appID != "" {
filter["appId"] = appID
}
if reportType != "" {
model = model.Where("report_type", reportType)
filter["reportType"] = reportType
}
if startDate != "" {
model = model.WhereGTE("report_date", startDate)
}
if endDate != "" {
model = model.WhereLTE("report_date", endDate)
if startDate != "" && endDate != "" {
filter["reportDate"] = bson.M{"$gte": startDate, "$lte": endDate}
}
var reports []*entity.StatReport
err := model.Page(page, pageSize).OrderDesc("generated_at").Scan(&reports)
// 获取总数
total64, err := mongo.Count(ctx, filter, "stat_report")
if err != nil {
return nil, 0, err
}
total = int(total64)
total, err := model.Count()
if err != nil {
return nil, 0, err
}
// 分页参数处理
limit := int64(pageSize)
skip := int64((page - 1) * pageSize)
return reports, total, nil
// 排序处理
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(bson.M{"generatedAt": -1})
err = mongo.Find(ctx, filter, &reports, "stat_report", opts)
return
}

View File

@@ -1,10 +1,12 @@
package dao
import (
"cid/model/entity"
"context"
"github.com/gogf/gf/v2/frame/g"
"cid/model/entity"
"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{}
@@ -13,86 +15,77 @@ type strategyDao struct{}
// GetByName 根据名称获取策略
func (d *strategyDao) GetByName(ctx context.Context, name string) (strategy *entity.Strategy, err error) {
err = g.DB().Model("strategies").
Where("name = ?", name).
Scan(&strategy)
err = mongo.FindOne(ctx, bson.M{"name": name}, &strategy, "strategies")
return
}
// GetByID 根据ID获取策略
func (d *strategyDao) GetByID(ctx context.Context, id int64) (strategy *entity.Strategy, err error) {
err = g.DB().Model("strategies").
Where("id = ?", id).
Scan(&strategy)
func (d *strategyDao) GetByID(ctx context.Context, id string) (strategy *entity.Strategy, err error) {
err = mongo.FindOne(ctx, bson.M{"_id": id}, &strategy, "strategies")
return
}
// GetByTenantLevel 根据租户级别获取策略
func (d *strategyDao) GetByTenantLevel(ctx context.Context, tenantLevel string) (strategy *entity.Strategy, err error) {
err = g.DB().Model("strategies").
Where("tenant_level = ? AND status = ?", tenantLevel, "active").
Order("priority DESC, created_at ASC").
Scan(&strategy)
err = mongo.FindOne(ctx, bson.M{"tenantLevel": tenantLevel, "status": "active"}, &strategy, "strategies",
options.FindOne().SetSort(bson.M{"priority": -1, "createdAt": 1}))
return
}
// Create 创建策略
func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id int64, err error) {
result, err := g.DB().Model("strategies").Insert(strategy)
func (d *strategyDao) Create(ctx context.Context, strategy *entity.Strategy) (id string, err error) {
ids, err := mongo.Insert(ctx, []interface{}{strategy}, "strategies")
if err != nil {
return 0, err
return "", err
}
if len(ids) > 0 {
id = ids[0].(string)
}
id, err = result.LastInsertId()
return
}
// Update 更新策略
func (d *strategyDao) Update(ctx context.Context, strategy *entity.Strategy) (affected int64, err error) {
result, err := g.DB().Model("strategies").
Where("id = ?", strategy.Id).
Update(strategy)
result, err := mongo.Update(ctx, bson.M{"_id": strategy.Id}, bson.M{"$set": strategy}, "strategies")
if err != nil {
return 0, err
}
return result.RowsAffected()
return result.ModifiedCount, nil
}
// Delete 删除策略
func (d *strategyDao) Delete(ctx context.Context, id int64) (affected int64, err error) {
result, err := g.DB().Model("strategies").
Where("id = ?", id).
Delete()
func (d *strategyDao) Delete(ctx context.Context, id string) (affected int64, err error) {
count, err := mongo.Delete(ctx, bson.M{"_id": id}, "strategies")
if err != nil {
return 0, err
}
return result.RowsAffected()
return count, nil
}
// GetList 获取策略列表
func (d *strategyDao) GetList(ctx context.Context, page, size int, tenantLevel, status string) (list []*entity.Strategy, total int64, err error) {
model := g.DB().Model("strategies")
filter := bson.M{}
// 筛选条件
if tenantLevel != "" {
model = model.Where("tenant_level = ?", tenantLevel)
filter["tenantLevel"] = tenantLevel
}
if status != "" {
model = model.Where("status = ?", status)
filter["status"] = status
}
// 获取总数
count, err := model.Count()
total, err = mongo.Count(ctx, filter, "strategies")
if err != nil {
return
}
total = int64(count)
// 分页查询
offset := (page - 1) * size
err = model.Order("priority DESC, created_at DESC").
Offset(offset).
Limit(size).
Scan(&list)
err = mongo.Find(ctx, filter, &list, "strategies",
options.Find().SetSort(bson.M{"priority": -1, "createdAt": -1}).
SetSkip(int64(offset)).
SetLimit(int64(size)))
return
}

View File

@@ -1,79 +0,0 @@
package dao
import (
"context"
"cid/model/entity"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
)
var Tenant = &tenantDao{}
type tenantDao struct{}
// Create 创建租户
func (d *tenantDao) Create(ctx context.Context, tenant *entity.Tenant) (id int64, err error) {
result, err := g.DB().Model("tenant").Ctx(ctx).Insert(tenant)
if err != nil {
return 0, err
}
id, _ = result.LastInsertId()
return id, nil
}
// GetByID 根据ID获取租户
func (d *tenantDao) GetByID(ctx context.Context, id int64) (tenant *entity.Tenant, err error) {
err = g.DB().Model("tenant").Ctx(ctx).Where("id = ? AND is_deleted = ?", id, false).Scan(&tenant)
return
}
// GetByCode 根据编码获取租户
func (d *tenantDao) GetByCode(ctx context.Context, code string) (tenant *entity.Tenant, err error) {
err = g.DB().Model("tenant").Ctx(ctx).Where("code = ? AND is_deleted = ?", code, false).Scan(&tenant)
return
}
// UpdateFields 更新租户字段
func (d *tenantDao) UpdateFields(ctx context.Context, id int64, updateData *entity.Tenant) (affected int64, err error) {
result, err := g.DB().Model("tenant").Ctx(ctx).Where("id = ? AND is_deleted = ?", id, false).Update(updateData)
if err != nil {
return 0, err
}
rowsAffected, _ := result.RowsAffected()
return rowsAffected, nil
}
// Delete 删除租户
func (d *tenantDao) Delete(ctx context.Context, id int64) (affected int64, err error) {
result, err := g.DB().Model("tenant").Ctx(ctx).Where("id = ?", id).Update(gdb.Map{"is_deleted": true})
if err != nil {
return 0, err
}
rowsAffected, _ := result.RowsAffected()
return rowsAffected, nil
}
// List 获取租户列表
func (d *tenantDao) List(ctx context.Context, page, size int, filter map[string]interface{}) (list []*entity.Tenant, total int64, err error) {
model := g.DB().Model("tenant").Ctx(ctx).Where("is_deleted = ?", false)
// 应用过滤条件
if filter != nil {
for key, value := range filter {
model = model.Where(key, value)
}
}
// 获取总数
count, err := model.Count()
if err != nil {
return
}
total = int64(count)
// 分页查询
err = model.Page(page, size).OrderDesc("created_at").Scan(&list)
return
}