From dd5b1276811f07841c255e18538e4e448303fd1d Mon Sep 17 00:00:00 2001 From: timerzz Date: Fri, 28 Mar 2025 12:31:15 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E4=BB=B7=E6=A0=BC=E6=B2=A1=E7=94=9F=E6=95=88=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spider/controller.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/spider/controller.go b/spider/controller.go index c611e5f..3b6502b 100644 --- a/spider/controller.go +++ b/spider/controller.go @@ -126,9 +126,15 @@ func (c *Controller) providerChange(ctx context.Context, message string) error { c.provider.CalculateProcess = provider.CalculateProcess if needUpdate { + logrus.Infof("供应商价格计算过程发生变化,重新计算所有商品价格") provider.Status = v2.ProviderStatus_Calculating - _ = c.storage.Provider().UpdateStatus(provider) + if err = c.storage.Provider().UpdateStatus(provider); err != nil { + logrus.Errorf("更新供应商状态失败: %v", err) + } + var results = make([]v2.ProviderArticle, 0, 20) + + var processed = 0 err = c.storage.ProviderArticle().FindInBatches(storage.NewGetProviderArticleQuery().SetProviderId(c.providerId), &results, func(tx *gorm.DB, batch int) error { for idx := range results { results[idx].Cost = utils.CalculateProviderPrice(append(provider.CalculateProcess, results[idx].CalculateProcess...), map[string]float64{ @@ -141,15 +147,19 @@ func (c *Controller) providerChange(ctx context.Context, message string) error { return err } ids := lo.Map(results, func(item v2.ProviderArticle, index int) string { - return fmt.Sprintf("%d", item.ID) + return fmt.Sprintf("%d", item.ArticleID) }) if err = c.subscribeClient.Publish(c.ctx, utils.ProfitRate_Channel, strings.Join(ids, ",")); err != nil { logrus.Errorf("通知商品利润率失败: %v", err) } + processed += len(results) return nil }) provider.Status = v2.ProviderStatus_Normal - _ = c.storage.Provider().UpdateStatus(provider) + if err = c.storage.Provider().UpdateStatus(provider); err != nil { + logrus.Errorf("更新供应商状态失败: %v", err) + } + logrus.Infof("重新计算所有商品价格完成共%d个", processed) } return err }