560 lines
16 KiB
Go
560 lines
16 KiB
Go
package repositories
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"math"
|
|
gm "sarga_updater/bagisto_models"
|
|
models "sarga_updater/trendyol_models"
|
|
"strconv"
|
|
"sync"
|
|
)
|
|
|
|
type Importer struct {
|
|
mainCategories []gm.Category
|
|
Baza *gorm.DB
|
|
families []gm.AttributeFamily
|
|
sellers map[string]gm.MarketplaceSeller
|
|
//wishlist map[string]gm.Product
|
|
AttributesMap map[string]gm.Attribute
|
|
Error error
|
|
ImportWGroup sync.WaitGroup
|
|
ColorOptions map[string]gm.AttributeOption
|
|
SexOptions map[string]gm.AttributeOption
|
|
ColorMutex sync.Mutex
|
|
SexMutex sync.Mutex
|
|
}
|
|
type GormErr struct {
|
|
Number int `json:"Number"`
|
|
Message string `json:"Message"`
|
|
}
|
|
|
|
func ParseImporterInstance(db *gorm.DB) (instance *Importer, err error) {
|
|
|
|
if err != nil {
|
|
log.Println(err)
|
|
return nil, err
|
|
}
|
|
|
|
instance = &Importer{Baza: db}
|
|
instance.ImportWGroup.Add(3)
|
|
//load families to memory
|
|
go func() {
|
|
defer instance.ImportWGroup.Done()
|
|
instance.families, instance.Error = gm.GetFamilies(db)
|
|
}()
|
|
|
|
//load attributes to memory
|
|
go func() {
|
|
defer instance.ImportWGroup.Done()
|
|
|
|
if attributes, err := gm.GetAttributes(db); err != nil {
|
|
instance.Error = err
|
|
return
|
|
} else {
|
|
instance.AttributesMap = make(map[string]gm.Attribute, len(attributes))
|
|
|
|
for _, attribute := range attributes {
|
|
instance.AttributesMap[attribute.Code] = attribute
|
|
}
|
|
}
|
|
|
|
if colorOptions, err := gm.GetAttrOptions(db, instance.AttributesMap["color"].ID); err != nil {
|
|
instance.Error = err
|
|
return
|
|
} else {
|
|
instance.ColorOptions = make(map[string]gm.AttributeOption, len(colorOptions))
|
|
|
|
for _, option := range colorOptions {
|
|
instance.ColorOptions[option.AdminName] = option
|
|
}
|
|
}
|
|
|
|
if sexOPtions, err := gm.GetAttrOptions(db, instance.AttributesMap["cinsiyet"].ID); err != nil {
|
|
instance.Error = err
|
|
return
|
|
} else {
|
|
instance.SexOptions = make(map[string]gm.AttributeOption, len(sexOPtions))
|
|
|
|
for _, option := range sexOPtions {
|
|
instance.SexOptions[option.AdminName] = option
|
|
}
|
|
}
|
|
|
|
}()
|
|
|
|
//load sellers to memory
|
|
go func() {
|
|
defer instance.ImportWGroup.Done()
|
|
|
|
var vendors, err = gm.GetSellers(db)
|
|
|
|
if err != nil {
|
|
instance.Error = err
|
|
return
|
|
}
|
|
//init sellers map
|
|
instance.sellers = make(map[string]gm.MarketplaceSeller, len(vendors))
|
|
|
|
for _, vendor := range vendors {
|
|
instance.sellers[vendor.Url] = vendor
|
|
}
|
|
}()
|
|
|
|
if instance.Error != nil {
|
|
log.Println(instance.Error)
|
|
return nil, instance.Error
|
|
}
|
|
|
|
return instance, nil
|
|
}
|
|
|
|
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) {
|
|
|
|
//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 err == nil {
|
|
if !product.IsSellable {
|
|
importer.disableVariant(*firstProductFlat)
|
|
|
|
} else {
|
|
importer.updateVariant(product, *firstProductFlat)
|
|
}
|
|
|
|
}
|
|
//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{firstProductFlat.Product}
|
|
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 {
|
|
// if variant, err = importer.importVariant(colorVariant); err == nil {
|
|
// newProducts = append(newProducts, *variant)
|
|
// }
|
|
//
|
|
//} else
|
|
|
|
if err == nil && !colorVariant.IsSellable {
|
|
importer.disableVariant(*variantFlat)
|
|
linkedProducts = append(linkedProducts, variantFlat.Product)
|
|
|
|
} else {
|
|
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)
|
|
// }
|
|
//}
|
|
|
|
}
|
|
return importer
|
|
}
|
|
|
|
func (importer *Importer) importVariant(product models.Product) (*gm.Product, error) {
|
|
|
|
productRepo := InitProductRepo(&product, importer.GetColorOption(product.Color), importer.GetSexOption(product.Cinsiyet))
|
|
|
|
if categories, err := gm.GetCatKeywords(importer.Baza, product.Categories); err != nil {
|
|
return nil, err
|
|
} else {
|
|
productRepo.SetCategories(categories)
|
|
}
|
|
|
|
if brand, err := gm.FindOrCreateBrand(importer.Baza, product.Brand, productRepo.Categories); err != nil {
|
|
return nil, 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.Commit()
|
|
log.Println(err, "er1")
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mainFlat := productRepo.makeProductFlat(mainPorduct.ID)
|
|
|
|
if err := tx.Create(&mainFlat).Error; err != nil {
|
|
tx.Rollback()
|
|
log.Println(err, "er5")
|
|
return nil, err
|
|
}
|
|
|
|
if productRepo.HasSizeVariants() {
|
|
var sizeVariants []gm.ProductFlat
|
|
for index, variant := range *product.SizeVariants {
|
|
|
|
sku := fmt.Sprintf("%s-%d", product.ProductNumber, variant.ItemNumber)
|
|
//todo finde variant first if not exists create
|
|
var variantFlat gm.ProductFlat
|
|
err := tx.Preload("Product").Preload("Variants").First(&variantFlat, "sku = ?", sku).Error
|
|
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)
|
|
}
|
|
|
|
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)
|
|
|
|
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 nil, errors.New("size 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()
|
|
log.Println(err, "er6")
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
sProduct := importer.createSellerProduct(&mainFlat, product.Vendor)
|
|
|
|
if errSProduct := tx.Create(&sProduct).Error; errSProduct != nil {
|
|
tx.Rollback()
|
|
return nil, errSProduct
|
|
}
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &mainPorduct, nil
|
|
}
|
|
|
|
func (importer *Importer) createSellerProduct(flat *gm.ProductFlat, sellerURL string) gm.MarketplaceProduct {
|
|
sellerID := importer.sellers[sellerURL].ID
|
|
|
|
if sellerID == 0 {
|
|
sellerID = 1
|
|
}
|
|
|
|
sellerProduct := gm.MarketplaceProduct{
|
|
MarketplaceSellerID: sellerID,
|
|
IsApproved: true,
|
|
Condition: "new",
|
|
Description: "scraped",
|
|
IsOwner: true,
|
|
ProductID: flat.ProductID,
|
|
}
|
|
|
|
for _, variant := range flat.Variants {
|
|
sellerProduct.Variants = append(sellerProduct.Variants, gm.MarketplaceProduct{
|
|
ProductID: variant.ProductID,
|
|
IsOwner: true,
|
|
IsApproved: true,
|
|
MarketplaceSellerID: sellerID,
|
|
Condition: "new",
|
|
})
|
|
}
|
|
|
|
return sellerProduct
|
|
|
|
}
|
|
|
|
func calcPrice(variants []gm.ProductFlat, flat *gm.ProductFlat) {
|
|
for _, variant := range variants {
|
|
|
|
if !variant.Status {
|
|
continue
|
|
}
|
|
|
|
if flat.MinPrice == 0 || flat.MinPrice > variant.MinPrice {
|
|
flat.MinPrice = variant.MinPrice
|
|
}
|
|
|
|
flat.MaxPrice = math.Max(flat.MaxPrice, variant.MaxPrice)
|
|
}
|
|
}
|
|
|
|
func (importer *Importer) GetColorOption(optionName string) gm.AttributeOption {
|
|
if optionName == "" {
|
|
return gm.AttributeOption{}
|
|
}
|
|
|
|
importer.ColorMutex.Lock()
|
|
var option gm.AttributeOption
|
|
var ok bool
|
|
|
|
if option, ok = importer.ColorOptions[optionName]; !ok {
|
|
option := gm.GetAttributeOption(importer.Baza, importer.AttributesMap["color"].ID, optionName)
|
|
|
|
importer.ColorOptions[optionName] = option
|
|
}
|
|
importer.ColorMutex.Unlock()
|
|
return option
|
|
}
|
|
|
|
func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption {
|
|
if optionName == "" {
|
|
return gm.AttributeOption{}
|
|
}
|
|
importer.SexMutex.Lock()
|
|
var option gm.AttributeOption
|
|
var ok bool
|
|
|
|
if option, ok = importer.ColorOptions[optionName]; !ok {
|
|
option = gm.GetAttributeOption(importer.Baza, importer.AttributesMap["cinsiyet"].ID, optionName)
|
|
importer.SexOptions[optionName] = option
|
|
}
|
|
importer.SexMutex.Unlock()
|
|
return option
|
|
}
|
|
|
|
func (importer *Importer) disableVariant(productFlat gm.ProductFlat) {
|
|
|
|
importer.Error = importer.Baza.Model(&productFlat).Update("status", false).Error
|
|
|
|
importer.Error = importer.Baza.Model(&gm.ProductAttributeValue{}).
|
|
Where("attribute_id=8 AND product_id=?", productFlat.ProductID).
|
|
Update("boolean_value", false).Error
|
|
|
|
if importer.Error != nil {
|
|
log.Println(importer.Error)
|
|
importer.Error = nil
|
|
}
|
|
|
|
}
|
|
|
|
func (importer *Importer) enableVariant(productFlat gm.ProductFlat) {
|
|
importer.Error = importer.Baza.Model(&productFlat).Update("status", true).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 {
|
|
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 {
|
|
log.Println(err)
|
|
return
|
|
} 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.disableVariant(flatVariant)
|
|
} else {
|
|
|
|
importer.updatePrice(variant.Price, &flatVariant)
|
|
|
|
if !flatVariant.Status {
|
|
importer.enableVariant(flatVariant)
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
|
|
sku := fmt.Sprintf("%s-%d", product.ProductNumber, variant.ItemNumber)
|
|
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())
|
|
} else {
|
|
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)
|
|
}
|
|
|
|
}
|
|
|
|
func (importer *Importer) updatePrice(price models.Price, flat *gm.ProductFlat) {
|
|
if price.OriginalPrice.Value > price.DiscountedPrice.Value {
|
|
|
|
importer.Baza.Model(&flat).Updates(map[string]interface{}{
|
|
"price": price.OriginalPrice.Value,
|
|
"special_price": price.DiscountedPrice.Value,
|
|
"min_price": price.DiscountedPrice.Value,
|
|
"max_price": price.OriginalPrice.Value,
|
|
})
|
|
|
|
importer.Baza.Model(&gm.ProductAttributeValue{}).
|
|
Where("attribute_id = 11 and product_id = ?", flat.ProductID).
|
|
Update("float_value", price.OriginalPrice.Value)
|
|
|
|
importer.Baza.Model(&gm.ProductAttributeValue{}).
|
|
Where("attribute_id = 13 and product_id = ?", flat.ProductID).
|
|
Update("float_value", price.DiscountedPrice.Value)
|
|
} else {
|
|
|
|
importer.Baza.Model(&flat).Updates(map[string]interface{}{
|
|
"price": price.DiscountedPrice.Value,
|
|
"special_price": nil,
|
|
"min_price": price.DiscountedPrice.Value,
|
|
"max_price": price.DiscountedPrice.Value,
|
|
})
|
|
|
|
importer.Baza.Model(&gm.ProductAttributeValue{}).
|
|
Where("attribute_id = 11 and product_id = ?", flat.ProductID).
|
|
Update("float_value", price.DiscountedPrice.Value)
|
|
|
|
importer.Baza.Where("attribute_id = 13 and product_id = ?", flat.ProductID).
|
|
Delete(&gm.ProductAttributeValue{})
|
|
}
|
|
}
|