fusenapi/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go

130 lines
4.4 KiB
Go
Raw Normal View History

2023-06-13 04:15:06 +00:00
package logic
import (
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"strings"
"time"
"context"
"fusenapi/server/shopping-cart-confirmation/internal/svc"
"fusenapi/server/shopping-cart-confirmation/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CartAddLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCartAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartAddLogic {
return &CartAddLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 添加入购物车
func (l *CartAddLogic) CartAdd(req *types.CartAddReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-06-15 04:00:32 +00:00
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
2023-06-13 04:15:06 +00:00
if req.BuyNum == 0 {
2023-06-14 06:05:27 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param buy_num can`t be 0")
2023-06-13 04:15:06 +00:00
}
if req.IsCheck != 0 && req.IsCheck != 1 {
2023-06-14 06:05:27 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param is_check should be 0 or 1")
2023-06-13 04:15:06 +00:00
}
req.DesignId = strings.Trim(req.DesignId, " ")
if req.DesignId == "" {
2023-06-14 06:05:27 +00:00
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param design_id can`t be empty")
2023-06-13 04:15:06 +00:00
}
//查询是否有此设计
productDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
2023-06-19 04:23:02 +00:00
productDesignInfo, err := productDesignModel.FindOneBySn(l.ctx, req.DesignId, userinfo.UserId)
2023-06-13 04:15:06 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product design info")
}
if productDesignInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "product design info is not exists")
}
//查找是否有此材质、产品、大小id的阶梯价格
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
priceStatus := int64(1)
priceReq := gmodel.FindOneProductPriceByParamsReq{
ProductId: productDesignInfo.ProductId,
MaterialId: productDesignInfo.MaterialId,
SizeId: productDesignInfo.SizeId,
Status: &priceStatus,
}
productPriceInfo, err := productPriceModel.FindOneProductPriceByParams(l.ctx, priceReq)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product price info")
}
if productPriceInfo.Id == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, " product price info is not exists")
}
if *productPriceInfo.EachBoxNum == 0 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, " product price info err: each box num can`t be zero")
}
//买的数量和每箱数量取余为0 且 份数大于等于最小购买数量才算满足条件
2023-06-13 05:07:09 +00:00
if req.BuyNum%*productPriceInfo.EachBoxNum != 0 {
2023-06-13 04:15:06 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid buy number,please check")
}
if int64(float64(req.BuyNum)/float64(*productPriceInfo.EachBoxNum)) < *productPriceInfo.MinBuyNum {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid buy number,please check!")
}
//查询购物车
cartModel := gmodel.NewFsCartModel(l.svcCtx.MysqlConn)
cartStatus := int64(1)
cartReq := gmodel.FindOneCartByParamsReq{
UserId: &userinfo.UserId,
ProductId: productDesignInfo.ProductId,
TemplateId: productDesignInfo.TemplateId,
PriceId: &productPriceInfo.Id,
DesignId: &productDesignInfo.Id,
MaterialId: productDesignInfo.MaterialId,
Status: &cartStatus,
}
cartInfo, err := cartModel.FindOneCartByParams(l.ctx, cartReq)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get cart info")
}
now := time.Now().Unix()
nowTime := time.Now()
data := gmodel.FsCart{
UserId: &userinfo.UserId,
ProductId: productPriceInfo.ProductId,
TemplateId: productDesignInfo.TemplateId,
PriceId: &productPriceInfo.Id,
MaterialId: productDesignInfo.MaterialId,
SizeId: productDesignInfo.SizeId,
BuyNum: &req.BuyNum,
Cover: productDesignInfo.Cover,
DesignId: &productDesignInfo.Id,
Ctime: &now,
Status: &cartStatus,
OptionalId: productDesignInfo.OptionalId,
IsCheck: &req.IsCheck,
TsTime: &nowTime,
}
if cartInfo.Id == 0 {
err = cartModel.Create(l.ctx, data)
} else {
err = cartModel.Update(l.ctx, cartInfo.Id, data)
}
2023-06-13 05:59:15 +00:00
if err != nil {
2023-06-13 04:15:06 +00:00
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to add to cart")
2023-06-13 05:59:15 +00:00
}
2023-06-13 06:13:40 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "add to cart success")
2023-06-13 04:15:06 +00:00
}