From bec654c8ce2494063ba7ed97a3ef924692f3dd87 Mon Sep 17 00:00:00 2001 From: timerzz Date: Thu, 21 Nov 2024 15:44:31 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=95=86=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spider/controller.go | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/spider/controller.go b/spider/controller.go index e3f0dea..2d8d5e4 100644 --- a/spider/controller.go +++ b/spider/controller.go @@ -269,6 +269,13 @@ func (c *Controller) saveProducts(articles []v2.Article) { } func (c *Controller) FetchArticleDetail(ctx context.Context, pid string) error { + old, err := c.storage.ProviderArticle().Get(storage.NewGetProviderArticleQuery().SetProviderId(c.providerId).SetPid(pid)) + if err == nil { + return fmt.Errorf("该商品已经存在") + } + if !errors.As(err, &gorm.ErrRecordNotFound) { + return err + } resp, err := c.client.RequestProductDetail(ctx, pid) if err != nil { return fmt.Errorf("请求商品信息失败: %v", err) @@ -279,18 +286,25 @@ func (c *Controller) FetchArticleDetail(ctx context.Context, pid string) error { EnglishName: resp.Name, Pid: resp.Id, Brand: v2.Brand_Coach, - Providers: []v2.ProviderArticle{ - { - ProviderId: c.providerId, - Brand: v2.Brand_Coach, - Pid: pid, - SkuID: pid, - Available: resp.Inventory.Orderable, - Ast: resp.Inventory.Ats, - Link: fmt.Sprintf("%s/%s", "https://www.coachoutlet.com", resp.Url), - }, - }, } + pArticle := v2.ProviderArticle{ + ProviderId: c.providerId, + Brand: v2.Brand_Coach, + Pid: pid, + SkuID: pid, + Available: resp.Inventory.Orderable, + Ast: resp.Inventory.Ats, + Link: fmt.Sprintf("%s/%s", "https://www.coachoutlet.com", resp.Url), + Cost: utils.CalculateProviderPrice( + append(c.provider.CalculateProcess, old.CalculateProcess...), + map[string]float64{ + "originalPrice": resp.Prices.CurrentPrice, + "freight": c.provider.Config.Freight, + "exchangeRate": c.provider.Config.ExchangeRate, + }), + } + pArticle.HistoryPrice = append(pArticle.HistoryPrice, pArticle.Cost) + article.Providers = append(article.Providers, pArticle) if len(resp.ImageGroups) > 0 { article.Image = resp.ImageGroups[0].Images[0].Src } @@ -298,7 +312,7 @@ func (c *Controller) FetchArticleDetail(ctx context.Context, pid string) error { return nil } -func (c *Controller)FetchArticleAts(ctx context.Context, pid string) error { +func (c *Controller) FetchArticleAts(ctx context.Context, pid string) error { pArticle, err := c.storage.ProviderArticle().Get(storage.NewGetProviderArticleQuery().SetProviderId(c.providerId).SetPid(pid)) if err != nil { return fmt.Errorf("获取商品信息失败: %v", err)