This commit is contained in:
laodaming
2023-09-14 14:11:22 +08:00
parent f56946fce9
commit 13d5219e53
7 changed files with 228 additions and 60 deletions

View File

@@ -1,12 +1,16 @@
package logic
import (
"context"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/shopping_cart"
"gorm.io/gorm"
"context"
"math"
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
@@ -40,8 +44,17 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
if err := l.AddToCartParamVerify(req); err != nil {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, err.Error())
}
//获取产品信息
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId)
//查询该用户购物车数量
cartCount, err := l.svcCtx.AllModels.FsShoppingCart.CountUserCart(l.ctx, userinfo.UserId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get the count of your shopping cart")
}
if cartCount >= 100 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "sorry,the count of your carts can`t greater than 100")
}
//获取产品是否存在
_, err = l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId, "id")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
@@ -49,23 +62,66 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
logx.Error("获取产品信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
}
//获取模板信息
templateInfo, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateId)
var (
templateJson string //模板表的记录中的json设计信息
templateTag string //模板表的模板标签
fittingJson string //配件的json设计信息
)
//有模板
if req.TemplateId > 0 {
templateInfo, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template is not exists")
}
logx.Error("获取模板信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
}
if *templateInfo.IsDel == 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template is deleted")
}
if *templateInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s status is unNormal")
}
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s design info is empty")
}
templateJson = *templateInfo.TemplateInfo
templateTag = *templateInfo.TemplateTag
}
//有配件
if req.FittingId > 0 {
fittingInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.FittingId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the fitting is not exists")
}
logx.Error("获取配件信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting info")
}
if *fittingInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s status is unNormal")
}
if fittingInfo.ModelInfo == nil || *fittingInfo.ModelInfo == "" {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s design info is empty")
}
fittingJson = *fittingInfo.ModelInfo
}
//获取尺寸信息
sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template is not exists")
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the size is not exists")
}
logx.Error("获取模板信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
logx.Error("获取尺寸信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size info")
}
if *templateInfo.IsDel == 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template is deleted")
}
if *templateInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s status is unNormal")
//状态
if *sizeInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "size info status is not normal")
}
//获取模型信息
modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, *templateInfo.ModelId)
modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(l.ctx, sizeInfo.Id, constants.TAG_MODEL)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template`s model is not exists")
@@ -73,39 +129,56 @@ func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserI
logx.Error("获取模型信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template`s model info")
}
//模型里面的size_id是否跟传入的size_id匹配
if req.SizeId != *modelInfo.SizeId {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param size_id is not match model`s size_id")
if *modelInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model` status is unNormal")
}
//获取尺寸信息
sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "the size is not exists")
}
logx.Error("获取尺寸信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "failed to get size info")
//如果模型是配件则返回
if *modelInfo.Tag != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model your post is not a model but fitting")
}
//状态
if *sizeInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "size info status is not normal")
if modelInfo.ModelInfo == nil || *modelInfo.ModelInfo == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model`s design info is empty")
}
//快照数据
snapshot := shopping_cart.CartSnapshot{
Logo: req.Logo,
CombineImage: req.CombineImage,
RenderImage: req.RenderImage,
TemplateInfo: shopping_cart.TemplateInfo{
TemplateJson: templateJson,
TemplateTag: templateTag,
},
ModelInfo: shopping_cart.ModelInfo{
ModelJson: *modelInfo.ModelInfo,
},
FittingInfo: shopping_cart.FittingInfo{
FittingJson: fittingJson,
},
SizeInfo: shopping_cart.SizeInfo{
Title: *sizeInfo.Title,
},
UserDiyInformation: shopping_cart.UserDiyInformation{
Phone: req.DiyInfo.Phone,
Address: req.DiyInfo.Address,
Website: req.DiyInfo.Website,
Qrcode: req.DiyInfo.Qrcode,
Slogan: req.DiyInfo.Slogan,
},
}
//根据
return resp.SetStatus(basic.CodeOK)
}
// 参数校验
func (l *AddToCartLogic) AddToCartParamVerify(req *types.AddToCartReq) error {
if req.ProductId <= 0 {
return errors.New("invalid param:product_id")
return errors.New("product_id is required")
}
if req.SizeId <= 0 {
return errors.New("invalid param:size_id")
}
if req.TemplateId <= 0 {
return errors.New("invalid param:template_id")
return errors.New("product size is required")
}
if req.PurchaseQuantity <= 0 {
return errors.New("invalid param:purchase_quantity")
return errors.New("purchase quantity can not less than 0 or equal 0")
}
return nil
}