fusenapi/server/shopping-cart/internal/logic/getcartslogic.go
laodaming e8546d615b fix
2023-09-21 18:34:29 +08:00

301 lines
9.8 KiB
Go

package logic
import (
"context"
"encoding/json"
"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"
"fusenapi/utils/s3url_to_s3id"
"math"
"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 !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
if req.CurrentPage <= 0 {
req.CurrentPage = constants.DEFAULT_PAGE
}
limit := 300
//获取用户购物车列表
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,
})
}
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 = l.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")
}
list := make([]types.CartItem, 0, len(carts))
for _, cart := range carts {
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_%d", *cart.ProductId, *cart.SizeId))
}
//如果有配件,单价也要加入配件价格
fittingPrice := int64(0)
if *cart.FittingId > 0 {
if curFittingInfo, ok := mapModel[*cart.FittingId]; ok {
fittingPrice = *curFittingInfo.Price
} else {
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId))
}
}
//计算价格
itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
//尺寸信息
sizeCapacity := snapShot.SizeInfo.Capacity
if sizeInfo, ok := mapSize[*cart.SizeId]; ok {
sizeCapacity = *sizeInfo.Capacity
}
//配件信息
fittingName := snapShot.FittingInfo.FittingName
if fittingInfo, ok := mapModel[*cart.FittingId]; ok {
fittingName = *fittingInfo.Name
}
productCover := "" //产品封面图
productName := snapShot.ProductInfo.ProductName
productSn := snapShot.ProductInfo.ProductSn
//产品封面图资源元数据
var productCoverMetadata interface{}
//产品信息
if productInfo, ok := mapProduct[*cart.ProductId]; ok {
productCover = *productInfo.Cover
productName = *productInfo.Title
productSn = *productInfo.Sn
if metadata, ok := mapResourceMetadata[*productInfo.Cover]; ok {
productCoverMetadata = metadata
}
}
item := types.CartItem{
CartId: cart.Id,
ProductInfo: types.ProductInfo{
ProductId: *cart.ProductId,
ProductName: productName,
ProductSn: productSn,
ProductCover: productCover,
ProductCoverMetadata: productCoverMetadata,
},
SizeInfo: types.SizeInfo{
SizeId: *cart.SizeId,
Capacity: sizeCapacity,
Title: types.SizeTitle{
Cm: snapShot.SizeInfo.Cm,
Inch: snapShot.SizeInfo.Inch,
},
},
FittingInfo: types.FittingInfo{
FittingId: *cart.FittingId,
FittingName: fittingName,
},
ItemPrice: format.CentitoDollar(itemPrice, 3),
TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2),
DiyInformation: types.DiyInformation{
Phone: snapShot.UserDiyInformation.Phone,
Address: snapShot.UserDiyInformation.Address,
Website: snapShot.UserDiyInformation.Website,
Qrcode: snapShot.UserDiyInformation.Qrcode,
Slogan: snapShot.UserDiyInformation.Slogan,
},
PurchaseQuantity: *cart.PurchaseQuantity,
MinPurchaseQuantity: *sizePrice.EachBoxNum * (*sizePrice.MinBuyNum),
StepPurchaseQuantity: *sizePrice.EachBoxNum,
IsHighlyCustomized: *cart.IsHighlyCustomized > 0,
IsSelected: *cart.IsSelected > 0,
}
//是否有失效的
if description, ok := mapCartChange[cart.Id]; ok {
item.IsInvalid = true
item.InvalidDescription = description
//失效了返回给前端也是不选中
item.IsSelected = false
}
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,
})
}
// 获取相关信息
type GetRelationInfoReq struct {
Carts []gmodel.FsShoppingCart
MapSize map[int64]gmodel.FsProductSize
MapModel map[int64]gmodel.FsProductModel3d
MapTemplate map[int64]gmodel.FsProductTemplateV2
MapSizePrice map[string]gmodel.FsProductPrice
MapProduct map[int64]gmodel.FsProduct
MapResourceMetadata map[string]interface{}
}
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)
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, "")
if err != nil {
logx.Error(err)
return errors.New("failed to get product list")
}
resourceIds := make([]string, 0, len(productList))
for _, v := range productList {
req.MapProduct[v.Id] = v
resourceIds = append(resourceIds, s3url_to_s3id.GetS3ResourceIdFormUrl(*v.Cover))
}
//根据resourceUrls找到对应的元数据
resourceMetadataList, err := l.svcCtx.AllModels.FsResource.FindAllByResourceIds(l.ctx, resourceIds)
if err != nil {
logx.Error(err)
return errors.New("failed to get resource list")
}
for _, v := range resourceMetadataList {
var metadata interface{}
if v.Metadata != nil {
_ = json.Unmarshal(*v.Metadata, &metadata)
}
req.MapResourceMetadata[*v.ResourceUrl] = metadata
}
//获取尺寸列表
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.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[fmt.Sprintf("%d_%d", *v.ProductId, *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)
// }