From 957494bb3717e520798d5ede8ad1f45abaa25759 Mon Sep 17 00:00:00 2001 From: merdan Date: Tue, 20 Sep 2022 18:41:12 +0500 Subject: [PATCH] link parse update --- controllers/ImportController.go | 179 +++++++++++++++++++++++++++++++- gorm_models/product.go | 2 +- 2 files changed, 179 insertions(+), 2 deletions(-) diff --git a/controllers/ImportController.go b/controllers/ImportController.go index 3a01a90..c23c1c6 100644 --- a/controllers/ImportController.go +++ b/controllers/ImportController.go @@ -5,6 +5,7 @@ import ( "db_service/models" helper "db_service/pkg" "encoding/json" + "errors" "fmt" "log" "math/big" @@ -748,6 +749,8 @@ func prepearAttributesWithFlat(data *models.Product) ([]gm.ProductAttributeValue func UpdateProduct(product models.Product, db *gorm.DB, productFlat gm.ProductFlat) error { productFlat.Status = true + minPrice := 0.0 + maxPrice := 0.0 if len(product.ColorVariants) == 0 && len(product.SizeVariants) == 0 { @@ -796,9 +799,183 @@ func UpdateProduct(product models.Product, db *gorm.DB, productFlat gm.ProductFl } } } - } else { + } else if len(product.ColorVariants) > 0 { //todo update configurable product + product.ColorVariants = append(product.ColorVariants, product) + for _, colorVariant := range product.ColorVariants { + + if len(colorVariant.SizeVariants) > 0 { + + for _, sizeVariant := range colorVariant.SizeVariants { + sku := fmt.Sprintf("%s-%s-%d-col-size", colorVariant.ProductGroupID, colorVariant.ProductNumber, sizeVariant.ItemNumber) + + var variantFlat gm.ProductFlat + + err := db.Where("sku=?", sku).First(&variantFlat).Error + + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + //todo insert variant + continue + } + + if !sizeVariant.Sellable { + variantFlat.Status = false + + errVar := db.Save(&variantFlat).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + continue + } + + prices := sizeVariant.Price + + if prices.OriginalPrice.Value > prices.DiscountedPrice.Value { + variantFlat.Price = prices.OriginalPrice.Value + variantFlat.SpecialPrice = prices.DiscountedPrice.Value + variantFlat.MinPrice = prices.DiscountedPrice.Value + variantFlat.MaxPrice = prices.OriginalPrice.Value + + errVar := db.Save(&variantFlat).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + + } else { + + variantFlat.MinPrice = prices.DiscountedPrice.Value + variantFlat.MaxPrice = prices.DiscountedPrice.Value + + errVar := db.Exec("Update product_flat set price = ? special_price = NULL where id = ?", prices.DiscountedPrice.Value, variantFlat.ID).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + } + + if minPrice == 0 || minPrice > variantFlat.MinPrice { + minPrice = variantFlat.MinPrice + } + + if maxPrice == 0 || maxPrice < variantFlat.MaxPrice { + maxPrice = variantFlat.MaxPrice + } + + } + } else { + sku := fmt.Sprintf("%s-%s-%s-%s", product.ProductGroupID, colorVariant.ProductNumber, colorVariant.ProductCode, colorVariant.Color) + var variantFlat gm.ProductFlat + err := db.Where("sku=?", sku).First(&variantFlat).Error + + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + //todo insert variant + continue + } + + if !colorVariant.IsSellable { + variantFlat.Status = false + + errVar := db.Save(&variantFlat).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + continue + } + prices := colorVariant.Price + + if prices.OriginalPrice.Value > prices.DiscountedPrice.Value { + variantFlat.Price = prices.OriginalPrice.Value + variantFlat.SpecialPrice = prices.DiscountedPrice.Value + variantFlat.MinPrice = prices.DiscountedPrice.Value + variantFlat.MaxPrice = prices.OriginalPrice.Value + + errVar := db.Save(&variantFlat).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + + } else { + + errVar := db.Exec("Update product_flat set price = ? special_price = NULL where id = ?", prices.DiscountedPrice.Value, variantFlat.ID).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + } + + if minPrice == 0 || minPrice > variantFlat.MinPrice { + minPrice = variantFlat.MinPrice + } + + if maxPrice == 0 || maxPrice < variantFlat.MaxPrice { + maxPrice = variantFlat.MaxPrice + } + } + + } + productFlat.MinPrice = minPrice + productFlat.MaxPrice = maxPrice + + } else if len(product.SizeVariants) > 0 { + + for _, sizeVariant := range product.SizeVariants { + sku := fmt.Sprintf("%s-%s-%d-size", product.ProductGroupID, product.ProductNumber, sizeVariant.ItemNumber) + var variantFlat gm.ProductFlat + err := db.Where("sku=?", sku).First(&variantFlat).Error + + if err != nil && errors.Is(err, gorm.ErrRecordNotFound) { + //todo insert variant + continue + } + if !sizeVariant.Sellable { + variantFlat.Status = false + + errVar := db.Save(&variantFlat).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + continue + } + + prices := sizeVariant.Price + + if prices.OriginalPrice.Value > prices.DiscountedPrice.Value { + variantFlat.Price = prices.OriginalPrice.Value + variantFlat.SpecialPrice = prices.DiscountedPrice.Value + variantFlat.MinPrice = prices.DiscountedPrice.Value + variantFlat.MaxPrice = prices.OriginalPrice.Value + + errVar := db.Save(&variantFlat).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + + } else { + + errVar := db.Exec("Update product_flat set price = ? special_price = NULL where id = ?", prices.DiscountedPrice.Value, variantFlat.ID).Error + + if errVar != nil { + log.Println(errVar.Error()) + } + } + + if minPrice == 0 || minPrice > variantFlat.MinPrice { + minPrice = variantFlat.MinPrice + } + + if maxPrice == 0 || maxPrice < variantFlat.MaxPrice { + maxPrice = variantFlat.MaxPrice + } + } + productFlat.MinPrice = minPrice + productFlat.MaxPrice = maxPrice } errFlat := db.Omit("Product", "ParentID", "CreatedAt").Save(&productFlat).Error diff --git a/gorm_models/product.go b/gorm_models/product.go index 9ae0825..dce2117 100644 --- a/gorm_models/product.go +++ b/gorm_models/product.go @@ -95,7 +95,7 @@ func (ProductFlat) TableName() string { func DeleteProducts(db *gorm.DB) error { //todo delete from elastico - ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) defer cancel() //qb := "DELETE FROM products WHERE id NOT IN (select product_id as id from wishlist) AND id NOT IN (select product_id as id from order_items) AND id NOT IN (select parent_id as idfrom order_items);" qb := "DELETE p FROM products p LEFT JOIN wishlist w on p.id = w.product_id LEFT JOIN order_items oi ON p.id = oi.product_id LEFT JOIN order_items op ON p.id = op.parent_id WHERE w.id IS NULL AND oi.id IS NULL AND op.id IS NULL;"