link parse update
This commit is contained in:
parent
0023f495a1
commit
957494bb37
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;"
|
||||
|
|
|
|||
Loading…
Reference in New Issue