diff --git a/initalize/service.go b/initalize/service.go index b503c6f1..50fcb142 100644 --- a/initalize/service.go +++ b/initalize/service.go @@ -8,8 +8,9 @@ import ( ) type Repositories struct { - ImageHandle repositories.ImageHandle - NewResource repositories.Resource + ImageHandle repositories.ImageHandle + NewResource repositories.Resource + NewShoppingCart repositories.ShoppingCart } type NewAllRepositorieData struct { @@ -20,7 +21,8 @@ type NewAllRepositorieData struct { func NewAllRepositories(newData *NewAllRepositorieData) *Repositories { return &Repositories{ - ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), - NewResource: repositories.NewResource(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), + ImageHandle: repositories.NewImageHandle(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), + NewResource: repositories.NewResource(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), + NewShoppingCart: repositories.NewShoppingCart(newData.GormDB, newData.BLMServiceUrl, newData.AwsSession), } } diff --git a/model/gmodel/fs_shopping_cart_logic.go b/model/gmodel/fs_shopping_cart_logic.go index 60932284..83a9b1d4 100644 --- a/model/gmodel/fs_shopping_cart_logic.go +++ b/model/gmodel/fs_shopping_cart_logic.go @@ -4,6 +4,7 @@ import ( "context" ) +// 关联查询 type RelaFsShoppingCart struct { FsShoppingCart ShoppingCartProduct *RelaFsProduct `json:"shopping_cart_product" gorm:"foreignkey:product_id;references:id"` @@ -12,6 +13,52 @@ type RelaFsShoppingCart struct { ShoppingCartProductModel3dFitting *FsProductModel3d `json:"shopping_cart_product_model3d_list_fitting" gorm:"foreignkey:fitting_id;references:id"` } +// 快照json数据结构 +// 购物车快照数据结构 +type CartSnapshot struct { + Logo string `json:"logo"` //logo地址 + CombineImage string `json:"combine_image"` //刀版图地址 + RenderImage string `json:"render_image"` //渲染结果图 + TemplateInfo TemplateInfo `json:"template_info"` //模板数据 + ModelInfo ModelInfo `json:"model_info"` //模型的数据 + FittingInfo FittingInfo `json:"fitting_info"` //配件信息 + SizeInfo SizeInfo `json:"size_info"` //尺寸基本信息 + ProductInfo ProductInfo `json:"product_info"` //产品基本信息(只记录不要使用) + UserDiyInformation UserDiyInformation `json:"user_diy_information"` //用户diy数据 + LightInfo LightInfo `json:"light_info"` //灯光数据 +} +type ProductInfo struct { + ProductName string `json:"product_name"` + ProductSn string `json:"product_sn"` +} +type ModelInfo struct { + ModelJson string `json:"model_json"` //模型设计json数据 +} +type FittingInfo struct { + FittingJson string `json:"fitting_json"` //配件设计json数据 + FittingName string `json:"fitting_name"` //配件名称 +} +type TemplateInfo struct { + TemplateJson string `json:"template_json"` //模板设计json数据 + TemplateTag string `json:"template_tag"` //模板标签 +} +type SizeInfo struct { + Inch string `json:"inch"` + Cm string `json:"cm"` + Capacity string `json:"capacity"` +} +type UserDiyInformation struct { + Phone string `json:"phone"` //电话 + Address string `json:"address"` //地址 + Website string `json:"website"` //网站 + Qrcode string `json:"qrcode"` //二维码 + Slogan string `json:"slogan"` //slogan +} +type LightInfo struct { + LightJson string `json:"light_json"` //灯光设计json数据 + LightName string `json:"light_name"` //名称 +} + // 获取单个 func (s *FsShoppingCartModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsShoppingCart, err error) { db := s.db.WithContext(ctx).Where("id = ?", id) diff --git a/server/shopping-cart/internal/config/config.go b/server/shopping-cart/internal/config/config.go index 57f6dab8..68c8e702 100644 --- a/server/shopping-cart/internal/config/config.go +++ b/server/shopping-cart/internal/config/config.go @@ -10,4 +10,22 @@ type Config struct { SourceMysql string Auth types.Auth SourceRabbitMq string + AWS struct { + S3 struct { + Credentials struct { + AccessKeyID string + Secret string + Token string + } + } + } + BLMService struct { + Url string + LogoCombine struct { + Url string + } + } + Unity struct { + Host string + } } diff --git a/server/shopping-cart/internal/logic/calculatecartpricelogic.go b/server/shopping-cart/internal/logic/calculatecartpricelogic.go index 6e2344de..eac4dfd6 100644 --- a/server/shopping-cart/internal/logic/calculatecartpricelogic.go +++ b/server/shopping-cart/internal/logic/calculatecartpricelogic.go @@ -123,7 +123,7 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri } } //计算价格 - itemPrice, totalPrice, _, _, err := shopping_cart.CaculateCartPrice(reqPurchaseQuantity, &sizePrice, fittingPrice) + itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(reqPurchaseQuantity, &sizePrice, fittingPrice) if err != nil { logx.Error(err) return err diff --git a/server/shopping-cart/internal/logic/getcartslogic.go b/server/shopping-cart/internal/logic/getcartslogic.go index a90254bf..4280f863 100644 --- a/server/shopping-cart/internal/logic/getcartslogic.go +++ b/server/shopping-cart/internal/logic/getcartslogic.go @@ -9,6 +9,7 @@ import ( "fusenapi/model/gmodel" "fusenapi/server/shopping-cart/internal/svc" "fusenapi/server/shopping-cart/internal/types" + "fusenapi/service/repositories" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/format" @@ -90,9 +91,9 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo } //定义map收集变更信息 mapCartChange := make(map[int64]string) - mapSnapshot := make(map[int64]shopping_cart.CartSnapshot) + mapSnapshot := make(map[int64]gmodel.CartSnapshot) //校验购物车数据是否变更 - err = shopping_cart.VerifyShoppingCartSnapshotDataChange(shopping_cart.VerifyShoppingCartSnapshotDataChangeReq{ + err = l.svcCtx.Repositories.NewShoppingCart.VerifyShoppingCartSnapshotDataChange(repositories.VerifyShoppingCartSnapshotDataChangeReq{ Carts: carts, MapSize: mapSize, MapModel: mapModel, @@ -123,7 +124,7 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo } } //计算价格 - itemPrice, totalPrice, stepNum, _, err := shopping_cart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice) + itemPrice, totalPrice, stepNum, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice) if err != nil { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error()) diff --git a/server/shopping-cart/internal/svc/servicecontext.go b/server/shopping-cart/internal/svc/servicecontext.go index 2b0f1a9a..f2b1f557 100644 --- a/server/shopping-cart/internal/svc/servicecontext.go +++ b/server/shopping-cart/internal/svc/servicecontext.go @@ -4,23 +4,34 @@ import ( "fusenapi/initalize" "fusenapi/model/gmodel" "fusenapi/server/shopping-cart/internal/config" + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/session" "gorm.io/gorm" ) type ServiceContext struct { - Config config.Config - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen - RabbitMq *initalize.RabbitMqHandle + Config config.Config + MysqlConn *gorm.DB + AllModels *gmodel.AllModelsGen + RabbitMq *initalize.RabbitMqHandle + Repositories *initalize.Repositories } func NewServiceContext(c config.Config) *ServiceContext { conn := initalize.InitMysql(c.SourceMysql) - + config := aws.Config{ + Credentials: credentials.NewStaticCredentials(c.AWS.S3.Credentials.AccessKeyID, c.AWS.S3.Credentials.Secret, c.AWS.S3.Credentials.Token), + } return &ServiceContext{ Config: c, MysqlConn: conn, AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), RabbitMq: initalize.InitRabbitMq(c.SourceRabbitMq, nil), + Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{ + GormDB: conn, + BLMServiceUrl: &c.BLMService.Url, + AwsSession: session.Must(session.NewSession(&config)), + }), } } diff --git a/utils/shopping_cart/verify_shopping_cart_channged.go b/service/repositories/shopping-cart.go similarity index 57% rename from utils/shopping_cart/verify_shopping_cart_channged.go rename to service/repositories/shopping-cart.go index fc43c0fa..dd2b83eb 100644 --- a/utils/shopping_cart/verify_shopping_cart_channged.go +++ b/service/repositories/shopping-cart.go @@ -1,12 +1,42 @@ -package shopping_cart +package repositories import ( "encoding/json" + "errors" + "fmt" "fusenapi/model/gmodel" + "fusenapi/utils/format" "fusenapi/utils/hash" + "fusenapi/utils/step_price" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" + "math" "strings" ) +func NewShoppingCart(gormDB *gorm.DB, bLMServiceUrl *string, awsSession *session.Session) ShoppingCart { + return &defaultShoppingCart{ + MysqlConn: gormDB, + BLMServiceUrl: bLMServiceUrl, + AwsSession: awsSession, + } +} + +type ( + defaultShoppingCart struct { + MysqlConn *gorm.DB + BLMServiceUrl *string + AwsSession *session.Session + } + ShoppingCart interface { + // 校验订单 + VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error + //计算购物车价格 + CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error) + } +) + // 校验购物车快照数据跟目前是否一致 type VerifyShoppingCartSnapshotDataChangeReq struct { Carts []gmodel.FsShoppingCart @@ -14,18 +44,18 @@ type VerifyShoppingCartSnapshotDataChangeReq struct { MapModel map[int64]gmodel.FsProductModel3d //模型跟配件都在 MapTemplate map[int64]gmodel.FsProductTemplateV2 MapCartChange map[int64]string - MapSnapshot map[int64]CartSnapshot + MapSnapshot map[int64]gmodel.CartSnapshot MapProduct map[int64]gmodel.FsProduct } -func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error { +func (d *defaultShoppingCart) VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChangeReq) error { for _, cartInfo := range req.Carts { descrptionBuilder := strings.Builder{} //产品下架/删除 if _, ok := req.MapProduct[*cartInfo.ProductId]; !ok { descrptionBuilder.WriteString("

the product is off shelf or deleted

") } - var snapShotParseInfo CartSnapshot + var snapShotParseInfo gmodel.CartSnapshot if err := json.Unmarshal([]byte(*cartInfo.Snapshot), &snapShotParseInfo); err != nil { return err } @@ -83,7 +113,7 @@ func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChan if !ok { descrptionBuilder.WriteString("

the size is lose

") } else { - var curSizeTitle SizeInfo + var curSizeTitle gmodel.SizeInfo if err := json.Unmarshal([]byte(*curSize.Title), &curSizeTitle); err != nil { return err } @@ -100,3 +130,35 @@ func VerifyShoppingCartSnapshotDataChange(req VerifyShoppingCartSnapshotDataChan } return nil } + +// 计算价格 +func (d *defaultShoppingCart) CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error) { + //阶梯数量切片 + stepNum, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepNum, ",")) + if err != nil { + logx.Error(err) + return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step number:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + } + lenStepNum := len(stepNum) + //阶梯价格切片 + stepPrice, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepPrice, ",")) + if err != nil { + logx.Error(err) + return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step price:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + } + lenStepPrice := len(stepPrice) + if lenStepPrice == 0 || lenStepNum == 0 { + return 0, 0, nil, nil, errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) + } + //请求的数量 + reqPurchaseQuantity := purchaseQuantity + //购买箱数 + boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*productPrice.EachBoxNum))) + //根据数量获取阶梯价格中对应的价格 + itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) + //如果有配件,单价也要加入配件价格 + itemPrice += fittingPrice + //单个购物车总价 + totalPrice = itemPrice * reqPurchaseQuantity + return itemPrice, totalPrice, stepNum, stepPrice, nil +} diff --git a/utils/shopping_cart/caculate_cart_price.go b/utils/shopping_cart/caculate_cart_price.go deleted file mode 100644 index 2acd7977..00000000 --- a/utils/shopping_cart/caculate_cart_price.go +++ /dev/null @@ -1,44 +0,0 @@ -package shopping_cart - -import ( - "errors" - "fmt" - "fusenapi/model/gmodel" - "fusenapi/utils/format" - "fusenapi/utils/step_price" - "github.com/zeromicro/go-zero/core/logx" - "math" - "strings" -) - -// 计算价格 -func CaculateCartPrice(purchaseQuantity int64, productPrice *gmodel.FsProductPrice, fittingPrice int64) (ItemPrice, totalPrice int64, stepNum, stepPrice []int, err error) { - //阶梯数量切片 - stepNum, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepNum, ",")) - if err != nil { - logx.Error(err) - return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step number:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) - } - lenStepNum := len(stepNum) - //阶梯价格切片 - stepPrice, err = format.StrSlicToIntSlice(strings.Split(*productPrice.StepPrice, ",")) - if err != nil { - logx.Error(err) - return 0, 0, nil, nil, errors.New(fmt.Sprintf("failed to parse step price:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) - } - lenStepPrice := len(stepPrice) - if lenStepPrice == 0 || lenStepNum == 0 { - return 0, 0, nil, nil, errors.New(fmt.Sprintf("step price or step number is not set:%d_%d", *productPrice.ProductId, *productPrice.SizeId)) - } - //请求的数量 - reqPurchaseQuantity := purchaseQuantity - //购买箱数 - boxQuantity := int(math.Ceil(float64(reqPurchaseQuantity) / float64(*productPrice.EachBoxNum))) - //根据数量获取阶梯价格中对应的价格 - itemPrice := step_price.GetCentStepPrice(boxQuantity, stepNum, stepPrice) - //如果有配件,单价也要加入配件价格 - itemPrice += fittingPrice - //单个购物车总价 - totalPrice = itemPrice * reqPurchaseQuantity - return itemPrice, totalPrice, stepNum, stepPrice, nil -} diff --git a/utils/shopping_cart/shopping_cart_snapshot.go b/utils/shopping_cart/shopping_cart_snapshot.go deleted file mode 100644 index 45539065..00000000 --- a/utils/shopping_cart/shopping_cart_snapshot.go +++ /dev/null @@ -1,46 +0,0 @@ -package shopping_cart - -// 购物车快照数据结构 -type CartSnapshot struct { - Logo string `json:"logo"` //logo地址 - CombineImage string `json:"combine_image"` //刀版图地址 - RenderImage string `json:"render_image"` //渲染结果图 - TemplateInfo TemplateInfo `json:"template_info"` //模板数据 - ModelInfo ModelInfo `json:"model_info"` //模型的数据 - FittingInfo FittingInfo `json:"fitting_info"` //配件信息 - SizeInfo SizeInfo `json:"size_info"` //尺寸基本信息 - ProductInfo ProductInfo `json:"product_info"` //产品基本信息(只记录不要使用) - UserDiyInformation UserDiyInformation `json:"user_diy_information"` //用户diy数据 - LightInfo LightInfo `json:"light_info"` //灯光数据 -} -type ProductInfo struct { - ProductName string `json:"product_name"` - ProductSn string `json:"product_sn"` -} -type ModelInfo struct { - ModelJson string `json:"model_json"` //模型设计json数据 -} -type FittingInfo struct { - FittingJson string `json:"fitting_json"` //配件设计json数据 - FittingName string `json:"fitting_name"` //配件名称 -} -type TemplateInfo struct { - TemplateJson string `json:"template_json"` //模板设计json数据 - TemplateTag string `json:"template_tag"` //模板标签 -} -type SizeInfo struct { - Inch string `json:"inch"` - Cm string `json:"cm"` - Capacity string `json:"capacity"` -} -type UserDiyInformation struct { - Phone string `json:"phone"` //电话 - Address string `json:"address"` //地址 - Website string `json:"website"` //网站 - Qrcode string `json:"qrcode"` //二维码 - Slogan string `json:"slogan"` //slogan -} -type LightInfo struct { - LightJson string `json:"light_json"` //灯光设计json数据 - LightName string `json:"light_name"` //名称 -}