2023-09-13 09:33:58 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2023-09-13 10:37:50 +00:00
|
|
|
"errors"
|
2023-09-13 09:33:58 +00:00
|
|
|
"fusenapi/utils/auth"
|
|
|
|
"fusenapi/utils/basic"
|
2023-09-13 10:37:50 +00:00
|
|
|
"gorm.io/gorm"
|
2023-09-13 09:33:58 +00:00
|
|
|
|
|
|
|
"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) {
|
2023-09-13 10:37:50 +00:00
|
|
|
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")
|
|
|
|
}
|
2023-09-13 09:33:58 +00:00
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
|
|
}
|
|
|
|
|
2023-09-13 10:37:50 +00:00
|
|
|
// 参数校验
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-13 09:33:58 +00:00
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
|
|
// func (l *AddToCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
// }
|