From da8f88b3e69f2dad42f0d9e445019ed0bc1acc49 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 15 Sep 2023 16:30:01 +0800 Subject: [PATCH] fix --- model/gmodel/fs_product_logic.go | 5 ++- model/gmodel/fs_product_price_logic.go | 6 ++-- .../internal/logic/getsizebyproductlogic.go | 12 +++---- .../internal/logic/getcartslogic.go | 33 +++++++++++++------ server/shopping-cart/internal/types/types.go | 8 ++--- server_api/shopping-cart.api | 7 ++-- .../verify_shopping_cart_channged.go | 7 +++- 7 files changed, 46 insertions(+), 32 deletions(-) diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index 8e7cca3c..dcd6d497 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -18,12 +18,15 @@ func (p *FsProductModel) FindOneBySn(ctx context.Context, sn string, fields ...s err = db.Take(&resp).Error return resp, err } -func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string) (resp []FsProduct, err error) { +func (p *FsProductModel) GetProductListByIds(ctx context.Context, productIds []int64, sort string, fields ...string) (resp []FsProduct, err error) { if len(productIds) == 0 { return } db := p.db.Model(&FsProduct{}).WithContext(ctx). Where("`id` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?", productIds, 0, 1, 1) + if len(fields) > 0 { + db = db.Select(fields[0]) + } switch sort { case "sort-asc": db = db.Order("`sort` ASC") diff --git a/model/gmodel/fs_product_price_logic.go b/model/gmodel/fs_product_price_logic.go index 65e61a3f..a4bee9cd 100755 --- a/model/gmodel/fs_product_price_logic.go +++ b/model/gmodel/fs_product_price_logic.go @@ -23,11 +23,11 @@ func (p *FsProductPriceModel) GetSimplePriceListByProductIds(ctx context.Context } return } -func (p *FsProductPriceModel) GetPriceListBySizeIds(ctx context.Context, sizeIds []int64) (resp []FsProductPrice, err error) { - if len(sizeIds) == 0 { +func (p *FsProductPriceModel) GetPriceListByProductIdsSizeIds(ctx context.Context, productIds, sizeIds []int64) (resp []FsProductPrice, err error) { + if len(sizeIds) == 0 || len(productIds) == 0 { return } - err = p.db.WithContext(ctx).Model(&FsProductPrice{}).Where("`size_id` in (?) and `status` = ? ", sizeIds, 1).Find(&resp).Error + err = p.db.WithContext(ctx).Model(&FsProductPrice{}).Where("`size_id` in (?) and `product_id` in (?) and `status` = ? ", sizeIds, productIds, 1).Find(&resp).Error if err != nil { return nil, err } diff --git a/server/product/internal/logic/getsizebyproductlogic.go b/server/product/internal/logic/getsizebyproductlogic.go index 5e88ef39..f8e56a96 100644 --- a/server/product/internal/logic/getsizebyproductlogic.go +++ b/server/product/internal/logic/getsizebyproductlogic.go @@ -74,14 +74,14 @@ func (l *GetSizeByProductLogic) GetSizeByProduct(req *types.Request, userinfo *a } //获取价格列表 productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn) - productPriceList, err := productPriceModel.GetPriceListBySizeIds(l.ctx, sizeIds) + productPriceList, err := productPriceModel.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list") } - mapProductPrice := make(map[int64]gmodel.FsProductPrice) + mapProductPrice := make(map[string]gmodel.FsProductPrice) for _, v := range productPriceList { - mapProductPrice[*v.SizeId] = v + mapProductPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v } //组装返回 list := make([]types.GetSizeByProductRsp, 0, len(tagsList)) @@ -103,7 +103,7 @@ func (l *GetSizeByProductLogic) GetSizeByProduct(req *types.Request, userinfo *a } // 第一层子层 -func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenList []types.Children, err error) { +func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[string]gmodel.FsProductPrice) (childrenList []types.Children, err error) { childrenList = make([]types.Children, 0, len(productList)) for _, product := range productList { if *product.Type != tag.Id { @@ -126,14 +126,14 @@ func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productL } // 第2层子层 -func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[int64]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) { +func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[string]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) { childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList)) for _, productSize := range productSizeList { if product.Id != *productSize.ProductId { continue } priceList := make([]types.PriceObj, 0, len(productSizeList)) - price, ok := mapProductPrice[productSize.Id] + price, ok := mapProductPrice[fmt.Sprintf("%d_%d", *productSize.ProductId, productSize.Id)] //无对应尺寸价格 if !ok { priceList = []types.PriceObj{ diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index 4b070b5d..0a52ba1e 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -69,7 +69,8 @@ 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[int64]gmodel.FsProductPrice) + mapSizePrice = make(map[string]gmodel.FsProductPrice) + mapProduct = make(map[int64]struct{}) ) //获取相关信息 err = l.GetRelationInfo(GetRelationInfoReq{ @@ -78,6 +79,7 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo MapModel: mapModel, MapTemplate: mapTemplate, MapSizePrice: mapSizePrice, + MapProduct: mapProduct, }) if err != nil { return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) @@ -93,6 +95,7 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo MapTemplate: mapTemplate, MapCartChange: mapCartChange, MapSnapshot: mapSnapshot, + MapProduct: mapProduct, }) if err != nil { logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error()) @@ -102,7 +105,7 @@ 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[*cart.SizeId] + 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)) } @@ -126,15 +129,12 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo //购买箱数 boxQuantity := int(math.Ceil(float64(*cart.PurchaseQuantity) / float64(*sizePrice.EachBoxNum))) //获取阶梯数量 - stepQuantityList := make([]types.StepNumItem, 0, 10) + stepQuantityList := make([]int64, 0, 20) tmpMinBuyNum := *sizePrice.MinBuyNum for tmpMinBuyNum < (int64(stepNum[lenStepNum-1]) + 5) { //阶梯数 tmpQuantity := tmpMinBuyNum * (*sizePrice.EachBoxNum) - stepQuantityList = append(stepQuantityList, types.StepNumItem{ - PurchaseQuantity: tmpQuantity, - IsSelected: *cart.PurchaseQuantity == tmpQuantity, - }) + stepQuantityList = append(stepQuantityList, tmpQuantity) tmpMinBuyNum++ } //根据数量获取阶梯价格中对应的价格 @@ -169,6 +169,7 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo Qrcode: snapShot.UserDiyInformation.Qrcode, Slogan: snapShot.UserDiyInformation.Slogan, }, + PurchaseQuantity: *cart.PurchaseQuantity, StepNum: stepQuantityList, IsInvalid: false, InvalidDescription: "", @@ -197,7 +198,8 @@ type GetRelationInfoReq struct { MapSize map[int64]gmodel.FsProductSize MapModel map[int64]gmodel.FsProductModel3d MapTemplate map[int64]gmodel.FsProductTemplateV2 - MapSizePrice map[int64]gmodel.FsProductPrice + MapSizePrice map[string]gmodel.FsProductPrice + MapProduct map[int64]struct{} } func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error { @@ -205,10 +207,21 @@ func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error { templateIds := make([]int64, 0, lenCarts) modelIds := make([]int64, 0, lenCarts) //模型 + 配件 sizeIds := make([]int64, 0, lenCarts) + productIds := make([]int64, 0, lenCarts) for index := range req.Carts { templateIds = append(templateIds, *req.Carts[index].TemplateId) modelIds = append(modelIds, *req.Carts[index].ModelId, *req.Carts[index].FittingId) sizeIds = append(sizeIds, *req.Carts[index].SizeId) + productIds = append(productIds, *req.Carts[index].ProductId) + } + //获取产品集合 + productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, productIds, "", "id") + if err != nil { + logx.Error(err) + return errors.New("failed to get product list") + } + for _, v := range productList { + req.MapProduct[v.Id] = struct{}{} } //获取尺寸列表 sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByIds(l.ctx, sizeIds, "") @@ -238,13 +251,13 @@ func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error { req.MapTemplate[v.Id] = v } //根据sizeid获取价格列表 - priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListBySizeIds(l.ctx, sizeIds) + 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[*v.SizeId] = v + 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 c692ba35..db117d2a 100644 --- a/server/shopping-cart/internal/types/types.go +++ b/server/shopping-cart/internal/types/types.go @@ -50,16 +50,12 @@ type CartItem struct { ItemPrice string `json:"item_price"` //单价 TotalPrice string `json:"total_price"` //单价X数量=总价 DiyInformation DiyInformation `json:"diy_information"` //diy信息 - StepNum []StepNumItem `json:"step_num"` //阶梯数量 + StepNum []int64 `json:"step_num"` //阶梯数量 + PurchaseQuantity int64 `json:"purchase_quantity"` //当前购买数量 IsInvalid bool `json:"is_invalid"` //是否无效 InvalidDescription string `json:"invalid_description"` //无效原因 } -type StepNumItem struct { - PurchaseQuantity int64 `json:"purchase_quantity"` //数量 - IsSelected bool `json:"is_selected"` //是否选中 -} - type SizeInfo struct { SizeId int64 `json:"size_id"` //尺寸id Capacity string `json:"capacity"` //尺寸名称 diff --git a/server_api/shopping-cart.api b/server_api/shopping-cart.api index 495e46d1..27249f45 100644 --- a/server_api/shopping-cart.api +++ b/server_api/shopping-cart.api @@ -66,14 +66,11 @@ type CartItem { ItemPrice string `json:"item_price"` //单价 TotalPrice string `json:"total_price"` //单价X数量=总价 DiyInformation DiyInformation `json:"diy_information"` //diy信息 - StepNum []StepNumItem `json:"step_num"` //阶梯数量 + StepNum []int64 `json:"step_num"` //阶梯数量 + PurchaseQuantity int64 `json:"purchase_quantity"` //当前购买数量 IsInvalid bool `json:"is_invalid"` //是否无效 InvalidDescription string `json:"invalid_description"` //无效原因 } -type StepNumItem { - PurchaseQuantity int64 `json:"purchase_quantity"` //数量 - IsSelected bool `json:"is_selected"` //是否选中 -} type SizeInfo { SizeId int64 `json:"size_id"` //尺寸id Capacity string `json:"capacity"` //尺寸名称 diff --git a/utils/shopping_cart/verify_shopping_cart_channged.go b/utils/shopping_cart/verify_shopping_cart_channged.go index d237e5ec..10315ffe 100644 --- a/utils/shopping_cart/verify_shopping_cart_channged.go +++ b/utils/shopping_cart/verify_shopping_cart_channged.go @@ -15,10 +15,16 @@ type VerifyShoppingCartSnapshotDataChangeReq struct { MapTemplate map[int64]gmodel.FsProductTemplateV2 MapCartChange map[int64]string MapSnapshot map[int64]CartSnapshot + MapProduct map[int64]struct{} } func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error { for _, cartInfo := range req.Carts { + descrptionBuilder := strings.Builder{} + //产品下架/删除 + if _, ok := req.MapProduct[*cartInfo.ProductId]; !ok { + descrptionBuilder.WriteString("

the product is off shelf or deleted

") + } var snapShotParseInfo CartSnapshot if err := json.Unmarshal([]byte(*cartInfo.Snapshot), &snapShotParseInfo); err != nil { return err @@ -30,7 +36,6 @@ func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChan snapshotModelJsonHash := hash.JsonHashKey(snapShotParseInfo.ModelInfo.ModelJson) //快照中配件设计json数据哈希值 snapshotFittingJsonHash := hash.JsonHashKey(snapShotParseInfo.FittingInfo.FittingJson) - descrptionBuilder := strings.Builder{} //有模板验证模板相关 if *cartInfo.TemplateId > 0 { if curTemplateInfo, ok := req.MapTemplate[*cartInfo.TemplateId]; !ok {