before test update and import
This commit is contained in:
parent
5600ae746f
commit
cbbf952748
|
|
@ -139,6 +139,7 @@ func GetWishlistProducts(db *gorm.DB) ([]Product, error) {
|
|||
|
||||
func GetFlatSources(db *gorm.DB) ([]ProductAttributeValue, error) {
|
||||
var productSources []ProductAttributeValue
|
||||
//todo add filter by updated_at query
|
||||
err := db.Joins("JOIN wishlist wp ON product_attribute_values.product_id = wp.product_id").
|
||||
Joins("JOIN marketplace_products mp ON product_attribute_values.product_id = mp.product_id").
|
||||
Find(&productSources, "mp.marketplace_seller_id=1 AND product_attribute_values.text_value IS NOT NULL AND product_attribute_values.attribute_id=31").Error
|
||||
|
|
|
|||
5
main.go
5
main.go
|
|
@ -82,7 +82,7 @@ func worker(db *gorm.DB, stopCh <-chan struct{}, updatePeriod time.Duration) {
|
|||
return
|
||||
case <-ticker.C:
|
||||
|
||||
result, err := bagisto_models.GetFlatSources(importer.Baza)
|
||||
result, err := bagisto_models.GetFlatSources(db)
|
||||
if err != nil {
|
||||
log.Println("Error retrieving products:", err)
|
||||
continue
|
||||
|
|
@ -111,8 +111,9 @@ func worker(db *gorm.DB, stopCh <-chan struct{}, updatePeriod time.Duration) {
|
|||
}
|
||||
|
||||
// Update the product in the database
|
||||
if err := importer.UpdateOrCreate(jsonProduct, product.ProductID).Error; err != nil {
|
||||
if importer.UpdateOrCreate(jsonProduct, product.ProductID).Error != nil {
|
||||
log.Println("Error updateing product information:", err)
|
||||
importer.Error = nil
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,51 +111,74 @@ func ParseImporterInstance(db *gorm.DB) (instance *Importer, err error) {
|
|||
return instance, nil
|
||||
}
|
||||
|
||||
func (importer *Importer) ImportProduct(product models.Product) (instance *Importer) {
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product, product_id uint) (instance *Importer) {
|
||||
|
||||
var linkedProducts []gm.Product
|
||||
var firstProduct *gm.Product
|
||||
if firstProduct, importer.Error = importer.importVariant(product); importer.Error != nil {
|
||||
var newProducts []gm.Product
|
||||
//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
|
||||
} else if product.ColorVariants != nil && len(*product.ColorVariants) > 0 {
|
||||
linkedProducts = append(linkedProducts, *firstProduct)
|
||||
}
|
||||
|
||||
} else if firstProduct, _ = importer.updateVariant(product, product_id); firstProduct == nil {
|
||||
return importer
|
||||
}
|
||||
|
||||
if &firstProduct != nil {
|
||||
newProducts = append(newProducts, *firstProduct)
|
||||
}
|
||||
|
||||
if product.ColorVariants != nil && len(*product.ColorVariants) > 0 {
|
||||
linkedProducts := []gm.Product{*firstProduct}
|
||||
|
||||
for _, colorVariant := range *product.ColorVariants {
|
||||
|
||||
if !colorVariant.IsSellable {
|
||||
continue
|
||||
var (
|
||||
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 variant, err := importer.importVariant(colorVariant); err == nil {
|
||||
} else if err == nil && !colorVariant.IsSellable {
|
||||
variant = importer.disableVariant(colorVariant, variantFlat.ProductID)
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
|
||||
} else {
|
||||
if variant, importer.Error = importer.updateVariant(colorVariant, variantFlat.ProductID); importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if len(linkedProducts) > 1 {
|
||||
if len(newProducts) > 0 {
|
||||
// relation
|
||||
|
||||
var relation []gm.ProductRelation
|
||||
for index, variant := range linkedProducts {
|
||||
//spoint := "color" + strconv.Itoa(index)
|
||||
temp := make([]gm.Product, len(linkedProducts))
|
||||
for _, linkedProduct := range linkedProducts {
|
||||
|
||||
copy(temp, linkedProducts)
|
||||
|
||||
if index+1 <= len(temp) {
|
||||
temp = append(temp[:index], temp[index+1:]...)
|
||||
|
||||
for _, item := range temp {
|
||||
relation = append(relation, gm.ProductRelation{ParentID: variant.ID, ChildID: item.ID})
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -211,17 +234,6 @@ func (importer *Importer) importVariant(product models.Product) (*gm.Product, er
|
|||
err := tx.Preload("Product").Preload("Variants").First(&variantFlat, "sku = ?", sku).Error
|
||||
savePoint := "size" + strconv.Itoa(index)
|
||||
tx.SavePoint(savePoint)
|
||||
if err == nil {
|
||||
tx.Model(&variantFlat).Updates(map[string]interface{}{"status": variant.Sellable, "parent_id": mainFlat.ID})
|
||||
tx.Model(&gm.ProductAttributeValue{}).Where("attribute_id=8 AND product_id=?", variantFlat.ProductID).Update("boolean_value", variant.Sellable)
|
||||
tx.Model(&variantFlat.Product).Update("parent_id", mainPorduct.ID)
|
||||
|
||||
if variant.Sellable {
|
||||
importer.updatePrice(variant.Price, &variantFlat)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
var sizeOPtion gm.AttributeOption
|
||||
|
||||
if variant.AttributeName == "Beden" {
|
||||
|
|
@ -230,6 +242,54 @@ func (importer *Importer) importVariant(product models.Product) (*gm.Product, er
|
|||
sizeOPtion = gm.GetAttributeOption(tx, importer.AttributesMap["boyut"].ID, variant.AttributeValue)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
err = tx.Model(&variantFlat).Updates(map[string]interface{}{"status": variant.Sellable, "parent_id": mainFlat.ID, "size": sizeOPtion.ID, "size_label": variant.AttributeValue}).Error
|
||||
err = tx.Model(&gm.ProductAttributeValue{}).Where("attribute_id=8 AND product_id=?", variantFlat.ProductID).Update("boolean_value", variant.Sellable).Error
|
||||
err = tx.Model(&variantFlat.Product).Update("parent_id", mainPorduct.ID).Error
|
||||
|
||||
if err == nil && variant.Sellable {
|
||||
|
||||
if variant.Price.OriginalPrice.Value > variant.Price.DiscountedPrice.Value {
|
||||
|
||||
err = tx.Model(&variantFlat).Updates(map[string]interface{}{
|
||||
"price": variant.Price.OriginalPrice.Value,
|
||||
"special_price": variant.Price.DiscountedPrice.Value,
|
||||
"min_price": variant.Price.DiscountedPrice.Value,
|
||||
"max_price": variant.Price.OriginalPrice.Value,
|
||||
}).Error
|
||||
|
||||
err = tx.Model(&gm.ProductAttributeValue{}).
|
||||
Where("attribute_id = 11 and product_id = ?", variantFlat.ProductID).
|
||||
Update("float_value", variant.Price.OriginalPrice.Value).Error
|
||||
|
||||
err = tx.Model(&gm.ProductAttributeValue{}).
|
||||
Where("attribute_id = 13 and product_id = ?", variantFlat.ProductID).
|
||||
Update("float_value", variant.Price.DiscountedPrice.Value).Error
|
||||
} else {
|
||||
|
||||
err = tx.Model(&variantFlat).Updates(map[string]interface{}{
|
||||
"price": variant.Price.DiscountedPrice.Value,
|
||||
"special_price": nil,
|
||||
"min_price": variant.Price.DiscountedPrice.Value,
|
||||
"max_price": variant.Price.DiscountedPrice.Value,
|
||||
}).Error
|
||||
|
||||
err = tx.Model(&gm.ProductAttributeValue{}).
|
||||
Where("attribute_id = 11 and product_id = ?", variantFlat.ProductID).
|
||||
Update("float_value", variant.Price.DiscountedPrice.Value).Error
|
||||
|
||||
err = tx.Where("attribute_id = 13 and product_id = ?", variantFlat.ProductID).
|
||||
Delete(&gm.ProductAttributeValue{}).Error
|
||||
}
|
||||
if err != nil {
|
||||
tx.RollbackTo(savePoint)
|
||||
}
|
||||
} else {
|
||||
tx.RollbackTo(savePoint)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
variantProduct := productRepo.makeVariant(mainPorduct.ID, mainPorduct.AttributeFamilyID, sku)
|
||||
|
||||
variantProduct.AttributeValues = productRepo.getVariantAttributes(importer.AttributesMap, &variant, sizeOPtion.ID)
|
||||
|
|
@ -362,13 +422,14 @@ func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption {
|
|||
importer.SexMutex.Unlock()
|
||||
return option
|
||||
}
|
||||
func (importer *Importer) disableVariant(product models.Product, product_id uint) (instance *Importer) {
|
||||
|
||||
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
|
||||
|
||||
if importer.Error != nil {
|
||||
return importer
|
||||
return nil
|
||||
}
|
||||
|
||||
importer.Error = importer.Baza.Model(&flat).Update("status", false).Error
|
||||
|
|
@ -377,20 +438,22 @@ func (importer *Importer) disableVariant(product models.Product, product_id uint
|
|||
Where("attribute_id=8 AND product_id=?", flat.ProductID).
|
||||
Update("boolean_value", false).Error
|
||||
|
||||
return importer
|
||||
return &flat.Product
|
||||
|
||||
}
|
||||
|
||||
func (importer *Importer) updateVariant(product models.Product, product_id uint) (*gm.Product, error) {
|
||||
|
||||
var flat gm.ProductFlat
|
||||
err := importer.Baza.Preload("Product").Preload("Variants").First(&flat, "product_id = ?", product_id).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
importer.Error = importer.Baza.Preload("Product").Preload("Variants").First(&flat, "product_id = ?", product_id).Error
|
||||
|
||||
if importer.Error != nil {
|
||||
if errors.Is(importer.Error, gorm.ErrRecordNotFound) {
|
||||
return importer.importVariant(product)
|
||||
}
|
||||
//todo not found bolsa create etmeli
|
||||
return nil, err
|
||||
return nil, importer.Error
|
||||
}
|
||||
|
||||
if flat.Product.Type == "configurable" {
|
||||
|
|
@ -694,144 +757,3 @@ func (importer *Importer) updateParsedVariant(product models.Product) (gm.Produc
|
|||
}
|
||||
return flat.Product, nil
|
||||
}
|
||||
|
||||
func (importer *Importer) UpdateOrCreateLCW(product models.Product) (instance *Importer) {
|
||||
var firstProduct gm.Product
|
||||
result := importer.Baza.First(&firstProduct, "sku=?", product.ProductNumber)
|
||||
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
firstProduct, importer.Error = importer.importParsedVariant(product)
|
||||
|
||||
} else {
|
||||
firstProduct, importer.Error = importer.updateParsedVariant(product)
|
||||
|
||||
}
|
||||
|
||||
var newProducts []gm.Product
|
||||
|
||||
if importer.Error != nil {
|
||||
return importer
|
||||
} else if &firstProduct != nil {
|
||||
newProducts = append(newProducts, firstProduct)
|
||||
}
|
||||
|
||||
if product.ColorVariants != nil && len(*product.ColorVariants) > 0 {
|
||||
linkedProducts := []gm.Product{firstProduct}
|
||||
|
||||
for _, colorVariant := range *product.ColorVariants {
|
||||
|
||||
var (
|
||||
variant gm.Product
|
||||
err error
|
||||
)
|
||||
if !colorVariant.IsSellable {
|
||||
|
||||
if err = importer.Baza.Model(&gm.ProductFlat{}).Where("sku=?", colorVariant.ProductNumber).Update("status", false).Error; err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
continue
|
||||
|
||||
} else {
|
||||
|
||||
//todo check parsed import variant
|
||||
if variant, err = importer.importParsedVariant(colorVariant); err != nil {
|
||||
|
||||
if variant, importer.Error = importer.updateParsedVariant(colorVariant); importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
|
||||
linkedProducts = append(linkedProducts, variant)
|
||||
|
||||
} else {
|
||||
newProducts = append(newProducts, variant)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product, product_id uint) (instance *Importer) {
|
||||
|
||||
if !product.IsSellable {
|
||||
return importer.disableVariant(product, product_id)
|
||||
}
|
||||
|
||||
firstProduct, err := importer.updateVariant(product, product_id)
|
||||
var newProducts []gm.Product
|
||||
if err != nil {
|
||||
importer.Error = err
|
||||
return importer
|
||||
} else if &firstProduct != nil {
|
||||
newProducts = append(newProducts, *firstProduct)
|
||||
}
|
||||
|
||||
if product.ColorVariants != nil && len(*product.ColorVariants) > 0 {
|
||||
linkedProducts := []gm.Product{*firstProduct}
|
||||
|
||||
for _, colorVariant := range *product.ColorVariants {
|
||||
|
||||
var (
|
||||
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 {
|
||||
variant, err = importer.importVariant(colorVariant)
|
||||
newProducts = append(newProducts, *variant)
|
||||
continue
|
||||
} else if err == nil && !colorVariant.IsSellable {
|
||||
importer.disableVariant(colorVariant, variantFlat.ProductID)
|
||||
continue
|
||||
}
|
||||
|
||||
if variant, importer.Error = importer.updateVariant(colorVariant, variantFlat.ProductID); importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -211,8 +211,8 @@ func (pr *ProductRepo) makeVariantFlat(variant models.Variant, SizID, parentID,
|
|||
ProductNumber: fmt.Sprintf("%d", variant.ItemNumber),
|
||||
Weight: pr.Weight,
|
||||
FavoritesCount: uint(pr.Data.FavoriteCount),
|
||||
//SizeLabel: variant.AttributeValue,
|
||||
//Size: int(SizID),
|
||||
SizeLabel: variant.AttributeValue,
|
||||
Size: int(SizID),
|
||||
MaxPrice: maxPRice,
|
||||
MinPrice: variant.Price.DiscountedPrice.Value,
|
||||
Price: maxPRice,
|
||||
|
|
@ -283,7 +283,7 @@ func (pr *ProductRepo) getVariantAttributes(AttributesMap map[string]gm.Attribut
|
|||
{AttributeID: AttributesMap["name"].ID, TextValue: pr.Data.Name, Channel: "default", Locale: "tm"},
|
||||
{AttributeID: AttributesMap["weight"].ID, TextValue: pr.Data.Weight},
|
||||
{AttributeID: AttributesMap["status"].ID, BooleanValue: true},
|
||||
//{AttributeID: AttributesMap["size"].ID, IntegerValue: int(SizID)},
|
||||
{AttributeID: AttributesMap["size"].ID, IntegerValue: int(SizID)},
|
||||
{AttributeID: AttributesMap["price"].ID, FloatValue: price},
|
||||
}
|
||||
if product.AttributeName == "Beden" {
|
||||
|
|
|
|||
Loading…
Reference in New Issue