From 3202eb827b52ee23452dfa15e4f8e98886780939 Mon Sep 17 00:00:00 2001 From: timerzz Date: Wed, 15 May 2024 20:41:42 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BF=AE=E6=94=B9gorm=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/main.go | 12 ++++++++++++ pkg/coach-client/model.go | 4 ++-- product/controller.go | 7 +++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 0b6ba53..72a7457 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -11,9 +11,12 @@ import ( "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/cors" "github.com/golang/glog" + "gorm.io/gorm/logger" + "log" "log/slog" "os" "os/signal" + "time" ) func main() { @@ -28,6 +31,15 @@ func main() { if err != nil { glog.Fatalf("初始化数据库失败:%v", err) } + db.Logger = logger.New( + log.New(os.Stdout, "\r\n", log.LstdFlags), + logger.Config{ + SlowThreshold: time.Second, + LogLevel: logger.Warn, + IgnoreRecordNotFoundError: false, + Colorful: true, + }, + ) // 代理池 pool := proxy.NewProxyPool(cfg.Proxy.Subscribes) diff --git a/pkg/coach-client/model.go b/pkg/coach-client/model.go index 19169dc..e5fb70a 100644 --- a/pkg/coach-client/model.go +++ b/pkg/coach-client/model.go @@ -40,8 +40,8 @@ type Variant struct { } type Price struct { - Sales Sales `json:"sales"` - MarkdownDiscPercent float32 `json:"markdownDiscPercent"` + Sales Sales `json:"sales"` + MarkdownDiscPercent int `json:"markdownDiscPercent"` } type Sales struct { diff --git a/product/controller.go b/product/controller.go index ec66aaa..435119e 100644 --- a/product/controller.go +++ b/product/controller.go @@ -5,6 +5,7 @@ import ( "fmt" coach_client "gitea.timerzz.com/kedaya_haitao/coach-spider/pkg/coach-client" "gitea.timerzz.com/kedaya_haitao/common/model/product" + "github.com/golang/glog" "github.com/samber/lo" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -72,6 +73,7 @@ func (c *Controller) Crawl() error { if err = c.saveRespData(resp.PageData.Products); err != nil { return fmt.Errorf("保存第%d页数据失败: %w", page, err) } + glog.Infof("第%d页数据保存成功", page) } return nil } @@ -90,13 +92,14 @@ func (c *Controller) saveRespData(list []coach_client.Product) error { c.Freight = saveFreight } products = append(products, productv1.Product{ + UpdatedAt: time.Now(), Name: resp.Name, Pid: color.VgId, Color: color.Text, Link: fmt.Sprintf("%s/%s", "https://www.coachoutlet.com", color.Url), Image: color.Media.Thumbnail.Src, Orderable: color.Orderable, - DiscPercent: int(price.Price.MarkdownDiscPercent), + DiscPercent: price.Price.MarkdownDiscPercent, USPrice: price.Price.Sales.Value, Freight: c.Freight, ExchangeRate: c.ExchangeRate, @@ -109,6 +112,6 @@ func (c *Controller) saveRespData(list []coach_client.Product) error { }) return c.db.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "pid"}}, - DoUpdates: clause.AssignmentColumns([]string{"name", "color", "link", "orderable", "us_price", "cny_price", "cal_mark", "rate", "price_status", "disc_percent"}), + DoUpdates: clause.AssignmentColumns([]string{"name", "color", "link", "orderable", "us_price", "cny_price", "cal_mark", "rate", "price_status", "disc_percent", "updated_at"}), }).Create(products).Error }