From b56bce9de78969b95758dbe780189fd6bb732323 Mon Sep 17 00:00:00 2001 From: hupeh Date: Mon, 2 Jan 2023 13:30:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:sparkles:=20=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E4=BB=B7=E6=A0=BC=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/rts.go | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/app/rts.go b/app/rts.go index 685f896..52a366a 100644 --- a/app/rts.go +++ b/app/rts.go @@ -307,16 +307,45 @@ func UpdateGoods(w *ResponseWriter, r *Request) { w.Error(err) return } - if goods.Name != name || goods.Price != price { + + // 信息未改变 + if goods.Name == name && goods.Price == price { + w.Error(NewError(2, "数据未变化")) + return + } + + // 价格未改变 + if goods.Price == price { + if goods.Name != name { + goods.Name = name + err = DB.Save(&goods).Error + if err != nil { + w.Error(err) + return + } + } + w.Ok(goods, "修改成功") + return + } + + // 修改商品信息并记录价格变化 + err = DB.Transaction(func(tx *gorm.DB) error { goods.Name = name goods.Price = price - err = DB.Save(&goods).Error - if err != nil { - w.Error(err) - return + if err = tx.Save(&goods).Error; err != nil { + return err } + // 记录价格变化 + return tx.Create(&Price{ + GoodsID: goods.ID, + Price: goods.Price, + }).Error + }) + if err != nil { + w.Error(err) + } else { + w.Ok(goods, "修改成功") } - w.Ok(goods, "修改成功") } // GetGoodsList 查询商品列表