product update first test

This commit is contained in:
merdan 2023-05-03 19:45:50 +05:00
parent 4201c053fb
commit d0e4ab8a4d
2 changed files with 31 additions and 15 deletions

12
main.go
View File

@ -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))
}
}
}

View File

@ -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)
}