From 48f65937abf36a44af4a826d754bbd158d3f17f1 Mon Sep 17 00:00:00 2001 From: timerzz Date: Wed, 15 May 2024 15:12:39 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=9B=B4=E6=96=B0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=90=8E=E6=9B=B4=E6=96=B0=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- product/option.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/product/option.go b/product/option.go index 0b641ee..c0a301c 100644 --- a/product/option.go +++ b/product/option.go @@ -1,6 +1,10 @@ package product -import "time" +import ( + productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product" + "gorm.io/gorm" + "time" +) type Option struct { ID uint `gorm:"primary_key" json:"id"` @@ -17,14 +21,37 @@ func (c *Controller) SaveOption(opt Option) { if opt.Interval > 0 { c.Option.Interval = opt.Interval } - if opt.ExchangeRate > 0 { + var change bool + var oldFreight float64 + if opt.ExchangeRate > 0 && opt.ExchangeRate != c.Option.ExchangeRate { c.Option.ExchangeRate = opt.ExchangeRate + change = true } - if opt.Freight > 0 { - c.Option.Freight = opt.Freight + if opt.Freight > 0 && opt.Freight != c.Option.Freight { + c.Option.Freight, oldFreight = opt.Freight, c.Option.Freight + change = true } opt.ID = 1 c.db.Updates(opt) + if change { + c.updateExchangeRateAndFreight(oldFreight) + } +} + +func (c *Controller) updateExchangeRateAndFreight(oldFreight float64) { + var results []*productv1.Product + c.db.FindInBatches(&results, 20, func(tx *gorm.DB, batch int) error { + for _, result := range results { + if result.Freight == oldFreight { + result.Freight = c.Option.Freight + } + result.ExchangeRate = c.Option.ExchangeRate + } + + // 保存对当前批记录的修改 + tx.Save(&results) + return nil + }) } func (c *Controller) GetOption() Option {