cmd

package
v0.0.0-...-d10ae67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InsertCmd = &cobra.Command{
	Use:     "insert",
	Aliases: []string{"i"},
	Short:   "insert products to webkum's marketplace tables.",
	Long:    "insert products to webkum's marketplace tables",
	Run: func(cmd *cobra.Command, args []string) {

		if !dryRun {
			var err error

			dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)
			db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
				NamingStrategy: schema.NamingStrategy{
					TablePrefix:   dbTablePrefix,
					SingularTable: true,
					NameReplacer:  strings.NewReplacer("ID", "Id"),
				},
			})
			if err != nil {
				log.Fatal(err)
			}

			if dbDrop {
				if db.Migrator().HasTable(&WkMpImport{}) {
					db.Migrator().DropTable(&WkMpImport{})
				}
			}
			if dbMigrate {
				db.Migrator().AutoMigrate(&WkMpImport{})
			}

		}

		err := godirwalk.Walk(importDir, &godirwalk.Options{
			Callback: func(osPathname string, de *godirwalk.Dirent) error {
				if !de.IsDir() {

					extension := filepath.Ext(osPathname)
					filename := path.Base(osPathname)
					basename := strings.Replace(filename, extension, "", -1)
					if inSlice(extension, importExt, false) {
						if options.verbose {
							log.Println("found=", osPathname, "filename=", filename, "extension=", extension, "basename=", basename)
						}
						loadData(db, osPathname, "wk_mp_import")

					}
				}
				return nil
			},
			Unsorted: true,
		})
		checkErr("Dir walk, error", err)

		var products []*WkMpImport
		db.Find(&products)

		for _, product := range products {

			if product.IDProduct == 0 {
				continue
			}

			sqlQuery := `INSERT IGNORE INTO eg_wk_mp_seller_product (
			id_seller,
			id_ps_product,
			id_ps_shop,
			id_category,
			price,
			wholesale_price,
			unity,
			unit_price,
			id_tax_rules_group,
			on_sale,
			additional_shipping_cost,
			quantity,
			minimal_quantity,
			low_stock_threshold,
			low_stock_alert,
			active,
			status_before_deactivate,
			show_condition,
			` + "`" + `condition` + "`" + `,
			available_for_order,
			show_price,
			online_only,
			visibility,
			admin_assigned,
			width,
			height,
			depth,
			weight,
			reference,
			ean13,
			upc,
			isbn,
			out_of_stock,
			available_date,
			ps_id_carrier_reference,
			admin_approved,
			additional_delivery_times,
			date_add,
			date_upd,
			csv_request_no)
		SELECT 
		  ` + fmt.Sprintf("%d", product.IdSeller) + `,
		  	id_product,
		  	id_shop_default,
		  	id_category_default,
		  	price,
			wholesale_price,
			unity,
			unit_price_ratio,
			id_tax_rules_group,
			on_sale,
			additional_shipping_cost,
			quantity,
			minimal_quantity,
			low_stock_threshold,
			low_stock_alert,
			active,
			0,
			show_condition,
			` + "`" + `condition` + "`" + `,
			available_for_order,
			show_price,
			online_only,
			visibility,
			1,
			width,
			height,
			depth,
			weight,
			reference,
			ean13,
			upc,
			isbn,
			out_of_stock,
			available_date,		
			0,
			1,
			0,
			date_add,
			date_upd,	
			0
		FROM eg_product
		WHERE id_product=` + fmt.Sprintf("%d", product.IDProduct)
			db.Exec(sqlQuery)

			// retrieve id_seller_product
			var wkMpSellerProduct WkMpSellerProduct
			err := db.Where("id_seller = ? AND id_ps_product = ? and id_ps_shop = ?", product.IdSeller, product.IDProduct, 1).First(&wkMpSellerProduct).Error
			if errors.Is(err, gorm.ErrRecordNotFound) {
				log.Fatal("product not found")
			}

			sqlQuery = `INSERT IGNORE INTO eg_wk_mp_seller_product_lang (id_mp_product, id_lang, product_name, short_description, description, available_now, available_later, meta_title, meta_description, link_rewrite, delivery_in_stock, delivery_out_stock)
						SELECT ` + fmt.Sprintf("%d", wkMpSellerProduct.IdMpProduct) + `, id_lang, name, description_short, description, available_now, available_later, meta_title, meta_description, link_rewrite, delivery_in_stock, delivery_out_stock FROM eg_product_lang WHERE
						id_product=` + fmt.Sprintf("%d", product.IDProduct)
			db.Exec(sqlQuery)

			sqlQuery = `INSERT IGNORE INTO eg_wk_mp_seller_product_category (
						  id_category,
						  id_seller_product,
						  is_default) 		
						VALUES ( 
						  ` + fmt.Sprintf("%d", product.DefaultCategory) + `,
						  ` + fmt.Sprintf("%d", wkMpSellerProduct.IdMpProduct) + `,
						  1)`
			db.Exec(sqlQuery)

			sqlQuery = `INSERT IGNORE INTO eg_wk_mp_seller_product_image (seller_product_id, seller_product_image_name, id_ps_image, position, cover, active) 
			  	VALUES (` + fmt.Sprintf("%d", wkMpSellerProduct.IdMpProduct) + `, "` + escape(product.Name) + `",` + fmt.Sprintf("%d", product.IdImage) + `, 0, 1, 1)`
			db.Exec(sqlQuery)

			sqlQuery = `INSERT IGNORE INTO eg_wk_mp_product_feature (ps_id_feature, mp_id_product, ps_id_feature_value, mp_id_feature_value)
						SELECT fp.id_feature, ` + fmt.Sprintf("%d", wkMpSellerProduct.IdMpProduct) + `, fvl.id_feature_value, wmpfv.mp_id_feature_value FROM eg_feature_product fp
						LEFT JOIN eg_feature_value_lang fvl ON (fp.id_feature_value = fvl.id_feature_value)
						LEFT JOIN eg_wk_mp_product_feature_value wmpfv ON wmpfv.ps_id_feature=fp.id_feature
						WHERE fp.id_product = ` + fmt.Sprintf("%d", product.IDProduct)
			db.Debug().Exec(sqlQuery)

			sqlQuery = `INSERT IGNORE INTO eg_wk_mp_product_feature_value_lang (ps_id_feature_value, mp_id_feature_value, id_lang, value)
							SELECT fp.id_feature, wmpfv.mp_id_feature_value, fvl.id_lang, fvl.value FROM eg_feature_product fp
							LEFT JOIN eg_feature_value_lang fvl ON (fp.id_feature_value = fvl.id_feature_value)
							LEFT JOIN eg_wk_mp_product_feature_value wmpfv ON wmpfv.ps_id_feature=fp.id_feature
							WHERE fp.id_product=` + fmt.Sprintf("%d", product.IDProduct)
			db.Debug().Exec(sqlQuery)

		}

	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "imp2ps",
	Short: "imp2ps is an helper to create products into webkul marketplace.",
	Long:  `imp2ps is an helper to create products into webkul marketplace.`,
}

RootCmd is the root command for ovh-qa

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately.

Types

type WkMpImport

type WkMpImport struct {
	gorm.Model
	IdSeller         uint `gorm:"primaryKey;uniqueIndex:idx_mp_product;column:id_seller;type:int(10) unsigned;not null"`
	IDProduct        uint `gorm:"uniqueIndex:idx_mp_product;index:image_product;column:id_product;type:int(10) unsigned;not null"`
	IdImage          uint
	Name             string
	Reference        string
	CategoryId       uint
	DefaultCategory  uint
	ShortDescription string `gorm:"column:description;type:text"`
	Description      string `gorm:"column:short_description;type:text"`
	Price            float64
	TaxId            uint
	Quantity         uint
	ImageRef         string
	FeatureName      string
	FeatureValues    string
	FormatUnitaire   string
	MinimumGarantie  string
	QuantiteUnitaire uint
	PrixTotal        float64
	UniteTotal       uint
	PoidsPiece       float64
	Ean13            string
	Sku              string
	Mpn              string
}

type WkMpSellerProduct

type WkMpSellerProduct struct {
	IdMpProduct uint
	IdSeller    uint
	IdProduct   uint
	IdCategory  uint
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL