fix: upsert repeated shopee sync tasks

This commit is contained in:
QiuSW
2026-09-14 10:32:58 +08:00
parent f8c90004f1
commit e81411cbbc
+14 -1
View File
@@ -110,7 +110,20 @@ func (e ShopeeProduct) SyncByShopeeID(c *gin.Context) {
e.Error(500, err, "同步任务初始化失败")
return
}
if err := db.Create(&row).Error; err != nil {
var existing models.ShopeeSyncTask
if err := db.Where("task_id = ?", task.TaskID).First(&existing).Error; err == nil {
row.Id = existing.Id
row.CreatedAt = existing.CreatedAt
if err := db.Save(&row).Error; err != nil {
e.Error(500, err, "保存同步任务失败")
return
}
} else if errors.Is(err, gorm.ErrRecordNotFound) {
if err := db.Create(&row).Error; err != nil {
e.Error(500, err, "保存同步任务失败")
return
}
} else {
e.Error(500, err, "保存同步任务失败")
return
}