From 7117d518607e71e35240e02f809ea8ed51dc71c0 Mon Sep 17 00:00:00 2001 From: Tooooommy Date: Wed, 21 Oct 2020 16:15:19 +0800 Subject: [PATCH] fix:bug --- controllers/client/good.go | 17 +++++++++------ controllers/client/order_entry.go | 46 ++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/controllers/client/good.go b/controllers/client/good.go index 13c6187..20f05a2 100644 --- a/controllers/client/good.go +++ b/controllers/client/good.go @@ -184,12 +184,14 @@ func (t *GoodCtl) Order() { for _, g := range param.Goods { goodIds = append(goodIds, g["id"]) } + goods := make([]*models.CustomerGoods, 0) err = s.Where("is_delete=0").In("id", goodIds).Find(&goods) if err != nil || len(goods) != len(goodIds) || len(goods) <= 0 { s.Rollback() t.ERROR("商品信息异常", code.MSG_DATA_NOT_EXIST) } + orderGoodNum, err := models.GetSubOrderGoodNumBySession(s, goodIds) if err != nil { s.Rollback() @@ -201,15 +203,16 @@ func (t *GoodCtl) Order() { var price = 0 // 检测库存 for _, good := range goods { + if good.Stock > -1 { + for _, v := range orderGoodNum { // 库存计算 + if v.GoodsId == good.Id { + good.Stock -= v.GoodsNum + } + } + } 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 { + if good.Stock != -1 && good.Stock-g["num"] < 0 { s.Rollback() t.ERROR("商品库存不足", code.MSG_DATA_NOT_EXIST) } else { diff --git a/controllers/client/order_entry.go b/controllers/client/order_entry.go index aa907ae..4fb12b3 100644 --- a/controllers/client/order_entry.go +++ b/controllers/client/order_entry.go @@ -60,9 +60,11 @@ func (t *OrderEntryCtl) List() { qrcode, err := utils.Qrcode2Base64(url) t.CheckErr(err) goods[index].Qrcode = qrcode - for _, v := range res { - if goods[index].Id == v.GoodsId { - goods[index].Stock -= v.GoodsNum + if goods[index].Stock == -1 { + for _, v := range res { + if goods[index].Id == v.GoodsId { + goods[index].Stock -= v.GoodsNum + } } } } @@ -129,20 +131,23 @@ func (t *OrderEntryCtl) Order() { t.ERROR("商品信息异常", code.MSG_ERR_Param) return } - // 找出商品库存 - ms, err := models.GetSubOrderGoodNumBySession(s, good.Id) - if err != nil { - s.Rollback() - t.CheckErr(err) - } - for _, m := range ms { - if m.GoodsNum == good.Id && good.Stock-m.GoodsNum <= 0 { + + if good.Stock > -1 { // 库存 + // 找出商品库存 + ms, err := models.GetSubOrderGoodNumBySession(s, good.Id) + if err != nil { s.Rollback() - t.ERROR(good.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH) - return + t.CheckErr(err) + } + for _, m := range ms { + if m.GoodsNum == good.Id && good.Stock-m.GoodsNum <= 0 { + s.Rollback() + t.ERROR(good.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH) + return + } } - } + } order := models.CustomerOrder{} // 查询库存 total, err := s.Where("is_delete=0").Count(&order) // 订单总数 @@ -342,11 +347,12 @@ func (t *OrderEntryCtl) ManualOrder() { } } - ms := make([]map[string]int, 0) - err = s.Table(&models.CustomerOrderSub{}).Alias("s").Select("s.goods_id, COALESCE(SUM(s.goods_num), 0) as goods_num"). - Join("left", (&models.CustomerOrder{}).Alias("o"), "o.id=s.order_id"). - Where("o.activity_id=? and o.rehearsal_id=? and o.arch_id=?", activity.Id, activity.RehearsalId, activity.ArchId). - In("s.goods_id", goodIds).GroupBy("s.goods_id").Find(&ms) + // ms := make([]map[string]int, 0) + // err = s.Table(&models.CustomerOrderSub{}).Alias("s").Select("s.goods_id, COALESCE(SUM(s.goods_num), 0) as goods_num"). + // Join("left", (&models.CustomerOrder{}).Alias("o"), "o.id=s.order_id"). + // Where("o.activity_id=? and o.rehearsal_id=? and o.arch_id=?", activity.Id, activity.RehearsalId, activity.ArchId). + // In("s.goods_id", goodIds).GroupBy("s.goods_id").Find(&ms) + ms, err := models.GetSubOrderGoodNumBySession(s, goodIds) if err != nil { s.Rollback() t.CheckErr(err) @@ -356,7 +362,7 @@ func (t *OrderEntryCtl) ManualOrder() { if g.Stock == -1 { // 无上限 break } - if m["goods_id"] == int(g.Id) && m["goods_num"] >= g.Stock { + if m.GoodsId == g.Id && g.Stock-m.GoodsNum <= 0 { s.Rollback() t.ERROR(g.Name+"商品库存不足", code.MSG_CUSTOMER_GOOD_NOT_ENOUGH) return