feat 修改历史价格计算

This commit is contained in:
timerzz 2024-06-14 16:27:31 +08:00
parent 17299b2bb8
commit 77e0c69168

View File

@ -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
}
}
}
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
}
}
return
}
func (p *Product) CalRate() {