From 6843bdb44b8d5294a21f2ee30886e0c5ad07a150 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 29 八月 2023 18:00:57 +0800 Subject: [PATCH] 价格调整 --- service/purchase/purchase.go | 21 +++++++++++++++++---- 1 files changed, 17 insertions(+), 4 deletions(-) diff --git a/service/purchase/purchase.go b/service/purchase/purchase.go index 352395d..257fed3 100644 --- a/service/purchase/purchase.go +++ b/service/purchase/purchase.go @@ -2,6 +2,7 @@ import ( "context" + "errors" "github.com/shopspring/decimal" "github.com/spf13/cast" "gorm.io/gorm" @@ -23,19 +24,31 @@ //@param: params *purchaserequest.AddPurchase //@return: err error -func (slf *PurchaseService) CreatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { +func (slf *PurchaseService) CreatePurchase(record *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { var quantity decimal.Decimal + var totalPrice decimal.Decimal + var realTotalPrice decimal.Decimal for _, product := range productList { quantity = quantity.Add(product.Amount) + totalPrice = totalPrice.Add(product.Price.Mul(product.Amount)) } - params.Quantity = quantity - err = tx.Create(¶ms).Error + if !totalPrice.Equal(record.TotalPrice) { + return errors.New("浠风◣鎬昏璁$畻閿欒") + } + if !quantity.Equal(record.Quantity) { + return errors.New("浜у搧鏁伴噺璁$畻閿欒") + } + realTotalPrice = record.CalcRealTotalPrice() + if !realTotalPrice.Equal(record.RealTotalPrice) { + return errors.New("鏈�缁堜环鏍艰绠楅敊璇�") + } + err = tx.Create(&record).Error if err != nil { return err } for _, product := range productList { - product.PurchaseId = cast.ToInt(params.ID) + product.PurchaseId = cast.ToInt(record.ID) } return tx.Create(productList).Error }) -- Gitblit v1.8.0