This commit is contained in:
laodaming
2023-09-15 14:19:14 +08:00
parent dd13dcbf0d
commit fd16d6ac67
9 changed files with 136 additions and 18 deletions

View File

@@ -36,6 +36,7 @@ func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCa
// }
func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
userinfo.UserId = 39
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}
@@ -65,6 +66,7 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
templateJson string //模板表的记录中的json设计信息
templateTag string //模板表的模板标签
fittingJson string //配件的json设计信息
fittingName string //配件名
)
//有模板
if req.TemplateId > 0 {
@@ -105,6 +107,7 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s design info is empty")
}
fittingJson = *fittingInfo.ModelInfo
fittingName = *fittingInfo.Title
}
//获取尺寸信息
sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
@@ -138,11 +141,12 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
if modelInfo.ModelInfo == nil || *modelInfo.ModelInfo == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model`s design info is empty")
}
var sizeTitleInfo shopping_cart.SizeInfo
if err = json.Unmarshal([]byte(*sizeInfo.Title), &sizeTitleInfo); err != nil {
var sizeKeyInfo shopping_cart.SizeInfo
if err = json.Unmarshal([]byte(*sizeInfo.Title), &sizeKeyInfo); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse size info`s title ")
}
sizeKeyInfo.Capacity = *sizeInfo.Capacity
//快照数据
snapshot := shopping_cart.CartSnapshot{
Logo: req.Logo,
@@ -157,8 +161,9 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
},
FittingInfo: shopping_cart.FittingInfo{
FittingJson: fittingJson,
FittingName: fittingName,
},
SizeInfo: sizeTitleInfo,
SizeInfo: sizeKeyInfo,
ProductInfo: shopping_cart.ProductInfo{
ProductName: *productInfo.Title,
ProductSn: *productInfo.Sn,

View File

@@ -31,6 +31,7 @@ func NewDeleteCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete
// }
func (l *DeleteCartLogic) DeleteCart(req *types.DeleteCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
userinfo.UserId = 39
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}

View File

@@ -2,12 +2,16 @@ 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"
@@ -34,6 +38,7 @@ func NewGetCartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCarts
// }
func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo) (resp *basic.Response) {
userinfo.UserId = 39
if req.CurrentPage <= 0 {
req.CurrentPage = constants.DEFAULT_PAGE
}
@@ -102,6 +107,7 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
}
//定义map收集变更信息
mapCartChange := make(map[int64]string)
mapSnapshot := make(map[int64]shopping_cart.CartSnapshot)
//校验购物车数据是否变更
err = shopping_cart.VerifyShoppingCartSnapshotDataChange(shopping_cart.VerifyShoppingCartSnapshotDataChangeReq{
Carts: carts,
@@ -109,16 +115,106 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
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")
}
/*list := make([]types.CartItem, 0, lenCarts)
for index := range carts {
}*/
return resp.SetStatus(basic.CodeOK)
//根据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.GetStepPrice(boxQuantity, stepNum, stepPrice)
totalPrice := itemPrice * float64(*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", itemPrice),
TotalPrice: fmt.Sprintf("%.3f", 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 必须重新处理

View File

@@ -34,6 +34,7 @@ func NewModifyCartPurchaseQuantityLogic(ctx context.Context, svcCtx *svc.Service
// }
func (l *ModifyCartPurchaseQuantityLogic) ModifyCartPurchaseQuantity(req *types.ModifyCartPurchaseQuantityReq, userinfo *auth.UserInfo) (resp *basic.Response) {
userinfo.UserId = 39
if !userinfo.IsUser() {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
}