309 lines
9.3 KiB
Go
309 lines
9.3 KiB
Go
package repositories
|
|
|
|
import (
|
|
gm "db_service/gorm_models"
|
|
"db_service/models"
|
|
"fmt"
|
|
"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
|
|
ImageType string
|
|
}
|
|
|
|
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,
|
|
ImageType: "cdn",
|
|
}
|
|
|
|
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) HasSizeVariants() bool {
|
|
return pr.Data.SizeVariants != nil && len(*pr.Data.SizeVariants) > 0
|
|
}
|
|
|
|
func (pr *ProductRepo) HasColorVariants() bool {
|
|
return pr.Data.ColorVariants != nil && len(*pr.Data.ColorVariants) > 0
|
|
}
|
|
|
|
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,
|
|
AttributeFamilyID: famID,
|
|
Categories: pr.Categories,
|
|
AttributeValues: pr.getProductAttributes(imp.AttributesMap, pr.Data),
|
|
}
|
|
|
|
if pr.Data.Brand != "" {
|
|
product.BrandID = pr.Brand.ID
|
|
}
|
|
|
|
if pr.HasSizeVariants() {
|
|
product.Type = "configurable"
|
|
if (*pr.Data.SizeVariants)[0].AttributeName == "Beden" {
|
|
product.SuperAttributes = []gm.Attribute{imp.AttributesMap["size"]}
|
|
} else {
|
|
product.SuperAttributes = []gm.Attribute{imp.AttributesMap["boyut"]}
|
|
product.AttributeFamilyID = 4 // todo fix hardcode
|
|
}
|
|
|
|
} else {
|
|
product.Type = "simple"
|
|
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: pr.ImageType, Path: element})
|
|
}
|
|
|
|
return product
|
|
}
|
|
|
|
func (pr *ProductRepo) makeVariant(parentID, famID uint, sku string) gm.Product {
|
|
|
|
product := gm.Product{
|
|
Sku: sku,
|
|
Type: "simple",
|
|
AttributeFamilyID: famID,
|
|
Categories: pr.Categories,
|
|
ParentID: parentID,
|
|
}
|
|
|
|
if pr.Data.Brand != "" {
|
|
product.BrandID = pr.Brand.ID
|
|
}
|
|
|
|
return product
|
|
}
|
|
|
|
func (pr *ProductRepo) makeProductFlat(productId uint) gm.ProductFlat {
|
|
|
|
flat := gm.ProductFlat{
|
|
ProductID: productId,
|
|
Status: true,
|
|
VisibleIndividually: true,
|
|
Name: pr.Data.Name,
|
|
Sku: pr.Data.ProductNumber,
|
|
//ProductNumber: pr.Data.ProductNumber,
|
|
Description: pr.Description,
|
|
//UrlKey: pr.Data.ProductGroupID,
|
|
Weight: pr.Weight,
|
|
FavoritesCount: uint(pr.Data.FavoriteCount),
|
|
MetaKeywords: pr.Keywords,
|
|
MaxPrice: 0,
|
|
MinPrice: 0,
|
|
Price: 0,
|
|
}
|
|
|
|
if pr.Data.Color != "" {
|
|
flat.Color = int(pr.ColorOption.ID)
|
|
flat.ColorLabel = pr.Data.Color
|
|
}
|
|
|
|
if pr.Data.Brand != "" {
|
|
flat.BrandID = pr.Brand.ID
|
|
}
|
|
|
|
if pr.Data.Cinsiyet != "" {
|
|
flat.Cinsiyet = int(pr.SexOption.ID)
|
|
flat.CinsiyetLabel = pr.Data.Cinsiyet
|
|
}
|
|
|
|
if !pr.HasSizeVariants() {
|
|
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
|
|
}
|
|
|
|
}
|
|
//else {//todo calculate price after variants inserted
|
|
// 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
|
|
// }
|
|
// }
|
|
//
|
|
//}
|
|
|
|
return flat
|
|
}
|
|
|
|
func (pr *ProductRepo) makeVariantFlat(variant models.Variant, SizID, parentID, productID uint) gm.ProductFlat {
|
|
|
|
maxPRice := math.Max(variant.Price.OriginalPrice.Value, variant.Price.DiscountedPrice.Value)
|
|
sku := fmt.Sprintf("%s-%d", pr.Data.ProductNumber, variant.ItemNumber)
|
|
|
|
flat := gm.ProductFlat{
|
|
ParentID: parentID,
|
|
Status: true,
|
|
Name: pr.Data.Name,
|
|
Sku: sku,
|
|
ProductNumber: fmt.Sprintf("%d", variant.ItemNumber),
|
|
Weight: pr.Weight,
|
|
FavoritesCount: uint(pr.Data.FavoriteCount),
|
|
SizeLabel: variant.AttributeValue,
|
|
Size: int(SizID),
|
|
MaxPrice: maxPRice,
|
|
MinPrice: variant.Price.DiscountedPrice.Value,
|
|
Price: maxPRice,
|
|
ProductID: productID,
|
|
}
|
|
|
|
if variant.AttributeName == "Beden" {
|
|
flat.Size = int(SizID)
|
|
flat.SizeLabel = variant.AttributeValue
|
|
} else {
|
|
flat.Boyut = int(SizID)
|
|
flat.BoyutLabel = variant.AttributeValue
|
|
}
|
|
|
|
if flat.MaxPrice > flat.MinPrice {
|
|
flat.SpecialPrice = flat.MinPrice
|
|
}
|
|
|
|
if pr.Data.Color != "" {
|
|
flat.Color = int(pr.ColorOption.ID)
|
|
flat.ColorLabel = pr.Data.Color
|
|
}
|
|
|
|
if pr.Data.Brand != "" {
|
|
flat.BrandID = pr.Brand.ID
|
|
}
|
|
|
|
if pr.Data.Cinsiyet != "" {
|
|
flat.Cinsiyet = int(pr.SexOption.ID)
|
|
flat.CinsiyetLabel = pr.Data.Cinsiyet
|
|
}
|
|
|
|
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 uint) []gm.ProductAttributeValue {
|
|
|
|
price := math.Max(product.Price.OriginalPrice.Value, product.Price.DiscountedPrice.Value)
|
|
sku := fmt.Sprintf("%s-%d", pr.Data.ProductNumber, product.ItemNumber)
|
|
attributes := []gm.ProductAttributeValue{
|
|
{AttributeID: AttributesMap["source"].ID, TextValue: pr.Data.URLKey},
|
|
{AttributeID: AttributesMap["sku"].ID, TextValue: sku}, //todo unique
|
|
{AttributeID: AttributesMap["product_number"].ID, TextValue: fmt.Sprintf("%d", 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: int(SizID)},
|
|
{AttributeID: AttributesMap["price"].ID, FloatValue: price},
|
|
}
|
|
if product.AttributeName == "Beden" {
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["size"].ID, IntegerValue: int(SizID)})
|
|
} else {
|
|
attributes = append(attributes, gm.ProductAttributeValue{AttributeID: AttributesMap["boyut"].ID, IntegerValue: int(SizID)})
|
|
}
|
|
|
|
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
|
|
}
|