From c549900b66691959771edf7c6526de69a1d8f3cc Mon Sep 17 00:00:00 2001 From: wangpengfei <274878379@qq.com> Date: 星期五, 18 八月 2023 20:19:41 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- service/salesDetails.go | 56 ++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 34 insertions(+), 22 deletions(-) diff --git a/service/salesDetails.go b/service/salesDetails.go index a2e5ad8..fa497bf 100644 --- a/service/salesDetails.go +++ b/service/salesDetails.go @@ -53,46 +53,58 @@ func (SalesDetailsService) UpdateSalesDetails(salesDetails *model.SalesDetails) int { // check salesDetails exist - old, err := model.NewSalesDetailsSearch().SetId(salesDetails.Id).First() + old, err := model.NewSalesDetailsSearch().SetId(salesDetails.Id).SetPreload(true).First() if err != nil { return ecode.SalesDetailsNotExist } var totalAmount decimal.Decimal totalAmount = salesDetails.AmountTotal - newProducts, removedProducts := NewProductsService().PickDiffProducts(salesDetails.Products, old.Products) - for _, product := range newProducts { + for _, product := range salesDetails.Products { totalAmount = totalAmount.Add(product.Amount.Mul(product.Price)) - } - removedProductIds := make([]uint, 0, len(removedProducts)) - for _, product := range removedProducts { - totalAmount = totalAmount.Sub(product.Amount.Mul(product.Price)) - removedProductIds = append(removedProductIds, product.Id) } salesDetails.AmountTotal = totalAmount.Round(2) salesDetails.AmountReceivable = salesDetails.AmountTotal.Sub(salesDetails.AmountReceived) salesDetails.AmountUnInvoiced = salesDetails.AmountTotal.Sub(salesDetails.AmountInvoiced) + newProducts, removedProducts := NewProductsService().PickDiffProducts(salesDetails.Products, old.Products) err = model.WithTransaction(func(db *gorm.DB) error { err = model.NewSalesDetailsSearch().SetId(salesDetails.Id).Update(salesDetails) if err != nil { return err } - err = model.NewProductSearch(db).SetIds(removedProductIds).Delete() - if err != nil { - return err + if len(removedProducts) > 0 { + removedProductIds := make([]uint, 0, len(removedProducts)) + for _, product := range removedProducts { + totalAmount = totalAmount.Sub(product.Amount.Mul(product.Price)) + removedProductIds = append(removedProductIds, product.Id) + } + err = model.NewProductSearch(db).SetIds(removedProductIds).Delete() + if err != nil { + return err + } } - err = model.NewProductSearch(db).CreateBatch(newProducts) - if err != nil { - return err + if len(newProducts) > 0 { + for _, p := range newProducts { + p.Id = 0 + } + err = model.NewProductSearch(db).CreateBatch(newProducts) + if err != nil { + return err + } + var rel []*model.SalesDetailsProduct + for _, p := range newProducts { + rel = append(rel, &model.SalesDetailsProduct{ + SalesDetailsId: salesDetails.Id, + ProductId: p.Id, + }) + } + err = model.NewSalesDetailsProductSearch().CreateBatch(rel) + if err != nil { + return err + } } - var rel []*model.SalesDetailsProduct - for _, p := range newProducts { - rel = append(rel, &model.SalesDetailsProduct{ - SalesDetailsId: salesDetails.Id, - ProductId: p.Id, - }) - } - return model.NewSalesDetailsProductSearch().CreateBatch(rel) + return nil + }) if err != nil { -- Gitblit v1.8.0