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) { if req.BuyNum == 0 { return resp.SetStatusWithMessage(basic.CodeApiErr, "param buy_num can`t be 0") } if req.IsCheck != 0 && req.IsCheck != 1 { return resp.SetStatusWithMessage(basic.CodeApiErr, "param is_check should be 0 or 1") } req.DesignId = strings.Trim(req.DesignId, " ") if req.DesignId == "" { return resp.SetStatusWithMessage(basic.CodeApiErr, "param design_id can`t be empty") } //查询是否有此设计 productDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn) productDesignInfo, err := productDesignModel.FindOneBySn(l.ctx, req.DesignId) 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 且 份数大于等于最小购买数量才算满足条件 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 { 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) } 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") }