代码提交

This commit is contained in:
2026-11-05 12:11:56 +08:00
parent 3d9bdb6f6e
commit 3fd3e82a01
18 changed files with 431 additions and 58 deletions
+21
View File
@@ -603,6 +603,27 @@ func InsertOneChannelStatYestoday(ctx context.Context, media *ChannelStatYesterd
return nil
}
// UpsertOneChannelStatYestoday 按 (channel, appId, sumDate) 覆盖写入一条同时段对比数据。
// 用 ReplaceOne 而不是 InsertOne: 整点任务可能因服务重启或重试在同一小时内跑第二次,
// 而该表有 (channel, appId, sumDate) 唯一索引,InsertOne 第二次必然冲突失败。
// 整条替换也保证快照不会残留上一次的旧值。
// 注意不可改用 UpsertMOne: 它内部用 Struct2Map 走 JSON 序列化,time.Time 会被写成字符串,
// sumDate 将不再是 BSON Date,按时间范围的查询会全部失效。
func UpsertOneChannelStatYestoday(ctx context.Context, doc *ChannelStatYesterday) error {
filter := bson.M{
FN.ChannelStatYestoday.Channel: doc.Channel,
FN.ChannelStatYestoday.AppId: doc.AppId,
FN.ChannelStatYestoday.SumDate: doc.SumDate,
}
opt := options.Replace().SetUpsert(true)
if _, err := dbStatYestoday.GetCollection().ReplaceOne(ctx, filter, doc, opt); err != nil {
log.Warn("ChannelStatYestoday:UpsertOneChannelStatYestoday ReplaceOne failed",
log.E(err), log.Any("filter", filter))
return err
}
return nil
}
// CountChannelStatYestoday 统计渠道扣量规则数量
func CountChannelStatYestoday(ctx context.Context, filter bson.M) (int64, error) {
count, err := dbStatYestoday.Count(ctx, filter)