Files
data-engine/dao/copydata/api_account_report_sum_dao.go

114 lines
3.0 KiB
Go
Raw Permalink Normal View History

2026-04-02 11:51:44 +08:00
package copydata
import (
"context"
2026-04-30 13:45:41 +08:00
consts "dataengine/consts/public"
dto "dataengine/model/dto/copydata"
entity "dataengine/model/entity/copydata"
2026-04-02 11:51:44 +08:00
"errors"
2026-06-10 15:56:02 +08:00
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
2026-04-02 11:51:44 +08:00
"github.com/gogf/gf/v2/util/gconv"
2026-04-08 09:03:20 +08:00
"github.com/sirupsen/logrus"
2026-04-02 11:51:44 +08:00
)
var CidAccountReportSum = new(CidAccountReportSumDao)
type CidAccountReportSumDao struct{}
2026-04-08 09:03:20 +08:00
// Insert 插入广告数据报表汇总
2026-04-02 11:51:44 +08:00
func (d *CidAccountReportSumDao) Insert(ctx context.Context, req *dto.CidAccountReportSumItem) (id int64, err error) {
var entityData *entity.CidAccountReportSum
if err = gconv.Struct(req, &entityData); err != nil {
return
}
r, err := gfdb.DB(ctx).Model(ctx, consts.CidAccountReportSumTable).Data(&entityData).Insert()
if err != nil {
return
}
return r.LastInsertId()
}
2026-04-08 09:03:20 +08:00
// BatchInsert 批量插入广告数据报表汇总(使用 OnConflict 实现幂等性)
2026-04-02 11:51:44 +08:00
func (d *CidAccountReportSumDao) BatchInsert(ctx context.Context, reqs []*dto.CidAccountReportSumItem) (successCount int64, failCount int64, failedIndexes []int64, err error) {
if len(reqs) == 0 {
return 0, 0, nil, errors.New("批量插入数据不能为空")
}
// 分批处理每批100条
batchSize := 100
successCount = 0
failCount = 0
failedIndexes = make([]int64, 0)
for i := 0; i < len(reqs); i += batchSize {
end := i + batchSize
if end > len(reqs) {
end = len(reqs)
}
batch := reqs[i:end]
entityList := make([]*entity.CidAccountReportSum, len(batch))
for j, req := range batch {
var entityData entity.CidAccountReportSum
if err = gconv.Struct(req, &entityData); err != nil {
failCount++
failedIndexes = append(failedIndexes, int64(i+j))
continue
}
entityList[j] = &entityData
}
if len(entityList) == 0 {
continue
}
2026-04-08 09:03:20 +08:00
// 执行批量插入,使用 OnConflict 实现幂等性
_, err = gfdb.DB(ctx).Model(ctx, consts.CidAccountReportSumTable).
Data(entityList).
OnConflict(
"report_date_str",
"page_number",
2026-04-08 17:26:00 +08:00
"campaign_id",
"creative_id",
2026-04-08 09:03:20 +08:00
).
Save()
2026-04-02 11:51:44 +08:00
if err != nil {
2026-04-08 09:03:20 +08:00
logrus.Warnf("批量插入失败,尝试逐条插入: %v", err)
2026-04-02 11:51:44 +08:00
// 批量插入失败,尝试逐条插入
for k := range batch {
_, singleErr := d.Insert(ctx, batch[k])
if singleErr != nil {
failCount++
failedIndexes = append(failedIndexes, int64(i+k))
} else {
successCount++
}
}
} else {
successCount += int64(len(entityList))
}
}
return successCount, failCount, failedIndexes, nil
}
2026-04-08 09:03:20 +08:00
// DeleteByDateRange 按日期范围删除数据(用于补偿前去重)
func (d *CidAccountReportSumDao) DeleteByDateRange(ctx context.Context, advertiserID int64, startDateStr, endDateStr string) (int64, error) {
cols := (&entity.CidAccountReportSum{}).GetCols()
result, err := gfdb.DB(ctx).Model(ctx, consts.CidAccountReportSumTable).
Where(cols.ReportDateStr+" >= ? AND "+cols.ReportDateStr+" <= ?", startDateStr, endDateStr).
Delete()
if err != nil {
return 0, err
}
affected, _ := result.RowsAffected()
return affected, nil
}