This commit is contained in:
parent
c93cfcf381
commit
48f65937ab
@ -1,6 +1,10 @@
|
|||||||
package product
|
package product
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Option struct {
|
type Option struct {
|
||||||
ID uint `gorm:"primary_key" json:"id"`
|
ID uint `gorm:"primary_key" json:"id"`
|
||||||
@ -17,14 +21,37 @@ func (c *Controller) SaveOption(opt Option) {
|
|||||||
if opt.Interval > 0 {
|
if opt.Interval > 0 {
|
||||||
c.Option.Interval = opt.Interval
|
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
|
c.Option.ExchangeRate = opt.ExchangeRate
|
||||||
|
change = true
|
||||||
}
|
}
|
||||||
if opt.Freight > 0 {
|
if opt.Freight > 0 && opt.Freight != c.Option.Freight {
|
||||||
c.Option.Freight = opt.Freight
|
c.Option.Freight, oldFreight = opt.Freight, c.Option.Freight
|
||||||
|
change = true
|
||||||
}
|
}
|
||||||
opt.ID = 1
|
opt.ID = 1
|
||||||
c.db.Updates(opt)
|
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 {
|
func (c *Controller) GetOption() Option {
|
||||||
|
Loading…
Reference in New Issue
Block a user