parse
This commit is contained in:
parent
65b4e8e00b
commit
da8c13310b
|
|
@ -1065,36 +1065,3 @@ func UpdateProduct(product models.Product, db *gorm.DB, productFlat gm.ProductFl
|
|||
|
||||
return errFlat
|
||||
}
|
||||
|
||||
//func productAttributesAndFlat(data *models.Product) ([]gm.ProductAttributeValue,gm.ProductFlat){
|
||||
//
|
||||
// var description string
|
||||
//
|
||||
// for _, desc := range data.Descriptions {
|
||||
// description += "<p>" + desc.Description + "</p>"
|
||||
// }
|
||||
//
|
||||
// weight, _ := strconv.ParseFloat(data.Weight, 64)
|
||||
//
|
||||
// flat := gm.ProductFlat{
|
||||
// Status: true,
|
||||
// VisibleIndividually: true,
|
||||
// Name: data.Name,
|
||||
// Sku: data.ProductGroupID,
|
||||
// ProductNumber: data.ProductNumber,
|
||||
// Description: description,
|
||||
// UrlKey: data.ProductGroupID,
|
||||
// Weight: weight,
|
||||
// FavoritesCount: uint(data.FavoriteCount),
|
||||
// }
|
||||
// return []gm.ProductAttributeValue{
|
||||
// {AttributeID: AttributesMap["favoritesCount"].ID, IntegerValue: data.FavoriteCount},
|
||||
// {AttributeID: AttributesMap["source"].ID, TextValue: data.URLKey},
|
||||
// {AttributeID: AttributesMap["product_number"].ID, TextValue: data.ProductNumber},
|
||||
// {AttributeID: AttributesMap["name"].ID, TextValue: data.Name, Channel: "default", Locale: "tm"},
|
||||
// {AttributeID: AttributesMap["weight"].ID, TextValue: data.Weight},
|
||||
// {AttributeID: AttributesMap["status"].ID, BooleanValue: true},
|
||||
// {AttributeID: AttributesMap["visible_individually"].ID, BooleanValue: true},
|
||||
// {AttributeID: AttributesMap["description"].ID, TextValue: description, Channel: "default", Locale: "tm"},
|
||||
// },flat
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -1,16 +1,11 @@
|
|||
package controller
|
||||
|
||||
import (
|
||||
gm "db_service/gorm_models"
|
||||
helper "db_service/pkg"
|
||||
"db_service/repositories"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -41,40 +36,20 @@ func ParseLink(w http.ResponseWriter, route *http.Request) {
|
|||
helper.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
baza, err := gorm.Open(mysql.Open(os.Getenv("database_url")), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
helper.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
var productFlat gm.ProductFlat
|
||||
|
||||
err = baza.Preload("Variants").
|
||||
//Preload("Product").
|
||||
//Preload("Proudct.AttributeValues","attribute_id in(11,13)").
|
||||
First(&productFlat, "sku = ?", jsonProduct.ProductGroupID).Error
|
||||
|
||||
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
helper.Error(err)
|
||||
|
||||
if len(AttributesMap) == 0 {
|
||||
var attributes, err = gm.GetAttributes(baza)
|
||||
/////////////////////////////
|
||||
importRepo, err := repositories.ImporterInstance()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
//wait until initialization data is loaded to memory
|
||||
importRepo.ImportWGroup.Wait()
|
||||
|
||||
for _, atrattribute := range attributes {
|
||||
AttributesMap[atrattribute.Code] = atrattribute
|
||||
if err = importRepo.UpdateOrCreate(jsonProduct).Error; err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = ImportProduct(jsonProduct, baza)
|
||||
} else if err == nil {
|
||||
err = UpdateProduct(jsonProduct, baza, productFlat)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"msg": "Link parsed successfully",
|
||||
|
|
@ -83,18 +58,6 @@ func ParseLink(w http.ResponseWriter, route *http.Request) {
|
|||
elapsed := time.Since(start)
|
||||
log.Printf("end parse took %s", elapsed)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
helper.Error(err)
|
||||
return
|
||||
}
|
||||
elapsed := time.Since(start)
|
||||
log.Printf("end parse took %s", elapsed)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"msg": "Link parsed successfully",
|
||||
"productGroupId": strconv.Itoa(product.ProductGroupID),
|
||||
})
|
||||
/////////////////////////////////////////
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -394,3 +394,90 @@ func (importer *Importer) GetSexOption(optionName string) gm.AttributeOption {
|
|||
importer.SexMutex.Unlock()
|
||||
return option
|
||||
}
|
||||
|
||||
func (importer *Importer) updateVariant(product models.Product) (*gm.Product, error) {
|
||||
|
||||
//if len(*product.SizeVariants) > 0{
|
||||
// for _, sizeVariant := range *product.SizeVariants {
|
||||
//
|
||||
// if !sizeVariant.Sellable{
|
||||
// importer.baza.Model(&gm.ProductFlat{}).
|
||||
// Where("sku like ? AND size_label = ?", product.ProductNumber+"-%",sizeVariant.AttributeValue).
|
||||
// Update("status",false)
|
||||
// }else {
|
||||
// //todo update price
|
||||
// //todo insert if not exists
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//}else {
|
||||
// var flat gm.ProductFlat
|
||||
// err := importer.baza.Preload("Product").First(&flat,"sku = ?", product.ProductNumber).Error
|
||||
//
|
||||
//}
|
||||
var flat gm.ProductFlat
|
||||
err := importer.baza.Preload("Product", "Variants").First(&flat, "sku = ?", product.ProductNumber).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if flat.Product.Type == "configurable" {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (importer *Importer) UpdateOrCreate(product models.Product) (instance *Importer) {
|
||||
|
||||
firstProduct, err := importer.importVariant(product)
|
||||
var newProducts []gm.Product
|
||||
if err != nil {
|
||||
helper.Error(err)
|
||||
|
||||
firstProduct, importer.Error = importer.updateVariant(product)
|
||||
|
||||
if importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
} else {
|
||||
newProducts[0] = *firstProduct
|
||||
}
|
||||
|
||||
if product.ColorVariants != nil && len(*product.ColorVariants) > 0 {
|
||||
linkedProducts := []gm.Product{*firstProduct}
|
||||
|
||||
for _, colorVariant := range *product.ColorVariants {
|
||||
|
||||
var (
|
||||
variant *gm.Product
|
||||
err error
|
||||
)
|
||||
if !colorVariant.IsSellable {
|
||||
|
||||
if err = importer.baza.Model(&gm.ProductFlat{}).Where("sku=?", colorVariant.ProductNumber).Update("status", false).Error; err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
continue
|
||||
|
||||
} else if variant, err = importer.importVariant(colorVariant); err != nil {
|
||||
|
||||
if variant, importer.Error = importer.updateVariant(colorVariant); importer.Error != nil {
|
||||
return importer
|
||||
}
|
||||
|
||||
linkedProducts = append(linkedProducts, *variant)
|
||||
|
||||
}
|
||||
|
||||
newProducts = append(newProducts, *variant)
|
||||
}
|
||||
|
||||
if len(newProducts) > 0 {
|
||||
//todo relation
|
||||
}
|
||||
|
||||
}
|
||||
return importer
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue