From 1a7ce5013e4f3558e0180b96ea20a98b94409565 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 12:31:51 +0800 Subject: [PATCH 1/6] fix --- model/gmodel/fs_product_model3d_logic.go | 8 +- .../internal/logic/calculatecartpricelogic.go | 84 +++++++++++++++++-- .../internal/logic/getcartslogic.go | 9 +- server/shopping-cart/internal/types/types.go | 1 + server_api/shopping-cart.api | 1 + service/repositories/order.go | 3 +- 6 files changed, 90 insertions(+), 16 deletions(-) diff --git a/model/gmodel/fs_product_model3d_logic.go b/model/gmodel/fs_product_model3d_logic.go index 5ff31216..74ffe4e6 100755 --- a/model/gmodel/fs_product_model3d_logic.go +++ b/model/gmodel/fs_product_model3d_logic.go @@ -37,11 +37,15 @@ func (d *FsProductModel3dModel) GetAllByIdsWithoutStatus(ctx context.Context, id err = db.Find(&resp).Error return resp, err } -func (d *FsProductModel3dModel) GetAllByIdsTag(ctx context.Context, ids []int64, tag int64) (resp []FsProductModel3d, err error) { +func (d *FsProductModel3dModel) GetAllByIdsTag(ctx context.Context, ids []int64, tag int64, fields ...string) (resp []FsProductModel3d, err error) { if len(ids) == 0 { return } - err = d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ? and `tag` = ?", ids, 1, tag).Find(&resp).Error + db := d.db.WithContext(ctx).Model(&FsProductModel3d{}).Where("`id` in (?) and `status` = ? and `tag` = ?", ids, 1, tag) + if len(fields) > 0 { + db = db.Select(fields[0]) + } + err = db.Find(&resp).Error return resp, err } diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 0c35521d..3b68a93c 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -3,9 +3,14 @@ package logic import ( "context" "fmt" + "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" + "fusenapi/utils/format" + "fusenapi/utils/step_price" + "math" + "strings" "fusenapi/server/shopping-cart/internal/svc" "fusenapi/server/shopping-cart/internal/types" @@ -32,6 +37,7 @@ func NewCalculateCartPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) // } func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { + userinfo.UserId = 39 if !userinfo.IsUser() { return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in") } @@ -39,13 +45,18 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{CalculateResultList: []types.CalculateResultItem{}}) } cartIds := make([]int64, 0, len(req.CalculateList)) + mapCalculateQuantity := make(map[int64]int64) for _, v := range req.CalculateList { cartIds = append(cartIds, v.CartId) + if v.PurchaseQuantity <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "purchase quantity must grater than 0") + } + mapCalculateQuantity[v.CartId] = v.PurchaseQuantity } //获取购物车列表 carts, _, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{ Ids: cartIds, - Fields: "id,size_id,product_id", + Fields: "id,size_id,product_id,fitting_id", UserId: userinfo.UserId, Page: 1, Limit: len(cartIds), @@ -54,6 +65,9 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get cart list") } + if len(carts) < len(req.CalculateList) { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "please refresh page for the shopping cart has changed!!") + } sizeIds := make([]int64, 0, len(carts)) productIds := make([]int64, 0, len(carts)) fittingIds := make([]int64, 0, len(carts)) @@ -70,14 +84,68 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list") } - mapPrice := make(map[string]int) - for k, v := range priceList { - mapPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = k + mapPrice := make(map[string]gmodel.FsProductPrice) + for _, v := range priceList { + mapPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v } - //获取配件列表 - // todo 下周写 - /*fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag()*/ - return resp.SetStatus(basic.CodeOK) + //获取配件列表(只有id跟价格) + fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag(l.ctx, fittingIds, constants.TAG_PARTS, "id,price") + if err != nil { + logx.Error(err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting list") + } + mapFitting := make(map[int64]int64) + for _, v := range fittingList { + mapFitting[v.Id] = *v.Price + } + //开始计算价格 + calculateResultList := make([]types.CalculateResultItem, 0, len(req.CalculateList)) + sumPrice := 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 + } + } + //单个购物车总价 + 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)), + }) + sumPrice += totalPrice + } + return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{ + SumPrice: fmt.Sprintf("%.3f", format.CentitoDollar(sumPrice)), + CalculateResultList: calculateResultList, + }) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index 2ed45ff5..303716f4 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -41,7 +41,6 @@ func NewGetCartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCarts // } func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo) (resp *basic.Response) { - userinfo.UserId = 39 if req.CurrentPage <= 0 { req.CurrentPage = constants.DEFAULT_PAGE } @@ -112,24 +111,24 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo snapShot := mapSnapshot[cart.Id] sizePrice, ok := mapSizePrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)] if !ok { - return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d", *cart.SizeId)) + return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%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", *cart.SizeId)) + 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", *cart.SizeId)) + 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 ", *cart.SizeId)) + 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(*cart.PurchaseQuantity) / float64(*sizePrice.EachBoxNum))) diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go index ec1ed92b..c8630940 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -98,6 +98,7 @@ type CalculateItem struct { } type CalculateCartPriceRsp struct { + SumPrice string `json:"sum_price"` CalculateResultList []CalculateResultItem `json:"calculate_result_list"` } diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index bd595d74..523e734f 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -111,6 +111,7 @@ type CalculateItem { PurchaseQuantity int64 `json:"purchase_quantity"` //数量 } type CalculateCartPriceRsp { + SumPrice string `json:"sum_price"` CalculateResultList []CalculateResultItem `json:"calculate_result_list"` } type CalculateResultItem { diff --git a/service/repositories/order.go b/service/repositories/order.go index b92668b8..02cbe08b 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "fusenapi/model/gmodel" "fusenapi/utils/basic" "fusenapi/utils/shopping_cart" @@ -157,7 +158,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe // 商品总价 productTotalPrice := productPrice * *shoppingCart.PurchaseQuantity - + fmt.Println(productTotalPrice) // 存储订单商品 orderProductList = append(orderProductList, &gmodel.OrderProduct{}) } From 5e2760849c4eb188a1d90b97f81b0490f92734f4 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 14:23:57 +0800 Subject: [PATCH 2/6] fix --- .../internal/logic/getproducttemplatetagslogic.go | 3 +++ server/product-template-tag/internal/types/types.go | 9 +++++---- server_api/product-template-tag.api | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go index 4a24ce61..c0384d90 100644 --- a/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go +++ b/server/product-template-tag/internal/logic/getproducttemplatetagslogic.go @@ -54,6 +54,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu logx.Error(err) return } + var colors interface{} if logoInfo.Metadata == nil || *logoInfo.Metadata == "" { // 返回固定模板A1a productTemplateTags, err = l.svcCtx.AllModels.FsProductTemplateTags.GetListByTagNames(l.ctx, []string{"A1"}, req.Limit, 1, "`id` DESC") @@ -68,6 +69,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu logx.Error(err) return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse user metadata") } + colors = metaData["colors"] var templateTagNameList []string b, _ := json.Marshal(metaData["template_tagid"]) if err = json.Unmarshal(b, &templateTagNameList); err != nil { @@ -106,6 +108,7 @@ func (l *GetProductTemplateTagsLogic) GetProductTemplateTags(req *types.GetProdu TemplateTag: *v.TemplateTag, Cover: *v.Cover, CoverMetadata: mapResourceMetadata[*v.Cover], + Colors: colors, }) } return resp.SetStatusWithMessage(basic.CodeOK, "success", list) diff --git a/server/product-template-tag/internal/types/types.go b/server/product-template-tag/internal/types/types.go index 6c7f6950..e81cb040 100644 --- a/server/product-template-tag/internal/types/types.go +++ b/server/product-template-tag/internal/types/types.go @@ -14,6 +14,7 @@ type GetProductTemplateTagsRsp struct { TemplateTag string `json:"template_tag"` Cover string `json:"cover"` CoverMetadata interface{} `json:"cover_metadata"` + Colors interface{} `json:"colors"` } type Request struct { @@ -39,10 +40,10 @@ type File struct { } type Meta struct { - TotalCount int64 `json:"totalCount"` - PageCount int64 `json:"pageCount"` - CurrentPage int `json:"currentPage"` - PerPage int `json:"perPage"` + TotalCount int64 `json:"total_count"` + PageCount int64 `json:"page_count"` + CurrentPage int `json:"current_page"` + PerPage int `json:"per_page"` } // Set 设置Response的Code和Message值 diff --git a/server_api/product-template-tag.api b/server_api/product-template-tag.api index bdc513b7..d663e109 100644 --- a/server_api/product-template-tag.api +++ b/server_api/product-template-tag.api @@ -24,4 +24,5 @@ type GetProductTemplateTagsRsp { TemplateTag string `json:"template_tag"` Cover string `json:"cover"` CoverMetadata interface{} `json:"cover_metadata"` + Colors interface{} `json:"colors"` } \ No newline at end of file From 43f394250820aeceabf4e033a2e140e906e417a8 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 14:27:55 +0800 Subject: [PATCH 3/6] fix --- service/repositories/order.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service/repositories/order.go b/service/repositories/order.go index b92668b8..02cbe08b 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "fusenapi/model/gmodel" "fusenapi/utils/basic" "fusenapi/utils/shopping_cart" @@ -157,7 +158,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe // 商品总价 productTotalPrice := productPrice * *shoppingCart.PurchaseQuantity - + fmt.Println(productTotalPrice) // 存储订单商品 orderProductList = append(orderProductList, &gmodel.OrderProduct{}) } From 9476414e68226424afaff5a141f26e7da02603ab Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 14:53:15 +0800 Subject: [PATCH 4/6] fix --- server/shopping-cart/internal/logic/calculatecartpricelogic.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 3b68a93c..8a9fe327 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -131,6 +131,8 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri 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) } } //单个购物车总价 From ceec00c5286d365bdb3e9948a9589c80be7426ee Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 15:18:55 +0800 Subject: [PATCH 5/6] fix --- .../shopping-cart/internal/logic/calculatecartpricelogic.go | 6 +++--- server/shopping-cart/internal/types/types.go | 2 +- server_api/shopping-cart.api | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 8a9fe327..25102872 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -100,7 +100,7 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri } //开始计算价格 calculateResultList := make([]types.CalculateResultItem, 0, len(req.CalculateList)) - sumPrice := int64(0) + subTotalPrice := int64(0) for _, cart := range carts { sizePrice, ok := mapPrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)] if !ok { @@ -142,10 +142,10 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), }) - sumPrice += totalPrice + subTotalPrice += totalPrice } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{ - SumPrice: fmt.Sprintf("%.3f", format.CentitoDollar(sumPrice)), + SubTotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(subTotalPrice)), CalculateResultList: calculateResultList, }) } diff --git a/server/shopping-cart/internal/types/types.go b/server/shopping-cart/internal/types/types.go index c8630940..1ed8b661 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -98,7 +98,7 @@ type CalculateItem struct { } type CalculateCartPriceRsp struct { - SumPrice string `json:"sum_price"` + SubTotalPrice string `json:"sub_total_price"` CalculateResultList []CalculateResultItem `json:"calculate_result_list"` } diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index 523e734f..216bdfe2 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -111,7 +111,7 @@ type CalculateItem { PurchaseQuantity int64 `json:"purchase_quantity"` //数量 } type CalculateCartPriceRsp { - SumPrice string `json:"sum_price"` + SubTotalPrice string `json:"sub_total_price"` CalculateResultList []CalculateResultItem `json:"calculate_result_list"` } type CalculateResultItem { From 990c7d335992af0649ee42c5c400c4608a63e80f Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Mon, 18 Sep 2023 15:19:42 +0800 Subject: [PATCH 6/6] fix --- server/shopping-cart/internal/logic/calculatecartpricelogic.go | 1 - 1 file changed, 1 deletion(-) diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 25102872..ce0bfc13 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -37,7 +37,6 @@ func NewCalculateCartPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) // } func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { - userinfo.UserId = 39 if !userinfo.IsUser() { return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in") }