fix:购物车下单

This commit is contained in:
momo
2023-09-18 15:39:12 +08:00
parent 87347515d1
commit 8265140148
6 changed files with 194 additions and 99 deletions

View File

@@ -4,8 +4,10 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"fusenapi/model/gmodel"
"fusenapi/utils/basic"
"fusenapi/utils/order"
"fusenapi/utils/shopping_cart"
"fusenapi/utils/step_price"
"math"
@@ -33,12 +35,13 @@ type (
/* 下单 */
CreateReq struct {
CurrentCurrency string `json:"current_currency"` // 当前货币
ExchangeRate string `json:"exchange_rate"` // 换算汇率
OriginalCurrency string `json:"original_currency"` // 原始货币
UserId int64 `json:"user_id"`
CartIds []int64 `json:"cart_ids"`
DeliveryMethod string `json:"delivery_method"`
ExpectedDeliveryTime string `json:"expected_delivery_time"` // 预计到货时间
ExchangeRate int64 `json:"exchange_rate"` // 换算汇率(厘)
CurrentCurrency string `json:"current_currency"` // 当前货币
OriginalCurrency string `json:"original_currency"` // 原始货币
UserId int64 `json:"user_id"`
CartIds []int64 `json:"cart_ids"`
DeliveryMethod string `json:"delivery_method"`
}
CreateRes struct {
ErrorCode basic.StatusResponse
@@ -53,7 +56,9 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// 查询购物车
var shoppingCartList []*gmodel.RelaFsShoppingCart
resShoppingCartFind := tx.Preload("ShoppingCartProduct").Preload("ShoppingCartProductPriceList").Preload("ShoppingCartProductModel3dList").
resShoppingCartFind := tx.Preload("ShoppingCartProduct", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(gmodel.NewFsProductModel(tx).TableName()).Preload("CoverResource")
}).Preload("ShoppingCartProductPriceList").Preload("ShoppingCartProductModel3dList").
Where("id IN ?", in.CartIds).
Where("user_id = ?", in.UserId).
Find(&shoppingCartList)
@@ -71,8 +76,29 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
return errors.New(errorCode.Message)
}
// 订单商品列表
var orderProductList []*gmodel.OrderProduct
var shippingFee gmodel.AmountInfo
// 订单税费总价(厘)
var shippingFeeTotal int64
var tax = gmodel.AmountInfo{}
// 订单邮费总价(厘)
var taxTotal int64
var discount gmodel.AmountInfo
// 订单折扣总价(厘)
var discountTotal int64
var subtotal gmodel.AmountInfo
// 订单商品总价(厘)
var orderProductTotal int64
var total gmodel.AmountInfo
// 订单总价(厘)
var orderTotal int64
for _, shoppingCart := range shoppingCartList {
// 购物车快照
var shoppingCartSnapshot shopping_cart.CartSnapshot
@@ -152,16 +178,76 @@ func (d *defaultOrder) Create(ctx context.Context, in *CreateReq) (res *CreateRe
} else {
json.Unmarshal([]byte(*shoppingCartProductPrice.StepPrice), &stepPrice)
}
// 商品单价
// 商品单价(厘)
productPrice := step_price.GetCentStepPrice(int(boxNum), stepNum, stepPrice)
// 商品总价
// 商品总价(厘)
productTotalPrice := productPrice * *shoppingCart.PurchaseQuantity
// 存储订单商品
orderProductList = append(orderProductList, &gmodel.OrderProduct{})
// 订单商品总价(厘)
orderProductTotal = orderProductTotal + productTotalPrice
// 订单商品
var productLogoResource *gmodel.Resource
if shoppingCart.ShoppingCartProduct.CoverResource != nil {
var coverResourceMetadata map[string]interface{}
if shoppingCart.ShoppingCartProduct.CoverResource.Metadata != nil {
json.Unmarshal(*shoppingCart.ShoppingCartProduct.CoverResource.Metadata, &coverResourceMetadata)
}
productLogoResource = &gmodel.Resource{
Metadata: coverResourceMetadata,
ResourceID: shoppingCart.ShoppingCartProduct.CoverResource.ResourceId,
ResourceType: *shoppingCart.ShoppingCartProduct.CoverResource.ResourceType,
ResourceURL: *shoppingCart.ShoppingCartProduct.CoverResource.ResourceUrl,
}
}
var productSnapshot = make(map[string]interface{}, 1)
productSnapshot["product_snapshot"] = shoppingCart.ShoppingCartProduct
orderProductList = append(orderProductList, &gmodel.OrderProduct{
Amount: order.GetAmountInfo(order.GetAmountInfoReq{
ExchangeRate: in.ExchangeRate,
Initiate: productTotalPrice,
Current: productTotalPrice,
CurrentCurrency: in.CurrentCurrency,
OriginalCurrency: in.OriginalCurrency,
}),
ExpectedDeliveryTime: in.ExpectedDeliveryTime,
Number: *shoppingCart.PurchaseQuantity,
ProductID: *shoppingCart.ProductId,
ProductLogo: *shoppingCart.ShoppingCartProduct.Cover,
ProductLogoResource: productLogoResource,
ProductName: *shoppingCart.ShoppingCartProduct.Title,
ProductPrice: order.GetAmountInfo(order.GetAmountInfoReq{
ExchangeRate: in.ExchangeRate,
Initiate: productPrice,
Current: productPrice,
CurrentCurrency: in.CurrentCurrency,
OriginalCurrency: in.OriginalCurrency,
}),
ProductSnapshot: productSnapshot,
ShoppingCartSnapshot: &shoppingCart.FsShoppingCart,
})
}
subtotal = order.GetAmountInfo(order.GetAmountInfoReq{
ExchangeRate: in.ExchangeRate,
Initiate: orderProductTotal,
Current: orderProductTotal,
CurrentCurrency: in.CurrentCurrency,
OriginalCurrency: in.OriginalCurrency,
})
orderTotal = orderProductTotal + shippingFeeTotal + taxTotal - discountTotal
total = order.GetAmountInfo(order.GetAmountInfoReq{
ExchangeRate: in.ExchangeRate,
Initiate: orderTotal,
Current: orderTotal,
CurrentCurrency: in.CurrentCurrency,
OriginalCurrency: in.OriginalCurrency,
})
fmt.Println(shippingFee, shippingFeeTotal, tax, taxTotal, discount, discountTotal, subtotal, orderProductTotal, total)
return nil
})
if err != nil {