feat 添加价格状态

This commit is contained in:
timerzz 2024-05-15 13:54:53 +08:00
parent 99c3e85dbc
commit 202f8a8911
2 changed files with 37 additions and 15 deletions

View File

@ -13,6 +13,14 @@ const (
defaultFreight float64 = 100 defaultFreight float64 = 100
) )
type PriceStatus int
const (
PriceStatus_NoChange PriceStatus = iota
PriceStatus_UP
PriceStatus_DOWN //降价
)
type Product struct { type Product struct {
ID uint `gorm:"primary_key" json:"id"` ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
@ -33,6 +41,7 @@ type Product struct {
Freight float64 `json:"freight"` //运费 Freight float64 `json:"freight"` //运费
ExchangeRate float64 `json:"exchangeRate" gorm:"-"` //汇率 ExchangeRate float64 `json:"exchangeRate" gorm:"-"` //汇率
Rate float64 `json:"rate"` //利润率 Rate float64 `json:"rate"` //利润率
PriceStatus PriceStatus `json:"priceStatus"` //价格状态
Remark string `json:"remark"` //备注 Remark string `json:"remark"` //备注
HistoryPrices []HistoryPrice `gorm:"foreignKey:Pid;references:Pid" json:"historyPrices"` 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) { func (p *Product) BeforeSave(tx *gorm.DB) (err error) {
tx.Model(&Product{}).Where("pid = ?", p.Pid).Pluck("freight", &p.Freight)
p.CalRate() p.CalRate()
var lastPrice float64 var lastPrice float64
tx.Model(&HistoryPrice{}).Where("pid = ?", p.Pid).Order("created_at desc").Limit(1).Pluck("us_price", &lastPrice) 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{ p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{
USPrice: p.USPrice, USPrice: p.USPrice,
}) })
if p.USPrice > lastPrice {
p.PriceStatus = PriceStatus_UP
} else {
p.PriceStatus = PriceStatus_DOWN
}
} }
return return
} }

View File

@ -12,7 +12,7 @@ func TestUpsert(t *testing.T) {
User: "timerzz", User: "timerzz",
Password: "zhhg1997", Password: "zhhg1997",
Port: "5432", Port: "5432",
DBName: "kedaya", DBName: "kedaya_dev",
}) })
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -23,28 +23,36 @@ func TestUpsert(t *testing.T) {
} }
db.Clauses(clause.OnConflict{ db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "pid"}}, 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{ }).Create([]Product{
{ {
Name: "test", Name: "test",
Pid: "test-1", Pid: "test-3",
Color: "green", Color: "green",
Link: "www.baidu.com", Link: "www.baidu.com",
Image: "image", Image: "image",
Orderable: false, Orderable: false,
USPrice: 123, USPrice: 123,
DWPrice: 3000, DWPrice: 3000,
Freight: 120, Freight: 130,
},
{
Name: "test2",
Pid: "test-2",
Color: "green",
Link: "www.baidu.com",
Image: "image",
Orderable: false,
USPrice: 421,
DWPrice: 3000,
}, },
}) })
} }
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)
}
}