Browse Source

fix:bug

dev
Tooooommy 4 years ago
parent
commit
397f8ac7be
  1. 53
      controllers/client/good.go
  2. 2
      controllers/client/order_entry.go
  3. 9
      models/customer_order_sub.go

53
controllers/client/good.go

@ -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

2
controllers/client/order_entry.go

@ -51,7 +51,7 @@ func (t *OrderEntryCtl) List() {
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 {

9
models/customer_order_sub.go

@ -2,6 +2,7 @@ package models
import (
"fmt"
"github.com/xormplus/xorm"
"time"
"github.com/ouxuanserver/osmanthuswine/src/core"
@ -69,8 +70,12 @@ type OrderGoodNum struct {
GoodsNum int `json:"goods_num"`
}
func GetSubOrderGoodNumByGoodIds(goodIds interface{}) (res []*OrderGoodNum, err error) {
err = core.GetXormAuto().Table(&CustomerOrderSub{}).Alias("s").Select("s.goods_id, SUM(s.goods_num) as goods_num").
func GetSubOrderGoodNum(goodIds interface{}) (res []*OrderGoodNum, err error) {
return GetSubOrderGoodNumBySession(core.GetXormAuto().NewSession(), goodIds)
}
func GetSubOrderGoodNumBySession(s *xorm.Session, goodIds interface{}) (res []*OrderGoodNum, err error) {
err = s.Table(&CustomerOrderSub{}).Alias("s").Select("s.goods_id, SUM(s.goods_num) as goods_num").
Join("left", (&CustomerOrder{}).TableName()+" as o", "o.id=s.order_id").
Where("o.is_delete=0 and s.is_delete=0 and o.type=0").In("s.goods_id", goodIds).
NotIn("o.status", "0", "8", "9").Find(&res)

Loading…
Cancel
Save