|
|
@ -307,16 +307,45 @@ func UpdateGoods(w *ResponseWriter, r *Request) { |
|
|
|
w.Error(err) |
|
|
|
w.Error(err) |
|
|
|
return |
|
|
|
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.Name = name |
|
|
|
goods.Price = price |
|
|
|
goods.Price = price |
|
|
|
err = DB.Save(&goods).Error |
|
|
|
if err = tx.Save(&goods).Error; err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
w.Error(err) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 记录价格变化
|
|
|
|
|
|
|
|
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 查询商品列表
|
|
|
|
// GetGoodsList 查询商品列表
|
|
|
|