package logic import ( "errors" "fusenapi/utils/auth" "fusenapi/utils/basic" "gorm.io/gorm" "context" "fusenapi/server/shopping-cart/internal/svc" "fusenapi/server/shopping-cart/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type AddToCartLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCartLogic { return &AddToCartLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *AddToCartLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) { 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) // }