From fbefb187eaffd927d36c84de3d5f3f02a817ff09 Mon Sep 17 00:00:00 2001 From: wood chen Date: Tue, 13 May 2025 11:12:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84UpdateProvider=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E4=BC=98=E5=8C=96=E6=8F=90=E4=BE=9B=E5=95=86?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=BB=E8=BE=91=E3=80=82=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=B0=E8=AE=B0=E5=BD=95=E3=80=81=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E4=BB=B7=E6=A0=BC=E8=A1=A8=E4=B8=AD=E7=9A=84channel?= =?UTF-8?q?=5Ftype=E5=92=8Ctemp=5Fchannel=5Ftype=E3=80=81=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=97=A7=E8=AE=B0=E5=BD=95=E7=9A=84=E6=AD=A5=E9=AA=A4?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E5=8D=87=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB?= =?UTF-8?q?=E6=80=A7=E5=92=8C=E4=BA=8B=E5=8A=A1=E5=A4=84=E7=90=86=E7=9A=84?= =?UTF-8?q?=E6=B8=85=E6=99=B0=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/handlers/providers.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/backend/handlers/providers.go b/backend/handlers/providers.go index 65c5158..de43857 100644 --- a/backend/handlers/providers.go +++ b/backend/handlers/providers.go @@ -96,36 +96,36 @@ func UpdateProvider(c *gin.Context) { // 开始事务 tx := database.DB.Begin() - // 更新price表中的channel_type + // 1. 先创建新记录 + provider.CreatedAt = time.Now() + provider.UpdatedAt = time.Now() + if err := tx.Create(&provider).Error; err != nil { + tx.Rollback() + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create new provider"}) + return + } + + // 2. 更新price表中的channel_type if err := tx.Model(&models.Price{}).Where("channel_type = ?", oldID).Update("channel_type", provider.ID).Error; err != nil { tx.Rollback() c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update price references"}) return } - // 更新price表中的temp_channel_type + // 3. 更新price表中的temp_channel_type if err := tx.Model(&models.Price{}).Where("temp_channel_type = ?", oldID).Update("temp_channel_type", provider.ID).Error; err != nil { tx.Rollback() c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update price temp references"}) return } - // 删除旧记录 + // 4. 删除旧记录 if err := tx.Delete(&existingProvider).Error; err != nil { tx.Rollback() c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete old provider"}) return } - // 创建新记录 - provider.CreatedAt = time.Now() - provider.UpdatedAt = time.Now() - if err := tx.Create(&provider).Error; err != nil { - tx.Rollback() - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create new provider"}) - return - } - // 提交事务 if err := tx.Commit().Error; err != nil { tx.Rollback()