From a4e217d17a3173c9bbaca2e76b296f96355c03b6 Mon Sep 17 00:00:00 2001 From: timerzz Date: Sun, 1 Dec 2024 16:32:13 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8DsaveProduct=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E7=9A=84id=E4=B8=BA0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spider/controller.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spider/controller.go b/spider/controller.go index 3dc9d71..1d73849 100644 --- a/spider/controller.go +++ b/spider/controller.go @@ -229,15 +229,16 @@ func (c *Controller) saveProducts(articles []v2.Article) { } // 如果已经存在了,那么不需要重新创建article if err == nil { + article.ID = oldArticle.ID // 查看现在有没有这个供应商的商品 oldProviderArticle, _, exist := lo.FindIndexOf(oldArticle.Providers, func(item v2.ProviderArticle) bool { return item.ProviderId == c.providerId }) // 创建供应商商品 if !exist { - oldArticle.Providers = append(oldArticle.Providers, article.Providers...) + oldArticle.Providers = article.Providers // 保存商品信息 - if err = c.storage.Article().Upsert(article); err != nil { + if err = c.storage.Article().Upsert(oldArticle); err != nil { glog.Errorf("保存商品信息失败: %v", err) continue } @@ -248,6 +249,7 @@ func (c *Controller) saveProducts(articles []v2.Article) { } oldProviderArticle.Cost = pArticle.Cost oldProviderArticle.Ast = pArticle.Ast + oldProviderArticle.Available = pArticle.Available if err = c.storage.ProviderArticle().Upsert(oldProviderArticle); err != nil { glog.Errorf("保存供应商商品信息失败: %v", err) continue @@ -261,6 +263,10 @@ func (c *Controller) saveProducts(articles []v2.Article) { glog.Errorf("保存商品信息失败: %v", err) continue } + if err = c.storage.DB().Model(&v2.Article{}).Where("pid = ? and brand = ?", article.Pid, article.Brand).Select("id").First(&article.ID).Error; err != nil { + glog.Errorf("获取商品ID失败: %v", err) + continue + } } if err = c.subscribeClient.Publish(c.ctx, utils.ProfitRate_Channel, strconv.Itoa(int(article.ID))); err != nil {