shimdilik importy disable yapiom ben
This commit is contained in:
parent
cbbf952748
commit
109e8ec19b
2
main.go
2
main.go
|
|
@ -111,7 +111,7 @@ func worker(db *gorm.DB, stopCh <-chan struct{}, updatePeriod time.Duration) {
|
|||
}
|
||||
|
||||
// Update the product in the database
|
||||
if importer.UpdateOrCreate(jsonProduct, product.ProductID).Error != nil {
|
||||
if importer.UpdateOrCreate(jsonProduct).Error != nil {
|
||||
log.Println("Error updateing product information:", err)
|
||||
importer.Error = nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,72 +111,77 @@ func ParseImporterInstance(db *gorm.DB) (instance *Importer, err error) {
|
|||
return instance, nil
|
||||
}
|
||||
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product, product_id uint) (instance *Importer) {
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) {
|
||||
|
||||
var firstProduct *gm.Product
|
||||
var newProducts []gm.Product
|
||||
//var newProducts []gm.Product
|
||||
var firstProductFlat *gm.ProductFlat
|
||||
err := importer.Baza.Preload("Product").Preload("Variants").First(&firstProductFlat, "sku = ?", product.ProductNumber).Error
|
||||
//haryt satuwdan ayrlan bolsa bagistoda disable et
|
||||
if !product.IsSellable {
|
||||
if firstProduct = importer.disableVariant(product, product_id); firstProduct == nil {
|
||||
//disable edende error doran bolsa dowam etme
|
||||
return importer
|
||||
|
||||
if err == nil {
|
||||
if !product.IsSellable {
|
||||
importer.disableVariant(*firstProductFlat)
|
||||
|
||||
} else {
|
||||
importer.updateVariant(product, *firstProductFlat)
|
||||
}
|
||||
|
||||
} else if firstProduct, _ = importer.updateVariant(product, product_id); firstProduct == nil {
|
||||
return importer
|
||||
}
|
||||
|
||||
if &firstProduct != nil {
|
||||
newProducts = append(newProducts, *firstProduct)
|
||||
}
|
||||
//else if err != nil && errors.Is(err, gorm.ErrRecordNotFound){
|
||||
// if firstProduct, err := importer.importVariant(product); err !=nil{
|
||||
// newProducts = append(newProducts, *firstProduct)
|
||||
// firstProductFlat = &gm.ProductFlat{}
|
||||
// firstProductFlat.Product = *firstProduct
|
||||
// }
|
||||
//}
|
||||
|
||||
if product.ColorVariants != nil && len(*product.ColorVariants) > 0 {
|
||||
linkedProducts := []gm.Product{*firstProduct}
|
||||
|
||||
linkedProducts := []gm.Product{firstProductFlat.Product}
|
||||
for _, colorVariant := range *product.ColorVariants {
|
||||
|
||||
var (
|
||||
variant *gm.Product
|
||||
//variant *gm.Product
|
||||
variantFlat *gm.ProductFlat
|
||||
err error
|
||||
)
|
||||
|
||||
err = importer.Baza.Preload("Product").Preload("Variants").First(&variantFlat, "sku = ?", colorVariant.ProductNumber).Error
|
||||
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) && colorVariant.IsSellable {
|
||||
if variant, err = importer.importVariant(colorVariant); err == nil {
|
||||
newProducts = append(newProducts, *variant)
|
||||
}
|
||||
//if err != nil && errors.Is(err, gorm.ErrRecordNotFound) && colorVariant.IsSellable {
|
||||
// if variant, err = importer.importVariant(colorVariant); err == nil {
|
||||
// newProducts = append(newProducts, *variant)
|
||||
// }
|
||||
//
|
||||
//} else
|
||||
|
||||
} else if err == nil && !colorVariant.IsSellable {
|
||||
variant = importer.disableVariant(colorVariant, variantFlat.ProductID)
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
if err == nil && !colorVariant.IsSellable {
|
||||
importer.disableVariant(*variantFlat)
|
||||
linkedProducts = append(linkedProducts, variantFlat.Product)
|
||||
|
||||
} else {
|
||||
if variant, importer.Error = importer.updateVariant(colorVariant, variantFlat.ProductID); importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
importer.updateVariant(colorVariant, *variantFlat)
|
||||
linkedProducts = append(linkedProducts, variantFlat.Product)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if len(newProducts) > 0 {
|
||||
// relation
|
||||
|
||||
var relation []gm.ProductRelation
|
||||
for _, linkedProduct := range linkedProducts {
|
||||
|
||||
for _, newProd := range newProducts {
|
||||
relation = append(relation, gm.ProductRelation{ParentID: linkedProduct.ID, ChildID: newProd.ID})
|
||||
relation = append(relation, gm.ProductRelation{ParentID: newProd.ID, ChildID: linkedProduct.ID})
|
||||
}
|
||||
}
|
||||
|
||||
if err := importer.Baza.Create(&relation).Error; err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
//if len(newProducts) > 0 {
|
||||
// // relation
|
||||
//
|
||||
// var relation []gm.ProductRelation
|
||||
// for _, linkedProduct := range linkedProducts {
|
||||
//
|
||||
// for _, newProd := range newProducts {
|
||||
// relation = append(relation, gm.ProductRelation{ParentID: linkedProduct.ID, ChildID: newProd.ID})
|
||||
// relation = append(relation, gm.ProductRelation{ParentID: newProd.ID, ChildID: linkedProduct.ID})
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if err := importer.Baza.Create(&relation).Error; err != nil {
|
||||
// log.Println(err)
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
return importer
|
||||
|
|
@ -184,12 +189,6 @@ func (importer *Importer) UpdateOrCreate(product models.Product, product_id uint
|
|||
|
||||
func (importer *Importer) importVariant(product models.Product) (*gm.Product, error) {
|
||||
|
||||
// check if wishlisted then update if.
|
||||
//if _, ok := importer.wishlist[product.ProductNumber]; ok {
|
||||
// delete(importer.wishlist,product.ProductNumber)
|
||||
// return importer.updateVariant(product)
|
||||
//}
|
||||
|
||||
productRepo := InitProductRepo(&product, importer.GetColorOption(product.Color), importer.GetSexOption(product.Cinsiyet))
|
||||
|
||||
if categories, err := gm.GetCatKeywords(importer.Baza, product.Categories); err != nil {
|
||||
|
|
@ -210,7 +209,7 @@ func (importer *Importer) importVariant(product models.Product) (*gm.Product, er
|
|||
tx := importer.Baza.Begin()
|
||||
|
||||
if err := tx.Omit("Categories.*", "SuperAttributes.*", "ParentID").Create(&mainPorduct).Error; err != nil {
|
||||
tx.Rollback()
|
||||
tx.Commit()
|
||||
log.Println(err, "er1")
|
||||
return nil, err
|
||||
|
||||
|
|
@ -423,44 +422,42 @@ func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption {
|
|||
return option
|
||||
}
|
||||
|
||||
func (importer *Importer) disableVariant(product models.Product, product_id uint) *gm.Product {
|
||||
var flat gm.ProductFlat
|
||||
importer.Error = importer.Baza.Preload("Product").Preload("Variants").
|
||||
First(&flat, "product_id = ?", product_id).Error
|
||||
func (importer *Importer) disableVariant(productFlat gm.ProductFlat) {
|
||||
|
||||
if importer.Error != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
importer.Error = importer.Baza.Model(&flat).Update("status", false).Error
|
||||
importer.Error = importer.Baza.Model(&productFlat).Update("status", false).Error
|
||||
|
||||
importer.Error = importer.Baza.Model(&gm.ProductAttributeValue{}).
|
||||
Where("attribute_id=8 AND product_id=?", flat.ProductID).
|
||||
Where("attribute_id=8 AND product_id=?", productFlat.ProductID).
|
||||
Update("boolean_value", false).Error
|
||||
|
||||
return &flat.Product
|
||||
if importer.Error != nil {
|
||||
log.Println(importer.Error)
|
||||
importer.Error = nil
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (importer *Importer) updateVariant(product models.Product, product_id uint) (*gm.Product, error) {
|
||||
func (importer *Importer) enableVariant(productFlat gm.ProductFlat) {
|
||||
importer.Error = importer.Baza.Model(&productFlat).Update("status", true).Error
|
||||
|
||||
var flat gm.ProductFlat
|
||||
|
||||
importer.Error = importer.Baza.Preload("Product").Preload("Variants").First(&flat, "product_id = ?", product_id).Error
|
||||
importer.Error = importer.Baza.Model(&gm.ProductAttributeValue{}).
|
||||
Where("attribute_id=8 AND product_id=?", productFlat.ProductID).
|
||||
Update("boolean_value", true).Error
|
||||
|
||||
if importer.Error != nil {
|
||||
if errors.Is(importer.Error, gorm.ErrRecordNotFound) {
|
||||
return importer.importVariant(product)
|
||||
}
|
||||
//todo not found bolsa create etmeli
|
||||
return nil, importer.Error
|
||||
log.Println(importer.Error)
|
||||
importer.Error = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (importer *Importer) updateVariant(product models.Product, flat gm.ProductFlat) {
|
||||
|
||||
if flat.Product.Type == "configurable" {
|
||||
|
||||
productRepo := InitProductRepo(&product, importer.GetColorOption(product.Color), importer.GetSexOption(product.Cinsiyet))
|
||||
if brand, err := gm.FindOrCreateBrand(importer.Baza, product.Brand, productRepo.Categories); err != nil {
|
||||
return nil, err
|
||||
log.Println(err)
|
||||
return
|
||||
} else {
|
||||
productRepo.Brand = brand
|
||||
}
|
||||
|
|
@ -473,16 +470,13 @@ func (importer *Importer) updateVariant(product models.Product, product_id uint)
|
|||
if variant.AttributeValue == flatVariant.BoyutLabel || variant.AttributeValue == flatVariant.SizeLabel {
|
||||
|
||||
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)
|
||||
importer.disableVariant(flatVariant)
|
||||
} else {
|
||||
|
||||
importer.updatePrice(variant.Price, &flatVariant)
|
||||
|
||||
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)
|
||||
importer.enableVariant(flatVariant)
|
||||
}
|
||||
}
|
||||
found = true
|
||||
|
|
@ -524,11 +518,9 @@ func (importer *Importer) updateVariant(product models.Product, product_id uint)
|
|||
importer.Baza.Omit("ParentID", "CreatedAt", "Variants", "SpecialPrice").Save(&flat)
|
||||
|
||||
} else { //simple
|
||||
|
||||
importer.updatePrice(product.Price, &flat)
|
||||
|
||||
}
|
||||
return &flat.Product, nil
|
||||
|
||||
}
|
||||
|
||||
func (importer *Importer) updatePrice(price models.Price, flat *gm.ProductFlat) {
|
||||
|
|
@ -565,195 +557,3 @@ func (importer *Importer) updatePrice(price models.Price, flat *gm.ProductFlat)
|
|||
Delete(&gm.ProductAttributeValue{})
|
||||
}
|
||||
}
|
||||
|
||||
func (importer *Importer) importParsedVariant(product models.Product) (gm.Product, error) {
|
||||
|
||||
//todo search if exists
|
||||
|
||||
productRepo := InitProductRepo(&product, importer.GetColorOption(product.Color), importer.GetSexOption(product.Cinsiyet))
|
||||
productRepo.ImageType = "lcw"
|
||||
if brand, err := gm.FindOrCreateBrand(importer.Baza, product.Brand, productRepo.Categories); err != nil {
|
||||
return gm.Product{}, err
|
||||
} else {
|
||||
productRepo.Brand = brand
|
||||
}
|
||||
mainPorduct := productRepo.makeProduct(importer)
|
||||
|
||||
//BEGIN TRANSACTION
|
||||
tx := importer.Baza.Begin()
|
||||
|
||||
if err := tx.Omit("Categories", "SuperAttributes.*", "ParentID").Create(&mainPorduct).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return gm.Product{}, err
|
||||
|
||||
}
|
||||
|
||||
mainFlat := productRepo.makeProductFlat(mainPorduct.ID)
|
||||
|
||||
if err := tx.Create(&mainFlat).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return gm.Product{}, err
|
||||
}
|
||||
|
||||
if productRepo.HasSizeVariants() {
|
||||
var sizeVariants []gm.ProductFlat
|
||||
for index, variant := range *product.SizeVariants {
|
||||
if !variant.Sellable {
|
||||
continue
|
||||
}
|
||||
savePoint := "size" + strconv.Itoa(index)
|
||||
tx.SavePoint(savePoint)
|
||||
|
||||
var sizeOPtion gm.AttributeOption
|
||||
|
||||
if variant.AttributeName == "Beden" {
|
||||
sizeOPtion = gm.GetAttributeOption(tx, importer.AttributesMap["size"].ID, variant.AttributeValue)
|
||||
} else {
|
||||
sizeOPtion = gm.GetAttributeOption(tx, importer.AttributesMap["boyut"].ID, variant.AttributeValue)
|
||||
}
|
||||
|
||||
sku := fmt.Sprintf("%s-%d", product.ProductNumber, variant.ItemNumber)
|
||||
log.Println(sku)
|
||||
log.Println(variant)
|
||||
|
||||
variantProduct := productRepo.makeVariant(mainPorduct.ID, mainPorduct.AttributeFamilyID, sku)
|
||||
|
||||
variantProduct.AttributeValues = productRepo.getVariantAttributes(importer.AttributesMap, &variant, sizeOPtion.ID)
|
||||
|
||||
if err := tx.Omit("Categories.*").Create(&variantProduct).Error; err != nil {
|
||||
log.Println("Variant Product Create Error: " + err.Error())
|
||||
tx.RollbackTo(savePoint)
|
||||
continue
|
||||
}
|
||||
|
||||
variantFlat := productRepo.makeVariantFlat(variant, sizeOPtion.ID, mainFlat.ID, variantProduct.ID)
|
||||
|
||||
if err := tx.Create(&variantFlat).Error; err != nil {
|
||||
log.Println("Variant Flat Create Error: " + err.Error())
|
||||
tx.RollbackTo(savePoint)
|
||||
continue
|
||||
}
|
||||
|
||||
sizeVariants = append(sizeVariants, variantFlat)
|
||||
}
|
||||
|
||||
if len(sizeVariants) == 0 {
|
||||
tx.Rollback()
|
||||
return gm.Product{}, errors.New("siz variantlary yok bolsa main productam girayenok")
|
||||
} else {
|
||||
calcPrice(sizeVariants, &mainFlat)
|
||||
|
||||
err := tx.Omit("ParentID", "CreatedAt", "Variants", "SpecialPrice").Save(&mainFlat).Error
|
||||
|
||||
mainFlat.Variants = sizeVariants
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return gm.Product{}, err
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
log.Println(product.Vendor)
|
||||
|
||||
sProduct := importer.createSellerProduct(&mainFlat, product.Vendor)
|
||||
|
||||
if errSProduct := tx.Create(&sProduct).Error; errSProduct != nil {
|
||||
tx.Rollback()
|
||||
return gm.Product{}, errSProduct
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
return gm.Product{}, err
|
||||
}
|
||||
|
||||
return mainPorduct, nil
|
||||
|
||||
}
|
||||
|
||||
func (importer *Importer) updateParsedVariant(product models.Product) (gm.Product, error) {
|
||||
var flat gm.ProductFlat
|
||||
err := importer.Baza.Preload("Product").Preload("Variants").First(&flat, "sku = ?", product.ProductNumber).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return importer.importParsedVariant(product)
|
||||
}
|
||||
//todo not found bolsa create etmeli
|
||||
return gm.Product{}, err
|
||||
}
|
||||
|
||||
if flat.Product.Type == "configurable" {
|
||||
|
||||
productRepo := InitProductRepo(&product, importer.GetColorOption(product.Color), importer.GetSexOption(product.Cinsiyet))
|
||||
productRepo.ImageType = "lcw"
|
||||
if brand, err := gm.FindOrCreateBrand(importer.Baza, product.Brand, productRepo.Categories); err != nil {
|
||||
return gm.Product{}, err
|
||||
} else {
|
||||
productRepo.Brand = brand
|
||||
}
|
||||
|
||||
for _, variant := range *product.SizeVariants {
|
||||
|
||||
found := false
|
||||
for _, flatVariant := range flat.Variants {
|
||||
|
||||
if variant.AttributeValue == flatVariant.BoyutLabel || variant.AttributeValue == flatVariant.SizeLabel {
|
||||
|
||||
if !variant.Sellable {
|
||||
importer.Baza.Model(&flatVariant).Update("status", false)
|
||||
} else {
|
||||
|
||||
importer.updatePrice(variant.Price, &flatVariant)
|
||||
|
||||
if !flatVariant.Status {
|
||||
|
||||
importer.Baza.Model(&flatVariant).Update("status", true)
|
||||
}
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if variant.Sellable && !found {
|
||||
// insert variant
|
||||
var sizeOPtion gm.AttributeOption
|
||||
|
||||
if variant.AttributeName == "Beden" {
|
||||
sizeOPtion = gm.GetAttributeOption(importer.Baza, importer.AttributesMap["size"].ID, variant.AttributeValue)
|
||||
} else {
|
||||
sizeOPtion = gm.GetAttributeOption(importer.Baza, importer.AttributesMap["boyut"].ID, variant.AttributeValue)
|
||||
}
|
||||
log.Println(variant)
|
||||
sku := fmt.Sprintf("%s-%d", product.ProductNumber, variant.ItemNumber)
|
||||
log.Println(sku)
|
||||
|
||||
variantProduct := productRepo.makeVariant(flat.ProductID, flat.Product.AttributeFamilyID, sku)
|
||||
|
||||
variantProduct.AttributeValues = productRepo.getVariantAttributes(importer.AttributesMap, &variant, sizeOPtion.ID)
|
||||
|
||||
if err := importer.Baza.Omit("Categories").Create(&variantProduct).Error; err != nil {
|
||||
log.Println("Variant Product Create Error: " + err.Error())
|
||||
}
|
||||
|
||||
variantFlat := productRepo.makeVariantFlat(variant, sizeOPtion.ID, flat.ID, variantProduct.ID)
|
||||
|
||||
if err := importer.Baza.Create(&variantFlat).Error; err != nil {
|
||||
log.Println("Variant Flat Create Error: " + err.Error())
|
||||
|
||||
}
|
||||
flat.Variants = append(flat.Variants, variantFlat)
|
||||
}
|
||||
}
|
||||
|
||||
calcPrice(flat.Variants, &flat)
|
||||
importer.Baza.Omit("ParentID", "CreatedAt", "Variants", "SpecialPrice").Save(&flat)
|
||||
|
||||
} else { //simple
|
||||
|
||||
importer.updatePrice(product.Price, &flat)
|
||||
|
||||
}
|
||||
return flat.Product, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue