diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index 8f66c64c..dc9f4499 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -2,6 +2,7 @@ package logic import ( "context" + "errors" "fmt" "fusenapi/constants" "fusenapi/model/gmodel" @@ -38,6 +39,7 @@ 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 } @@ -64,44 +66,22 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo CartList: nil, }) } - lenCarts := len(carts) - templateIds := make([]int64, 0, lenCarts) - modelIds := make([]int64, 0, lenCarts) //模型 + 配件 - sizeIds := make([]int64, 0, lenCarts) - for index := range carts { - templateIds = append(templateIds, *carts[index].TemplateId) - modelIds = append(modelIds, *carts[index].ModelId, *carts[index].FittingId) - sizeIds = append(sizeIds, *carts[index].SizeId) - } - //获取尺寸列表 - sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByIds(l.ctx, sizeIds, "") + var ( + mapSize = make(map[int64]gmodel.FsProductSize) + mapModel = make(map[int64]gmodel.FsProductModel3d) + mapTemplate = make(map[int64]gmodel.FsProductTemplateV2) + mapSizePrice = make(map[int64]gmodel.FsProductPrice) + ) + //获取相关信息 + err = l.GetRelationInfo(GetRelationInfoReq{ + Carts: carts, + MapSize: mapSize, + MapModel: mapModel, + MapTemplate: mapTemplate, + MapSizePrice: mapSizePrice, + }) if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list") - } - mapSize := make(map[int64]gmodel.FsProductSize) - for _, v := range sizeList { - mapSize[v.Id] = v - } - //获取模型和配件信息 - modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "") - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list") - } - mapModel := make(map[int64]gmodel.FsProductModel3d) - for _, v := range modelList { - mapModel[v.Id] = v - } - //获取模板列表 - templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIds(l.ctx, templateIds) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template list") - } - mapTemplate := make(map[int64]gmodel.FsProductTemplateV2) - for _, v := range templateList { - mapTemplate[v.Id] = v + return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) } //定义map收集变更信息 mapCartChange := make(map[int64]string) @@ -119,17 +99,8 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error()) return resp.SetStatusWithMessage(basic.CodeServiceErr, "system err:failed to check shopping cart change data") } - //根据sizeid获取价格列表 - priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListBySizeIds(l.ctx, sizeIds) - if err != nil { - logx.Error(err) - return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get cart`s product price list") - } - mapSizePrice := make(map[int64]gmodel.FsProductPrice) - for _, v := range priceList { - mapSizePrice[*v.SizeId] = v - } - list := make([]types.CartItem, 0, lenCarts) + + list := make([]types.CartItem, 0, len(carts)) for _, cart := range carts { snapShot := mapSnapshot[cart.Id] sizePrice, ok := mapSizePrice[*cart.SizeId] @@ -221,6 +192,64 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo }) } +// 获取相关信息 +type GetRelationInfoReq struct { + Carts []gmodel.FsShoppingCart + MapSize map[int64]gmodel.FsProductSize + MapModel map[int64]gmodel.FsProductModel3d + MapTemplate map[int64]gmodel.FsProductTemplateV2 + MapSizePrice map[int64]gmodel.FsProductPrice +} + +func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error { + lenCarts := len(req.Carts) + templateIds := make([]int64, 0, lenCarts) + modelIds := make([]int64, 0, lenCarts) //模型 + 配件 + sizeIds := 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) + } + //获取尺寸列表 + sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByIds(l.ctx, sizeIds, "") + if err != nil { + logx.Error(err) + return errors.New("failed to get size list") + } + for _, v := range sizeList { + req.MapSize[v.Id] = v + } + //获取模型和配件信息 + modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "") + if err != nil { + logx.Error(err) + return errors.New("failed to get model list") + } + for _, v := range modelList { + req.MapModel[v.Id] = v + } + //获取模板列表 + templateList, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByIds(l.ctx, templateIds) + if err != nil { + logx.Error(err) + return errors.New("failed to get template list") + } + for _, v := range templateList { + req.MapTemplate[v.Id] = v + } + //根据sizeid获取价格列表 + priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListBySizeIds(l.ctx, 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 + } + return nil +} + // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp)