From 95b826d8a6276ad23fef507ae6fa7f4d0a5ef463 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Fri, 15 Sep 2023 18:30:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=90=88=E5=B9=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/repositories/order.go | 34 ++++++++++++++++++++++++++++------ utils/order/order.go | 26 ++++++++++---------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/service/repositories/order.go b/service/repositories/order.go index 1a34308e..b92668b8 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -7,6 +7,7 @@ import ( "fusenapi/model/gmodel" "fusenapi/utils/basic" "fusenapi/utils/shopping_cart" + "fusenapi/utils/step_price" "math" "github.com/aws/aws-sdk-go/aws/session" @@ -85,13 +86,13 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe // 商品异常 if shoppingCart.ShoppingCartProduct == nil || (shoppingCart.ShoppingCartProduct != nil && *shoppingCart.ShoppingCartProduct.IsShelf == 0) { errorCode = *basic.CodeErrOrderCreatProductAbsent - errorCode.Message = "create order failed, product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent。" + errorCode.Message = "create order failed, product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent" return errors.New(errorCode.Message) } // 商品价格异常 if len(shoppingCart.ShoppingCartProductPriceList) == 0 { errorCode = *basic.CodeErrOrderCreatProductPriceAbsent - errorCode.Message = "create order failed, price of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent。" + errorCode.Message = "create order failed, price of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent" return errors.New(errorCode.Message) } else { var isProductPrice bool @@ -104,7 +105,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe } if !isProductPrice { errorCode = *basic.CodeErrOrderCreatProductPriceAbsent - errorCode.Message = "create order failed, price of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent。" + errorCode.Message = "create order failed, price of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent" return errors.New(errorCode.Message) } shoppingCart.ShoppingCartProductPriceList = []*gmodel.FsProductPrice{shoppingCartProductPrice} @@ -113,7 +114,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe // 商品模型异常 if len(shoppingCart.ShoppingCartProductModel3dList) == 0 { errorCode = *basic.CodeErrOrderCreatProductAccessoryAbsent - errorCode.Message = "create order failed, accessoryof product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent。" + errorCode.Message = "create order failed, accessoryof product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent" return errors.New(errorCode.Message) } else { var isProductModel bool @@ -126,7 +127,7 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe } if !isProductModel { errorCode = *basic.CodeErrOrderCreatProductAccessoryAbsent - errorCode.Message = "create order failed, accessoryof product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent。" + errorCode.Message = "create order failed, accessory of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is absent" return errors.New(errorCode.Message) } shoppingCart.ShoppingCartProductModel3dList = []*gmodel.FsProductModel3d{shoppingCartProductModel3d} @@ -134,7 +135,28 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe var purchaseQuantity float64 = float64(*shoppingCart.PurchaseQuantity) var eachBoxNum float64 = float64(*shoppingCartProductPrice.EachBoxNum) - var boxNum int64 = math.Ceil(purchaseQuantity / eachBoxNum) + var boxNum float64 = math.Ceil(purchaseQuantity / eachBoxNum) + var stepNum []int + var stepPrice []int + if *shoppingCartProductPrice.StepNum == "" { + errorCode = *basic.CodeErrOrderCreatProductPriceAbsent + errorCode.Message = "create order failed, step num of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is failed" + return errors.New(errorCode.Message) + } else { + json.Unmarshal([]byte(*shoppingCartProductPrice.StepNum), &stepNum) + } + if *shoppingCartProductPrice.StepPrice == "" { + errorCode = *basic.CodeErrOrderCreatProductPriceAbsent + errorCode.Message = "create order failed, step price of product '" + shoppingCartSnapshot.ProductInfo.ProductName + "'is failed" + return errors.New(errorCode.Message) + } else { + json.Unmarshal([]byte(*shoppingCartProductPrice.StepPrice), &stepPrice) + } + // 商品单价 + productPrice := step_price.GetCentStepPrice(int(boxNum), stepNum, stepPrice) + + // 商品总价 + productTotalPrice := productPrice * *shoppingCart.PurchaseQuantity // 存储订单商品 orderProductList = append(orderProductList, &gmodel.OrderProduct{}) diff --git a/utils/order/order.go b/utils/order/order.go index bcd3bbaa..ca040188 100644 --- a/utils/order/order.go +++ b/utils/order/order.go @@ -1,25 +1,19 @@ package order -import ( - "fusenapi/constants" - "fusenapi/model/gmodel" - "strconv" -) +type AmountCurrency struct { + ExchangeRate float64 `json:"exchange_rate"` // 换算汇率 + CurrentAmount float64 `json:"current_amount"` // 当前金额 + OriginalAmount float64 `json:"original_amount"` // 原始金额 + CurrentCurrency string `json:"current_currency"` // 当前货币 + OriginalCurrency string `json:"original_currency"` // 原始货币 +} -func GetAmountCurrency(req *gmodel.AmountCurrency) (*float64, error) { +// 汇率换算 +func GetAmountCurrency(req *AmountCurrency) error { if req.CurrentCurrency == req.OriginalCurrency { req.CurrentAmount = req.OriginalAmount } else { - f1, err1 := strconv.ParseFloat(string(req.OriginalAmount), 64) - if err1 != nil { - return nil, err1 - } - f2, err2 := strconv.ParseFloat(string(req.OriginalAmount), 64) - if err2 != nil { - return nil, err2 - } - result := f1 * f2 - req.CurrentAmount = constants.AmountUnit(strconv.FormatFloat(result, 'f', -1, 64)) + req.CurrentAmount = req.OriginalAmount * req.ExchangeRate } return nil }