fusenapi/server/shopping-cart/internal/logic/addtocartlogic.go

290 lines
10 KiB
Go
Raw Normal View History

2023-09-13 09:33:58 +00:00
package logic
import (
2023-09-14 06:11:22 +00:00
"context"
2023-09-14 07:20:37 +00:00
"encoding/json"
2023-09-13 10:37:50 +00:00
"errors"
2023-09-14 06:11:22 +00:00
"fusenapi/constants"
"fusenapi/model/gmodel"
2023-09-14 07:20:37 +00:00
"fusenapi/server/shopping-cart/internal/svc"
"fusenapi/server/shopping-cart/internal/types"
2023-09-13 09:33:58 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-09-22 03:36:10 +00:00
"fusenapi/utils/file"
"fusenapi/utils/hash"
2023-09-13 10:37:50 +00:00
"gorm.io/gorm"
2023-10-07 08:25:39 +00:00
"strings"
2023-09-14 07:20:37 +00:00
"time"
2023-09-13 09:33:58 +00:00
"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")
2023-09-13 10:37:50 +00:00
}
//校验参数
if err := l.AddToCartParamVerify(req); err != nil {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, err.Error())
}
2023-09-14 06:11:22 +00:00
//查询该用户购物车数量
cartCount, err := l.svcCtx.AllModels.FsShoppingCart.CountUserCart(l.ctx, userinfo.UserId)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get the count of your shopping cart")
}
if cartCount >= 100 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "sorry,the count of your carts can`t greater than 100")
}
2023-10-07 08:25:39 +00:00
//不是传路径则就是传base64
if !strings.Contains(req.RenderImage, "https://") {
2023-09-22 03:36:10 +00:00
//上传base64文件
var upload = file.Upload{
Ctx: l.ctx,
MysqlConn: l.svcCtx.MysqlConn,
AwsSession: l.svcCtx.AwsSession,
}
uploadRes, err := upload.UploadFileByBase64(&file.UploadBaseReq{
Source: "webGl render image",
FileHash: hash.JsonHashKey(req.RenderImage),
FileData: req.RenderImage,
Metadata: "",
UploadBucket: 1,
ApiType: 2,
UserId: userinfo.UserId,
GuestId: userinfo.GuestId,
FileByte: nil,
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeFileUploadErr, "failed to upload webGl render image")
}
req.RenderImage = uploadRes.ResourceUrl
}
2023-09-14 06:11:22 +00:00
//获取产品是否存在
2023-09-14 10:33:29 +00:00
productInfo, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId)
2023-09-13 10:37:50 +00:00
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")
}
2023-09-14 06:11:22 +00:00
var (
2023-10-07 02:13:15 +00:00
templateJson interface{} //模板表的记录中的json设计信息
templateTag string //模板表的模板标签
fittingJson interface{} //配件的json设计信息
fittingName string //配件名
lightJson interface{} //灯光设计数据
lightName string //灯光名字
2023-10-07 02:43:33 +00:00
modelJson interface{} //模型json
2023-09-14 06:11:22 +00:00
)
//有模板
if req.TemplateId > 0 {
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")
}
if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s design info is empty")
}
2023-10-07 02:13:15 +00:00
if err = json.Unmarshal([]byte(*templateInfo.TemplateInfo), &templateJson); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse template design info")
}
2023-09-14 06:11:22 +00:00
templateTag = *templateInfo.TemplateTag
}
//有配件
if req.FittingId > 0 {
fittingInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.FittingId)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the fitting is not exists")
}
logx.Error("获取配件信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting info")
}
if *fittingInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s status is unNormal")
}
if fittingInfo.ModelInfo == nil || *fittingInfo.ModelInfo == "" {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s design info is empty")
}
2023-10-07 02:13:15 +00:00
if err = json.Unmarshal([]byte(*fittingInfo.ModelInfo), &fittingJson); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse fitting design info")
}
2023-09-15 06:19:14 +00:00
fittingName = *fittingInfo.Title
2023-09-14 06:11:22 +00:00
}
//获取尺寸信息
sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
2023-09-13 10:37:50 +00:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2023-09-14 06:11:22 +00:00
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the size is not exists")
2023-09-13 10:37:50 +00:00
}
2023-09-14 06:11:22 +00:00
logx.Error("获取尺寸信息错误:", err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size info")
2023-09-13 10:37:50 +00:00
}
2023-09-14 06:11:22 +00:00
//状态
if *sizeInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "size info status is not normal")
2023-09-13 10:37:50 +00:00
}
//获取模型信息
2023-09-14 06:11:22 +00:00
modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(l.ctx, sizeInfo.Id, constants.TAG_MODEL)
2023-09-13 10:37:50 +00:00
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")
}
2023-09-14 06:11:22 +00:00
if *modelInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model` status is unNormal")
2023-09-13 10:37:50 +00:00
}
2023-09-14 06:11:22 +00:00
//如果模型是配件则返回
if *modelInfo.Tag != 1 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model your post is not a model but fitting")
2023-09-13 10:37:50 +00:00
}
2023-09-14 06:11:22 +00:00
if modelInfo.ModelInfo == nil || *modelInfo.ModelInfo == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model`s design info is empty")
}
2023-10-07 02:43:33 +00:00
if err = json.Unmarshal([]byte(*modelInfo.ModelInfo), &modelJson); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse model design info")
}
//获取灯光信息
if *modelInfo.Light > 0 {
lightInfo, err := l.svcCtx.AllModels.FsProductModel3dLight.FindOne(l.ctx, *modelInfo.Light)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the model`s light info is not exists")
}
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get light info")
}
lightName = *lightInfo.Name
if lightInfo.Info != nil && *lightInfo.Info != "" {
2023-10-07 02:13:15 +00:00
if err = json.Unmarshal([]byte(*lightInfo.Info), &lightJson); err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse light design info")
}
}
}
2023-09-20 07:28:57 +00:00
var sizeKeyInfo gmodel.SizeInfo
2023-09-15 06:19:14 +00:00
if err = json.Unmarshal([]byte(*sizeInfo.Title), &sizeKeyInfo); err != nil {
2023-09-14 10:33:29 +00:00
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse size info`s title ")
}
2023-09-15 06:19:14 +00:00
sizeKeyInfo.Capacity = *sizeInfo.Capacity
2023-09-14 06:11:22 +00:00
//快照数据
2023-09-20 07:28:57 +00:00
snapshot := gmodel.CartSnapshot{
2023-09-14 06:11:22 +00:00
Logo: req.Logo,
CombineImage: req.CombineImage,
RenderImage: req.RenderImage,
2023-09-20 07:28:57 +00:00
TemplateInfo: gmodel.TemplateInfo{
2023-10-07 09:07:53 +00:00
TemplateJson: templateJson,
TemplateTag: templateTag,
SelectColorIndex: req.SelectColorIndex,
2023-09-14 06:11:22 +00:00
},
2023-09-20 07:28:57 +00:00
ModelInfo: gmodel.ModelInfo{
2023-10-07 02:43:33 +00:00
ModelJson: modelJson,
2023-09-14 06:11:22 +00:00
},
2023-09-20 07:28:57 +00:00
FittingInfo: gmodel.FittingInfo{
2023-09-14 06:11:22 +00:00
FittingJson: fittingJson,
2023-09-15 06:19:14 +00:00
FittingName: fittingName,
2023-09-14 06:11:22 +00:00
},
2023-09-15 06:19:14 +00:00
SizeInfo: sizeKeyInfo,
2023-09-20 07:28:57 +00:00
ProductInfo: gmodel.ProductInfo{
2023-09-14 10:33:29 +00:00
ProductName: *productInfo.Title,
ProductSn: *productInfo.Sn,
2023-09-14 06:11:22 +00:00
},
2023-09-20 07:28:57 +00:00
UserDiyInformation: gmodel.UserDiyInformation{
2023-09-14 06:11:22 +00:00
Phone: req.DiyInfo.Phone,
Address: req.DiyInfo.Address,
Website: req.DiyInfo.Website,
Qrcode: req.DiyInfo.Qrcode,
Slogan: req.DiyInfo.Slogan,
},
2023-09-20 07:28:57 +00:00
LightInfo: gmodel.LightInfo{
LightJson: lightJson,
LightName: lightName,
},
2023-09-13 10:37:50 +00:00
}
2023-09-14 07:20:37 +00:00
snapshotJsonBytes, _ := json.Marshal(snapshot)
snapshotJsonStr := string(snapshotJsonBytes)
now := time.Now().UTC()
err = l.svcCtx.AllModels.FsShoppingCart.Create(l.ctx, &gmodel.FsShoppingCart{
2023-10-08 10:00:21 +00:00
UserId: &userinfo.UserId,
ProductId: &req.ProductId,
TemplateId: &req.TemplateId,
ModelId: &modelInfo.Id,
IsHighlyCustomized: &req.IsHighlyCustomized,
LightId: modelInfo.Light,
SizeId: &req.SizeId,
FittingId: &req.FittingId,
PurchaseQuantity: &req.PurchaseQuantity,
Snapshot: &snapshotJsonStr,
Ctime: &now,
Utime: &now,
2023-09-14 07:20:37 +00:00
})
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to add to cart ")
}
return resp.SetStatus(basic.CodeOK, "success")
2023-09-13 09:33:58 +00:00
}
2023-09-13 10:37:50 +00:00
// 参数校验
func (l *AddToCartLogic) AddToCartParamVerify(req *types.AddToCartReq) error {
if req.ProductId <= 0 {
2023-09-14 06:11:22 +00:00
return errors.New("product_id is required")
2023-09-13 10:37:50 +00:00
}
if req.SizeId <= 0 {
2023-09-14 06:11:22 +00:00
return errors.New("product size is required")
2023-09-13 10:37:50 +00:00
}
if req.PurchaseQuantity <= 0 {
2023-09-14 06:11:22 +00:00
return errors.New("purchase quantity can not less than 0 or equal 0")
2023-09-13 10:37:50 +00:00
}
2023-10-07 08:25:39 +00:00
req.RenderImage = strings.Trim(req.RenderImage, " ")
if req.RenderImage == "" {
return errors.New("render image is required")
}
2023-10-07 09:07:53 +00:00
if req.SelectColorIndex < 0 {
return errors.New("invalid select color index")
}
2023-09-13 10:37:50 +00:00
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)
// }