package logic import ( "context" "fmt" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" "fusenapi/utils/shopping_cart" "fusenapi/utils/step_price" "math" "strings" "fusenapi/server/shopping-cart/internal/svc" "fusenapi/server/shopping-cart/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetCartsLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetCartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCartsLogic { return &GetCartsLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *GetCartsLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo) (resp *basic.Response) { if req.CurrentPage <= 0 { req.CurrentPage = constants.DEFAULT_PAGE } limit := 10 //获取用户购物车列表 carts, total, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{ UserId: userinfo.UserId, Sort: "id DESC", Page: req.CurrentPage, Limit: limit, }) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get your shopping carts") } if len(carts) == 0 { return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartsRsp{ Meta: types.Meta{ TotalCount: total, PageCount: int64(math.Ceil(float64(total) / float64(limit))), CurrentPage: req.CurrentPage, PerPage: limit, }, 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, "") 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 } //定义map收集变更信息 mapCartChange := make(map[int64]string) mapSnapshot := make(map[int64]shopping_cart.CartSnapshot) //校验购物车数据是否变更 err = shopping_cart.VerifyShoppingCartSnapshotDataChange(shopping_cart.VerifyShoppingCartSnapshotDataChangeReq{ Carts: carts, MapSize: mapSize, MapModel: mapModel, MapTemplate: mapTemplate, MapCartChange: mapCartChange, MapSnapshot: mapSnapshot, }) if err != nil { 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) for _, cart := range carts { snapShot := mapSnapshot[cart.Id] sizePrice, ok := mapSizePrice[*cart.SizeId] if !ok { return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d", *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)) } 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)) } 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)) } //购买箱数 boxQuantity := int(math.Ceil(float64(*cart.PurchaseQuantity) / float64(*sizePrice.EachBoxNum))) //获取阶梯数量 stepQuantityList := make([]types.StepNumItem, 0, 10) 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, }) tmpMinBuyNum++ } //根据数量获取阶梯价格中对应的价格 itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) //如果有配件,单价也要加入配件价格 if *cart.FittingId > 0 { if curFittingInfo, ok := mapModel[*cart.FittingId]; ok { itemPrice += *curFittingInfo.Price } } totalPrice := itemPrice * (*cart.PurchaseQuantity) item := types.CartItem{ ProductId: *cart.ProductId, SizeInfo: types.SizeInfo{ SizeId: *cart.SizeId, Capacity: snapShot.SizeInfo.Capacity, Title: types.SizeTitle{ Cm: snapShot.SizeInfo.Cm, Inch: snapShot.SizeInfo.Inch, }, }, FittingInfo: types.FittingInfo{ FittingId: *cart.FittingId, FittingName: snapShot.FittingInfo.FittingName, }, ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), DiyInformation: types.DiyInformation{ Phone: snapShot.UserDiyInformation.Phone, Address: snapShot.UserDiyInformation.Address, Website: snapShot.UserDiyInformation.Website, Qrcode: snapShot.UserDiyInformation.Qrcode, Slogan: snapShot.UserDiyInformation.Slogan, }, StepNum: stepQuantityList, IsInvalid: false, InvalidDescription: "", } //是否有失效的 if description, ok := mapCartChange[cart.Id]; ok { item.IsInvalid = true item.InvalidDescription = description } list = append(list, item) } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetCartsRsp{ Meta: types.Meta{ TotalCount: total, PageCount: int64(math.Ceil(float64(total) / float64(limit))), CurrentPage: req.CurrentPage, PerPage: limit, }, CartList: list, }) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }