2024-05-14 15:27:40 +08:00
|
|
|
package product
|
|
|
|
|
2024-05-15 15:12:39 +08:00
|
|
|
import (
|
|
|
|
productv1 "gitea.timerzz.com/kedaya_haitao/common/model/product"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"time"
|
|
|
|
)
|
2024-05-14 15:27:40 +08:00
|
|
|
|
|
|
|
type Option struct {
|
2024-05-14 16:17:04 +08:00
|
|
|
ID uint `gorm:"primary_key" json:"id"`
|
2024-05-14 21:18:24 +08:00
|
|
|
Interval time.Duration `yaml:"interval" json:"interval"`
|
|
|
|
ExchangeRate float64 `yaml:"exchangeRate" json:"exchangeRate"` //汇率
|
|
|
|
Freight float64 `yaml:"freight" json:"freight"` //运费
|
2024-05-14 15:27:40 +08:00
|
|
|
}
|
2024-05-14 16:17:04 +08:00
|
|
|
|
|
|
|
func (c *Controller) LoadOption() {
|
2024-06-15 21:10:37 +08:00
|
|
|
c.db.Where("id = ?", 1).Attrs(Option{Interval: time.Hour * 12, ExchangeRate: 7.3, Freight: 100}).FirstOrCreate(&c.Option)
|
2024-05-14 16:17:04 +08:00
|
|
|
}
|
2024-05-14 21:18:24 +08:00
|
|
|
|
|
|
|
func (c *Controller) SaveOption(opt Option) {
|
|
|
|
if opt.Interval > 0 {
|
|
|
|
c.Option.Interval = opt.Interval
|
|
|
|
}
|
2024-05-15 15:12:39 +08:00
|
|
|
var change bool
|
|
|
|
var oldFreight float64
|
|
|
|
if opt.ExchangeRate > 0 && opt.ExchangeRate != c.Option.ExchangeRate {
|
2024-05-14 21:18:24 +08:00
|
|
|
c.Option.ExchangeRate = opt.ExchangeRate
|
2024-05-15 15:12:39 +08:00
|
|
|
change = true
|
2024-05-14 21:18:24 +08:00
|
|
|
}
|
2024-05-15 15:12:39 +08:00
|
|
|
if opt.Freight > 0 && opt.Freight != c.Option.Freight {
|
|
|
|
c.Option.Freight, oldFreight = opt.Freight, c.Option.Freight
|
|
|
|
change = true
|
2024-05-14 21:18:24 +08:00
|
|
|
}
|
2024-06-15 21:10:37 +08:00
|
|
|
|
2024-05-14 21:18:24 +08:00
|
|
|
opt.ID = 1
|
|
|
|
c.db.Updates(opt)
|
2024-05-15 15:12:39 +08:00
|
|
|
if change {
|
2024-06-15 21:10:37 +08:00
|
|
|
go c.updateRate(oldFreight)
|
2024-05-15 15:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:48:04 +08:00
|
|
|
func (c *Controller) updateRate(oldFreight float64) {
|
2024-05-15 15:12:39 +08:00
|
|
|
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
|
2024-06-15 21:10:37 +08:00
|
|
|
var calculate []productv1.CalculateProcess
|
|
|
|
c.db.Find(&calculate, "pid = ? AND website = ?", result.Pid, productv1.WebSite_US_Coach_Outlet)
|
|
|
|
result.CalCNY(append(calculate, c.calculates...))
|
2024-05-15 15:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 保存对当前批记录的修改
|
|
|
|
tx.Save(&results)
|
|
|
|
return nil
|
|
|
|
})
|
2024-05-14 21:18:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GetOption() Option {
|
|
|
|
return c.Option
|
|
|
|
}
|