fusenapi/server/shopping-cart/internal/logic/getcartslogic.go

292 lines
9.7 KiB
Go
Raw Normal View History

2023-09-13 09:33:58 +00:00
package logic
import (
2023-09-15 02:23:40 +00:00
"context"
2023-09-18 03:45:29 +00:00
"encoding/json"
2023-09-15 07:54:12 +00:00
"errors"
2023-09-15 06:19:14 +00:00
"fmt"
2023-09-14 10:33:29 +00:00
"fusenapi/constants"
"fusenapi/model/gmodel"
2023-09-20 04:27:40 +00:00
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
2023-09-20 07:21:42 +00:00
"fusenapi/service/repositories"
2023-09-13 09:33:58 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-09-15 06:19:14 +00:00
"fusenapi/utils/format"
2023-09-18 03:45:29 +00:00
"fusenapi/utils/s3url_to_s3id"
2023-09-14 10:33:29 +00:00
"math"
2023-09-13 09:33:58 +00:00
"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) {
2023-09-19 03:42:10 +00:00
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
2023-09-22 09:04:01 +00:00
currentPage := constants.DEFAULT_PAGE
2023-09-21 06:41:20 +00:00
limit := 300
2023-09-22 09:04:01 +00:00
if len(req.CartIds) > limit {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "err param:request cart item is too much")
}
2023-09-14 10:33:29 +00:00
//获取用户购物车列表
carts, total, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
2023-09-22 09:04:01 +00:00
Ids: req.CartIds,
2023-09-14 10:33:29 +00:00
UserId: userinfo.UserId,
Sort: "id DESC",
2023-09-22 09:04:01 +00:00
Page: currentPage,
2023-09-14 10:33:29 +00:00
Limit: limit,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get your shopping carts")
}
2023-09-15 07:54:12 +00:00
var (
2023-09-18 03:45:29 +00:00
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{})
2023-09-15 07:54:12 +00:00
)
//获取相关信息
err = l.GetRelationInfo(GetRelationInfoReq{
2023-09-18 03:45:29 +00:00
Carts: carts,
MapSize: mapSize,
MapModel: mapModel,
MapTemplate: mapTemplate,
MapSizePrice: mapSizePrice,
MapProduct: mapProduct,
MapResourceMetadata: mapResourceMetadata,
2023-09-15 07:54:12 +00:00
})
2023-09-14 10:33:29 +00:00
if err != nil {
2023-09-15 07:54:12 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
2023-09-14 10:33:29 +00:00
}
//定义map收集变更信息
mapCartChange := make(map[int64]string)
2023-09-20 07:21:42 +00:00
mapSnapshot := make(map[int64]gmodel.CartSnapshot)
2023-09-14 10:33:29 +00:00
//校验购物车数据是否变更
2023-09-20 07:21:42 +00:00
err = l.svcCtx.Repositories.NewShoppingCart.VerifyShoppingCartSnapshotDataChange(repositories.VerifyShoppingCartSnapshotDataChangeReq{
2023-09-14 10:33:29 +00:00
Carts: carts,
MapSize: mapSize,
MapModel: mapModel,
MapTemplate: mapTemplate,
MapCartChange: mapCartChange,
2023-09-15 06:19:14 +00:00
MapSnapshot: mapSnapshot,
2023-09-15 08:30:01 +00:00
MapProduct: mapProduct,
2023-09-14 10:33:29 +00:00
})
if err != nil {
logx.Error("VerifyShoppingCartSnapshotDataChange err:", err.Error())
return resp.SetStatusWithMessage(basic.CodeServiceErr, "system err:failed to check shopping cart change data")
}
2023-09-15 07:54:12 +00:00
list := make([]types.CartItem, 0, len(carts))
2023-09-15 06:19:14 +00:00
for _, cart := range carts {
snapShot := mapSnapshot[cart.Id]
2023-09-15 08:30:01 +00:00
sizePrice, ok := mapSizePrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)]
2023-09-15 06:19:14 +00:00
if !ok {
2023-09-18 04:31:51 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d_%d", *cart.ProductId, *cart.SizeId))
2023-09-15 06:19:14 +00:00
}
2023-09-20 04:13:27 +00:00
//如果有配件,单价也要加入配件价格
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))
}
}
//计算价格
2023-09-21 07:40:56 +00:00
itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice)
2023-09-20 04:27:40 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
2023-09-21 06:55:23 +00:00
//尺寸信息
2023-09-18 03:45:29 +00:00
sizeCapacity := snapShot.SizeInfo.Capacity
if sizeInfo, ok := mapSize[*cart.SizeId]; ok {
sizeCapacity = *sizeInfo.Capacity
}
2023-09-21 06:55:23 +00:00
//配件信息
2023-09-18 03:45:29 +00:00
fittingName := snapShot.FittingInfo.FittingName
if fittingInfo, ok := mapModel[*cart.FittingId]; ok {
fittingName = *fittingInfo.Name
}
productCover := "" //产品封面图
productName := snapShot.ProductInfo.ProductName
productSn := snapShot.ProductInfo.ProductSn
2023-09-21 06:55:23 +00:00
//产品封面图资源元数据
2023-09-18 03:45:29 +00:00
var productCoverMetadata interface{}
2023-09-21 06:55:23 +00:00
//产品信息
2023-09-18 03:45:29 +00:00
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
}
}
2023-09-15 06:19:14 +00:00
item := types.CartItem{
2023-09-20 04:13:27 +00:00
CartId: cart.Id,
2023-09-18 03:45:29 +00:00
ProductInfo: types.ProductInfo{
ProductId: *cart.ProductId,
ProductName: productName,
ProductSn: productSn,
ProductCover: productCover,
ProductCoverMetadata: productCoverMetadata,
},
2023-09-15 06:19:14 +00:00
SizeInfo: types.SizeInfo{
SizeId: *cart.SizeId,
2023-09-18 03:45:29 +00:00
Capacity: sizeCapacity,
2023-09-15 06:19:14 +00:00
Title: types.SizeTitle{
Cm: snapShot.SizeInfo.Cm,
Inch: snapShot.SizeInfo.Inch,
},
},
FittingInfo: types.FittingInfo{
FittingId: *cart.FittingId,
2023-09-18 03:45:29 +00:00
FittingName: fittingName,
2023-09-15 06:19:14 +00:00
},
2023-09-21 10:21:31 +00:00
ItemPrice: format.CentitoDollar(itemPrice, 3),
2023-09-21 10:34:29 +00:00
TotalPrice: format.CentitoDollarWithNoHalfAdjust(totalPrice, 2),
2023-09-15 06:19:14 +00:00
DiyInformation: types.DiyInformation{
Phone: snapShot.UserDiyInformation.Phone,
Address: snapShot.UserDiyInformation.Address,
Website: snapShot.UserDiyInformation.Website,
Qrcode: snapShot.UserDiyInformation.Qrcode,
Slogan: snapShot.UserDiyInformation.Slogan,
},
2023-09-21 07:40:56 +00:00
PurchaseQuantity: *cart.PurchaseQuantity,
MinPurchaseQuantity: *sizePrice.EachBoxNum * (*sizePrice.MinBuyNum),
StepPurchaseQuantity: *sizePrice.EachBoxNum,
IsHighlyCustomized: *cart.IsHighlyCustomized > 0,
IsSelected: *cart.IsSelected > 0,
2023-09-15 06:19:14 +00:00
}
//是否有失效的
if description, ok := mapCartChange[cart.Id]; ok {
item.IsInvalid = true
item.InvalidDescription = description
2023-09-21 08:10:49 +00:00
//失效了返回给前端也是不选中
item.IsSelected = false
2023-09-15 06:19:14 +00:00
}
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))),
2023-09-22 09:04:01 +00:00
CurrentPage: currentPage,
2023-09-15 06:19:14 +00:00
PerPage: limit,
},
2023-09-21 06:41:20 +00:00
CartList: list,
2023-09-15 06:19:14 +00:00
})
2023-09-13 09:33:58 +00:00
}
2023-09-15 07:54:12 +00:00
// 获取相关信息
type GetRelationInfoReq struct {
2023-09-18 03:45:29 +00:00
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{}
2023-09-15 07:54:12 +00:00
}
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)
2023-09-15 08:30:01 +00:00
productIds := make([]int64, 0, lenCarts)
2023-09-15 07:54:12 +00:00
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)
2023-09-15 08:30:01 +00:00
productIds = append(productIds, *req.Carts[index].ProductId)
}
//获取产品集合
2023-09-18 03:45:29 +00:00
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByIds(l.ctx, productIds, "")
2023-09-15 08:30:01 +00:00
if err != nil {
logx.Error(err)
return errors.New("failed to get product list")
}
2023-09-18 03:45:29 +00:00
resourceIds := make([]string, 0, len(productList))
2023-09-15 08:30:01 +00:00
for _, v := range productList {
2023-09-18 03:45:29 +00:00
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
2023-09-15 07:54:12 +00:00
}
//获取尺寸列表
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获取价格列表
2023-09-15 08:30:01 +00:00
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds)
2023-09-15 07:54:12 +00:00
if err != nil {
logx.Error(err)
return errors.New("failed to get cart`s product price list")
}
for _, v := range priceList {
2023-09-15 08:30:01 +00:00
req.MapSizePrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v
2023-09-15 07:54:12 +00:00
}
return nil
}
2023-09-13 09:33:58 +00:00
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetCartsLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }