diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index fc461746..0317398e 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -2,9 +2,9 @@ package logic import ( "context" + "encoding/json" "errors" "fmt" - "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/server/shopping-cart/internal/svc" "fusenapi/server/shopping-cart/internal/types" @@ -67,33 +67,24 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri } sizeIds := make([]int64, 0, len(carts)) productIds := make([]int64, 0, len(carts)) - fittingIds := make([]int64, 0, len(carts)) + modelIds := make([]int64, 0, len(carts)) //模型+配件 for _, v := range carts { sizeIds = append(sizeIds, *v.SizeId) productIds = append(productIds, *v.ProductId) + modelIds = append(modelIds, *v.ModelId) if *v.FittingId > 0 { - fittingIds = append(fittingIds, *v.FittingId) + modelIds = append(modelIds, *v.FittingId) } } - //根据sizeid获取价格列表 - priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list") - } - mapPrice := make(map[string]gmodel.FsProductPrice) - for _, v := range priceList { - mapPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v - } //获取配件列表(只有id跟价格) - fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag(l.ctx, fittingIds, constants.TAG_PARTS, "id,price") + modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "id,step_price,price") if err != nil { logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting list") + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") } - mapFitting := make(map[int64]int64) - for _, v := range fittingList { - mapFitting[v.Id] = *v.Price + mapModel := make(map[int64]gmodel.FsProductModel3d) + for _, v := range modelList { + mapModel[v.Id] = v } //开始计算价格 calculateResultList := make([]types.CalculateResultItem, 0, len(req.CalculateList)) @@ -102,9 +93,13 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri 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)] + modelInfo, ok := mapModel[*cart.ModelId] if !ok { - return errors.New(fmt.Sprintf("there carts contain some one which have no price info:%d_%d", *cart.ProductId, *cart.SizeId)) + return err + } + var stepPrice gmodel.StepPriceJsonStruct + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + return err } //请求的数量 reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity @@ -115,16 +110,15 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri //如果有配件,单价也要加入配件价格 fittingPrice := int64(0) if *cart.FittingId > 0 { - if fPrice, ok := mapFitting[*cart.FittingId]; ok { - fittingPrice = fPrice + if fittingInfo, ok := mapModel[*cart.FittingId]; ok { + fittingPrice = *fittingInfo.Price } else { return errors.New(fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId)) } } //计算价格 - itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(reqPurchaseQuantity, &sizePrice, fittingPrice) + itemPrice, totalPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(reqPurchaseQuantity, stepPrice, fittingPrice) if err != nil { - logx.Error(err) return err } calculateResultList = append(calculateResultList, types.CalculateResultItem{ @@ -149,6 +143,7 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri return nil }) if err != nil { + logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error()) } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{ diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index 1f798027..f73025a8 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -63,7 +63,6 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo mapSize = make(map[int64]gmodel.FsProductSize) mapModel = make(map[int64]gmodel.FsProductModel3d) mapTemplate = make(map[int64]gmodel.FsProductTemplateV2) - mapSizePrice = make(map[string]gmodel.FsProductPrice) mapProduct = make(map[int64]gmodel.FsProduct) mapResourceMetadata = make(map[string]interface{}) ) @@ -73,7 +72,6 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo MapSize: mapSize, MapModel: mapModel, MapTemplate: mapTemplate, - MapSizePrice: mapSizePrice, MapProduct: mapProduct, MapResourceMetadata: mapResourceMetadata, }) @@ -101,21 +99,32 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo list := make([]types.CartItem, 0, len(carts)) for _, cart := range carts { snapShot := mapSnapshot[cart.Id] - sizePrice, ok := mapSizePrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)] + modelInfo, ok := mapModel[*cart.ModelId] if !ok { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d_%d", *cart.ProductId, *cart.SizeId)) + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s model info is not exists:%d_%d", *cart.ProductId, *cart.SizeId)) } + var stepPrice gmodel.StepPriceJsonStruct + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse model step price:%d", *cart.ModelId)) + } + //购买数量步进量 + stepPurchaseQuantity := *modelInfo.PackedUnit //如果有配件,单价也要加入配件价格 fittingPrice := int64(0) if *cart.FittingId > 0 { - if curFittingInfo, ok := mapModel[*cart.FittingId]; ok { - fittingPrice = *curFittingInfo.Price - } else { + curFittingInfo, ok := mapModel[*cart.FittingId] + if !ok { return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId)) } + fittingPrice = *curFittingInfo.Price + //取大的为步进量基数 + if *curFittingInfo.PackedUnit > stepPurchaseQuantity { + stepPurchaseQuantity = *curFittingInfo.PackedUnit + } } - //计算价格 - itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice) + //计算阶梯价格 + itemPrice, totalPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(*cart.PurchaseQuantity, stepPrice, fittingPrice) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) @@ -175,10 +184,12 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo Slogan: snapShot.UserDiyInformation.Slogan, }, PurchaseQuantity: *cart.PurchaseQuantity, - MinPurchaseQuantity: *sizePrice.EachBoxNum * (*sizePrice.MinBuyNum), - StepPurchaseQuantity: *sizePrice.EachBoxNum, + MinPurchaseQuantity: stepPrice.MinBuyUnitsNum, + StepPurchaseQuantity: stepPurchaseQuantity, IsHighlyCustomized: *cart.IsHighlyCustomized > 0, IsSelected: *cart.IsSelected > 0, + TemplateTag: *mapTemplate[*cart.TemplateId].TemplateTag, + Logo: snapShot.Logo, } //是否有失效的 if description, ok := mapCartChange[cart.Id]; ok { @@ -206,7 +217,6 @@ type GetRelationInfoReq struct { MapSize map[int64]gmodel.FsProductSize MapModel map[int64]gmodel.FsProductModel3d MapTemplate map[int64]gmodel.FsProductTemplateV2 - MapSizePrice map[string]gmodel.FsProductPrice MapProduct map[int64]gmodel.FsProduct MapResourceMetadata map[string]interface{} } @@ -274,15 +284,6 @@ func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error { for _, v := range templateList { req.MapTemplate[v.Id] = v } - //根据sizeid获取价格列表 - priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds) - if err != nil { - logx.Error(err) - return errors.New("failed to get cart`s product price list") - } - for _, v := range priceList { - req.MapSizePrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v - } return nil } diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go index 366f7858..e905920a 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -53,6 +53,8 @@ type CartItem struct { IsInvalid bool `json:"is_invalid"` //是否无效 InvalidDescription string `json:"invalid_description"` //无效原因 IsSelected bool `json:"is_selected"` //是否选中 + TemplateTag string `json:"template_tag"` //模板标签 + Logo string `json:"logo"` } type ProductInfo struct { diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index 89019534..d38e4b12 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -70,6 +70,8 @@ type CartItem { IsInvalid bool `json:"is_invalid"` //是否无效 InvalidDescription string `json:"invalid_description"` //无效原因 IsSelected bool `json:"is_selected"` //是否选中 + TemplateTag string `json:"template_tag"` //模板标签 + Logo string `json:"logo"` } type ProductInfo { ProductId int64 `json:"product_id"` //产品id diff --git a/service/repositories/shopping-cart.go b/service/repositories/shopping-cart.go index dd2b83eb..afc7e18f 100644 --- a/service/repositories/shopping-cart.go +++ b/service/repositories/shopping-cart.go @@ -3,15 +3,10 @@ package repositories import ( "encoding/json" "errors" - "fmt" "fusenapi/model/gmodel" - "fusenapi/utils/format" "fusenapi/utils/hash" - "fusenapi/utils/step_price" "github.com/aws/aws-sdk-go/aws/session" - "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" - "math" "strings" ) @@ -33,7 +28,7 @@ type ( // 校验订单 VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error //计算购物车价格 - CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error) + CaculateStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error) } ) @@ -132,33 +127,23 @@ func (d *defaultShoppingCart) VerifyShoppingCartSnapshotDataChange(req VerifySho } // 计算价格 -func (d *defaultShoppingCart) CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error) { - //阶梯数量切片 - stepNum, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepNum, ",")) - if err != nil { - logx.Error(err) - return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step number:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) +func (d *defaultShoppingCart) CaculateStepPrice(purchaseQuantity int64, stepPrice gmodel.StepPriceJsonStruct, fittingPrice int64) (totalPrice, itemPrice int64, err error) { + l := len(stepPrice.PriceRange) + if l == 0 { + return 0, 0, errors.New("price range is not set") } - lenStepNum := len(stepNum) - //阶梯价格切片 - stepPrice, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepPrice, ",")) - if err != nil { - logx.Error(err) - return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step price:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + //遍历查询合适的价格 + for k, v := range stepPrice.PriceRange { + //购买数量>起点 + if purchaseQuantity > v.StartQuantity { + //最后一个 || 小于等于终点 + if k == l-1 || purchaseQuantity <= v.EndQuantity { + itemPrice = v.Price + fittingPrice + return itemPrice * purchaseQuantity, itemPrice, nil + } + } } - lenStepPrice := len(stepPrice) - if lenStepPrice == 0 || lenStepNum == 0 { - return 0, 0, nil, nil, errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) - } - //请求的数量 - reqPurchaseQuantity := purchaseQuantity - //购买箱数 - boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*productPrice.EachBoxNum))) - //根据数量获取阶梯价格中对应的价格 - itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) - //如果有配件,单价也要加入配件价格 - itemPrice += fittingPrice - //单个购物车总价 - totalPrice = itemPrice * reqPurchaseQuantity - return itemPrice, totalPrice, stepNum, stepPrice, nil + //遍历里面没有则返回第一个 + itemPrice = stepPrice.PriceRange[0].Price + fittingPrice + return itemPrice * purchaseQuantity, itemPrice, nil }