112 lines
3.1 KiB
Go
112 lines
3.1 KiB
Go
package tencent
|
||
|
||
import (
|
||
"context"
|
||
consts "dataengine/consts/public"
|
||
entity "dataengine/model/entity/tencent"
|
||
|
||
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/sirupsen/logrus"
|
||
)
|
||
|
||
type audioDao struct{}
|
||
|
||
var Audio = new(audioDao)
|
||
|
||
// BatchUpsert 批量插入或更新(使用 OnConflict 实现 Upsert)
|
||
func (d *audioDao) BatchUpsert(ctx context.Context, items []*entity.Audio) (successCount int, err error) {
|
||
if len(items) == 0 {
|
||
return 0, nil
|
||
}
|
||
|
||
logrus.Infof("开始批量Upsert音乐素材: %d 条记录", len(items))
|
||
|
||
// 分批处理,每批100条
|
||
batchSize := 100
|
||
successCount = 0
|
||
|
||
for i := 0; i < len(items); i += batchSize {
|
||
end := i + batchSize
|
||
if end > len(items) {
|
||
end = len(items)
|
||
}
|
||
|
||
batch := items[i:end]
|
||
|
||
logrus.Infof("处理第 %d-%d 条音乐素材记录", i+1, end)
|
||
|
||
// 执行批量插入,使用 OnConflict 实现 Upsert
|
||
result, err := gfdb.DB(ctx).Model(ctx, consts.TencentAudioTable).
|
||
Data(batch).
|
||
OnConflict(entity.AudioCols.AudioId).
|
||
Save()
|
||
|
||
if err != nil {
|
||
logrus.Errorf("批量Upsert音乐素材失败: %v,尝试逐条处理", err)
|
||
// 批量失败,逐条处理
|
||
for _, item := range batch {
|
||
if upsertErr := d.upsertSingle(ctx, item); upsertErr != nil {
|
||
logrus.Errorf("逐条Upsert音乐素材失败: audio_id=%s, err=%v", item.AudioId, upsertErr)
|
||
continue
|
||
}
|
||
successCount++
|
||
}
|
||
} else {
|
||
affected, _ := result.RowsAffected()
|
||
successCount += int(affected)
|
||
logrus.Infof("批量Upsert音乐素材成功: 影响 %d 条记录", affected)
|
||
}
|
||
}
|
||
|
||
logrus.Infof("批量Upsert音乐素材完成: 成功 %d 条", successCount)
|
||
return successCount, nil
|
||
}
|
||
|
||
// upsertSingle 单条插入或更新
|
||
func (d *audioDao) upsertSingle(ctx context.Context, item *entity.Audio) error {
|
||
var existing entity.Audio
|
||
err := gfdb.DB(ctx).Model(ctx, consts.TencentAudioTable).
|
||
Where("tenant_id", item.TenantId).
|
||
Where(entity.AudioCols.AudioId, item.AudioId).
|
||
WhereNull(entity.AudioCols.DeletedAt).
|
||
Scan(&existing)
|
||
|
||
if err != nil && existing.Id == 0 {
|
||
// 记录不存在,执行插入
|
||
_, err = gfdb.DB(ctx).Model(ctx, consts.TencentAudioTable).
|
||
Data(item).
|
||
Insert()
|
||
return err
|
||
}
|
||
|
||
// 记录存在,执行更新
|
||
_, err = gfdb.DB(ctx).Model(ctx, consts.TencentAudioTable).
|
||
Where("id", existing.Id).
|
||
Data(g.Map{
|
||
entity.AudioCols.CoverImageUrl: item.CoverImageUrl,
|
||
entity.AudioCols.AudioName: item.AudioName,
|
||
entity.AudioCols.Author: item.Author,
|
||
entity.AudioCols.Duration: item.Duration,
|
||
entity.AudioCols.ExpireTime: item.ExpireTime,
|
||
entity.AudioCols.FeelTags: item.FeelTags,
|
||
entity.AudioCols.GenreTags: item.GenreTags,
|
||
entity.AudioCols.Updater: item.Updater,
|
||
entity.AudioCols.UpdatedAt: item.UpdatedAt,
|
||
}).
|
||
Update()
|
||
|
||
return err
|
||
}
|
||
|
||
// ListAll 获取所有音乐素材
|
||
func (d *audioDao) ListAll(ctx context.Context) ([]entity.Audio, error) {
|
||
var list []entity.Audio
|
||
err := gfdb.DB(ctx).Model(ctx, consts.TencentAudioTable).
|
||
WhereNull(entity.AudioCols.DeletedAt).
|
||
OrderAsc(entity.AudioCols.AudioId).
|
||
Scan(&list)
|
||
|
||
return list, err
|
||
}
|