From 87dd1832797765c6c55a82e573cafffbf17ea7ab Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Sat, 7 Oct 2023 14:24:57 +0800 Subject: [PATCH] fix --- .../logic/calculateproductpricelogic.go | 30 +++++- .../logic/getproductsteppricelogic.go | 96 +++++++------------ server/product/internal/types/types.go | 6 +- server_api/product.api | 6 +- 4 files changed, 65 insertions(+), 73 deletions(-) diff --git a/server/product/internal/logic/calculateproductpricelogic.go b/server/product/internal/logic/calculateproductpricelogic.go index 22bb653a..683e98f6 100644 --- a/server/product/internal/logic/calculateproductpricelogic.go +++ b/server/product/internal/logic/calculateproductpricelogic.go @@ -3,6 +3,7 @@ package logic import ( "encoding/json" "errors" + "fmt" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" @@ -83,15 +84,38 @@ func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateP } fittingPrice = *fittingInfo.Price } + rangeLen := len(stepPrice.PriceRange) + stepRange := make([]interface{}, 0, rangeLen) + for rIndex, rangeInfo := range stepPrice.PriceRange { + //最后一个 + if rIndex+1 == rangeLen { + begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity)) + stepRange = append(stepRange, map[string]interface{}{ + "start": rangeInfo.StartQuantity, + "end": rangeInfo.EndQuantity, + "range_description": fmt.Sprintf(">=%s Units", begin), + "item_price": format.CentitoDollar(rangeInfo.Price+fittingPrice, 3), + }) + break + } + begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity)) + end := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.EndQuantity)) + stepRange = append(stepRange, map[string]interface{}{ + "start": rangeInfo.StartQuantity, + "end": rangeInfo.EndQuantity, + "range_description": fmt.Sprintf("%s-%s Units", begin, end), + "item_price": format.CentitoDollar(rangeInfo.Price+fittingPrice, 3), + }) + } totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to calculate product price ") } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateProductPriceRsp{ - ItemPrice: format.CentitoDollar(itemPrice, 3), - TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2), - PurchaseQuantity: req.PurchaseQuantity, + ItemPrice: format.CentitoDollar(itemPrice, 3), + TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2), + StepRange: stepPrice, }) } diff --git a/server/product/internal/logic/getproductsteppricelogic.go b/server/product/internal/logic/getproductsteppricelogic.go index 8db90850..71a4f8fa 100644 --- a/server/product/internal/logic/getproductsteppricelogic.go +++ b/server/product/internal/logic/getproductsteppricelogic.go @@ -11,6 +11,7 @@ import ( "fusenapi/utils/basic" "fusenapi/utils/format" "gorm.io/gorm" + "strings" "fusenapi/server/product/internal/svc" "fusenapi/server/product/internal/types" @@ -50,15 +51,21 @@ func (l *GetProductStepPriceLogic) GetProductStepPrice(req *types.GetProductStep return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") } //查询产品价格 - modelPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdTag(l.ctx, req.ProductId, constants.TAG_MODEL, "id,size_id,part_id,step_price,packed_unit") + modelPriceList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdTag(l.ctx, req.ProductId, constants.TAG_MODEL, "id,size_id,part_list,part_id,step_price,packed_unit") if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model price list") } fittingIds := make([]int64, 0, len(modelPriceList)) for _, v := range modelPriceList { - if *v.PartId > 0 { - fittingIds = append(fittingIds, *v.PartId) + *v.PartList = strings.Trim(*v.PartList, ",") + if *v.PartList != "" { + tmpPartIds, err := format.StrSlicToInt64Slice(strings.Split(*v.PartList, ",")) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model part list ") + } + fittingIds = append(fittingIds, tmpPartIds...) } } //查询配件价格列表 @@ -73,77 +80,38 @@ func (l *GetProductStepPriceLogic) GetProductStepPrice(req *types.GetProductStep } //遍历处理模型价格 mapRsp := make(map[string]interface{}) - for _, modelPriceInfo := range modelPriceList { + for _, modelInfo := range modelPriceList { var stepPrice gmodel.StepPriceJsonStruct - if err = json.Unmarshal(*modelPriceInfo.StepPrice, &stepPrice); err != nil { + if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price json") } rangeLen := len(stepPrice.PriceRange) if rangeLen == 0 { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price is not set:%d", modelPriceInfo.Id)) + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("step price is not set:%d", modelInfo.Id)) } - //最小单价 - minPrice := stepPrice.PriceRange[rangeLen-1].Price - //最大单价 - maxPrice := stepPrice.PriceRange[0].Price - //购买数步进基数 - stepPurchaseQuantity := *modelPriceInfo.PackedUnit - //购买数步进基数描述 - stepPurchaseQuantityDescription := "主体装箱数为步进基数" - //配件价格 - fittingPrice := int64(0) - if *modelPriceInfo.PartId > 0 { - fittingPriceInfo, ok := mapFitting[*modelPriceInfo.PartId] - if !ok { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("fitting price is not exists:%d", *modelPriceInfo.PartId)) + *modelInfo.PartList = strings.Trim(*modelInfo.PartList, ",") + mapFittingUnit := make(map[string]interface{}) + if *modelInfo.PartList != "" { + tmpPartIds, err := format.StrSlicToInt64Slice(strings.Split(*modelInfo.PartList, ",")) + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse model part list!! ") } - //最小/最大价格要加配件 - fittingPrice = *fittingPriceInfo.Price - minPrice += fittingPrice - maxPrice += fittingPrice - //如果配件装箱基数比主体大,则基数以配件为主 - if *fittingPriceInfo.PackedUnit > stepPurchaseQuantity { - stepPurchaseQuantity = *fittingPriceInfo.PackedUnit - stepPurchaseQuantityDescription = "配件装箱数为步进基数" + for _, partId := range tmpPartIds { + fittingInfo, ok := mapFitting[*modelInfo.PartId] + if !ok { + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("fitting price is not exists:%d", *modelInfo.PartId)) + } + mapFittingUnit[fmt.Sprintf("_%d", partId)] = map[string]interface{}{ + "packed_unit": *fittingInfo.PackedUnit, //装箱个数 + } } } - stepRange := make([]interface{}, 0, rangeLen) - for rIndex, rangeInfo := range stepPrice.PriceRange { - //最后一个 - if rIndex+1 == rangeLen { - begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity)) - stepRange = append(stepRange, map[string]interface{}{ - "start": rangeInfo.StartQuantity, - "end": rangeInfo.EndQuantity, - "range_description": fmt.Sprintf(">=%s Units", begin), - "item_price": format.CentitoDollar(rangeInfo.Price+fittingPrice, 3), - }) - break - } - begin := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.StartQuantity)) - end := format.NumToStringWithThousandthPercentile(fmt.Sprintf("%d", rangeInfo.EndQuantity)) - stepRange = append(stepRange, map[string]interface{}{ - "start": rangeInfo.StartQuantity, - "end": rangeInfo.EndQuantity, - "range_description": fmt.Sprintf("%s-%s Units", begin, end), - "item_price": format.CentitoDollar(rangeInfo.Price+fittingPrice, 3), - }) - } - //计算起购数量的单价 - _, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(stepPrice.MinBuyUnitsNum, stepPrice, fittingPrice) - if err != nil { - return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get min buy quantity item price") - } - mapRsp[fmt.Sprintf("_%d", *modelPriceInfo.SizeId)] = map[string]interface{}{ - "step_purchase_quantity": stepPurchaseQuantity, - "step_purchase_quantity_description": stepPurchaseQuantityDescription, - "min_price": minPrice, - "max_price": maxPrice, - "step_range": stepRange, - "min_buy_units_quantity": stepPrice.MinBuyUnitsNum, - "min_buy_units_quantity_total_price": format.CentitoDollarWithNoHalfAdjust(itemPrice*stepPrice.MinBuyUnitsNum, 2), - "min_buy_units_quantity_item_price": format.CentitoDollar(itemPrice, 3), + mapRsp[fmt.Sprintf("_%d", *modelInfo.SizeId)] = map[string]interface{}{ + "min_buy_units_quantity": stepPrice.MinBuyUnitsNum, //起购数量 + "main_packed_unit": *modelInfo.PackedUnit, //主体装箱数 + "fitting": mapFittingUnit, } } return resp.SetStatusWithMessage(basic.CodeOK, "success", mapRsp) diff --git a/server/product/internal/types/types.go b/server/product/internal/types/types.go index 59bcb8a7..e1951538 100644 --- a/server/product/internal/types/types.go +++ b/server/product/internal/types/types.go @@ -343,9 +343,9 @@ type CalculateProductPriceReq struct { } type CalculateProductPriceRsp struct { - ItemPrice string `json:"item_price"` - TotalPrice string `json:"total_price"` - PurchaseQuantity int64 `json:"purchase_quantity"` + ItemPrice string `json:"item_price"` + TotalPrice string `json:"total_price"` + StepRange interface{} `json:"step_range"` } type GetSizeByPidReq struct { diff --git a/server_api/product.api b/server_api/product.api index 23c3e8fd..68bc4033 100644 --- a/server_api/product.api +++ b/server_api/product.api @@ -394,9 +394,9 @@ type CalculateProductPriceReq { PurchaseQuantity int64 `json:"purchase_quantity"` } type CalculateProductPriceRsp { - ItemPrice string `json:"item_price"` - TotalPrice string `json:"total_price"` - PurchaseQuantity int64 `json:"purchase_quantity"` + ItemPrice string `json:"item_price"` + TotalPrice string `json:"total_price"` + StepRange interface{} `json:"step_range"` } //获取产品尺寸列表 type GetSizeByPidReq {