194 lines
4.7 KiB
Go
194 lines
4.7 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
|
||
"cidservice/model/dto"
|
||
"cidservice/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
|
||
}
|