From 77e0c69168aefc361202e3ce255c24f68b45511b Mon Sep 17 00:00:00 2001 From: timerzz Date: Fri, 14 Jun 2024 16:27:31 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BF=AE=E6=94=B9=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=E4=BB=B7=E6=A0=BC=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/product/model.go | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/model/product/model.go b/model/product/model.go index 773e1d5..cf3d548 100644 --- a/model/product/model.go +++ b/model/product/model.go @@ -64,6 +64,20 @@ func (p *Product) TableName() string { func (p *Product) BeforeSave(tx *gorm.DB) (err error) { p.CalRate() + p.fillPriceStatus(tx) + return +} + +func (p *Product) fillPriceStatus(tx *gorm.DB) { + switch p.Website { + case WebSite_US_Coach_Outlet: + p.fillUSCoachPriceStatus(tx) + case WebSite_CN_Coach: + p.fillCNCoachPriceStatus(tx) + } +} + +func (p *Product) fillUSCoachPriceStatus(tx *gorm.DB) { var lastPrice float64 tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("us_price", &lastPrice) if p.USPrice != lastPrice { @@ -74,10 +88,23 @@ func (p *Product) BeforeSave(tx *gorm.DB) (err error) { p.PriceStatus = PriceStatus_UP } else { p.PriceStatus = PriceStatus_DOWN - } } - return +} + +func (p *Product) fillCNCoachPriceStatus(tx *gorm.DB) { + var lastPrice float64 + tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("cny_price", &lastPrice) + if p.CNYPrice != lastPrice { + p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{ + USPrice: p.CNYPrice, + }) + if p.CNYPrice > lastPrice { + p.PriceStatus = PriceStatus_UP + } else { + p.PriceStatus = PriceStatus_DOWN + } + } } func (p *Product) CalRate() {