fusenapi/server/shopping-cart/internal/logic/getcartslogic.go
laodaming d113ef1c6e fix
2023-10-07 11:24:18 +08:00

306 lines
9.9 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")
}
currentPage := constants.DEFAULT_PAGE
limit := 300
//获取用户购物车列表
var cartIds []int64
if req.CartId > 0 {
cartIds = append(cartIds, req.CartId)
}
carts, total, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
Ids: cartIds,
UserId: userinfo.UserId,
Sort: "id DESC",
Page: currentPage,
Limit: limit,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get your shopping carts")
}
var (
mapSize = make(map[int64]gmodel.FsProductSize)
mapModel = make(map[int64]gmodel.FsProductModel3d)
mapTemplate = make(map[int64]gmodel.FsProductTemplateV2)
mapProduct = make(map[int64]gmodel.FsProduct)
mapResourceMetadata = make(map[string]interface{})
)
//获取相关信息
err = l.GetRelationInfo(GetRelationInfoReq{
Carts: carts,
MapSize: mapSize,
MapModel: mapModel,
MapTemplate: mapTemplate,
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]
modelInfo, ok := mapModel[*cart.ModelId]
if !ok {
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s model info is not exists:%d_%d", *cart.ProductId, *cart.SizeId))
}
var stepPrice gmodel.StepPriceJsonStruct
if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 {
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse model step price:%d", *cart.ModelId))
}
}
//购买数量步进量
stepPurchaseQuantity := *modelInfo.PackedUnit
//如果有配件,单价也要加入配件价格
fittingPrice := int64(0)
if *cart.FittingId > 0 {
curFittingInfo, ok := mapModel[*cart.FittingId]
if !ok {
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId))
}
fittingPrice = *curFittingInfo.Price
//取大的为步进量基数
if *curFittingInfo.PackedUnit > stepPurchaseQuantity {
stepPurchaseQuantity = *curFittingInfo.PackedUnit
}
}
//计算阶梯价格
totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(*cart.PurchaseQuantity, stepPrice, 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{}
cartIsSelected := *cart.IsSelected > 0
//产品信息
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
}
//产品下架了
if *productInfo.IsShelf == 0 {
cartIsSelected = false
}
}
templateTag := ""
if templateInfo, ok := mapTemplate[*cart.TemplateId]; ok {
templateTag = *templateInfo.TemplateTag
}
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: stepPrice.MinBuyUnitsNum,
StepPurchaseQuantity: stepPurchaseQuantity,
IsHighlyCustomized: *cart.IsHighlyCustomized > 0,
IsSelected: cartIsSelected,
TemplateTag: templateTag,
Logo: snapShot.Logo,
RenderImage: snapShot.RenderImage,
}
//是否有失效的
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: 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
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
}
return nil
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }