From d0e4ab8a4de20a2a74fdc3263eae8cc696d5f037 Mon Sep 17 00:00:00 2001 From: merdan Date: Wed, 3 May 2023 19:45:50 +0500 Subject: [PATCH] product update first test --- main.go | 12 +++++------ repositories/ImportRepository.go | 34 ++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 38ddbc0..a22e417 100644 --- a/main.go +++ b/main.go @@ -110,15 +110,13 @@ func worker(db *gorm.DB, stopCh <-chan struct{}, updatePeriod time.Duration) { continue } - log.Printf("Product %d updated: %+v\n", product.ID, jsonProduct) // Update the product in the database - //if err := importer.UpdateOrCreate(jsonProduct).Error; err != nil { - // log.Println("Error decoding product information:", err) - // continue - //} - // - //log.Printf("Product %d updated: %+v\n", product.ID, product) + if err := importer.UpdateOrCreate(jsonProduct).Error; err != nil { + log.Println("Error updateing product information:", err) + } + } + log.Printf("%d Products updated", len(result)) } } } diff --git a/repositories/ImportRepository.go b/repositories/ImportRepository.go index c2c8555..f8c463a 100644 --- a/repositories/ImportRepository.go +++ b/repositories/ImportRepository.go @@ -8,7 +8,6 @@ import ( "log" "math" gm "sarga_updater/bagisto_models" - helper "sarga_updater/helpers" models "sarga_updater/trendyol_models" "strconv" "sync" @@ -383,6 +382,24 @@ func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption { importer.SexMutex.Unlock() return option } +func (importer *Importer) disableVariant(product models.Product) (instance *Importer) { + var flat gm.ProductFlat + importer.Error = importer.Baza.Preload("Product").Preload("Variants"). + First(&flat, "sku = ?", product.ProductNumber).Error + + if importer.Error != nil { + return importer + } + + importer.Error = importer.Baza.Model(&flat).Update("status", false).Error + + importer.Error = importer.Baza.Model(&gm.ProductAttributeValue{}). + Where("attribute_id=8 AND product_id=?", flat.ProductID). + Update("boolean_value", false).Error + + return importer + +} func (importer *Importer) updateVariant(product models.Product) (*gm.Product, error) { @@ -414,6 +431,7 @@ func (importer *Importer) updateVariant(product models.Product) (*gm.Product, er if !variant.Sellable { importer.Baza.Model(&flatVariant).Update("status", false) + importer.Baza.Model(&gm.ProductAttributeValue{}).Where("attribute_id=8 AND product_id=?", flatVariant.ProductID).Update("boolean_value", false) } else { importer.updatePrice(variant.Price, &flatVariant) @@ -421,6 +439,7 @@ func (importer *Importer) updateVariant(product models.Product) (*gm.Product, er if !flatVariant.Status { importer.Baza.Model(&flatVariant).Update("status", true) + importer.Baza.Model(&gm.ProductAttributeValue{}).Where("attribute_id=8 AND product_id=?", flatVariant.ProductID).Update("boolean_value", true) } } found = true @@ -773,16 +792,15 @@ func (importer *Importer) UpdateOrCreateLCW(product models.Product) (instance *I func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) { + if !product.IsSellable { + return importer.disableVariant(product) + } + firstProduct, err := importer.updateVariant(product) var newProducts []gm.Product if err != nil { - helper.Error(err) - - firstProduct, importer.Error = importer.updateVariant(product) - - if importer.Error != nil { - return importer - } + importer.Error = err + return importer } else if &firstProduct != nil { newProducts = append(newProducts, *firstProduct) }