gomod引用
This commit is contained in:
@@ -1,193 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cid/model/dto"
|
||||
"cid/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
)
|
||||
|
||||
// AdStatistics DAO 单例
|
||||
var AdStatistics = &adStatistics{}
|
||||
|
||||
type adStatistics struct{}
|
||||
|
||||
// Insert 插入统计数据
|
||||
func (d *adStatistics) Insert(ctx context.Context, data *entity.AdStatistics) (err error) {
|
||||
// 如果 ID 为空,生成一个新的 ObjectID
|
||||
if data.Id.IsZero() {
|
||||
data.Id = bson.NewObjectID()
|
||||
}
|
||||
|
||||
// 使用 common/mongo.Insert,自动添加 tenantId、creator、updater 等字段
|
||||
// 确保查询时能通过 tenantId 正确过滤数据
|
||||
_, err = mongo.Insert(ctx, []interface{}{data}, entity.AdStatisticsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// BatchInsert 批量插入统计数据
|
||||
func (d *adStatistics) BatchInsert(ctx context.Context, dataList []*entity.AdStatistics) (err error) {
|
||||
if len(dataList) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var list []interface{}
|
||||
for _, data := range dataList {
|
||||
// 如果 ID 为空,生成一个新的 ObjectID
|
||||
if data.Id.IsZero() {
|
||||
data.Id = bson.NewObjectID()
|
||||
}
|
||||
list = append(list, data)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, list, entity.AdStatisticsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Upsert 插入或更新统计数据
|
||||
func (d *adStatistics) Upsert(ctx context.Context, data *entity.AdStatistics) (err error) {
|
||||
filter := bson.M{
|
||||
"statType": data.StatType,
|
||||
"statDimension": data.StatDimension,
|
||||
"targetId": data.TargetId,
|
||||
"statDate": data.StatDate,
|
||||
}
|
||||
|
||||
update := bson.M{"$set": data}
|
||||
|
||||
// 这里使用简单的Update方法,如果需要Upsert功能,可以扩展
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdStatisticsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *adStatistics) buildListFilter(req *dto.GetAdStatisticsReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.StatType) {
|
||||
filter["statType"] = req.StatType
|
||||
}
|
||||
if !g.IsEmpty(req.StatDimension) {
|
||||
filter["statDimension"] = req.StatDimension
|
||||
}
|
||||
if !g.IsEmpty(req.TargetId) {
|
||||
filter["targetId"] = req.TargetId
|
||||
}
|
||||
|
||||
// 时间范围
|
||||
filter["statDate"] = bson.M{
|
||||
"$gte": req.StartDate,
|
||||
"$lte": req.EndDate,
|
||||
}
|
||||
|
||||
// 筛选条件
|
||||
if !g.IsEmpty(req.DeviceType) {
|
||||
filter["deviceType"] = req.DeviceType
|
||||
}
|
||||
if !g.IsEmpty(req.Platform) {
|
||||
filter["platform"] = req.Platform
|
||||
}
|
||||
if !g.IsEmpty(req.Region) {
|
||||
filter["region"] = req.Region
|
||||
}
|
||||
if !g.IsEmpty(req.Gender) {
|
||||
filter["gender"] = req.Gender
|
||||
}
|
||||
if !g.IsEmpty(req.AgeGroup) {
|
||||
filter["ageGroup"] = req.AgeGroup
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *adStatistics) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, filter, entity.AdStatisticsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取统计数据列表
|
||||
func (d *adStatistics) List(ctx context.Context, req *dto.GetAdStatisticsReq) (list []*entity.AdStatistics, 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{"statDate": -1}
|
||||
if !g.IsEmpty(req.SortBy) {
|
||||
sortDirection := 1
|
||||
if req.SortDirection == "desc" {
|
||||
sortDirection = -1
|
||||
}
|
||||
sort = bson.M{req.SortBy: sortDirection}
|
||||
}
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdStatisticsCollection, opts)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个统计记录
|
||||
func (d *adStatistics) GetOne(ctx context.Context, id string) (data *entity.AdStatistics, err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
data = &entity.AdStatistics{}
|
||||
err = mongo.FindOne(ctx, filter, data, entity.AdStatisticsCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetStatistics 获取统计数据
|
||||
func (d *adStatistics) GetStatistics(ctx context.Context, req *dto.GetAdStatisticsReq) (list []*entity.AdStatistics, total int64, err error) {
|
||||
// 构建查询过滤条件
|
||||
filter := d.buildListFilter(req)
|
||||
|
||||
// 检查总数
|
||||
total, err = d.checkTotalCount(ctx, filter)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 排序处理
|
||||
sort := bson.M{"statDate": -1}
|
||||
if !g.IsEmpty(req.SortBy) {
|
||||
sortDirection := 1
|
||||
if req.SortDirection == "desc" {
|
||||
sortDirection = -1
|
||||
}
|
||||
sort = bson.M{req.SortBy: sortDirection}
|
||||
}
|
||||
|
||||
opts := options.Find().SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdStatisticsCollection, opts)
|
||||
return
|
||||
}
|
||||
@@ -15,7 +15,7 @@ type cidRequestDao struct{}
|
||||
|
||||
// Create 创建CID请求记录
|
||||
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) {
|
||||
ids, err := mongo.Insert(ctx, []interface{}{request}, "cid_requests")
|
||||
ids, err := mongo.Insert(ctx, []interface{}{request}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -30,14 +30,14 @@ func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, siz
|
||||
filter := bson.M{"userId": userId}
|
||||
|
||||
// 获取总数
|
||||
total, err = mongo.Count(ctx, filter, "cid_requests")
|
||||
total, err = mongo.Count(ctx, filter, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (page - 1) * size
|
||||
err = mongo.Find(ctx, filter, &list, "cid_requests",
|
||||
err = mongo.Find(ctx, filter, &list, entity.CidRequestCollection,
|
||||
options.Find().SetSort(bson.M{"createdAt": -1}).
|
||||
SetSkip(int64(offset)).
|
||||
SetLimit(int64(size)))
|
||||
@@ -50,14 +50,14 @@ func (d *cidRequestDao) GetStatistics(ctx context.Context, userId string) (stats
|
||||
stats = make(map[string]interface{})
|
||||
|
||||
// 总请求数
|
||||
totalRequests, err := mongo.Count(ctx, bson.M{"userId": userId}, "cid_requests")
|
||||
totalRequests, err := mongo.Count(ctx, bson.M{"userId": userId}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stats["total_requests"] = totalRequests
|
||||
|
||||
// 成功请求数
|
||||
successfulRequests, err := mongo.Count(ctx, bson.M{"userId": userId, "status": "completed"}, "cid_requests")
|
||||
successfulRequests, err := mongo.Count(ctx, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -1,88 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// statReportDao 统计报表DAO
|
||||
type statReportDao struct{}
|
||||
|
||||
var StatReport = &statReportDao{}
|
||||
|
||||
// Create 创建统计报表
|
||||
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 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, 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) (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 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, reportType, startDate, endDate string, page, pageSize int) (reports []*entity.StatReport, total int, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
if tenantID != "" {
|
||||
filter["tenantId"] = tenantID
|
||||
}
|
||||
if appID != "" {
|
||||
filter["appId"] = appID
|
||||
}
|
||||
if reportType != "" {
|
||||
filter["reportType"] = reportType
|
||||
}
|
||||
if startDate != "" && endDate != "" {
|
||||
filter["reportDate"] = bson.M{"$gte": startDate, "$lte": endDate}
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
total64, err := mongo.Count(ctx, filter, "stat_report")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total = int(total64)
|
||||
|
||||
// 分页参数处理
|
||||
limit := int64(pageSize)
|
||||
skip := int64((page - 1) * pageSize)
|
||||
|
||||
// 排序处理
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(bson.M{"generatedAt": -1})
|
||||
|
||||
err = mongo.Find(ctx, filter, &reports, "stat_report", opts)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user