129 lines
3.2 KiB
Go
129 lines
3.2 KiB
Go
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 MaterialReport = new(materialReportDao)
|
|
|
|
type materialReportDao struct{}
|
|
|
|
// Insert 插入素材报表数据
|
|
func (d *materialReportDao) Insert(ctx context.Context, req *dto.MaterialReportItem) (id int64, err error) {
|
|
var entityData *entity.MaterialReport
|
|
if err = gconv.Struct(req, &entityData); err != nil {
|
|
return
|
|
}
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.MaterialReportTable).Data(&entityData).Insert()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return r.LastInsertId()
|
|
}
|
|
|
|
// BatchInsert 批量插入素材报表数据
|
|
func (d *materialReportDao) BatchInsert(ctx context.Context, reqs []*dto.MaterialReportItem) (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.MaterialReport, len(batch))
|
|
|
|
for j, req := range batch {
|
|
var entityData entity.MaterialReport
|
|
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.MaterialReportTable).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
|
|
}
|
|
|
|
// List 查询素材报表数据列表
|
|
func (d *materialReportDao) List(ctx context.Context, req *dto.ListMaterialReportReq) ([]*entity.MaterialReport, int, error) {
|
|
model := gfdb.DB(ctx).Model(ctx, consts.MaterialReportTable).Model
|
|
|
|
if req.ReportDateStr != "" {
|
|
model = model.Where("report_date_str", req.ReportDateStr)
|
|
}
|
|
if req.PhotoId != "" {
|
|
model = model.Where("photo_id", req.PhotoId)
|
|
}
|
|
if req.CampaignId != nil {
|
|
model = model.Where("campaign_id", req.CampaignId)
|
|
}
|
|
if req.UnitId != nil {
|
|
model = model.Where("unit_id", req.UnitId)
|
|
}
|
|
if req.CreativeId != nil {
|
|
model = model.Where("creative_id", req.CreativeId)
|
|
}
|
|
if req.Keyword != "" {
|
|
model = model.Where("(photo_name LIKE ? OR campaign_name LIKE ? OR unit_name LIKE ? OR creative_name LIKE ?)",
|
|
"%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
|
}
|
|
|
|
model = model.OrderDesc("created_at")
|
|
|
|
total, err := model.Count()
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var list []*entity.MaterialReport
|
|
if req.Page != nil {
|
|
err = model.Page(int(req.Page.PageNum), int(req.Page.PageSize)).Scan(&list)
|
|
} else {
|
|
err = model.Scan(&list)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return list, total, nil
|
|
}
|