diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index ce0bfc13..3f34b490 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -2,6 +2,7 @@ package logic import ( "context" + "errors" "fmt" "fusenapi/constants" "fusenapi/model/gmodel" @@ -9,6 +10,7 @@ import ( "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/step_price" + "gorm.io/gorm" "math" "strings" @@ -100,48 +102,66 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri //开始计算价格 calculateResultList := make([]types.CalculateResultItem, 0, len(req.CalculateList)) subTotalPrice := int64(0) - for _, cart := range carts { - sizePrice, ok := mapPrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)] - if !ok { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, fmt.Sprintf("there carts contain some one which have no price info:%d_%d", *cart.ProductId, *cart.SizeId)) - } - //阶梯数量切片 - stepNum, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepNum, ",")) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step number:%d_%d", *cart.ProductId, *cart.SizeId)) - } - lenStepNum := len(stepNum) - //阶梯价格切片 - stepPrice, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepPrice, ",")) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("failed to parse step price:%d_%d", *cart.ProductId, *cart.SizeId)) - } - lenStepPrice := len(stepPrice) - if lenStepPrice == 0 || lenStepNum == 0 { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price or step number is not set:%d_%d", *cart.ProductId, *cart.SizeId)) - } - //购买箱数 - boxQuantity := int(math.Ceil(float64(mapCalculateQuantity[cart.Id]) / float64(*sizePrice.EachBoxNum))) - //根据数量获取阶梯价格中对应的价格 - itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) - //如果有配件,单价也要加入配件价格 - if *cart.FittingId > 0 { - if fittingPrice, ok := mapFitting[*cart.FittingId]; ok { - itemPrice += fittingPrice - } else { - return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "cart contain some one witch lose fitting:%d", *cart.FittingId) + //开启事物 + err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error { + shoppingCartModel := gmodel.NewFsShoppingCartModel(tx) + for _, cart := range carts { + sizePrice, ok := mapPrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)] + if !ok { + return errors.New(fmt.Sprintf("there carts contain some one which have no price info:%d_%d", *cart.ProductId, *cart.SizeId)) + } + //阶梯数量切片 + stepNum, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepNum, ",")) + if err != nil { + logx.Error(err) + return errors.New(fmt.Sprintf("failed to parse step number:%d_%d", *cart.ProductId, *cart.SizeId)) + } + lenStepNum := len(stepNum) + //阶梯价格切片 + stepPrice, err := format.StrSlicToIntSlice(strings.Split(*sizePrice.StepPrice, ",")) + if err != nil { + logx.Error(err) + return errors.New(fmt.Sprintf("failed to parse step price:%d_%d", *cart.ProductId, *cart.SizeId)) + } + lenStepPrice := len(stepPrice) + if lenStepPrice == 0 || lenStepNum == 0 { + return errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *cart.ProductId, *cart.SizeId)) + } + //请求的数量 + reqPurchaseQuantity := mapCalculateQuantity[cart.Id] + //购买箱数 + boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*sizePrice.EachBoxNum))) + //根据数量获取阶梯价格中对应的价格 + itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) + //如果有配件,单价也要加入配件价格 + if *cart.FittingId > 0 { + if fittingPrice, ok := mapFitting[*cart.FittingId]; ok { + itemPrice += fittingPrice + } else { + return errors.New(fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId)) + } + } + //单个购物车总价 + totalPrice := itemPrice * reqPurchaseQuantity + calculateResultList = append(calculateResultList, types.CalculateResultItem{ + CartId: cart.Id, + ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), + TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), + }) + subTotalPrice += totalPrice + //更新购物车购买数量 + err = shoppingCartModel.Update(l.ctx, cart.Id, userinfo.UserId, &gmodel.FsShoppingCart{ + PurchaseQuantity: &reqPurchaseQuantity, + }) + if err != nil { + logx.Error(err) + return errors.New(fmt.Sprintf("failed to update cart`s purchase quantity:%d", cart.Id)) } } - //单个购物车总价 - totalPrice := itemPrice * mapCalculateQuantity[cart.Id] - calculateResultList = append(calculateResultList, types.CalculateResultItem{ - CartId: cart.Id, - ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), - TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), - }) - subTotalPrice += totalPrice + return nil + }) + if err != nil { + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error()) } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{ SubTotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(subTotalPrice)),