From 826514014837c07c7d5f1889f2d212709610dc42 Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Mon, 18 Sep 2023 15:39:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=B4=AD=E7=89=A9=E8=BD=A6=E4=B8=8B?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants/orders.go | 5 +- model/gmodel/fs_order_logic.go | 87 +++----------------- model/gmodel/fs_product_logic.go | 9 +++ model/gmodel/fs_shopping_cart_logic.go | 2 +- service/repositories/order.go | 108 ++++++++++++++++++++++--- utils/order/order.go | 82 ++++++++++++++++--- 6 files changed, 194 insertions(+), 99 deletions(-) diff --git a/constants/orders.go b/constants/orders.go index 7fe2de34..c2bc07b2 100644 --- a/constants/orders.go +++ b/constants/orders.go @@ -8,7 +8,10 @@ const ( ) // 金额单位 -type AmountUnit string +type AmountUnit interface{} + +// 汇率单位 +type ExchangeRateUnit interface{} // 交易方式 type PayMethods string diff --git a/model/gmodel/fs_order_logic.go b/model/gmodel/fs_order_logic.go index 78f82f1d..5cad94b5 100644 --- a/model/gmodel/fs_order_logic.go +++ b/model/gmodel/fs_order_logic.go @@ -59,22 +59,20 @@ type PayInfo struct { // 金额明细 type AmountInfo struct { - ExchangeRate string `json:"exchange_rate"` // 换算汇率 - Change constants.AmountUnit `json:"change,omitempty"` // 变动金额 - ChangeRemark string `json:"change_remark,omitempty"` // 变动备注状态编码 - Currency string `json:"currency"` // 货币 - Current constants.AmountUnit `json:"current"` // 当前金额 - Initiate constants.AmountUnit `json:"initiate"` // 初始金额 + Change AmountCurrency `json:"change,omitempty"` // 变动金额 + ChangeRemark string `json:"change_remark,omitempty"` // 变动备注 + Current AmountCurrency `json:"current"` // 当前金额 + Initiate AmountCurrency `json:"initiate"` // 初始金额 Metadata map[string]interface{} `json:"metadata"` // 额外明细 } // 金额货币 type AmountCurrency struct { - CurrentAmount constants.AmountUnit `json:"current_amount"` // 当前金额 - CurrentCurrency string `json:"current_currency"` // 当前货币 - ExchangeRate string `json:"exchange_rate"` // 换算汇率 - OriginalAmount constants.AmountUnit `json:"original_amount"` // 原始金额 - OriginalCurrency string `json:"original_currency"` // 原始货币 + ExchangeRate constants.ExchangeRateUnit `json:"exchange_rate"` // 换算汇率 + CurrentCurrency string `json:"current_currency"` // 当前货币 + CurrentAmount constants.AmountUnit `json:"current_amount"` // 当前金额 + OriginalCurrency string `json:"original_currency"` // 原始货币 + OriginalAmount constants.AmountUnit `json:"original_amount"` // 原始金额 } // 支付状态 @@ -108,16 +106,16 @@ type OrderStatus struct { // 订单商品 type OrderProduct struct { - Amount AmountCurrency `json:"amount"` // 商品总价 + Amount AmountInfo `json:"amount"` // 商品总价 ExpectedDeliveryTime string `json:"expected_delivery_time"` // 预计到货时间 Number int64 `json:"number"` // 商品数量 - ProductID string `json:"product_id"` // 商品ID + ProductID int64 `json:"product_id"` // 商品ID ProductLogo string `json:"product_logo"` // 商品logo ProductLogoResource *Resource `json:"product_logo_resource"` // 商品封面--资源详情 ProductName string `json:"product_name"` // 商品名称 - ProductPrice AmountCurrency `json:"product_price"` // 商品单价 + ProductPrice AmountInfo `json:"product_price"` // 商品单价 ProductSnapshot map[string]interface{} `json:"product_snapshot"` // 商品快照 - ShoppingCartSnapshot *ShoppingCartSnapshot `json:"shopping_cart_snapshot"` // 购物车快照 + ShoppingCartSnapshot *FsShoppingCart `json:"shopping_cart_snapshot"` // 购物车快照 Unit string `json:"unit"` // 商品单位 } @@ -128,62 +126,3 @@ type Resource struct { ResourceType string `json:"resource_type"` // 资源类型 ResourceURL string `json:"resource_url"` // 资源地址 } - -// 购物车 -type ShoppingCartSnapshot struct { - Ctime string `json:"ctime"` // 创建时间 - FittingID int64 `json:"fitting_id"` // 配件id - ID int64 `json:"id"` // 购物车ID - IsHighlyCustomized int64 `json:"is_highly_customized"` // 是否高度定制 0非 1是(针对客人高度定制只能后台增加如购物车) - ModelID int64 `json:"model_id"` // 模型id - ProductID int64 `json:"product_id"` // 产品id - PurchaseQuantity int64 `json:"purchase_quantity"` // 购买数量 - SizeID int64 `json:"size_id"` // 尺寸id - Snapshot Snapshot `json:"snapshot"` // 购物车快照数据 - TemplateID int64 `json:"template_id"` // 模板id - UserID int64 `json:"user_id"` // 用户id - Utime string `json:"utime"` // 更新时间 -} - -// 购物车快照 -type Snapshot struct { - CombineImage string `json:"combine_image"` // 刀版图地址 - FittingInfo FittingInfo `json:"fitting_info"` - Logo string `json:"logo"` // logo地址 - ModelInfo ModelInfo `json:"model_info"` - RenderImage string `json:"render_image"` // 模板数据 - SizeInfo SizeInfo `json:"size_info"` - TemplateInfo TemplateInfo `json:"template_info"` - UserDiyInformation UserDiyInformation `json:"user_diy_information"` -} - -// 配件信息 -type FittingInfo struct { - FittingJSON string `json:"fitting_json"` -} - -// 模型数据 -type ModelInfo struct { - ModelJSON string `json:"model_json"` -} - -// 尺寸信息 -type SizeInfo struct { - CM string `json:"cm"` - Inch string `json:"inch"` -} - -// 模板数据 -type TemplateInfo struct { - TemplateJSON string `json:"template_json"` - TemplateTag string `json:"template_tag"` -} - -// DIY数据 -type UserDiyInformation struct { - Address string `json:"address"` // 地址 - Phone string `json:"phone"` // 电话 - Qrcode string `json:"qrcode"` // 二维码 - Slogan string `json:"slogan"` // slogan - Website string `json:"website"` // 网站 -} diff --git a/model/gmodel/fs_product_logic.go b/model/gmodel/fs_product_logic.go index dcd6d497..314886c0 100755 --- a/model/gmodel/fs_product_logic.go +++ b/model/gmodel/fs_product_logic.go @@ -2,6 +2,15 @@ package gmodel import "context" +type RelaFsProduct struct { + FsProduct + CoverResource *FsResource `json:"cover_resource" gorm:"foreignkey:cover;references:resource_id"` +} + +func (m *FsProductModel) TableName() string { + return m.name +} + func (p *FsProductModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsProduct, err error) { db := p.db.WithContext(ctx).Model(&FsProduct{}).Where("`id` = ? ", id) if len(fields) != 0 { diff --git a/model/gmodel/fs_shopping_cart_logic.go b/model/gmodel/fs_shopping_cart_logic.go index a8184c5d..8f13c0aa 100644 --- a/model/gmodel/fs_shopping_cart_logic.go +++ b/model/gmodel/fs_shopping_cart_logic.go @@ -6,7 +6,7 @@ import ( type RelaFsShoppingCart struct { FsShoppingCart - ShoppingCartProduct *FsProduct `json:"shopping_cart_product" gorm:"foreignkey:product_id;references:id"` + ShoppingCartProduct *RelaFsProduct `json:"shopping_cart_product" gorm:"foreignkey:product_id;references:id"` ShoppingCartProductPriceList []*FsProductPrice `json:"shopping_cart_product_price_list" gorm:"foreignkey:product_id;references:product_id"` ShoppingCartProductModel3dList []*FsProductModel3d `json:"shopping_cart_product_model3d_list" gorm:"foreignkey:product_id;references:product_id"` } diff --git a/service/repositories/order.go b/service/repositories/order.go index b92668b8..fcc76367 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -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 { diff --git a/utils/order/order.go b/utils/order/order.go index ca040188..20e62d8d 100644 --- a/utils/order/order.go +++ b/utils/order/order.go @@ -1,19 +1,77 @@ package order -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"` // 原始货币 +import ( + "fusenapi/model/gmodel" +) + +type AmountCurrencyReq struct { + ExchangeRate int64 `json:"exchange_rate"` // 换算汇率 + CurrentAmount int64 `json:"current_amount"` // 当前金额 + OriginalAmount int64 `json:"original_amount"` // 原始金额 + CurrentCurrency string `json:"current_currency"` // 当前货币 + OriginalCurrency string `json:"original_currency"` // 原始货币 } // 汇率换算 -func GetAmountCurrency(req *AmountCurrency) error { - if req.CurrentCurrency == req.OriginalCurrency { - req.CurrentAmount = req.OriginalAmount - } else { - req.CurrentAmount = req.OriginalAmount * req.ExchangeRate +func GetAmountCurrency(req *AmountCurrencyReq) gmodel.AmountCurrency { + if req.CurrentAmount != 0 { + if req.CurrentCurrency == req.OriginalCurrency { + req.CurrentAmount = req.OriginalAmount + } else { + req.CurrentAmount = req.OriginalAmount * req.ExchangeRate + } + } + + return gmodel.AmountCurrency{ + ExchangeRate: req.ExchangeRate, + CurrentAmount: req.CurrentAmount, + OriginalAmount: req.OriginalAmount, + CurrentCurrency: req.CurrentCurrency, + OriginalCurrency: req.OriginalCurrency, + } +} + +type GetAmountInfoReq struct { + ExchangeRate int64 + Initiate int64 + Current int64 + Change int64 + ChangeRemark string + Metadata map[string]interface{} + CurrentCurrency string + OriginalCurrency string +} + +// Change AmountCurrency `json:"change,omitempty"` // 变动金额 +// ChangeRemark string `json:"change_remark,omitempty"` // 变动备注 +// Current AmountCurrency `json:"current"` // 当前金额 +// Initiate AmountCurrency `json:"initiate"` // 初始金额 +// Metadata map[string]interface{} `json:"metadata"` // 额外明细 + +func GetAmountInfo(req GetAmountInfoReq) gmodel.AmountInfo { + return gmodel.AmountInfo{ + Change: GetAmountCurrency(&AmountCurrencyReq{ + ExchangeRate: req.ExchangeRate, + CurrentAmount: req.Change, + OriginalAmount: req.Change, + CurrentCurrency: req.OriginalCurrency, + OriginalCurrency: req.OriginalCurrency, + }), + ChangeRemark: req.ChangeRemark, + Current: GetAmountCurrency(&AmountCurrencyReq{ + ExchangeRate: req.ExchangeRate, + CurrentAmount: req.Current, + OriginalAmount: req.Current, + CurrentCurrency: req.OriginalCurrency, + OriginalCurrency: req.OriginalCurrency, + }), + Initiate: GetAmountCurrency(&AmountCurrencyReq{ + ExchangeRate: req.ExchangeRate, + CurrentAmount: req.Initiate, + OriginalAmount: req.Initiate, + CurrentCurrency: req.OriginalCurrency, + OriginalCurrency: req.OriginalCurrency, + }), + Metadata: req.Metadata, } - return nil }