Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop
This commit is contained in:
@@ -11,4 +11,22 @@ type Config struct {
|
||||
SourceMysql string
|
||||
Auth types.Auth
|
||||
ReplicaId uint64
|
||||
AWS struct {
|
||||
S3 struct {
|
||||
Credentials struct {
|
||||
AccessKeyID string
|
||||
Secret string
|
||||
Token string
|
||||
}
|
||||
}
|
||||
}
|
||||
BLMService struct {
|
||||
Url string
|
||||
LogoCombine struct {
|
||||
Url string
|
||||
}
|
||||
}
|
||||
Unity struct {
|
||||
Host string
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"fusenapi/utils/format"
|
||||
"fusenapi/utils/step_price"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"context"
|
||||
@@ -65,9 +64,11 @@ func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateP
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model info")
|
||||
}
|
||||
var stepPrice gmodel.StepPriceJsonStruct
|
||||
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price")
|
||||
if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 {
|
||||
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse step price")
|
||||
}
|
||||
}
|
||||
//配件
|
||||
fittingPrice := int64(0)
|
||||
@@ -82,7 +83,7 @@ func (l *CalculateProductPriceLogic) CalculateProductPrice(req *types.CalculateP
|
||||
}
|
||||
fittingPrice = *fittingInfo.Price
|
||||
}
|
||||
totalPrice, itemPrice, err := step_price.GetNewCentStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice)
|
||||
totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(req.PurchaseQuantity, stepPrice, fittingPrice)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to calculate product price ")
|
||||
|
||||
@@ -5,6 +5,9 @@ import (
|
||||
"fmt"
|
||||
"fusenapi/server/product/internal/config"
|
||||
"fusenapi/shared"
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"net/http"
|
||||
|
||||
"fusenapi/initalize"
|
||||
@@ -18,15 +21,28 @@ type ServiceContext struct {
|
||||
Config config.Config
|
||||
SharedState *shared.SharedState
|
||||
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
MysqlConn *gorm.DB
|
||||
AllModels *gmodel.AllModelsGen
|
||||
Repositories *initalize.Repositories
|
||||
AwsSession *session.Session
|
||||
}
|
||||
|
||||
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: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
Config: c,
|
||||
MysqlConn: initalize.InitMysql(c.SourceMysql),
|
||||
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{
|
||||
GormDB: conn,
|
||||
BLMServiceUrl: &c.BLMService.Url,
|
||||
AwsSession: session.Must(session.NewSession(&config)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/constants"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/server/shopping-cart/internal/svc"
|
||||
"fusenapi/server/shopping-cart/internal/types"
|
||||
@@ -53,7 +53,7 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri
|
||||
//获取购物车列表
|
||||
carts, _, err := l.svcCtx.AllModels.FsShoppingCart.GetAllCartsByParam(l.ctx, gmodel.GetAllCartsByParamReq{
|
||||
Ids: cartIds,
|
||||
Fields: "id,size_id,product_id,fitting_id",
|
||||
Fields: "id,size_id,product_id,fitting_id,model_id",
|
||||
UserId: userinfo.UserId,
|
||||
Page: 1,
|
||||
Limit: len(cartIds),
|
||||
@@ -67,33 +67,24 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri
|
||||
}
|
||||
sizeIds := make([]int64, 0, len(carts))
|
||||
productIds := make([]int64, 0, len(carts))
|
||||
fittingIds := make([]int64, 0, len(carts))
|
||||
modelIds := make([]int64, 0, len(carts)) //模型+配件
|
||||
for _, v := range carts {
|
||||
sizeIds = append(sizeIds, *v.SizeId)
|
||||
productIds = append(productIds, *v.ProductId)
|
||||
modelIds = append(modelIds, *v.ModelId)
|
||||
if *v.FittingId > 0 {
|
||||
fittingIds = append(fittingIds, *v.FittingId)
|
||||
modelIds = append(modelIds, *v.FittingId)
|
||||
}
|
||||
}
|
||||
//根据sizeid获取价格列表
|
||||
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get price list")
|
||||
}
|
||||
mapPrice := make(map[string]gmodel.FsProductPrice)
|
||||
for _, v := range priceList {
|
||||
mapPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v
|
||||
}
|
||||
//获取配件列表(只有id跟价格)
|
||||
fittingList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIdsTag(l.ctx, fittingIds, constants.TAG_PARTS, "id,price")
|
||||
modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByIds(l.ctx, modelIds, "id,step_price,price")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting list")
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
|
||||
}
|
||||
mapFitting := make(map[int64]int64)
|
||||
for _, v := range fittingList {
|
||||
mapFitting[v.Id] = *v.Price
|
||||
mapModel := make(map[int64]gmodel.FsProductModel3d)
|
||||
for _, v := range modelList {
|
||||
mapModel[v.Id] = v
|
||||
}
|
||||
//开始计算价格
|
||||
calculateResultList := make([]types.CalculateResultItem, 0, len(req.CalculateList))
|
||||
@@ -102,9 +93,16 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri
|
||||
err = l.svcCtx.MysqlConn.Transaction(func(tx *gorm.DB) error {
|
||||
shoppingCartModel := gmodel.NewFsShoppingCartModel(tx)
|
||||
for _, cart := range carts {
|
||||
sizePrice, ok := mapPrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)]
|
||||
modelInfo, ok := mapModel[*cart.ModelId]
|
||||
if !ok {
|
||||
return errors.New(fmt.Sprintf("there carts contain some one which have no price info:%d_%d", *cart.ProductId, *cart.SizeId))
|
||||
return err
|
||||
}
|
||||
var stepPrice gmodel.StepPriceJsonStruct
|
||||
if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 {
|
||||
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
|
||||
logx.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
//请求的数量
|
||||
reqPurchaseQuantity := mapCalculateQuantity[cart.Id].PurchaseQuantity
|
||||
@@ -115,16 +113,15 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri
|
||||
//如果有配件,单价也要加入配件价格
|
||||
fittingPrice := int64(0)
|
||||
if *cart.FittingId > 0 {
|
||||
if fPrice, ok := mapFitting[*cart.FittingId]; ok {
|
||||
fittingPrice = fPrice
|
||||
if fittingInfo, ok := mapModel[*cart.FittingId]; ok {
|
||||
fittingPrice = *fittingInfo.Price
|
||||
} else {
|
||||
return errors.New(fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId))
|
||||
}
|
||||
}
|
||||
//计算价格
|
||||
itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(reqPurchaseQuantity, &sizePrice, fittingPrice)
|
||||
totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(reqPurchaseQuantity, stepPrice, fittingPrice)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return err
|
||||
}
|
||||
calculateResultList = append(calculateResultList, types.CalculateResultItem{
|
||||
@@ -149,6 +146,7 @@ func (l *CalculateCartPriceLogic) CalculateCartPrice(req *types.CalculateCartPri
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, err.Error())
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.CalculateCartPriceRsp{
|
||||
|
||||
@@ -63,7 +63,6 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
|
||||
mapSize = make(map[int64]gmodel.FsProductSize)
|
||||
mapModel = make(map[int64]gmodel.FsProductModel3d)
|
||||
mapTemplate = make(map[int64]gmodel.FsProductTemplateV2)
|
||||
mapSizePrice = make(map[string]gmodel.FsProductPrice)
|
||||
mapProduct = make(map[int64]gmodel.FsProduct)
|
||||
mapResourceMetadata = make(map[string]interface{})
|
||||
)
|
||||
@@ -73,7 +72,6 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
|
||||
MapSize: mapSize,
|
||||
MapModel: mapModel,
|
||||
MapTemplate: mapTemplate,
|
||||
MapSizePrice: mapSizePrice,
|
||||
MapProduct: mapProduct,
|
||||
MapResourceMetadata: mapResourceMetadata,
|
||||
})
|
||||
@@ -101,21 +99,34 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
|
||||
list := make([]types.CartItem, 0, len(carts))
|
||||
for _, cart := range carts {
|
||||
snapShot := mapSnapshot[cart.Id]
|
||||
sizePrice, ok := mapSizePrice[fmt.Sprintf("%d_%d", *cart.ProductId, *cart.SizeId)]
|
||||
modelInfo, ok := mapModel[*cart.ModelId]
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s price info is not exists:%d_%d", *cart.ProductId, *cart.SizeId))
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("the size`s model info is not exists:%d_%d", *cart.ProductId, *cart.SizeId))
|
||||
}
|
||||
var stepPrice gmodel.StepPriceJsonStruct
|
||||
if modelInfo.StepPrice != nil && len(*modelInfo.StepPrice) != 0 {
|
||||
if err = json.Unmarshal(*modelInfo.StepPrice, &stepPrice); err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeJsonErr, fmt.Sprintf("failed to parse model step price:%d", *cart.ModelId))
|
||||
}
|
||||
}
|
||||
//购买数量步进量
|
||||
stepPurchaseQuantity := *modelInfo.PackedUnit
|
||||
//如果有配件,单价也要加入配件价格
|
||||
fittingPrice := int64(0)
|
||||
if *cart.FittingId > 0 {
|
||||
if curFittingInfo, ok := mapModel[*cart.FittingId]; ok {
|
||||
fittingPrice = *curFittingInfo.Price
|
||||
} else {
|
||||
curFittingInfo, ok := mapModel[*cart.FittingId]
|
||||
if !ok {
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, fmt.Sprintf("cart contain some one witch lose fitting:%d", *cart.FittingId))
|
||||
}
|
||||
fittingPrice = *curFittingInfo.Price
|
||||
//取大的为步进量基数
|
||||
if *curFittingInfo.PackedUnit > stepPurchaseQuantity {
|
||||
stepPurchaseQuantity = *curFittingInfo.PackedUnit
|
||||
}
|
||||
}
|
||||
//计算价格
|
||||
itemPrice, totalPrice, _, _, err := l.svcCtx.Repositories.NewShoppingCart.CaculateCartPrice(*cart.PurchaseQuantity, &sizePrice, fittingPrice)
|
||||
//计算阶梯价格
|
||||
totalPrice, itemPrice, err := l.svcCtx.Repositories.NewShoppingCart.CaculateStepPrice(*cart.PurchaseQuantity, stepPrice, fittingPrice)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
||||
@@ -144,6 +155,10 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
|
||||
productCoverMetadata = metadata
|
||||
}
|
||||
}
|
||||
templateTag := ""
|
||||
if templateInfo, ok := mapTemplate[*cart.TemplateId]; ok {
|
||||
templateTag = *templateInfo.TemplateTag
|
||||
}
|
||||
item := types.CartItem{
|
||||
CartId: cart.Id,
|
||||
ProductInfo: types.ProductInfo{
|
||||
@@ -175,10 +190,12 @@ func (l *GetCartsLogic) GetCarts(req *types.GetCartsReq, userinfo *auth.UserInfo
|
||||
Slogan: snapShot.UserDiyInformation.Slogan,
|
||||
},
|
||||
PurchaseQuantity: *cart.PurchaseQuantity,
|
||||
MinPurchaseQuantity: *sizePrice.EachBoxNum * (*sizePrice.MinBuyNum),
|
||||
StepPurchaseQuantity: *sizePrice.EachBoxNum,
|
||||
MinPurchaseQuantity: stepPrice.MinBuyUnitsNum,
|
||||
StepPurchaseQuantity: stepPurchaseQuantity,
|
||||
IsHighlyCustomized: *cart.IsHighlyCustomized > 0,
|
||||
IsSelected: *cart.IsSelected > 0,
|
||||
TemplateTag: templateTag,
|
||||
Logo: snapShot.Logo,
|
||||
}
|
||||
//是否有失效的
|
||||
if description, ok := mapCartChange[cart.Id]; ok {
|
||||
@@ -206,7 +223,6 @@ type GetRelationInfoReq struct {
|
||||
MapSize map[int64]gmodel.FsProductSize
|
||||
MapModel map[int64]gmodel.FsProductModel3d
|
||||
MapTemplate map[int64]gmodel.FsProductTemplateV2
|
||||
MapSizePrice map[string]gmodel.FsProductPrice
|
||||
MapProduct map[int64]gmodel.FsProduct
|
||||
MapResourceMetadata map[string]interface{}
|
||||
}
|
||||
@@ -274,15 +290,6 @@ func (l *GetCartsLogic) GetRelationInfo(req GetRelationInfoReq) error {
|
||||
for _, v := range templateList {
|
||||
req.MapTemplate[v.Id] = v
|
||||
}
|
||||
//根据sizeid获取价格列表
|
||||
priceList, err := l.svcCtx.AllModels.FsProductPrice.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return errors.New("failed to get cart`s product price list")
|
||||
}
|
||||
for _, v := range priceList {
|
||||
req.MapSizePrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,8 @@ type CartItem struct {
|
||||
IsInvalid bool `json:"is_invalid"` //是否无效
|
||||
InvalidDescription string `json:"invalid_description"` //无效原因
|
||||
IsSelected bool `json:"is_selected"` //是否选中
|
||||
TemplateTag string `json:"template_tag"` //模板标签
|
||||
Logo string `json:"logo"`
|
||||
}
|
||||
|
||||
type ProductInfo struct {
|
||||
|
||||
Reference in New Issue
Block a user