actualize items list in db

This commit is contained in:
Aleksandr Trushkin
2024-02-04 13:14:56 +03:00
parent be6aec73df
commit 5c8f238fb8
4 changed files with 138 additions and 43 deletions

View File

@ -875,10 +875,17 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
var i int
var start int
seenItems, err := entity.IterIntoMap[string, entity.GoodsItem](repository.GoodsItem().List(ctx)).Map(func(gi entity.GoodsItem) (string, error) {
return gi.Articul, nil
})
if err != nil {
return fmt.Errorf("making seen items map: %w", err)
}
goodsItems := make([]entity.GoodsItem, 0, batchSize)
productIDs := make([]int, 0, batchSize)
knownCategories := make(map[string]struct{})
knownCategories := make(map[string]struct{})
err = entity.IterWithErr(repository.Category().List(ctx)).Do(func(c entity.Category) error {
knownCategories[c.Name] = struct{}{}
return nil
@ -887,6 +894,8 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
return fmt.Errorf("filling known categories: %w", err)
}
itemsUpdated := make(map[string]struct{}, len(seenItems))
startFrom := time.Now()
for {
select {
@ -918,9 +927,15 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
goodsItems = goodsItems[:0]
for _, item := range items {
pi, err := client.GetProductInfo(ctx, int64(item.Cart))
if err != nil {
return fmt.Errorf("getting product info: %w", err)
var pi entity.GoodsItemInfo
if seenItem, ok := seenItems[item.SKU]; ok {
pi.Parameters = seenItem.Parameters
pi.PhotoURLs = seenItem.PhotoURLs
} else {
pi, err = client.GetProductInfo(ctx, int64(item.Cart))
if err != nil {
return fmt.Errorf("getting product info: %w", err)
}
}
goodsItem, err := entity.MakeGoodsItem(item, remnants, pi)
@ -929,8 +944,9 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
continue
}
goodsItems = append(goodsItems, goodsItem)
itemsUpdated[goodsItem.Articul] = struct{}{}
goodsItems = append(goodsItems, goodsItem)
if goodsItem.Type == "" {
continue
}
@ -982,6 +998,13 @@ func parseEwayDumpAction(ctx context.Context, cmd *cli.Command) error {
i++
}
for k := range itemsUpdated {
delete(seenItems, k)
}
for k := range seenItems {
repository.GoodsItem().Delete(ctx, k)
}
return nil
}