252 lines
7.7 KiB
Go
252 lines
7.7 KiB
Go
|
|
package repositories
|
||
|
|
|
||
|
|
import (
|
||
|
|
gm "db_service/gorm_models"
|
||
|
|
"db_service/models"
|
||
|
|
"math"
|
||
|
|
"strconv"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ProductRepo struct {
|
||
|
|
Categories []gm.Category
|
||
|
|
Brand gm.Brand
|
||
|
|
Weight float64
|
||
|
|
Description string
|
||
|
|
Keywords string
|
||
|
|
Data *models.Product
|
||
|
|
Error error
|
||
|
|
ColorOption gm.AttributeOption
|
||
|
|
SexOption gm.AttributeOption
|
||
|
|
}
|
||
|
|
|
||
|
|
func InitProductRepo(data *models.Product, color, sex gm.AttributeOption) *ProductRepo {
|
||
|
|
|
||
|
|
weight, wError := strconv.ParseFloat(data.Weight, 64)
|
||
|
|
|
||
|
|
if wError != nil || weight == 0 {
|
||
|
|
weight = 0.5
|
||
|
|
}
|
||
|
|
|
||
|
|
var description string
|
||
|
|
|
||
|
|
for _, desc := range data.Descriptions {
|
||
|
|
description += "<p>" + desc.Description + "</p>"
|
||
|
|
}
|
||
|
|
|
||
|
|
instance := &ProductRepo{
|
||
|
|
Weight: weight,
|
||
|
|
Description: description,
|
||
|
|
Data: data,
|
||
|
|
ColorOption: color,
|
||
|
|
SexOption: sex,
|
||
|
|
}
|
||
|
|
|
||
|
|
return instance
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) SetCategories(categories []gm.Category) {
|
||
|
|
pr.Categories = categories
|
||
|
|
pr.Keywords = pr.Data.Brand
|
||
|
|
|
||
|
|
for _, cat := range categories {
|
||
|
|
//log.Println(cat)
|
||
|
|
if len(cat.Translations) > 0 && cat.Translations[0].MetaKeywords != "" {
|
||
|
|
translation := cat.Translations[0]
|
||
|
|
|
||
|
|
pr.Keywords += "," + translation.MetaKeywords
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) makeProduct(imp *Importer) gm.Product {
|
||
|
|
var famID uint = 1
|
||
|
|
|
||
|
|
if len(imp.families) > 0 { //todo make real fam function
|
||
|
|
famID = imp.families[0].ID
|
||
|
|
}
|
||
|
|
|
||
|
|
product := gm.Product{
|
||
|
|
Sku: pr.Data.ProductNumber,
|
||
|
|
Type: "simple",
|
||
|
|
AttributeFamilyID: famID,
|
||
|
|
BrandID: pr.Brand.ID,
|
||
|
|
Categories: pr.Categories,
|
||
|
|
AttributeValues: pr.getProductAttributes(imp.AttributesMap, pr.Data),
|
||
|
|
}
|
||
|
|
|
||
|
|
if pr.Data.SizeVariants != nil && len(*pr.Data.SizeVariants) > 0 {
|
||
|
|
product.Type = "configurable"
|
||
|
|
product.SuperAttributes = []gm.Attribute{imp.AttributesMap["size"]}
|
||
|
|
} else {
|
||
|
|
price := pr.Data.Price
|
||
|
|
|
||
|
|
if price.OriginalPrice.Value > price.DiscountedPrice.Value {
|
||
|
|
product.AttributeValues = append(product.AttributeValues, []gm.ProductAttributeValue{
|
||
|
|
{AttributeID: imp.AttributesMap["price"].ID, FloatValue: price.OriginalPrice.Value},
|
||
|
|
{AttributeID: imp.AttributesMap["special_price"].ID, FloatValue: price.DiscountedPrice.Value},
|
||
|
|
}...)
|
||
|
|
} else {
|
||
|
|
product.AttributeValues = append(product.AttributeValues, gm.ProductAttributeValue{
|
||
|
|
AttributeID: imp.AttributesMap["price"].ID, FloatValue: price.DiscountedPrice.Value,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, element := range pr.Data.Images {
|
||
|
|
product.Images = append(product.Images, gm.ProductImage{Type: "cdn", Path: element})
|
||
|
|
}
|
||
|
|
|
||
|
|
return product
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) makeVariant(parentID, famID uint) gm.Product {
|
||
|
|
|
||
|
|
//todo
|
||
|
|
product := gm.Product{
|
||
|
|
Sku: pr.Data.ProductNumber,
|
||
|
|
Type: "simple",
|
||
|
|
AttributeFamilyID: famID,
|
||
|
|
BrandID: pr.Brand.ID,
|
||
|
|
Categories: pr.Categories,
|
||
|
|
ParentID: parentID,
|
||
|
|
}
|
||
|
|
|
||
|
|
return product
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) makeProductFlat(im *Importer) gm.ProductFlat {
|
||
|
|
|
||
|
|
flat := gm.ProductFlat{
|
||
|
|
|
||
|
|
Status: true,
|
||
|
|
VisibleIndividually: true,
|
||
|
|
Name: pr.Data.Name,
|
||
|
|
Sku: pr.Data.ProductGroupID,
|
||
|
|
//ProductNumber: pr.Data.ProductNumber,
|
||
|
|
Description: pr.Description,
|
||
|
|
//UrlKey: pr.Data.ProductGroupID,
|
||
|
|
Weight: pr.Weight,
|
||
|
|
FavoritesCount: uint(pr.Data.FavoriteCount),
|
||
|
|
MaxPrice: 0,
|
||
|
|
MinPrice: 0,
|
||
|
|
Price: 0,
|
||
|
|
}
|
||
|
|
|
||
|
|
if pr.Data.Color != "" {
|
||
|
|
flat.Color = int(im.GetColorOption(pr.Data.Color).ID)
|
||
|
|
flat.ColorLabel = pr.Data.Color
|
||
|
|
}
|
||
|
|
|
||
|
|
if pr.Data.SizeVariants != nil && len(*pr.Data.SizeVariants) > 0 {
|
||
|
|
|
||
|
|
for _, variant := range *pr.Data.SizeVariants {
|
||
|
|
price := variant.Price
|
||
|
|
|
||
|
|
if flat.MinPrice == 0 || flat.MinPrice > price.DiscountedPrice.Value {
|
||
|
|
flat.MaxPrice = price.DiscountedPrice.Value
|
||
|
|
}
|
||
|
|
|
||
|
|
maxPrice := math.Max(price.OriginalPrice.Value, price.DiscountedPrice.Value)
|
||
|
|
if flat.MaxPrice == 0 || flat.MaxPrice < maxPrice {
|
||
|
|
flat.MaxPrice = maxPrice
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
} else {
|
||
|
|
flat.MinPrice = pr.Data.Price.DiscountedPrice.Value
|
||
|
|
flat.MaxPrice = math.Max(pr.Data.Price.OriginalPrice.Value, pr.Data.Price.DiscountedPrice.Value)
|
||
|
|
flat.Price = flat.MaxPrice
|
||
|
|
|
||
|
|
if flat.MinPrice < flat.MaxPrice {
|
||
|
|
flat.SpecialPrice = flat.MinPrice
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
return flat
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) makeVariantFlat(variant models.Variant, SizID int) gm.ProductFlat {
|
||
|
|
|
||
|
|
maxPRice := math.Max(variant.Price.OriginalPrice.Value, variant.Price.DiscountedPrice.Value)
|
||
|
|
|
||
|
|
flat := gm.ProductFlat{
|
||
|
|
Status: true,
|
||
|
|
Name: pr.Data.Name,
|
||
|
|
Sku: pr.Data.ProductGroupID,
|
||
|
|
ProductNumber: pr.Data.ProductNumber,
|
||
|
|
Weight: pr.Weight,
|
||
|
|
FavoritesCount: uint(pr.Data.FavoriteCount),
|
||
|
|
SizeLabel: variant.AttributeValue,
|
||
|
|
Size: SizID,
|
||
|
|
MaxPrice: maxPRice,
|
||
|
|
MinPrice: variant.Price.DiscountedPrice.Value,
|
||
|
|
Price: maxPRice,
|
||
|
|
}
|
||
|
|
|
||
|
|
if flat.MaxPrice > flat.MinPrice {
|
||
|
|
flat.SpecialPrice = flat.MinPrice
|
||
|
|
}
|
||
|
|
|
||
|
|
if pr.Data.Color != "" {
|
||
|
|
flat.Color = int(pr.ColorOption.ID)
|
||
|
|
flat.ColorLabel = pr.Data.Color
|
||
|
|
}
|
||
|
|
|
||
|
|
return flat
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) getProductAttributes(AttributesMap map[string]gm.Attribute, product *models.Product) []gm.ProductAttributeValue {
|
||
|
|
attributes := []gm.ProductAttributeValue{
|
||
|
|
{AttributeID: AttributesMap["source"].ID, TextValue: product.URLKey},
|
||
|
|
{AttributeID: AttributesMap["favoritesCount"].ID, IntegerValue: product.FavoriteCount},
|
||
|
|
{AttributeID: AttributesMap["sku"].ID, TextValue: product.ProductNumber},
|
||
|
|
{AttributeID: AttributesMap["name"].ID, TextValue: product.Name, Channel: "default", Locale: "tm"},
|
||
|
|
{AttributeID: AttributesMap["weight"].ID, TextValue: product.Weight},
|
||
|
|
{AttributeID: AttributesMap["status"].ID, BooleanValue: true},
|
||
|
|
{AttributeID: AttributesMap["visible_individually"].ID, BooleanValue: true},
|
||
|
|
{AttributeID: AttributesMap["description"].ID, TextValue: pr.Description, Channel: "default", Locale: "tm"},
|
||
|
|
{AttributeID: AttributesMap["meta_keywords"].ID, TextValue: pr.Keywords, Channel: "default", Locale: "tm"},
|
||
|
|
}
|
||
|
|
|
||
|
|
if product.Color != "" {
|
||
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["color"].ID, IntegerValue: int(pr.ColorOption.ID)})
|
||
|
|
}
|
||
|
|
|
||
|
|
if product.Cinsiyet != "" {
|
||
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["cinsiyet"].ID, IntegerValue: int(pr.SexOption.ID)})
|
||
|
|
}
|
||
|
|
|
||
|
|
return attributes
|
||
|
|
}
|
||
|
|
|
||
|
|
func (pr *ProductRepo) getVariantAttributes(AttributesMap map[string]gm.Attribute, product *models.Variant, SizID int) []gm.ProductAttributeValue {
|
||
|
|
|
||
|
|
price := math.Max(product.Price.OriginalPrice.Value, product.Price.DiscountedPrice.Value)
|
||
|
|
|
||
|
|
attributes := []gm.ProductAttributeValue{
|
||
|
|
{AttributeID: AttributesMap["source"].ID, TextValue: pr.Data.URLKey},
|
||
|
|
{AttributeID: AttributesMap["sku"].ID, TextValue: string(rune(product.ItemNumber))}, //todo unique
|
||
|
|
{AttributeID: AttributesMap["product_number"].ID, TextValue: string(rune(product.ItemNumber))}, //todo unique
|
||
|
|
{AttributeID: AttributesMap["name"].ID, TextValue: pr.Data.Name, Channel: "default", Locale: "tm"},
|
||
|
|
{AttributeID: AttributesMap["weight"].ID, TextValue: pr.Data.Weight},
|
||
|
|
{AttributeID: AttributesMap["status"].ID, BooleanValue: true},
|
||
|
|
{AttributeID: AttributesMap["size"].ID, IntegerValue: SizID},
|
||
|
|
{AttributeID: AttributesMap["price"].ID, FloatValue: price},
|
||
|
|
}
|
||
|
|
|
||
|
|
if pr.Data.Color != "" {
|
||
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["color"].ID, IntegerValue: int(pr.ColorOption.ID)})
|
||
|
|
}
|
||
|
|
|
||
|
|
if pr.Data.Cinsiyet != "" {
|
||
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["cinsiyet"].ID, IntegerValue: int(pr.SexOption.ID)})
|
||
|
|
}
|
||
|
|
|
||
|
|
if product.Price.OriginalPrice.Value > product.Price.DiscountedPrice.Value {
|
||
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["special_price"].ID, FloatValue: product.Price.DiscountedPrice.Value})
|
||
|
|
}
|
||
|
|
|
||
|
|
return attributes
|
||
|
|
}
|