diff --git a/server/spider.go b/server/spider.go index 63b3b2b..6004c53 100644 --- a/server/spider.go +++ b/server/spider.go @@ -28,6 +28,7 @@ func (s *SpiderSvc) Registry(r fiber.Router) { api.Post(fmt.Sprintf("provider/%s/price/fetch/:id", s.providerId), s.FetchArticlePrice) api.Post(fmt.Sprintf("provider/%s/detail/fetch/:pid", s.providerId), s.FetchArticleDetail) api.Post(fmt.Sprintf("provider/%s/ats/fetch/:pid", s.providerId), s.FetchArticleAts) + api.Get(fmt.Sprintf("provider/%s/ats/:pid", s.providerId), s.GetArticleAts) //r.Post("spider/us/coach-outlet/calculate", s.UpsertCalculate) //r.Delete("spider/us/coach-outlet/calculate/u/:id", s.DelCalculate) @@ -73,6 +74,18 @@ func (s *SpiderSvc) FetchArticleAts(ctx fiber.Ctx) error { return ctx.JSON(web.NewResponse("ok")) } +func (s *SpiderSvc) GetArticleAts(ctx fiber.Ctx) error { + pid := ctx.Params("pid") + if pid == "" { + return fmt.Errorf("pid is empty") + } + ats, err := s.ctl.GetArticleAts(ctx.Context(), pid) + if err != nil { + return err + } + return ctx.JSON(web.NewResponse(ats)) +} + //func (s *SpiderSvc) DelCalculate(ctx fiber.Ctx) error { // idStr := ctx.Params("id") // if idStr == "" { diff --git a/spider/controller.go b/spider/controller.go index 2d8d5e4..007c3ee 100644 --- a/spider/controller.go +++ b/spider/controller.go @@ -325,6 +325,14 @@ func (c *Controller) FetchArticleAts(ctx context.Context, pid string) error { return c.storage.ProviderArticle().Update(pArticle, "ast") } +func (c *Controller) GetArticleAts(ctx context.Context, pid string) (int, error) { + inv, err := c.client.RequestInventory(ctx, pid) + if err != nil { + return 0, fmt.Errorf("请求商品库存失败: %v", err) + } + return inv.Ats, nil +} + // 更新某个商品的价格 func (c *Controller) FetchArticlePrice(ctx context.Context, id uint) error { pArticle, err := c.storage.ProviderArticle().Get(storage.NewGetProviderArticleQuery().SetProviderId(c.providerId).SetID(id))