From 55f3c785c940847e97a1eb9bff31cc0956d3fb1e Mon Sep 17 00:00:00 2001 From: merdan Date: Thu, 19 Jan 2023 16:32:55 +0500 Subject: [PATCH] wishlist fix --- controllers/ImportV2.go | 1 + gorm_models/product.go | 22 +++++++++++++++++++++- gorm_models/vendor.go | 1 + gorm_models/wishlist.go | 11 +++++++++++ repositories/ImportRepository.go | 28 +++++++++++++++++++++++++++- 5 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 gorm_models/wishlist.go diff --git a/controllers/ImportV2.go b/controllers/ImportV2.go index 8a14748..80290ba 100644 --- a/controllers/ImportV2.go +++ b/controllers/ImportV2.go @@ -46,6 +46,7 @@ func StartProductImport(w http.ResponseWriter, _ *http.Request) { } importRepo.ImportWGroup.Wait() + //todo delete galan wishlist //scout index flush if err = gm.Flush(); err != nil { diff --git a/gorm_models/product.go b/gorm_models/product.go index 7b95f72..c8a4d48 100644 --- a/gorm_models/product.go +++ b/gorm_models/product.go @@ -4,6 +4,7 @@ import ( "context" helper "db_service/pkg" "gorm.io/gorm" + "log" "os" "time" ) @@ -107,13 +108,32 @@ func DeleteProducts(db *gorm.DB) error { 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 order_items oi ON p.id = oi.product_id LEFT JOIN order_items op ON p.id = op.parent_id WHERE oi.id IS NULL AND op.id IS NULL AND product_id IN (SELECT product_id from marketplace_products where marketplace_seller_id=1);" + qb := "DELETE p FROM products p " + + "LEFT JOIN order_items oi ON p.id = oi.product_id " + + "LEFT JOIN order_items op ON p.id = op.parent_id " + + "LEFT JOIN wishlist wp ON p.id = wp.product_id " + + "WHERE oi.id IS NULL AND op.id IS NULL AND wp.id IS NULL AND product_id IN (SELECT product_id from marketplace_products where marketplace_seller_id=1);" db.WithContext(ctx).Exec(qb) db.WithContext(ctx).Exec("UPDATE product_flat set sku=concat(id,\"-ordered\"), status=0 where status=1 AND product_id IN (SELECT product_id from marketplace_products where marketplace_seller_id=1)" ) db.WithContext(ctx).Exec("UPDATE products set sku=concat(id,\"-ordered\") WHERE id IN (SELECT product_id from marketplace_products where marketplace_seller_id=1)") return db.Error } +func GetWishlistProducts(db *gorm.DB) ([]Product, error){ + var products [] Product + err := db.Joins("JOIN wishlist wp ON products.id = wp.product_id").Joins("JOIN marketplace_products mp ON products.id = mp.product_id where marketplace_seller_id=1").Find(&products).Error + + if err != nil { + log.Println(err.Error()) + return nil, err + } + + return products, nil + //qb := "SELECT p.id,p.sku FROM products p " + + // "JOIN wishlist wp ON p.id = wp.product_id " + + // "JOIN marketplace_products mp ON p.id = mp.product_id where marketplace_seller_id=1);" +} + //func DisableProducts (db *gorm.DB) error { // ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute) // defer cancel() diff --git a/gorm_models/vendor.go b/gorm_models/vendor.go index f630f56..a1bf4fc 100644 --- a/gorm_models/vendor.go +++ b/gorm_models/vendor.go @@ -11,6 +11,7 @@ type MarketplaceProduct struct { CreatedAt time.Time `sql:"DEFAULT:NULL" gorm:"default:null"` UpdatedAt time.Time `sql:"DEFAULT:NULL" gorm:"default:null"` ProductID uint + Product Product ParentID *uint `sql:"DEFAULT:NULL" gorm:"default:null"` Condition string `sql:"DEFAULT:NULL" gorm:"default:null"` Price float64 diff --git a/gorm_models/wishlist.go b/gorm_models/wishlist.go new file mode 100644 index 0000000..258dc4e --- /dev/null +++ b/gorm_models/wishlist.go @@ -0,0 +1,11 @@ +package gorm_models + +import "time" + +type Wishlist struct { + ID uint `gorm:"primaryKey"` + CreatedAt time.Time `sql:"DEFAULT:NULL" gorm:"default:null"` + UpdatedAt time.Time `sql:"DEFAULT:NULL" gorm:"default:null"` + ProductID uint + Product Product +} diff --git a/repositories/ImportRepository.go b/repositories/ImportRepository.go index db982a8..262564c 100644 --- a/repositories/ImportRepository.go +++ b/repositories/ImportRepository.go @@ -22,6 +22,7 @@ type Importer struct { 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 @@ -42,7 +43,7 @@ func ImporterInstance() (instance *Importer, err error) { instance = &Importer{baza: db} - instance.ImportWGroup.Add(4) + instance.ImportWGroup.Add(5) //load main categories to memory go func(db *gorm.DB) { @@ -114,6 +115,24 @@ func ImporterInstance() (instance *Importer, err error) { } }() + //load wishlist to memory + go func(){ + defer instance.ImportWGroup.Done() + + var wishlist, err = gm.GetWishlistProducts(db) + + if err != nil { + instance.Error = err + return + } + + instance.wishlist = make(map[string]gm.Product, len(wishlist)) + + for _, product := range wishlist { + instance.wishlist[product.Sku] = product + } + }() + if instance.Error != nil { log.Println(instance.Error) return nil, instance.Error @@ -266,6 +285,13 @@ func (importer *Importer) ImportProduct(product models.Product) (instance *Impor } 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 {