From f56946fce9254a7a7a9c2efe12aeba99095835f9 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Wed, 13 Sep 2023 18:37:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B7=BB=E5=8A=A0=E8=B4=AD?= =?UTF-8?q?=E7=89=A9=E8=BD=A6=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/fs_shopping_cart_gen.go | 1 + .../internal/logic/addtocartlogic.go | 79 ++++++++++++++++++- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/model/gmodel/fs_shopping_cart_gen.go b/model/gmodel/fs_shopping_cart_gen.go index e58ae44b..0cdf795f 100644 --- a/model/gmodel/fs_shopping_cart_gen.go +++ b/model/gmodel/fs_shopping_cart_gen.go @@ -8,6 +8,7 @@ import ( // fs_shopping_cart 新版购物车表 type FsShoppingCart struct { Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` // id + UserId *int64 `gorm:"default:0;" json:"user_id"` // 用户id ProductId *int64 `gorm:"default:0;" json:"product_id"` // 产品id TemplateId *int64 `gorm:"default:0;" json:"template_id"` // 模板id ModelId *int64 `gorm:"default:0;" json:"model_id"` // 模型id diff --git a/server/shopping-cart/internal/logic/addtocartlogic.go b/server/shopping-cart/internal/logic/addtocartlogic.go index e360240d..858ad0d6 100644 --- a/server/shopping-cart/internal/logic/addtocartlogic.go +++ b/server/shopping-cart/internal/logic/addtocartlogic.go @@ -1,8 +1,10 @@ package logic import ( + "errors" "fusenapi/utils/auth" "fusenapi/utils/basic" + "gorm.io/gorm" "context" @@ -31,12 +33,83 @@ func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCa // } func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - + if !userinfo.IsUser() { + return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in before add to shopping cart") + } + //校验参数 + 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) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists") + } + logx.Error("获取产品信息错误:", err) + return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info") + } + //获取模板信息 + 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") + } + //获取模型信息 + modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, *templateInfo.ModelId) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template`s model is not exists") + } + 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") + } + //获取尺寸信息 + 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 *sizeInfo.Status != 1 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "size info status is not normal") + } return resp.SetStatus(basic.CodeOK) } +// 参数校验 +func (l *AddToCartLogic) AddToCartParamVerify(req *types.AddToCartReq) error { + if req.ProductId <= 0 { + return errors.New("invalid param:product_id") + } + if req.SizeId <= 0 { + return errors.New("invalid param:size_id") + } + if req.TemplateId <= 0 { + return errors.New("invalid param:template_id") + } + if req.PurchaseQuantity <= 0 { + return errors.New("invalid param:purchase_quantity") + } + return nil +} + // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *AddToCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp)