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 查询商品列表