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)