package logic import ( "context" "errors" "fmt" "fusenapi/constants" "fusenapi/model/gmodel" "fusenapi/server/shopping-cart/internal/svc" "fusenapi/server/shopping-cart/internal/types" "fusenapi/service/repositories" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" "gorm.io/gorm" "github.com/zeromicro/go-zero/core/logx" ) type CalculateCartPriceLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewCalculateCartPriceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CalculateCartPriceLogic { return &CalculateCartPriceLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *CalculateCartPriceLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPriceReq, userinfo *auth.UserInfo) (resp *basic.Response) { if !userinfo.IsUser() { return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in") } if len(req.CalculateList) == 0 { return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{CalculateResultList: []types.CalculateResultItem{}}) } cartIds := make([]int64, 0, len(req.CalculateList)) mapCalculateQuantity := make(map[int64]types.CalculateItem) 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 } //获取购物车列表 carts, _, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{ Ids: cartIds, UserId: userinfo.UserId, Page: 1, Limit: len(cartIds), }) if err != nil { 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!!") } //**************获取相关数据校验变更************ var ( mapSize = make(map[int64]gmodel.FsProductSize) mapModel = make(map[int64]gmodel.FsProductModel3d) mapTemplate = make(map[int64]gmodel.FsProductTemplateV2) mapSizePrice = make(map[string]gmodel.FsProductPrice) mapProduct = make(map[int64]gmodel.FsProduct) mapResourceMetadata = make(map[string]interface{}) ) //获取相关信息 err = NewGetCartsLogic(l.ctx, l.svcCtx).GetRelationInfo(GetRelationInfoReq{ Carts: carts, MapSize: mapSize, MapModel: mapModel, MapTemplate: mapTemplate, MapSizePrice: mapSizePrice, MapProduct: mapProduct, MapResourceMetadata: mapResourceMetadata, }) if err != nil { return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) } //定义map收集变更信息 mapCartChange := make(map[int64]string) mapSnapshot := make(map[int64]gmodel.CartSnapshot) //校验购物车数据是否变更 err = l.svcCtx.Repositories.NewShoppingCart.VerifyShoppingCartSnapshotDataChange(repositories.VerifyShoppingCartSnapshotDataChangeReq{ Carts: carts, MapSize: mapSize, MapModel: mapModel, MapTemplate: mapTemplate, MapCartChange: mapCartChange, MapSnapshot: mapSnapshot, MapProduct: mapProduct, }) if err != nil { logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error()) return resp.SetStatusWithMessage(basic.CodeServiceErr, "system err:failed to check shopping cart change data") } //************************** sizeIds := make([]int64, 0, len(carts)) productIds := make([]int64, 0, len(carts)) fittingIds := make([]int64, 0, len(carts)) for _, v := range carts { sizeIds = append(sizeIds, *v.SizeId) productIds = append(productIds, *v.ProductId) if *v.FittingId > 0 { fittingIds = append(fittingIds, *v.FittingId) } } //根据sizeid获取价格列表 priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list") } mapPrice := make(map[string]gmodel.FsProductPrice) for _, v := range priceList { mapPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v } //获取配件列表(只有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)) subTotalPrice := int64(0) //开启事物 err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error { shoppingCartModel := gmodel.NewFsShoppingCartModel(tx) for _, cart := range carts { sizePrice, ok := mapPrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)] if !ok { return errors.New(fmt.Sprintf("there carts contain some one which have no price info:%d_%d", *cart.ProductId, *cart.SizeId)) } //请求的数量 reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity //传入数量是<= 0则取数据库的 if reqPurchaseQuantity <= 0 { reqPurchaseQuantity = *cart.PurchaseQuantity } isSelected := int64(0) if mapCalculateQuantity[cart.Id].IsSelected { isSelected = 1 } //如果有配件,单价也要加入配件价格 fittingPrice := int64(0) if *cart.FittingId > 0 { if fPrice, ok := mapFitting[*cart.FittingId]; ok { fittingPrice = fPrice } else { return errors.New(fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId)) } } //计算价格 itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(reqPurchaseQuantity, &sizePrice, fittingPrice) if err != nil { logx.Error(err) return err } calculateResultList = append(calculateResultList, types.CalculateResultItem{ CartId: cart.Id, ItemPrice: fmt.Sprintf("%.3f", format.CentitoDollar(itemPrice)), TotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(totalPrice)), }) updData := &gmodel.FsShoppingCart{ PurchaseQuantity: &reqPurchaseQuantity, } //如果是没有变更且是选中则累加总价 if _, ok = mapCartChange[cart.Id]; !ok && isSelected == 1 { subTotalPrice += totalPrice updData.IsSelected = &isSelected } //更新购物车购买数量 if err = shoppingCartModel.Update(l.ctx, cart.Id, userinfo.UserId, updData); err != nil { logx.Error(err) return errors.New(fmt.Sprintf("failed to update cart`s purchase quantity:%d", cart.Id)) } } return nil }) if err != nil { return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error()) } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{ SubTotalPrice: fmt.Sprintf("%.3f", format.CentitoDollar(subTotalPrice)), CalculateResultList: calculateResultList, }) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *CalculateCartPriceLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }