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

@ -24,22 +24,22 @@ func (s *FsShoppingCartModel) FineOneUserCart(ctx context.Context, id, userId in
// 创建
func (s *FsShoppingCartModel) Create(ctx context.Context, data *FsShoppingCart) error {
return s.db.WithContext(ctx).Create(&data).Error
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Create(&data).Error
}
// 删除
func (s *FsShoppingCartModel) Delete(ctx context.Context, id, userId int64) error {
return s.db.WithContext(ctx).Where("user_id = ? and id = ?", userId, id).Delete(&FsShoppingCart{}).Error
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ? and id = ?", userId, id).Delete(&FsShoppingCart{}).Error
}
// 更新
func (s *FsShoppingCartModel) Update(ctx context.Context, id, userId int64, data *FsShoppingCart) error {
return s.db.WithContext(ctx).Where("user_id = ? and id = ?", userId, id).Updates(&data).Error
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ? and id = ?", userId, id).Updates(&data).Error
}
// 获取用户购物车数量
func (s *FsShoppingCartModel) CountUserCart(ctx context.Context, userId int64) (total int64, err error) {
err = s.db.WithContext(ctx).Where("user_id = ?", userId).Limit(1).Count(&total).Error
err = s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ?", userId).Limit(1).Count(&total).Error
return total, err
}
@ -48,7 +48,7 @@ func (s *FsShoppingCartModel) GetAllByIds(ctx context.Context, ids []int64, sort
if len(ids) == 0 {
return
}
db := s.db.WithContext(ctx).Where("id in (?)", ids)
db := s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("id in (?)", ids)
if len(fields) > 0 {
db = db.Select(fields[0])
}
@ -70,7 +70,7 @@ type GetAllCartsByParamReq struct {
}
func (s *FsShoppingCartModel) GetAllCartsByParam(ctx context.Context, req GetAllCartsByParamReq) (resp []FsShoppingCart, total int64, err error) {
db := s.db.WithContext(ctx)
db := s.db.WithContext(ctx).Model(&FsShoppingCart{})
if req.UserId > 0 {
db = db.Where("user_id = ?", req.UserId)
}

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")
}

View File

@ -50,11 +50,16 @@ type CartItem struct {
ItemPrice string `json:"item_price"` //单价
TotalPrice string `json:"totalPrice"` //单价X数量=总价
DiyInformation DiyInformation `json:"diy_information"` //diy信息
StepNum []int64 `json:"step_num"` //阶梯数量
StepNum []StepNumItem `json:"step_num"` //阶梯数量
IsInvalid bool `json:"is_invalid"` //是否无效
InvalidDescription string `json:"invalid_description"` //无效原因
}
type StepNumItem struct {
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
IsSelected bool `json:"is_selected"` //是否选中
}
type SizeInfo struct {
SizeId int64 `json:"size_id"` //尺寸id
Capacity string `json:"capacity"` //尺寸名称
@ -76,6 +81,7 @@ type DiyInformation struct {
Address string `json:"address"`
Website string `json:"website"`
Qrcode string `json:"qrcode"`
Slogan string `json:"slogan"`
}
type Request struct {

View File

@ -66,10 +66,14 @@ type CartItem {
ItemPrice string `json:"item_price"` //单价
TotalPrice string `json:"totalPrice"` //单价X数量=总价
DiyInformation DiyInformation `json:"diy_information"` //diy信息
StepNum []int64 `json:"step_num"` //阶梯数量
StepNum []StepNumItem `json:"step_num"` //阶梯数量
IsInvalid bool `json:"is_invalid"` //是否无效
InvalidDescription string `json:"invalid_description"` //无效原因
}
type StepNumItem {
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
IsSelected bool `json:"is_selected"` //是否选中
}
type SizeInfo {
SizeId int64 `json:"size_id"` //尺寸id
Capacity string `json:"capacity"` //尺寸名称
@ -88,4 +92,5 @@ type DiyInformation {
Address string `json:"address"`
Website string `json:"website"`
Qrcode string `json:"qrcode"`
Slogan string `json:"slogan"`
}

View File

@ -21,14 +21,16 @@ type ModelInfo struct {
}
type FittingInfo struct {
FittingJson string `json:"fitting_json"` //配件设计json数据
FittingName string `json:"fitting_name"` //配件名称
}
type TemplateInfo struct {
TemplateJson string `json:"template_json"` //模板设计json数据
TemplateTag string `json:"template_tag"` //模板标签
}
type SizeInfo struct {
Inch string `json:"inch"`
Cm string `json:"cm"`
Inch string `json:"inch"`
Cm string `json:"cm"`
Capacity string `json:"capacity"`
}
type UserDiyInformation struct {
Phone string `json:"phone"` //电话

View File

@ -14,6 +14,7 @@ type VerifyShoppingCartSnapshotDataChangeReq struct {
MapModel map[int64]gmodel.FsProductModel3d //模型跟配件都在
MapTemplate map[int64]gmodel.FsProductTemplateV2
MapCartChange map[int64]string
MapSnapshot map[int64]CartSnapshot
}
func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error {
@ -22,6 +23,7 @@ func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChan
if err := json.Unmarshal([]byte(*cartInfo.Snapshot), &snapShotParseInfo); err != nil {
return err
}
req.MapSnapshot[cartInfo.Id] = snapShotParseInfo
//快照中模板设计json数据哈希值
snapshotTemplateJsonHash := hash.JsonHashKey(snapShotParseInfo.TemplateInfo.TemplateJson)
//快照中模型设计json数据哈希值