初始化项目
This commit is contained in:
257
dao/ad_position_dao.go
Normal file
257
dao/ad_position_dao.go
Normal file
@@ -0,0 +1,257 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cidService/model/dto"
|
||||
"cidService/model/entity"
|
||||
"context"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var AdPosition = &adPosition{}
|
||||
|
||||
type adPosition struct{}
|
||||
|
||||
// Insert 插入广告位
|
||||
func (d *adPosition) Insert(ctx context.Context, adPosition *entity.AdPosition) (err error) {
|
||||
// 获取stream消息
|
||||
redis := g.Redis()
|
||||
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "ad_position_stream", "$")
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
|
||||
} else {
|
||||
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{adPosition}, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告位
|
||||
func (d *adPosition) Update(ctx context.Context, req *dto.UpdateAdPositionReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
|
||||
// 基本信息
|
||||
if !g.IsEmpty(req.Name) {
|
||||
updateFields["name"] = req.Name
|
||||
}
|
||||
if !g.IsEmpty(req.Description) {
|
||||
updateFields["description"] = req.Description
|
||||
}
|
||||
if !g.IsEmpty(req.PositionCode) {
|
||||
updateFields["positionCode"] = req.PositionCode
|
||||
}
|
||||
if !g.IsEmpty(req.AdFormat) {
|
||||
updateFields["adFormat"] = req.AdFormat
|
||||
}
|
||||
|
||||
// 尺寸信息
|
||||
if req.Width != nil {
|
||||
updateFields["width"] = *req.Width
|
||||
}
|
||||
if req.Height != nil {
|
||||
updateFields["height"] = *req.Height
|
||||
}
|
||||
|
||||
// 位置信息
|
||||
if !g.IsEmpty(req.Page) {
|
||||
updateFields["page"] = req.Page
|
||||
}
|
||||
if !g.IsEmpty(req.Section) {
|
||||
updateFields["section"] = req.Section
|
||||
}
|
||||
if !g.IsEmpty(req.Location) {
|
||||
updateFields["location"] = req.Location
|
||||
}
|
||||
|
||||
// 展示设置
|
||||
if req.MaxAds != nil {
|
||||
updateFields["maxAds"] = *req.MaxAds
|
||||
}
|
||||
if req.RefreshInterval != nil {
|
||||
updateFields["refreshInterval"] = *req.RefreshInterval
|
||||
}
|
||||
if req.IsLazyLoad != nil {
|
||||
updateFields["isLazyLoad"] = *req.IsLazyLoad
|
||||
}
|
||||
|
||||
// 定价设置
|
||||
if !g.IsEmpty(req.PricingModel) {
|
||||
updateFields["pricingModel"] = req.PricingModel
|
||||
}
|
||||
if req.BasePrice != nil {
|
||||
updateFields["basePrice"] = *req.BasePrice
|
||||
}
|
||||
if req.FloorPrice != nil {
|
||||
updateFields["floorPrice"] = *req.FloorPrice
|
||||
}
|
||||
if !g.IsEmpty(req.PriceUnit) {
|
||||
updateFields["priceUnit"] = req.PriceUnit
|
||||
}
|
||||
|
||||
// 展示规则
|
||||
if req.DisplayRules != nil {
|
||||
updateFields["displayRules"] = req.DisplayRules
|
||||
}
|
||||
|
||||
// 状态信息
|
||||
if req.Status != nil {
|
||||
updateFields["status"] = *req.Status
|
||||
}
|
||||
if req.IsExclusive != nil {
|
||||
updateFields["isExclusive"] = *req.IsExclusive
|
||||
}
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
update := bson.M{"$set": updateFields}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdPositionCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新广告位状态
|
||||
func (d *adPosition) UpdateStatus(ctx context.Context, id, status string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": bson.M{"status": status}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatistics 更新广告位统计数据
|
||||
func (d *adPosition) UpdateStatistics(ctx context.Context, id string, stats map[string]interface{}) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": stats}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个广告位
|
||||
func (d *adPosition) GetOne(ctx context.Context, id string) (adPosition *entity.AdPosition, err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
adPosition = &entity.AdPosition{}
|
||||
err = mongo.FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByCode 根据编码获取广告位
|
||||
func (d *adPosition) GetByCode(ctx context.Context, code string) (adPosition *entity.AdPosition, err error) {
|
||||
filter := bson.M{"positionCode": code}
|
||||
|
||||
adPosition = &entity.AdPosition{}
|
||||
err = mongo.FindOne(ctx, filter, adPosition, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *adPosition) buildListFilter(req *dto.ListAdPositionReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.Name) {
|
||||
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.PositionCode) {
|
||||
filter["positionCode"] = req.PositionCode
|
||||
}
|
||||
if !g.IsEmpty(req.PageName) {
|
||||
filter["page"] = req.PageName
|
||||
}
|
||||
if !g.IsEmpty(req.Section) {
|
||||
filter["section"] = req.Section
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.AdFormat) {
|
||||
filter["adFormat"] = req.AdFormat
|
||||
}
|
||||
|
||||
// 处理日期范围
|
||||
if len(req.DateRange) == 2 {
|
||||
startTime := gconv.Int64(req.DateRange[0])
|
||||
endTime := gconv.Int64(req.DateRange[1])
|
||||
filter["createdAt"] = bson.M{
|
||||
"$gte": startTime,
|
||||
"$lte": endTime,
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *adPosition) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, filter, entity.AdPositionCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取广告位列表
|
||||
func (d *adPosition) List(ctx context.Context, req *dto.ListAdPositionReq) (list []*entity.AdPosition, 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{"createdAt": -1}
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdPositionCollection, opts)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAvailableAdPositions 获取可用的广告位列表
|
||||
func (d *adPosition) GetAvailableAdPositions(ctx context.Context) (list []*entity.AdPosition, err error) {
|
||||
filter := bson.M{
|
||||
"status": "启用", // 只返回启用的广告位
|
||||
}
|
||||
|
||||
opts := options.Find().SetSort(bson.M{"createdAt": -1})
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdPositionCollection, opts)
|
||||
return
|
||||
}
|
||||
193
dao/ad_statistics_dao.go
Normal file
193
dao/ad_statistics_dao.go
Normal file
@@ -0,0 +1,193 @@
|
||||
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
|
||||
}
|
||||
256
dao/advertisement_dao.go
Normal file
256
dao/advertisement_dao.go
Normal file
@@ -0,0 +1,256 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cidService/model/dto"
|
||||
"cidService/model/entity"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var Advertisement = &advertisement{}
|
||||
|
||||
type advertisement struct{}
|
||||
|
||||
// Insert 插入广告
|
||||
func (d *advertisement) Insert(ctx context.Context, advertisement *entity.Advertisement) (err error) {
|
||||
// 获取stream消息
|
||||
redis := g.Redis()
|
||||
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "advertisement_stream", "$")
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
|
||||
} else {
|
||||
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{advertisement}, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告
|
||||
func (d *advertisement) Update(ctx context.Context, req *dto.UpdateAdvertisementReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
|
||||
// 广告基本信息
|
||||
if !g.IsEmpty(req.Title) {
|
||||
updateFields["title"] = req.Title
|
||||
}
|
||||
if !g.IsEmpty(req.Description) {
|
||||
updateFields["description"] = req.Description
|
||||
}
|
||||
if !g.IsEmpty(req.AdvertiserId) {
|
||||
updateFields["advertiserId"] = req.AdvertiserId
|
||||
}
|
||||
if !g.IsEmpty(req.AdPositionId) {
|
||||
updateFields["adPositionId"] = req.AdPositionId
|
||||
}
|
||||
if !g.IsEmpty(req.AdType) {
|
||||
updateFields["adType"] = req.AdType
|
||||
}
|
||||
if !g.IsEmpty(req.AdFormat) {
|
||||
updateFields["adFormat"] = req.AdFormat
|
||||
}
|
||||
if !g.IsEmpty(req.MaterialUrl) {
|
||||
updateFields["materialUrl"] = req.MaterialUrl
|
||||
}
|
||||
if !g.IsEmpty(req.LinkUrl) {
|
||||
updateFields["linkUrl"] = req.LinkUrl
|
||||
}
|
||||
if !g.IsEmpty(req.LandingPageUrl) {
|
||||
updateFields["landingPageUrl"] = req.LandingPageUrl
|
||||
}
|
||||
|
||||
// 投放设置
|
||||
if req.StartDate != nil {
|
||||
updateFields["startDate"] = *req.StartDate
|
||||
}
|
||||
if req.EndDate != nil {
|
||||
updateFields["endDate"] = *req.EndDate
|
||||
}
|
||||
if req.Budget != nil {
|
||||
updateFields["budget"] = *req.Budget
|
||||
}
|
||||
if req.DailyBudget != nil {
|
||||
updateFields["dailyBudget"] = *req.DailyBudget
|
||||
}
|
||||
if req.BidAmount != nil {
|
||||
updateFields["bidAmount"] = *req.BidAmount
|
||||
}
|
||||
if !g.IsEmpty(req.BillingType) {
|
||||
updateFields["billingType"] = req.BillingType
|
||||
}
|
||||
|
||||
// 投放条件
|
||||
if req.Targeting != nil {
|
||||
updateFields["targeting"] = req.Targeting
|
||||
}
|
||||
|
||||
// 状态信息
|
||||
if req.Status != nil {
|
||||
updateFields["status"] = *req.Status
|
||||
}
|
||||
if req.AuditStatus != nil {
|
||||
updateFields["auditStatus"] = *req.AuditStatus
|
||||
}
|
||||
if req.AuditReason != nil {
|
||||
updateFields["auditReason"] = *req.AuditReason
|
||||
}
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
update := bson.M{"$set": updateFields}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新广告状态
|
||||
func (d *advertisement) UpdateStatus(ctx context.Context, id, status string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": bson.M{"status": status}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Audit 审核广告
|
||||
func (d *advertisement) Audit(ctx context.Context, id, auditStatus, auditReason string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 获取当前用户ID(实际项目中应从上下文获取)
|
||||
auditBy := "system"
|
||||
auditTime := time.Now().Unix()
|
||||
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"auditStatus": auditStatus,
|
||||
"auditReason": auditReason,
|
||||
"auditTime": auditTime,
|
||||
"auditBy": auditBy,
|
||||
},
|
||||
}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatistics 更新广告统计数据
|
||||
func (d *advertisement) UpdateStatistics(ctx context.Context, id string, stats map[string]interface{}) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": stats}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个广告
|
||||
func (d *advertisement) GetOne(ctx context.Context, id string) (advertisement *entity.Advertisement, err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
advertisement = &entity.Advertisement{}
|
||||
err = mongo.FindOne(ctx, filter, advertisement, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *advertisement) buildListFilter(req *dto.ListAdvertisementReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.AdvertiserId) {
|
||||
filter["advertiserId"] = req.AdvertiserId
|
||||
}
|
||||
if !g.IsEmpty(req.AdPositionId) {
|
||||
filter["adPositionId"] = req.AdPositionId
|
||||
}
|
||||
if !g.IsEmpty(req.AdType) {
|
||||
filter["adType"] = req.AdType
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.AuditStatus) {
|
||||
filter["auditStatus"] = req.AuditStatus
|
||||
}
|
||||
if !g.IsEmpty(req.Title) {
|
||||
filter["title"] = bson.M{"$regex": req.Title, "$options": "i"}
|
||||
}
|
||||
|
||||
// 处理日期范围
|
||||
if len(req.DateRange) == 2 {
|
||||
startTime := gconv.Int64(req.DateRange[0])
|
||||
endTime := gconv.Int64(req.DateRange[1])
|
||||
filter["createdAt"] = bson.M{
|
||||
"$gte": startTime,
|
||||
"$lte": endTime,
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *advertisement) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, filter, entity.AdvertisementCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取广告列表
|
||||
func (d *advertisement) List(ctx context.Context, req *dto.ListAdvertisementReq) (list []*entity.Advertisement, 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{"createdAt": -1}
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdvertisementCollection, opts)
|
||||
return
|
||||
}
|
||||
298
dao/advertiser_dao.go
Normal file
298
dao/advertiser_dao.go
Normal file
@@ -0,0 +1,298 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cidService/model/dto"
|
||||
"cidService/model/entity"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var Advertiser = &advertiser{}
|
||||
|
||||
type advertiser struct{}
|
||||
|
||||
// Insert 插入广告主
|
||||
func (d *advertiser) Insert(ctx context.Context, advertiser *entity.Advertiser) (err error) {
|
||||
// 获取stream消息
|
||||
redis := g.Redis()
|
||||
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "advertiser_stream", "$")
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
|
||||
} else {
|
||||
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{advertiser}, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新广告主
|
||||
func (d *advertiser) Update(ctx context.Context, req *dto.UpdateAdvertiserReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
|
||||
// 基本信息
|
||||
if !g.IsEmpty(req.Name) {
|
||||
updateFields["name"] = req.Name
|
||||
}
|
||||
if !g.IsEmpty(req.ContactName) {
|
||||
updateFields["contactName"] = req.ContactName
|
||||
}
|
||||
if !g.IsEmpty(req.ContactPhone) {
|
||||
updateFields["contactPhone"] = req.ContactPhone
|
||||
}
|
||||
if !g.IsEmpty(req.ContactEmail) {
|
||||
updateFields["contactEmail"] = req.ContactEmail
|
||||
}
|
||||
if !g.IsEmpty(req.Company) {
|
||||
updateFields["company"] = req.Company
|
||||
}
|
||||
if !g.IsEmpty(req.Industry) {
|
||||
updateFields["industry"] = req.Industry
|
||||
}
|
||||
if !g.IsEmpty(req.Scale) {
|
||||
updateFields["scale"] = req.Scale
|
||||
}
|
||||
|
||||
// 证件信息
|
||||
if !g.IsEmpty(req.BusinessLicenseUrl) {
|
||||
updateFields["businessLicenseUrl"] = req.BusinessLicenseUrl
|
||||
}
|
||||
if !g.IsEmpty(req.ICPLicenseUrl) {
|
||||
updateFields["icpLicenseUrl"] = req.ICPLicenseUrl
|
||||
}
|
||||
if req.OtherLicenseUrls != nil {
|
||||
updateFields["otherLicenseUrls"] = req.OtherLicenseUrls
|
||||
}
|
||||
|
||||
// 财务信息
|
||||
if !g.IsEmpty(req.BankName) {
|
||||
updateFields["bankName"] = req.BankName
|
||||
}
|
||||
if !g.IsEmpty(req.BankAccount) {
|
||||
updateFields["bankAccount"] = req.BankAccount
|
||||
}
|
||||
if !g.IsEmpty(req.AccountName) {
|
||||
updateFields["accountName"] = req.AccountName
|
||||
}
|
||||
|
||||
// 合同信息
|
||||
if !g.IsEmpty(req.ContractId) {
|
||||
updateFields["contractId"] = req.ContractId
|
||||
}
|
||||
if !g.IsEmpty(req.ContractType) {
|
||||
updateFields["contractType"] = req.ContractType
|
||||
}
|
||||
if !g.IsEmpty(req.ContractUrl) {
|
||||
updateFields["contractUrl"] = req.ContractUrl
|
||||
}
|
||||
if req.SignDate != nil {
|
||||
updateFields["signDate"] = *req.SignDate
|
||||
}
|
||||
if req.ExpireDate != nil {
|
||||
updateFields["expireDate"] = *req.ExpireDate
|
||||
}
|
||||
|
||||
// 系统信息
|
||||
if req.AccountBalance != nil {
|
||||
updateFields["accountBalance"] = *req.AccountBalance
|
||||
}
|
||||
if req.CreditLimit != nil {
|
||||
updateFields["creditLimit"] = *req.CreditLimit
|
||||
}
|
||||
if !g.IsEmpty(req.Remark) {
|
||||
updateFields["remark"] = req.Remark
|
||||
}
|
||||
|
||||
// 状态信息
|
||||
if req.Status != nil {
|
||||
updateFields["status"] = *req.Status
|
||||
}
|
||||
if req.AuditStatus != nil {
|
||||
updateFields["auditStatus"] = *req.AuditStatus
|
||||
}
|
||||
if req.AuditReason != nil {
|
||||
updateFields["auditReason"] = *req.AuditReason
|
||||
}
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
update := bson.M{"$set": updateFields}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新广告主状态
|
||||
func (d *advertiser) UpdateStatus(ctx context.Context, id, status string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": bson.M{"status": status}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Audit 审核广告主
|
||||
func (d *advertiser) Audit(ctx context.Context, id, auditStatus, auditReason string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 获取当前用户ID(实际项目中应从上下文获取)
|
||||
auditBy := "system"
|
||||
auditTime := time.Now().Unix()
|
||||
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"auditStatus": auditStatus,
|
||||
"auditReason": auditReason,
|
||||
"auditTime": auditTime,
|
||||
"auditBy": auditBy,
|
||||
},
|
||||
}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Recharge 充值
|
||||
func (d *advertiser) Recharge(ctx context.Context, id string, amount int64, remark string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 先获取当前余额
|
||||
advertiser := &entity.Advertiser{}
|
||||
err = mongo.FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 更新余额
|
||||
newBalance := advertiser.AccountBalance + amount
|
||||
update := bson.M{"$set": bson.M{"accountBalance": newBalance}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateCreditLimit 更新授信额度
|
||||
func (d *advertiser) UpdateCreditLimit(ctx context.Context, id string, creditLimit int64) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
update := bson.M{"$set": bson.M{"creditLimit": creditLimit}}
|
||||
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个广告主
|
||||
func (d *advertiser) GetOne(ctx context.Context, id string) (advertiser *entity.Advertiser, err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
advertiser = &entity.Advertiser{}
|
||||
err = mongo.FindOne(ctx, filter, advertiser, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *advertiser) buildListFilter(req *dto.ListAdvertiserReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.Name) {
|
||||
filter["name"] = bson.M{"$regex": req.Name, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.ContactName) {
|
||||
filter["contactName"] = bson.M{"$regex": req.ContactName, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.Company) {
|
||||
filter["company"] = bson.M{"$regex": req.Company, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.Industry) {
|
||||
filter["industry"] = req.Industry
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.AuditStatus) {
|
||||
filter["auditStatus"] = req.AuditStatus
|
||||
}
|
||||
|
||||
// 处理日期范围
|
||||
if len(req.DateRange) == 2 {
|
||||
startTime := gconv.Int64(req.DateRange[0])
|
||||
endTime := gconv.Int64(req.DateRange[1])
|
||||
filter["createdAt"] = bson.M{
|
||||
"$gte": startTime,
|
||||
"$lte": endTime,
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *advertiser) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, filter, entity.AdvertiserCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取广告主列表
|
||||
func (d *advertiser) List(ctx context.Context, req *dto.ListAdvertiserReq) (list []*entity.Advertiser, 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{"createdAt": -1}
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdvertiserCollection, opts)
|
||||
return
|
||||
}
|
||||
121
dao/data_dao.go
121
dao/data_dao.go
@@ -1,121 +0,0 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cidService/model/dto"
|
||||
"cidService/model/entity"
|
||||
"context"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
var Data = &data{}
|
||||
|
||||
type data struct{}
|
||||
|
||||
// Insert 插入数据
|
||||
func (d *data) Insert(ctx context.Context, data *entity.Data) (err error) {
|
||||
// 获取stream消息
|
||||
redis := g.Redis()
|
||||
streamMsg, err := redis.Do(ctx, "XREAD", "STREAMS", "data_stream", "$")
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "获取stream消息失败: %v", err)
|
||||
} else {
|
||||
g.Log().Infof(ctx, "获取到stream消息: %v", streamMsg)
|
||||
}
|
||||
|
||||
_, err = mongo.Insert(ctx, []interface{}{data}, entity.DataCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新数据
|
||||
func (d *data) Update(ctx context.Context, req *dto.UpdateDataReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
if !g.IsEmpty(req.CustomerId) {
|
||||
updateFields["customerId"] = req.CustomerId
|
||||
}
|
||||
if !g.IsEmpty(req.CustomerServiceId) {
|
||||
updateFields["customerServiceId"] = req.CustomerServiceId
|
||||
}
|
||||
if req.IsInbound != nil {
|
||||
updateFields["isInbound"] = *req.IsInbound
|
||||
}
|
||||
if req.IsActive != nil {
|
||||
updateFields["isActive"] = *req.IsActive
|
||||
}
|
||||
if req.IsServed != nil {
|
||||
updateFields["isServed"] = *req.IsServed
|
||||
}
|
||||
if req.HasSentContactCard != nil {
|
||||
updateFields["hasSentContactCard"] = *req.HasSentContactCard
|
||||
}
|
||||
if req.HasSentNameCard != nil {
|
||||
updateFields["hasSentNameCard"] = *req.HasSentNameCard
|
||||
}
|
||||
if req.HasLeftContactInfo != nil {
|
||||
updateFields["hasLeftContactInfo"] = *req.HasLeftContactInfo
|
||||
}
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
update := bson.M{"$set": updateFields}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.DataCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询的过滤条件
|
||||
func (d *data) buildListFilter(req *dto.ListDataReq) bson.M {
|
||||
filter := bson.M{}
|
||||
if !g.IsEmpty(req.CustomerId) {
|
||||
filter["customerId"] = req.CustomerId
|
||||
}
|
||||
if !g.IsEmpty(req.CustomerServiceId) {
|
||||
filter["customerServiceId"] = req.CustomerServiceId
|
||||
}
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkTotalCount 检查总数
|
||||
func (d *data) checkTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, filter, entity.DataCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取数据列表
|
||||
func (d *data) List(ctx context.Context, req *dto.ListDataReq) (list []*entity.Data, 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)
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(bson.M{"sessionStartTime": -1})
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.DataCollection, opts)
|
||||
return
|
||||
}
|
||||
171
dao/report_dao.go
Normal file
171
dao/report_dao.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cidService/model/dto"
|
||||
"cidService/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"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"
|
||||
)
|
||||
|
||||
// Report DAO 单例
|
||||
var Report = &report{}
|
||||
|
||||
type report struct{}
|
||||
|
||||
// Insert 插入报表
|
||||
func (d *report) Insert(ctx context.Context, report *entity.AdReport) (err error) {
|
||||
// 如果 ID 为空,生成一个新的 ObjectID
|
||||
if report.Id.IsZero() {
|
||||
report.Id = bson.NewObjectID()
|
||||
}
|
||||
_, err = mongo.Insert(ctx, []interface{}{report}, entity.AdReportCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// Update 更新报表
|
||||
func (d *report) Update(ctx context.Context, req *dto.UpdateReportReq) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(req.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
// 构建动态更新字段
|
||||
updateFields := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.ReportName) {
|
||||
updateFields["reportName"] = req.ReportName
|
||||
}
|
||||
if !g.IsEmpty(req.ReportType) {
|
||||
updateFields["reportType"] = req.ReportType
|
||||
}
|
||||
if !g.IsEmpty(req.ReportPeriod) {
|
||||
updateFields["reportPeriod"] = req.ReportPeriod
|
||||
}
|
||||
if req.StartDate != nil {
|
||||
updateFields["startDate"] = *req.StartDate
|
||||
}
|
||||
if req.EndDate != nil {
|
||||
updateFields["endDate"] = *req.EndDate
|
||||
}
|
||||
if req.ReportConfig != nil {
|
||||
updateFields["reportConfig"] = req.ReportConfig
|
||||
}
|
||||
if !g.IsEmpty(req.FileFormat) {
|
||||
updateFields["fileFormat"] = req.FileFormat
|
||||
}
|
||||
if req.EmailRecipients != nil {
|
||||
updateFields["emailRecipients"] = req.EmailRecipients
|
||||
}
|
||||
if !g.IsEmpty(req.Schedule) {
|
||||
updateFields["schedule"] = req.Schedule
|
||||
}
|
||||
|
||||
if len(updateFields) > 0 {
|
||||
update := bson.M{"$set": updateFields}
|
||||
_, err = mongo.Update(ctx, filter, update, entity.AdReportCollection)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除报表
|
||||
func (d *report) Delete(ctx context.Context, id string) (err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
_, err = mongo.Delete(ctx, filter, entity.AdReportCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个报表
|
||||
func (d *report) GetOne(ctx context.Context, id string) (result *entity.AdReport, err error) {
|
||||
objectId, err := bson.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
filter := bson.M{"_id": objectId}
|
||||
|
||||
result = &entity.AdReport{}
|
||||
err = mongo.FindOne(ctx, filter, result, entity.AdReportCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// buildReportListFilter 构建报表列表查询的过滤条件
|
||||
func (d *report) buildReportListFilter(req *dto.ListReportReq) bson.M {
|
||||
filter := bson.M{}
|
||||
|
||||
if !g.IsEmpty(req.ReportName) {
|
||||
filter["reportName"] = bson.M{"$regex": req.ReportName, "$options": "i"}
|
||||
}
|
||||
if !g.IsEmpty(req.ReportType) {
|
||||
filter["reportType"] = req.ReportType
|
||||
}
|
||||
if !g.IsEmpty(req.Status) {
|
||||
filter["status"] = req.Status
|
||||
}
|
||||
if !g.IsEmpty(req.Operator) {
|
||||
filter["operator"] = req.Operator
|
||||
}
|
||||
|
||||
// 处理日期范围
|
||||
if len(req.DateRange) == 2 {
|
||||
startTime := gconv.Int64(req.DateRange[0])
|
||||
endTime := gconv.Int64(req.DateRange[1])
|
||||
filter["createdAt"] = bson.M{
|
||||
"$gte": startTime,
|
||||
"$lte": endTime,
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
// checkReportTotalCount 检查报表总数
|
||||
func (d *report) checkReportTotalCount(ctx context.Context, filter bson.M) (total int64, err error) {
|
||||
total, err = mongo.Count(ctx, filter, entity.AdReportCollection)
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取报表列表
|
||||
func (d *report) List(ctx context.Context, req *dto.ListReportReq) (list []*entity.AdReport, total int64, err error) {
|
||||
// 构建查询过滤条件
|
||||
filter := d.buildReportListFilter(req)
|
||||
|
||||
// 检查总数
|
||||
total, err = d.checkReportTotalCount(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{"createdAt": -1}
|
||||
|
||||
opts := options.Find().SetLimit(limit).SetSkip(skip).SetSort(sort)
|
||||
|
||||
err = mongo.Find(ctx, filter, &list, entity.AdReportCollection, opts)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user