fusenapi/server/shopping-cart-confirmation/internal/logic/cartaddlogic.go
2023-08-28 14:21:06 +08:00

133 lines
4.5 KiB
Go

package logic
import (
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"strings"
"time"
"gorm.io/gorm"
"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) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please login first")
}
if req.BuyNum == 0 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param buy_num can`t be 0")
}
if req.IsCheck != 0 && req.IsCheck != 1 {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param is_check should be 0 or 1")
}
req.DesignId = strings.Trim(req.DesignId, " ")
if req.DesignId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "param design_id can`t be empty")
}
//查询是否有此设计
productDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
productDesignInfo, err := productDesignModel.FindOneBySn(l.ctx, req.DesignId, userinfo.UserId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "design info is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product design info")
}
//查找是否有此材质、产品、大小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 且 份数大于等于最小购买数量才算满足条件
if req.BuyNum%*productPriceInfo.EachBoxNum != 0 {
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 && !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get cart info")
}
now := time.Now().UTC().Unix()
nowTime := time.Now().UTC()
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 == nil {
err = cartModel.Create(l.ctx, data)
} else {
err = cartModel.Update(l.ctx, cartInfo.Id, data)
}
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to add to cart")
}
return resp.SetStatusWithMessage(basic.CodeOK, "add to cart success")
}