|
|
@ -295,14 +295,37 @@ func CreateGoods(w *ResponseWriter, r *Request) { |
|
|
|
w.Error(NewError(2, "商品价格错误")) |
|
|
|
w.Error(NewError(2, "商品价格错误")) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 加载登录用户信息
|
|
|
|
|
|
|
|
ut, ok := UserTokenFromContext(r.Context()) |
|
|
|
|
|
|
|
if !ok { |
|
|
|
|
|
|
|
w.Error(NewError(1, "找不到登录用户消息")) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 防止商品重名
|
|
|
|
var goods Goods |
|
|
|
var goods Goods |
|
|
|
err := DB.First(&goods, "name = ?", name).Error |
|
|
|
err := DB.First(&goods, "name = ?", name).Error |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
goods = Goods{ |
|
|
|
goods = Goods{ |
|
|
|
Name: name, |
|
|
|
Name: name, |
|
|
|
Price: price, |
|
|
|
Price: price, |
|
|
|
|
|
|
|
CreatedBy: ut.UserID, |
|
|
|
|
|
|
|
Creator: ut.User, |
|
|
|
|
|
|
|
UpdatedBy: ut.UserID, |
|
|
|
|
|
|
|
Updater: ut.User, |
|
|
|
} |
|
|
|
} |
|
|
|
err = DB.Create(&goods).Error |
|
|
|
err = DB.Transaction(func(tx *gorm.DB) error { |
|
|
|
|
|
|
|
if err := tx.Create(&goods).Error; err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return tx.Create(&Price{ |
|
|
|
|
|
|
|
GoodsID: goods.ID, |
|
|
|
|
|
|
|
Price: goods.Price, |
|
|
|
|
|
|
|
CreatedBy: goods.CreatedBy, |
|
|
|
|
|
|
|
CreatedAt: goods.CreatedAt, |
|
|
|
|
|
|
|
}).Error |
|
|
|
|
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
w.Error(NewError(3, "创建商品失败")) |
|
|
|
w.Error(NewError(3, "创建商品失败")) |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -328,6 +351,15 @@ func UpdateGoods(w *ResponseWriter, r *Request) { |
|
|
|
w.Error(NewError(2, "商品价格错误")) |
|
|
|
w.Error(NewError(2, "商品价格错误")) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 加载登录用户信息
|
|
|
|
|
|
|
|
ut, ok := UserTokenFromContext(r.Context()) |
|
|
|
|
|
|
|
if !ok { |
|
|
|
|
|
|
|
w.Error(NewError(1, "找不到登录用户消息")) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 查询商品
|
|
|
|
var goods Goods |
|
|
|
var goods Goods |
|
|
|
err := DB.First(&goods, "id = ?", id).Error |
|
|
|
err := DB.First(&goods, "id = ?", id).Error |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) { |
|
|
@ -355,6 +387,8 @@ func UpdateGoods(w *ResponseWriter, r *Request) { |
|
|
|
if goods.Price == price { |
|
|
|
if goods.Price == price { |
|
|
|
if goods.Name != name { |
|
|
|
if goods.Name != name { |
|
|
|
goods.Name = name |
|
|
|
goods.Name = name |
|
|
|
|
|
|
|
goods.UpdatedBy = ut.UserID |
|
|
|
|
|
|
|
goods.Updater = ut.User |
|
|
|
err = DB.Save(&goods).Error |
|
|
|
err = DB.Save(&goods).Error |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
w.Error(err) |
|
|
|
w.Error(err) |
|
|
@ -369,13 +403,17 @@ func UpdateGoods(w *ResponseWriter, r *Request) { |
|
|
|
err = DB.Transaction(func(tx *gorm.DB) error { |
|
|
|
err = DB.Transaction(func(tx *gorm.DB) error { |
|
|
|
goods.Name = name |
|
|
|
goods.Name = name |
|
|
|
goods.Price = price |
|
|
|
goods.Price = price |
|
|
|
|
|
|
|
goods.UpdatedBy = ut.UserID |
|
|
|
|
|
|
|
goods.Updater = ut.User |
|
|
|
if err = tx.Save(&goods).Error; err != nil { |
|
|
|
if err = tx.Save(&goods).Error; err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
// 记录价格变化
|
|
|
|
// 记录价格变化
|
|
|
|
return tx.Create(&Price{ |
|
|
|
return tx.Create(&Price{ |
|
|
|
GoodsID: goods.ID, |
|
|
|
GoodsID: goods.ID, |
|
|
|
Price: goods.Price, |
|
|
|
Price: goods.Price, |
|
|
|
|
|
|
|
CreatedBy: ut.UserID, |
|
|
|
|
|
|
|
Creator: ut.User, |
|
|
|
}).Error |
|
|
|
}).Error |
|
|
|
}) |
|
|
|
}) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|