|
|
@ -71,7 +71,7 @@ func (t *GoodCtl) ListGood() { |
|
|
|
for _, g := range goods { |
|
|
|
goodIds = append(goodIds, g.Id) |
|
|
|
} |
|
|
|
res, err := models.GetSubOrderGoodNumByGoodIds(goodIds) |
|
|
|
res, err := models.GetSubOrderGoodNum(goodIds) |
|
|
|
t.CheckErr(err) |
|
|
|
|
|
|
|
for index := range goods { |
|
|
@ -176,20 +176,26 @@ func (t *GoodCtl) Order() { |
|
|
|
t.Assert(exist, code.MSG_DATA_NOT_EXIST, "订单活动不存在") |
|
|
|
|
|
|
|
// 注意库存 --> 开启事务
|
|
|
|
session := core.GetXormAuto().NewSession() |
|
|
|
defer session.Close() |
|
|
|
session.Begin() |
|
|
|
s := core.GetXormAuto().NewSession() |
|
|
|
defer s.Close() |
|
|
|
s.Begin() |
|
|
|
|
|
|
|
goodIds := make([]interface{}, 0) |
|
|
|
for _, g := range param.Goods { |
|
|
|
goodIds = append(goodIds, g["id"]) |
|
|
|
} |
|
|
|
goods := make([]*models.CustomerGoods, 0) |
|
|
|
err = session.Where("is_delete=0").In("id", goodIds).Find(&goods) |
|
|
|
err = s.Where("is_delete=0").In("id", goodIds).Find(&goods) |
|
|
|
if err != nil || len(goods) != len(goodIds) || len(goods) <= 0 { |
|
|
|
session.Rollback() |
|
|
|
s.Rollback() |
|
|
|
t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) |
|
|
|
} |
|
|
|
orderGoodNum, err := models.GetSubOrderGoodNumBySession(s, goodIds) |
|
|
|
if err != nil { |
|
|
|
s.Rollback() |
|
|
|
t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
subOrders := make([]*models.CustomerOrderSub, 0) |
|
|
|
var price = 0 |
|
|
@ -197,15 +203,22 @@ func (t *GoodCtl) Order() { |
|
|
|
for _, good := range goods { |
|
|
|
for _, g := range param.Goods { |
|
|
|
if good.Id == g["id"] { |
|
|
|
for _, v := range orderGoodNum { // 库存计算
|
|
|
|
if v.GoodsId == good.Id { |
|
|
|
good.Stock -= v.GoodsNum |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if good.Stock-g["num"] < 0 { |
|
|
|
session.Rollback() |
|
|
|
s.Rollback() |
|
|
|
t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) |
|
|
|
} else { |
|
|
|
_, err = session.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good) |
|
|
|
if err != nil { |
|
|
|
session.Rollback() |
|
|
|
t.CheckErr(err) |
|
|
|
} |
|
|
|
// 需要减掉库存
|
|
|
|
//_, err = s.ID(good.Id).Decr("stock", g["num"]).Cols("stock").Update(good)
|
|
|
|
//if err != nil {
|
|
|
|
// s.Rollback()
|
|
|
|
// t.CheckErr(err)
|
|
|
|
//}
|
|
|
|
price += int(good.Price*100) * g["num"] |
|
|
|
subOrders = append(subOrders, &models.CustomerOrderSub{ |
|
|
|
IsDelete: false, |
|
|
@ -223,9 +236,9 @@ func (t *GoodCtl) Order() { |
|
|
|
} |
|
|
|
|
|
|
|
var expireAt = time.Now().Add(30 * time.Second).Unix() |
|
|
|
count, err := session.Where("is_delete=0").Count(&models.CustomerOrder{}) |
|
|
|
count, err := s.Where("is_delete=0").Count(&models.CustomerOrder{}) |
|
|
|
if err != nil { |
|
|
|
session.Rollback() |
|
|
|
s.Rollback() |
|
|
|
t.CheckErr(err) |
|
|
|
} |
|
|
|
|
|
|
@ -257,16 +270,16 @@ func (t *GoodCtl) Order() { |
|
|
|
activity.Id, expireAt) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
session.Rollback() |
|
|
|
s.Rollback() |
|
|
|
t.CheckErr(err) |
|
|
|
} |
|
|
|
order.OutTradeNo = res["out_trade_no"].(string) |
|
|
|
order.Status = 0 |
|
|
|
} |
|
|
|
|
|
|
|
_, err = session.InsertOne(&order) |
|
|
|
_, err = s.InsertOne(&order) |
|
|
|
if err != nil { |
|
|
|
session.Rollback() |
|
|
|
s.Rollback() |
|
|
|
t.CheckErr(err) |
|
|
|
} |
|
|
|
|
|
|
@ -276,13 +289,13 @@ func (t *GoodCtl) Order() { |
|
|
|
subOrder.OrderId = order.Id |
|
|
|
beans = append(beans, subOrder) |
|
|
|
} |
|
|
|
session.Insert(beans...) |
|
|
|
s.Insert(beans...) |
|
|
|
if err != nil { |
|
|
|
session.Rollback() |
|
|
|
s.Rollback() |
|
|
|
t.CheckErr(err) |
|
|
|
} |
|
|
|
|
|
|
|
err = session.Commit() |
|
|
|
err = s.Commit() |
|
|
|
t.CheckErr(err) |
|
|
|
res["order_id"] = order.Id |
|
|
|
|
|
|
|