fix colorvariant

This commit is contained in:
merdan 2023-05-03 20:19:22 +05:00
parent d40cb4d8f8
commit 5a211c501f
2 changed files with 27 additions and 61 deletions

View File

@ -111,7 +111,7 @@ func worker(db *gorm.DB, stopCh <-chan struct{}, updatePeriod time.Duration) {
} }
// Update the product in the database // Update the product in the database
if err := importer.UpdateOrCreate(jsonProduct).Error; err != nil { if err := importer.UpdateOrCreate(jsonProduct, product.ProductID).Error; err != nil {
log.Println("Error updateing product information:", err) log.Println("Error updateing product information:", err)
} }

View File

@ -1,7 +1,6 @@
package repositories package repositories
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"gorm.io/gorm" "gorm.io/gorm"
@ -188,38 +187,9 @@ func (importer *Importer) importVariant(product models.Product) (*gm.Product, er
tx := importer.Baza.Begin() tx := importer.Baza.Begin()
if err := tx.Omit("Categories.*", "SuperAttributes.*", "ParentID").Create(&mainPorduct).Error; err != nil { if err := tx.Omit("Categories.*", "SuperAttributes.*", "ParentID").Create(&mainPorduct).Error; err != nil {
//todo update categories tx.Rollback()
byteErr, _ := json.Marshal(err) log.Println(err, "er1")
var newError GormErr return nil, err
if err1 := json.Unmarshal((byteErr), &newError); err1 != nil {
tx.Rollback()
log.Println(err1, "err2")
return nil, err1
}
if newError.Number == 1062 {
var barProduct gm.Product
if err2 := tx.First(&barProduct, "sku = ?", mainPorduct.Sku).Error; err2 != nil {
tx.Rollback()
log.Println(err2, "err3")
return nil, err2
}
if err3 := tx.Model(&barProduct).Association("Categories").Append(mainPorduct.Categories); err3 != nil {
tx.Rollback()
log.Println(err3, "err4")
return nil, err3
}
if err4 := tx.Commit().Error; err4 == nil {
return importer.updateVariant(product)
}
} else {
tx.Rollback()
log.Println(err, "er1")
return nil, err
}
} }
@ -382,10 +352,10 @@ func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption {
importer.SexMutex.Unlock() importer.SexMutex.Unlock()
return option return option
} }
func (importer *Importer) disableVariant(product models.Product) (instance *Importer) { func (importer *Importer) disableVariant(product models.Product, product_id uint) (instance *Importer) {
var flat gm.ProductFlat var flat gm.ProductFlat
importer.Error = importer.Baza.Preload("Product").Preload("Variants"). importer.Error = importer.Baza.Preload("Product").Preload("Variants").
First(&flat, "sku = ?", product.ProductNumber).Error First(&flat, "product_id = ?", product_id).Error
if importer.Error != nil { if importer.Error != nil {
return importer return importer
@ -401,10 +371,10 @@ func (importer *Importer) disableVariant(product models.Product) (instance *Impo
} }
func (importer *Importer) updateVariant(product models.Product) (*gm.Product, error) { func (importer *Importer) updateVariant(product models.Product, product_id uint) (*gm.Product, error) {
var flat gm.ProductFlat var flat gm.ProductFlat
err := importer.Baza.Preload("Product").Preload("Variants").First(&flat, "sku = ?", product.ProductNumber).Error err := importer.Baza.Preload("Product").Preload("Variants").First(&flat, "product_id = ?", product_id).Error
if err != nil { if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return importer.importVariant(product) return importer.importVariant(product)
@ -790,13 +760,13 @@ func (importer *Importer) UpdateOrCreateLCW(product models.Product) (instance *I
return importer return importer
} }
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) { func (importer *Importer) UpdateOrCreate(product models.Product, product_id uint) (instance *Importer) {
if !product.IsSellable { if !product.IsSellable {
return importer.disableVariant(product) return importer.disableVariant(product, product_id)
} }
firstProduct, err := importer.updateVariant(product) firstProduct, err := importer.updateVariant(product, product_id)
var newProducts []gm.Product var newProducts []gm.Product
if err != nil { if err != nil {
importer.Error = err importer.Error = err
@ -811,31 +781,27 @@ func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Impo
for _, colorVariant := range *product.ColorVariants { for _, colorVariant := range *product.ColorVariants {
var ( var (
variant *gm.Product variant *gm.Product
err error variantFlat *gm.ProductFlat
err error
) )
if !colorVariant.IsSellable {
if err = importer.Baza.Model(&gm.ProductFlat{}).Where("sku=?", colorVariant.ProductNumber).Update("status", false).Error; err != nil { err = importer.Baza.Preload("Product").Preload("Variants").First(&variantFlat, "sku = ?", colorVariant.ProductNumber).Error
log.Println(err)
} if err != nil && errors.Is(err, gorm.ErrRecordNotFound) && colorVariant.IsSellable {
variant, err = importer.importVariant(colorVariant)
newProducts = append(newProducts, *variant)
} else if err == nil && !colorVariant.IsSellable {
importer.disableVariant(colorVariant, variantFlat.ProductID)
continue continue
} else {
if variant, err = importer.importVariant(colorVariant); err != nil {
if variant, importer.Error = importer.updateVariant(colorVariant); importer.Error != nil {
return importer
}
linkedProducts = append(linkedProducts, *variant)
} else {
newProducts = append(newProducts, *variant)
}
} }
if variant, importer.Error = importer.updateVariant(colorVariant, variantFlat.ProductID); importer.Error != nil {
return importer
}
linkedProducts = append(linkedProducts, *variant)
} }
if len(newProducts) > 0 { if len(newProducts) > 0 {