This commit is contained in:
laodaming
2023-09-26 15:28:17 +08:00
parent a1d78b7a0c
commit 821cbb8879
5 changed files with 63 additions and 78 deletions

View File

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

View File

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