From c1d9c77312a7949c4949baf69985e5686b992cef Mon Sep 17 00:00:00 2001 From: timerzz Date: Tue, 14 May 2024 20:43:05 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=8A=BD=E7=A6=BB=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/model.go | 105 ----------------------------------------- database/model_test.go | 50 -------------------- go.mod | 3 +- go.sum | 6 ++- product/controller.go | 10 ++-- 5 files changed, 11 insertions(+), 163 deletions(-) delete mode 100644 database/model.go delete mode 100644 database/model_test.go diff --git a/database/model.go b/database/model.go deleted file mode 100644 index 8806500..0000000 --- a/database/model.go +++ /dev/null @@ -1,105 +0,0 @@ -package database - -import ( - "fmt" - "gorm.io/gorm" - "math" - "strings" - "time" -) - -const ( - defaultExchangeRate float64 = 7.3 - defaultFreight float64 = 100 -) - -type Product struct { - ID uint `gorm:"primary_key" json:"id"` - CreatedAt time.Time `json:"createdAt"` - UpdatedAt time.Time `json:"updatedAt"` - DeletedAt gorm.DeletedAt `gorm:"index"` - - Name string `gorm:"index" json:"name"` - Pid string `gorm:"unique;not null" json:"pid"` - Color string `json:"color"` - Link string `json:"link"` - Image string `json:"image"` - Orderable bool `json:"orderable"` - - USPrice float64 `json:"usPrice"` - CNYPrice float64 `json:"cnyPrice"` - CalMark string `json:"calMark"` //计算价格的过程 - DWPrice float64 `json:"dwPrice"` - Freight float64 `json:"freight"` //运费 - ExchangeRate float64 `json:"exchangeRate" gorm:"-"` //汇率 - Rate float64 `json:"rate"` //利润率 - Remark string `json:"remark"` //备注 - HistoryPrices []HistoryPrice `gorm:"foreignKey:Pid;references:Pid" json:"historyPrices"` -} - -func (p *Product) TableName() string { - return "products" -} - -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) - if p.USPrice != lastPrice { - p.HistoryPrices = append(p.HistoryPrices, HistoryPrice{ - USPrice: p.USPrice, - }) - } - return -} - -func (p *Product) CalRate() { - calculationProcess := make([]string, 0, 2) //存放计算过程 - if p.ExchangeRate == 0 { - p.ExchangeRate = defaultExchangeRate - } - if p.Freight == 0 { - p.Freight = defaultFreight - } - - //先计算打九折的价格 - tmpPrice := p.USPrice * 0.9 // 这是打九折的价格 - p.CNYPrice = tmpPrice*p.ExchangeRate + p.Freight - calculationProcess = append(calculationProcess, fmt.Sprintf("【九折】%.2f * 0.9 * %.2f + %.2f = %.2f", p.USPrice, p.ExchangeRate, p.Freight, p.CNYPrice)) - if p.USPrice >= 150 { - calculationProcess = append(calculationProcess, fmt.Sprintf("【150 -20 】(%.2f - 20) * %.2f + %.2f = %.2f", p.USPrice, p.ExchangeRate, p.Freight, (p.USPrice-20)*p.ExchangeRate+p.Freight)) - if p.USPrice-20 < tmpPrice { - // 符合满150-20,而且比九折便宜 - tmpPrice = p.USPrice - 20 - p.CNYPrice = tmpPrice*p.ExchangeRate + p.Freight - } - } else if p.USPrice >= 100 { - calculationProcess = append(calculationProcess, fmt.Sprintf("【100 -10 】(%.2f - 10) * %.2f + %.2f = %.2f", p.USPrice, p.ExchangeRate, p.Freight, (p.USPrice-10)*p.ExchangeRate+p.Freight)) - if p.USPrice-10 < tmpPrice { - // 符合满100 -10,而且比九折便宜 - tmpPrice = p.USPrice - 10 - p.CNYPrice = tmpPrice*p.ExchangeRate + p.Freight - } - } - p.CalMark = strings.Join(calculationProcess, "\n") - if p.DWPrice > 0 { - // 如果有得物价格,就计算收益率 - p.Rate = Decimal((p.DWPrice - p.CNYPrice) / p.CNYPrice * 100) - } - - p.CNYPrice = Decimal(p.CNYPrice) -} - -// Decimal 保留两位小数 -func Decimal(value float64) float64 { - return math.Trunc(value*1e2+0.5) * 1e-2 -} - -type HistoryPrice struct { - ID uint `gorm:"primary_key" json:"id"` - CreatedAt time.Time `json:"createdAt"` - - Pid string `gorm:"index" json:"pid"` - USPrice float64 `json:"usPrice"` -} diff --git a/database/model_test.go b/database/model_test.go deleted file mode 100644 index 2f60374..0000000 --- a/database/model_test.go +++ /dev/null @@ -1,50 +0,0 @@ -package database - -import ( - "gitea.timerzz.com/kedaya_haitao/common/pkg/database" - "gorm.io/gorm/clause" - "testing" -) - -func TestUpsert(t *testing.T) { - db, err := database.InitDatabase(&database.DBOption{ - Host: "192.168.31.55", - User: "timerzz", - Password: "zhhg1997", - Port: "5432", - DBName: "kedaya", - }) - if err != nil { - t.Fatal(err) - } - err = db.AutoMigrate(&Product{}, &HistoryPrice{}) - if err != nil { - t.Fatal(err) - } - 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"}), - }).Create([]Product{ - { - Name: "test", - Pid: "test-1", - 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, - }, - }) -} diff --git a/go.mod b/go.mod index 738b0d1..1c050d7 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitea.timerzz.com/kedaya_haitao/coach-spider go 1.22.2 require ( - gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240513121252-d83795b6d236 + gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240514123648-99c3e85dbc18 github.com/go-resty/resty/v2 v2.13.1 github.com/golang/glog v1.2.1 github.com/pkg/errors v0.9.1 @@ -32,6 +32,7 @@ require ( github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/coreos/go-iptables v0.7.0 // indirect + github.com/corpix/uarand v0.2.0 // indirect github.com/ericlagergren/aegis v0.0.0-20230312195928-b4ce538b56f9 // indirect github.com/ericlagergren/polyval v0.0.0-20220411101811-e25bc10ba391 // indirect github.com/ericlagergren/siv v0.0.0-20220507050439-0b757b3aa5f1 // indirect diff --git a/go.sum b/go.sum index db4a189..a947563 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240513121252-d83795b6d236 h1:1t5dh6l20cexlTqmGesIY2ClqrLBqy+OE8LAVp//JC8= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240513121252-d83795b6d236/go.mod h1:jQhygETzidSqipdL3N77SJBJVEcvKYxdNDUPfIuHdL8= +gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240514123648-99c3e85dbc18 h1:lTARBVopKcR47VJGzumhNDTmxc0QUpzHnk9AuD/6kfs= +gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240514123648-99c3e85dbc18/go.mod h1:cgLy4gB0z2UZD3kBp1IAemt28dkaKNdt4RC0LNkE1I8= github.com/3andne/restls-client-go v0.1.6 h1:tRx/YilqW7iHpgmEL4E1D8dAsuB0tFF3uvncS+B6I08= github.com/3andne/restls-client-go v0.1.6/go.mod h1:iEdTZNt9kzPIxjIGSMScUFSBrUH6bFRNg0BWlP4orEY= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -51,6 +51,8 @@ github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/coreos/go-iptables v0.7.0 h1:XWM3V+MPRr5/q51NuWSgU0fqMad64Zyxs8ZUoMsamr8= github.com/coreos/go-iptables v0.7.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFETJConOQ//Q= +github.com/corpix/uarand v0.2.0 h1:U98xXwud/AVuCpkpgfPF7J5TQgr7R5tqT8VZP5KWbzE= +github.com/corpix/uarand v0.2.0/go.mod h1:/3Z1QIqWkDIhf6XWn/08/uMHoQ8JUoTIKc2iPchBOmM= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/product/controller.go b/product/controller.go index 6253a9e..d094165 100644 --- a/product/controller.go +++ b/product/controller.go @@ -3,8 +3,8 @@ package product import ( "context" "fmt" - "gitea.timerzz.com/kedaya_haitao/coach-spider/database" coach_client "gitea.timerzz.com/kedaya_haitao/coach-spider/pkg/coach-client" + "gitea.timerzz.com/kedaya_haitao/common/model/product" "github.com/samber/lo" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -31,7 +31,7 @@ func NewController(client *coach_client.Client, db *gorm.DB) *Controller { } func (c *Controller) AutoMigrate() { - if err := c.db.AutoMigrate(&database.Product{}, &database.HistoryPrice{}, &Option{}); err != nil { + if err := c.db.AutoMigrate(&productv1.Product{}, &productv1.HistoryPrice{}, &Option{}); err != nil { panic(err) } } @@ -77,13 +77,13 @@ func (c *Controller) Crawl() error { } func (c *Controller) saveRespData(list []coach_client.Product) error { - var products = make([]database.Product, 0, len(list)) + var products = make([]productv1.Product, 0, len(list)) for _, resp := range list { for _, color := range resp.Colors { price, _ := lo.Find(resp.VariantsOnSale, func(item coach_client.Variant) bool { return item.Id == color.VgId }) - products = append(products, database.Product{ + products = append(products, productv1.Product{ Name: resp.Name, Pid: color.VgId, Color: color.Text, @@ -97,7 +97,7 @@ func (c *Controller) saveRespData(list []coach_client.Product) error { } } // 去重 - products = lo.UniqBy(products, func(p database.Product) string { + products = lo.UniqBy(products, func(p productv1.Product) string { return p.Pid }) return c.db.Clauses(clause.OnConflict{