From 202f8a89111af12eb67655b31c94a4bc7f7f810c Mon Sep 17 00:00:00 2001 From: timerzz Date: Wed, 15 May 2024 13:54:53 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E4=BB=B7=E6=A0=BC?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/product/model.go | 16 +++++++++++++++- model/product/model_test.go | 36 ++++++++++++++++++++++-------------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/model/product/model.go b/model/product/model.go index 6471745..cfa387d 100644 --- a/model/product/model.go +++ b/model/product/model.go @@ -13,6 +13,14 @@ const ( defaultFreight float64 = 100 ) +type PriceStatus int + +const ( + PriceStatus_NoChange PriceStatus = iota + PriceStatus_UP + PriceStatus_DOWN //降价 +) + type Product struct { ID uint `gorm:"primary_key" json:"id"` CreatedAt time.Time `json:"createdAt"` @@ -33,6 +41,7 @@ type Product struct { Freight float64 `json:"freight"` //运费 ExchangeRate float64 `json:"exchangeRate" gorm:"-"` //汇率 Rate float64 `json:"rate"` //利润率 + PriceStatus PriceStatus `json:"priceStatus"` //价格状态 Remark string `json:"remark"` //备注 HistoryPrices []HistoryPrice `gorm:"foreignKey:Pid;references:Pid" json:"historyPrices"` } @@ -42,7 +51,6 @@ func (p *Product) TableName() string { } func (p *Product) BeforeSave(tx *gorm.DB) (err error) { - tx.Model(&Product{}).Where("pid = ?", p.Pid).Pluck("freight", &p.Freight) p.CalRate() var lastPrice float64 tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("us_price", &lastPrice) @@ -50,6 +58,12 @@ func (p *Product) BeforeSave(tx *gorm.DB) (err error) { p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{ USPrice: p.USPrice, }) + if p.USPrice > lastPrice { + p.PriceStatus = PriceStatus_UP + } else { + p.PriceStatus = PriceStatus_DOWN + + } } return } diff --git a/model/product/model_test.go b/model/product/model_test.go index db1cf1b..0317cbf 100644 --- a/model/product/model_test.go +++ b/model/product/model_test.go @@ -12,7 +12,7 @@ func TestUpsert(t *testing.T) { User: "timerzz", Password: "zhhg1997", Port: "5432", - DBName: "kedaya", + DBName: "kedaya_dev", }) if err != nil { t.Fatal(err) @@ -23,28 +23,36 @@ func TestUpsert(t *testing.T) { } db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "pid"}}, - DoUpdates: clause.AssignmentColumns([]string{"name", "color", "link", "orderable", "us_price", "cny_price", "cal_mark", "freight", "rate"}), + DoUpdates: clause.AssignmentColumns([]string{"name", "color", "link", "orderable", "us_price", "cny_price", "cal_mark", "rate"}), }).Create([]Product{ { Name: "test", - Pid: "test-1", + Pid: "test-3", Color: "green", Link: "www.baidu.com", Image: "image", Orderable: false, USPrice: 123, DWPrice: 3000, - Freight: 120, - }, - { - Name: "test2", - Pid: "test-2", - Color: "green", - Link: "www.baidu.com", - Image: "image", - Orderable: false, - USPrice: 421, - DWPrice: 3000, + Freight: 130, }, }) } + +func TestUpdate(t *testing.T) { + db, err := database.InitDatabase(&database.DBOption{ + Host: "192.168.31.55", + User: "timerzz", + Password: "zhhg1997", + Port: "5432", + DBName: "kedaya_dev", + }) + if err != nil { + t.Fatal(err) + } + var p = Product{Pid: "CJ575-QBO4G", DWPrice: 3000, Freight: 150, Remark: "test", USPrice: 209.3, ExchangeRate: 7.3} + err = db.Model(&p).Where("pid = ?", p.Pid).Select("dw_price", "freight", "remark", "rate", "cal_mark", "cny_price").Updates(&p).Error + if err != nil { + t.Fatal(err) + } +}