批量新增 获取广告计划数据
This commit is contained in:
81
dao/copydata/campaign_report_detail_dao.go
Normal file
81
dao/copydata/campaign_report_detail_dao.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package copydata
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/copydata"
|
||||
entity "cid/model/entity/copydata"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var CampaignReportDetail = new(CampaignReportDetailDao)
|
||||
|
||||
type CampaignReportDetailDao struct{}
|
||||
|
||||
func (d *CampaignReportDetailDao) Insert(ctx context.Context, req *dto.CampaignReportDetailItem) (id int64, err error) {
|
||||
var entityData *entity.CampaignReportDetail
|
||||
if err = gconv.Struct(req, &entityData); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.CampaignReportDetailTable).Data(&entityData).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
func (d *CampaignReportDetailDao) BatchInsert(ctx context.Context, reqs []*dto.CampaignReportDetailItem) (successCount int64, failCount int64, failedIndexes []int64, err error) {
|
||||
if len(reqs) == 0 {
|
||||
return 0, 0, nil, errors.New("批量插入数据不能为空")
|
||||
}
|
||||
|
||||
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.CampaignReportDetail, len(batch))
|
||||
|
||||
for j, req := range batch {
|
||||
var entityData entity.CampaignReportDetail
|
||||
if err = gconv.Struct(req, &entityData); err != nil {
|
||||
failCount++
|
||||
failedIndexes = append(failedIndexes, int64(i+j))
|
||||
continue
|
||||
}
|
||||
entityList[j] = &entityData
|
||||
}
|
||||
|
||||
if len(entityList) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
_, err = gfdb.DB(ctx).Model(ctx, consts.CampaignReportDetailTable).Data(entityList).Insert()
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
86
dao/copydata/campaign_report_sum_dao.go
Normal file
86
dao/copydata/campaign_report_sum_dao.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package copydata
|
||||
|
||||
import (
|
||||
consts "cid/consts/public"
|
||||
dto "cid/model/dto/copydata"
|
||||
entity "cid/model/entity/copydata"
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var CampaignReportSum = new(CampaignReportSumDao)
|
||||
|
||||
type CampaignReportSumDao struct{}
|
||||
|
||||
// Insert 插入广告计划效果指标表
|
||||
func (d *CampaignReportSumDao) Insert(ctx context.Context, req *dto.CampaignReportSumItem) (id int64, err error) {
|
||||
var entityData *entity.CampaignReportSum
|
||||
if err = gconv.Struct(req, &entityData); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
r, err := gfdb.DB(ctx).Model(ctx, consts.CampaignReportSumTable).Data(&entityData).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// BatchInsert 批量插入广告计划效果指标表
|
||||
func (d *CampaignReportSumDao) BatchInsert(ctx context.Context, reqs []*dto.CampaignReportSumItem) (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.CampaignReportSum, len(batch))
|
||||
|
||||
for j, req := range batch {
|
||||
var entityData entity.CampaignReportSum
|
||||
if err = gconv.Struct(req, &entityData); err != nil {
|
||||
failCount++
|
||||
failedIndexes = append(failedIndexes, int64(i+j))
|
||||
continue
|
||||
}
|
||||
entityList[j] = &entityData
|
||||
}
|
||||
|
||||
if len(entityList) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 执行批量插入
|
||||
_, err = gfdb.DB(ctx).Model(ctx, consts.CampaignReportSumTable).Data(entityList).Insert()
|
||||
if err != nil {
|
||||
// 批量插入失败,尝试逐条插入
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user