2022-09-07 12:55:55 +00:00
package controller
2022-08-27 06:43:56 +00:00
import (
gm "db_service/gorm_models"
"db_service/models"
helper "db_service/pkg"
"encoding/json"
2022-09-20 13:41:12 +00:00
"errors"
2022-08-27 06:43:56 +00:00
"fmt"
"log"
"math/big"
"net/http"
"os"
"strconv"
"sync"
2022-09-21 06:42:19 +00:00
"sync/atomic"
2022-08-27 06:43:56 +00:00
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
2022-09-07 12:55:55 +00:00
var (
mainCategories [ ] gm . Category
baza * gorm . DB
mainImportWG , famAndSellerWG sync . WaitGroup
families [ ] gm . AttributeFamily
sellers = make ( map [ string ] gm . MarketplaceSeller )
2022-09-15 12:24:06 +00:00
AttributesMap = make ( map [ string ] gm . Attribute )
2022-09-21 06:42:19 +00:00
locker = stateUnlocked
)
const (
stateUnlocked uint32 = iota
stateLocked
2022-09-07 12:55:55 +00:00
)
2022-08-27 06:43:56 +00:00
2022-09-07 12:55:55 +00:00
func StartImport ( w http . ResponseWriter , route * http . Request ) {
2022-09-21 06:42:19 +00:00
// lock the request
if ! atomic . CompareAndSwapUint32 ( & locker , stateUnlocked , stateLocked ) {
w . WriteHeader ( http . StatusTooManyRequests )
2022-09-26 08:46:10 +00:00
err := json . NewEncoder ( w ) . Encode ( map [ string ] string {
"msg" : "Product import in progress!" ,
2022-09-21 06:42:19 +00:00
} )
2022-09-26 08:46:10 +00:00
log . Println ( err )
2022-09-21 06:42:19 +00:00
return
}
defer atomic . StoreUint32 ( & locker , stateUnlocked )
2022-08-27 06:43:56 +00:00
start := time . Now ( )
r := new ( big . Int )
fmt . Println ( "start import" , r . Binomial ( 1000 , 10 ) )
2022-09-22 15:10:51 +00:00
baza , err := gorm . Open ( mysql . Open ( os . Getenv ( "database_url" ) ) , & gorm . Config { SkipDefaultTransaction : true } )
2022-08-27 06:43:56 +00:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
//get categories from mysql on main thread
mainCategories = gm . GetMainCategories ( baza )
famAndSellerWG . Add ( 3 )
2022-09-26 08:46:10 +00:00
//get attribute families from mysql
2022-08-27 06:43:56 +00:00
go func ( ) {
defer famAndSellerWG . Done ( )
2022-09-03 13:27:46 +00:00
families , err = gm . GetFamilies ( baza )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2022-08-27 06:43:56 +00:00
} ( )
2022-09-26 08:46:10 +00:00
//get sellers families from mysql
2022-08-27 06:43:56 +00:00
go func ( ) {
defer famAndSellerWG . Done ( )
2022-09-03 13:27:46 +00:00
var vendors , err = gm . GetSellers ( baza )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
for _ , vendor := range vendors {
2022-08-27 06:43:56 +00:00
sellers [ vendor . Url ] = vendor
}
} ( )
go func ( ) {
defer famAndSellerWG . Done ( )
2022-09-03 13:27:46 +00:00
var attributes , err = gm . GetAttributes ( baza )
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
return
}
2022-09-26 08:46:10 +00:00
for _ , attribute := range attributes {
AttributesMap [ attribute . Code ] = attribute
2022-08-27 06:43:56 +00:00
}
} ( )
mainImportWG . Add ( len ( mainCategories ) )
2022-09-21 06:33:40 +00:00
log . Println ( "Start Product delete" )
2022-09-26 08:46:10 +00:00
deleteTime := time . Now ( )
2022-09-25 17:20:42 +00:00
errDel := gm . DeleteProducts ( baza )
2022-09-26 08:46:10 +00:00
deleteElapsed := time . Since ( deleteTime )
2022-09-25 17:20:42 +00:00
log . Printf ( "Delete products took %s" , deleteElapsed )
if errDel != nil {
http . Error ( w , errDel . Error ( ) , http . StatusInternalServerError )
return
}
2022-09-01 11:26:15 +00:00
2022-08-27 06:43:56 +00:00
for _ , element := range mainCategories {
slug := element . Translations [ 0 ] . Slug
2022-09-07 12:55:55 +00:00
go importCategoryProducts ( "ty_db_" + slug , baza )
2022-08-27 06:43:56 +00:00
// fmt.Println(<-result)
}
mainImportWG . Wait ( )
2022-09-05 12:35:26 +00:00
err = gm . Flush ( )
2022-08-27 06:43:56 +00:00
2022-09-05 12:35:26 +00:00
if err != nil {
http . Error ( w , err . Error ( ) , http . StatusInternalServerError )
}
2022-09-06 13:49:08 +00:00
2022-08-27 06:43:56 +00:00
elapsed := time . Since ( start )
log . Printf ( "end import took %s" , elapsed )
2022-09-06 13:49:08 +00:00
http . Error ( w , fmt . Sprintf ( "end import took %s" , elapsed ) , http . StatusOK )
2022-08-27 06:43:56 +00:00
}
2022-09-07 12:55:55 +00:00
func importCategoryProducts ( dbName string , db * gorm . DB ) {
2022-08-27 06:43:56 +00:00
defer mainImportWG . Done ( )
2022-09-24 08:14:56 +00:00
if dbExists := helper . CheckDBExists ( os . Getenv ( "couch_db_source" ) + dbName ) ; dbExists {
2022-08-27 06:43:56 +00:00
totalDocCount := getTotalDocumentCount ( dbName )
skip := 0
2022-09-24 08:14:56 +00:00
limit := 100
totalImport := 0
2022-08-27 06:43:56 +00:00
for skip < totalDocCount {
var response models . BagistoModelResponse
url := fmt . Sprintf ( "%s%s/_all_docs?include_docs=true&limit=%v&skip=%v" , os . Getenv ( "couch_db_source" ) , dbName , limit , skip )
fmt . Println ( url )
skip += limit
2022-09-07 12:55:55 +00:00
body , err := helper . SendRequest ( "GET" , url , nil , "" )
2022-08-27 06:43:56 +00:00
if err != nil {
fmt . Println ( err . Error ( ) )
continue
}
2022-09-24 08:14:56 +00:00
if err = json . Unmarshal ( body , & response ) ; err != nil {
2022-08-27 06:43:56 +00:00
log . Println ( err . Error ( ) )
continue
}
2022-09-26 08:46:10 +00:00
//iterate 100 row products
2022-08-27 06:43:56 +00:00
for _ , element := range response . Rows {
2022-09-24 08:14:56 +00:00
if err := ImportProduct ( element . Doc , db ) ; err != nil {
log . Println ( err )
} else {
totalImport ++
2022-09-22 13:43:01 +00:00
}
2022-08-27 06:43:56 +00:00
}
2022-09-24 08:14:56 +00:00
log . Printf ( "%s total imported documents count %d \n" , dbName , totalImport )
2022-08-27 06:43:56 +00:00
}
} else {
fmt . Println ( dbName + "+doesnt exist" )
}
}
func getTotalDocumentCount ( db string ) int {
var response models . DBDocCountResponse
url := os . Getenv ( "couch_db_source" ) + db
2022-09-07 12:55:55 +00:00
body , err := helper . SendRequest ( "GET" , url , nil , "" )
2022-08-27 06:43:56 +00:00
if err != nil {
log . Println ( err . Error ( ) )
return 0
}
err = json . Unmarshal ( body , & response )
if err != nil {
log . Println ( err . Error ( ) )
return 0
}
return response . DocCount
}
2022-09-25 17:24:33 +00:00
func getCats ( db * gorm . DB , catIDs [ ] int ) ( [ ] gm . Category , string , error ) {
var categories [ ] gm . Category
2022-08-27 06:43:56 +00:00
var keywords string
2022-09-27 14:04:16 +00:00
if errCat := db . Preload ( "Translations" , "locale=?" , "tm" ) . Find ( & categories , catIDs ) . Error ; errCat != nil {
2022-09-25 17:20:42 +00:00
log . Println ( errCat )
return categories , keywords , errCat
2022-08-27 06:43:56 +00:00
}
for _ , cat := range categories {
2022-09-27 13:56:57 +00:00
log . Println ( cat )
2022-08-27 06:43:56 +00:00
if len ( cat . Translations ) > 0 && cat . Translations [ 0 ] . MetaKeywords != "" {
translation := cat . Translations [ 0 ]
keywords += "," + translation . MetaKeywords
}
}
return categories , keywords , nil
}
2022-09-07 12:55:55 +00:00
func ImportProduct ( product models . Product , db * gorm . DB ) error {
2022-08-27 06:43:56 +00:00
famAndSellerWG . Wait ( ) //wait until attribute families and sellers are not get from mysql
2022-09-22 14:52:27 +00:00
//BEGIN TRANSACTION
2022-09-25 17:03:30 +00:00
2022-09-22 14:52:27 +00:00
tx := * db . Begin ( )
2022-08-27 06:43:56 +00:00
2022-09-24 08:14:56 +00:00
categories , keywords , errCat := getCats ( & tx , product . Categories )
2022-09-25 16:57:34 +00:00
2022-08-27 06:43:56 +00:00
if errCat != nil {
2022-09-25 16:47:16 +00:00
log . Println ( "ERR0 Categories" + errCat . Error ( ) )
tx . Rollback ( )
2022-09-07 12:55:55 +00:00
return errCat
2022-08-27 06:43:56 +00:00
}
2022-09-27 13:53:17 +00:00
return tx . Rollback ( ) . Error
2022-09-22 15:10:51 +00:00
brand , err := gm . FindOrCreateBrand ( & tx , product . Brand , categories )
if err != nil {
tx . Rollback ( )
2022-09-25 16:47:16 +00:00
log . Println ( "ERR BRAND" + err . Error ( ) )
2022-09-22 15:10:51 +00:00
return err
2022-08-27 06:43:56 +00:00
}
attributes , mainProductFlat := prepearAttributesWithFlat ( & product )
2022-09-22 11:22:16 +00:00
2022-09-22 14:52:27 +00:00
colorOption := gm . GetAttributeOption ( & tx , AttributesMap [ "color" ] . ID , product . Color )
2022-09-26 08:46:10 +00:00
cinsiyetOption := gm . GetAttributeOption ( & tx , AttributesMap [ "cinsiyet" ] . ID , product . Cinsiyet )
2022-09-22 11:22:16 +00:00
2022-09-05 13:09:43 +00:00
mainProductFlat . Color = int ( colorOption . ID )
mainProductFlat . ColorLabel = product . Color
2022-09-22 11:22:16 +00:00
2022-09-05 13:09:43 +00:00
attributes = append ( attributes , [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "color" ] . ID , IntegerValue : int ( colorOption . ID ) } ,
2022-09-26 08:46:10 +00:00
{ AttributeID : AttributesMap [ "cinsiyet" ] . ID , IntegerValue : int ( cinsiyetOption . ID ) } ,
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "meta_keywords" ] . ID , TextValue : keywords , Channel : "default" , Locale : "tm" } } ... )
2022-08-27 06:43:56 +00:00
2022-09-22 11:22:16 +00:00
var productImages [ ] gm . ProductImage
2022-09-05 16:16:10 +00:00
for _ , element := range product . Images {
productImages = append ( productImages , gm . ProductImage { Type : "cdn" , Path : element } )
}
2022-09-09 06:25:47 +00:00
var famID uint = 1
if len ( families ) > 0 {
famID = families [ 0 ] . ID
}
2022-08-27 06:43:56 +00:00
iproduct := gm . Product {
Sku : product . ProductGroupID ,
Type : "simple" ,
2022-09-09 06:25:47 +00:00
AttributeFamilyID : famID ,
2022-08-27 06:43:56 +00:00
BrandID : brand . ID ,
Categories : categories ,
AttributeValues : attributes ,
2022-09-05 16:16:10 +00:00
Images : productImages ,
2022-08-27 06:43:56 +00:00
}
2022-09-03 13:27:46 +00:00
if len ( product . ColorVariants ) > 0 {
2022-09-15 12:24:06 +00:00
iproduct . SuperAttributes = append ( iproduct . SuperAttributes , AttributesMap [ "color" ] )
2022-08-27 06:43:56 +00:00
}
if len ( product . SizeVariants ) > 0 {
2022-09-15 12:24:06 +00:00
iproduct . SuperAttributes = append ( iproduct . SuperAttributes , AttributesMap [ "size" ] )
2022-08-27 06:43:56 +00:00
}
if len ( iproduct . SuperAttributes ) > 0 {
iproduct . Type = "configurable"
2022-09-09 06:25:47 +00:00
famID = 2
if len ( families ) > 1 {
famID = families [ 1 ] . ID
}
iproduct . AttributeFamilyID = famID
2022-08-27 06:43:56 +00:00
}
2022-09-05 16:16:10 +00:00
2022-09-22 11:22:16 +00:00
if errMainProduct := tx . Omit ( "Categories.*" , "SuperAttributes.*" , "ParentID" ) . Create ( & iproduct ) . Error ; errMainProduct != nil {
2022-09-22 13:02:26 +00:00
log . Println ( "ERR1" + errMainProduct . Error ( ) )
2022-09-22 11:22:16 +00:00
tx . Rollback ( )
2022-09-07 12:55:55 +00:00
return errMainProduct
2022-08-27 06:43:56 +00:00
}
mainProductFlat . ProductID = iproduct . ID
mainProductFlat . BrandID = brand . ID
mainProductFlat . MetaKeywords = keywords
2022-09-22 11:22:16 +00:00
if errProductMainFlat := tx . Create ( & mainProductFlat ) . Error ; errProductMainFlat != nil {
tx . Rollback ( )
2022-09-22 13:02:26 +00:00
log . Println ( "ERR2" + errProductMainFlat . Error ( ) )
2022-09-07 12:55:55 +00:00
return errProductMainFlat
2022-08-27 06:43:56 +00:00
}
2022-09-01 11:16:43 +00:00
if len ( product . ColorVariants ) > 0 {
2022-09-21 13:03:09 +00:00
product . ColorVariants = append ( [ ] models . Product { product } , product . ColorVariants ... )
2022-09-01 11:16:43 +00:00
2022-09-22 13:38:59 +00:00
for colorIndex , colorVariant := range product . ColorVariants {
weight , _ := strconv . ParseFloat ( colorVariant . Weight , 64 )
var description string
for _ , desc := range colorVariant . Descriptions {
description += "<p>" + desc . Description + "</p>"
}
2022-09-22 14:52:27 +00:00
colorOption := gm . GetAttributeOption ( & tx , AttributesMap [ "color" ] . ID , colorVariant . Color )
2022-09-22 13:38:59 +00:00
if len ( colorVariant . SizeVariants ) > 0 {
for index , sizeVariant := range colorVariant . SizeVariants {
2022-09-22 13:43:01 +00:00
savePoint := "Size" + strconv . Itoa ( colorIndex ) + "sp" + strconv . Itoa ( index )
2022-09-22 13:38:59 +00:00
tx . SavePoint ( savePoint )
2022-09-22 13:57:42 +00:00
if sizeVariant . ItemNumber == 0 {
sizeVariant . ItemNumber = index
}
2022-09-22 14:25:12 +00:00
sku := fmt . Sprintf ( "%s-%s-%d-%d" , colorVariant . ProductGroupID , colorVariant . ProductNumber , sizeVariant . ItemNumber , index )
2022-09-22 13:38:59 +00:00
productVariant := gm . Product {
ParentID : mainProductFlat . ProductID ,
Type : "simple" ,
AttributeFamilyID : iproduct . AttributeFamilyID ,
Sku : sku ,
BrandID : mainProductFlat . BrandID ,
Categories : categories ,
}
for _ , element := range colorVariant . Images {
productVariant . Images = append ( productVariant . Images , gm . ProductImage { Type : "cdn" , Path : element } )
}
if errProdVariant := tx . Omit ( "Categories.*" ) . Create ( & productVariant ) . Error ; errProdVariant != nil {
log . Println ( "ERR3" + errProdVariant . Error ( ) )
tx . RollbackTo ( savePoint )
2022-09-24 08:31:09 +00:00
continue
2022-09-22 13:38:59 +00:00
}
2022-09-22 14:52:27 +00:00
sizeOption := gm . GetAttributeOption ( & tx , AttributesMap [ "size" ] . ID , sizeVariant . AttributeValue )
2022-09-22 13:38:59 +00:00
productNumber := fmt . Sprintf ( "%s-%d" , colorVariant . ProductNumber , sizeVariant . ItemNumber )
attributes := [ ] gm . ProductAttributeValue {
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "source" ] . ID , TextValue : colorVariant . URLKey } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "favoritesCount" ] . ID , IntegerValue : colorVariant . FavoriteCount } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "sku" ] . ID , TextValue : sku } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "product_number" ] . ID , TextValue : productNumber } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "name" ] . ID , TextValue : colorVariant . Name , Channel : "default" , Locale : "tm" } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "weight" ] . ID , TextValue : colorVariant . Weight } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "status" ] . ID , BooleanValue : true } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "url_key" ] . ID , TextValue : sku } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "meta_keywords" ] . ID , TextValue : keywords , Channel : "default" , Locale : "tm" } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "description" ] . ID , TextValue : description , Channel : "default" , Locale : "tm" } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "color" ] . ID , IntegerValue : int ( colorOption . ID ) } ,
2022-09-26 08:46:10 +00:00
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "cinsiyet" ] . ID , IntegerValue : int ( cinsiyetOption . ID ) } ,
2022-09-22 13:38:59 +00:00
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "size" ] . ID , IntegerValue : int ( sizeOption . ID ) } ,
}
flatVariant := gm . ProductFlat {
//ProductID: productVariant.ID,
BrandID : mainProductFlat . BrandID ,
Status : true ,
//VisibleIndividually: false,
Name : colorVariant . Name ,
Sku : sku ,
ProductNumber : productNumber ,
Description : description ,
UrlKey : sku ,
Weight : weight ,
// Source: colorVariant.URLKey,
FavoritesCount : uint ( colorVariant . FavoriteCount ) ,
Color : int ( colorOption . ID ) ,
Size : int ( sizeOption . ID ) ,
ColorLabel : colorOption . AdminName ,
SizeLabel : sizeOption . AdminName ,
MetaKeywords : keywords ,
ParentID : mainProductFlat . ID ,
ProductID : productVariant . ID ,
}
if sizeVariant . Price . OriginalPrice . Value > sizeVariant . Price . DiscountedPrice . Value {
attributes = append ( attributes , [ ] gm . ProductAttributeValue {
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "price" ] . ID , FloatValue : sizeVariant . Price . OriginalPrice . Value } ,
{ ProductID : productVariant . ID , AttributeID : AttributesMap [ "special_price" ] . ID , FloatValue : sizeVariant . Price . DiscountedPrice . Value } ,
} ... )
flatVariant . Price = sizeVariant . Price . OriginalPrice . Value
flatVariant . SpecialPrice = sizeVariant . Price . DiscountedPrice . Value
flatVariant . MinPrice = sizeVariant . Price . DiscountedPrice . Value
flatVariant . MaxPrice = sizeVariant . Price . OriginalPrice . Value
} else {
attributes = append ( attributes , gm . ProductAttributeValue { ProductID : productVariant . ID , AttributeID : AttributesMap [ "price" ] . ID , FloatValue : sizeVariant . Price . DiscountedPrice . Value } )
flatVariant . Price = sizeVariant . Price . DiscountedPrice . Value
flatVariant . MinPrice = sizeVariant . Price . DiscountedPrice . Value
flatVariant . MaxPrice = sizeVariant . Price . DiscountedPrice . Value
}
if attError := tx . Create ( & attributes ) . Error ; attError != nil {
tx . RollbackTo ( savePoint )
log . Println ( "ERR4" + attError . Error ( ) )
continue
}
2022-09-24 08:31:09 +00:00
if errVariant := tx . Create ( & flatVariant ) . Error ; errVariant != nil {
log . Println ( "ERR5" + errVariant . Error ( ) )
tx . RollbackTo ( savePoint )
continue
}
2022-09-22 13:38:59 +00:00
if mainProductFlat . MinPrice > flatVariant . MinPrice || mainProductFlat . MinPrice == 0.0 {
mainProductFlat . MinPrice = flatVariant . MinPrice
}
if mainProductFlat . MaxPrice < flatVariant . MaxPrice {
mainProductFlat . MaxPrice = flatVariant . MaxPrice
}
mainProductFlat . Variants = append ( mainProductFlat . Variants , flatVariant )
}
} else {
2022-09-22 13:57:42 +00:00
colorSavePoint := "ColorSavePoint" + strconv . Itoa ( colorIndex )
2022-09-22 14:13:41 +00:00
tx . SavePoint ( colorSavePoint )
2022-09-22 14:52:27 +00:00
colorOption := gm . GetAttributeOption ( & tx , AttributesMap [ "color" ] . ID , colorVariant . Color )
2022-09-26 08:46:10 +00:00
cinsiyetOption := gm . GetAttributeOption ( & tx , AttributesMap [ "cinsiyet" ] . ID , colorVariant . Cinsiyet )
2022-09-22 13:43:01 +00:00
attributes , variantFlat := collectAttributes ( & colorVariant , & colorOption )
2022-09-26 08:46:10 +00:00
attributes = append ( attributes , [ ] gm . ProductAttributeValue {
{ AttributeID : AttributesMap [ "meta_keywords" ] . ID , TextValue : keywords , Channel : "default" , Locale : "tm" } ,
{ AttributeID : AttributesMap [ "cinsiyet" ] . ID , IntegerValue : int ( cinsiyetOption . ID ) } ,
} ... )
2022-09-22 13:43:01 +00:00
2022-09-26 08:46:10 +00:00
sku := fmt . Sprintf ( "%s-%s-%d-%s" , iproduct . Sku , colorVariant . ProductNumber , colorIndex , colorVariant . Color )
2022-09-22 13:43:01 +00:00
productVariant := gm . Product {
ParentID : mainProductFlat . ProductID ,
Type : "simple" ,
Sku : sku ,
BrandID : mainProductFlat . BrandID ,
AttributeValues : attributes ,
AttributeFamilyID : iproduct . AttributeFamilyID ,
Categories : categories ,
}
for _ , element := range colorVariant . Images {
productVariant . Images = append ( productVariant . Images , gm . ProductImage { Type : "cdn" , Path : element } )
}
if errProdVariant := tx . Omit ( "Categories.*" ) . Create ( & productVariant ) . Error ; errProdVariant != nil {
2022-09-22 13:57:42 +00:00
log . Println ( "ERR55" + errProdVariant . Error ( ) )
tx . RollbackTo ( colorSavePoint )
2022-09-22 13:43:01 +00:00
continue
}
variantFlat . ProductID = productVariant . ID
variantFlat . Color = int ( colorOption . ID )
variantFlat . ColorLabel = colorOption . AdminName
variantFlat . MetaKeywords = keywords
variantFlat . ParentID = mainProductFlat . ID
if errVariant := tx . Create ( & variantFlat ) . Error ; errVariant != nil {
log . Println ( "ERR6" + errVariant . Error ( ) )
tx . RollbackTo ( colorSavePoint )
continue
}
2022-09-24 08:31:09 +00:00
if mainProductFlat . MinPrice > variantFlat . MinPrice || mainProductFlat . MinPrice == 0 {
mainProductFlat . MinPrice = variantFlat . MinPrice
}
if mainProductFlat . MaxPrice < variantFlat . MaxPrice {
mainProductFlat . MaxPrice = variantFlat . MaxPrice
}
2022-09-22 13:43:01 +00:00
mainProductFlat . Variants = append ( mainProductFlat . Variants , variantFlat )
2022-09-22 13:38:59 +00:00
}
}
2022-09-22 11:22:16 +00:00
if len ( mainProductFlat . Variants ) == 0 {
2022-09-24 08:31:09 +00:00
return tx . Rollback ( ) . Error
2022-08-27 06:43:56 +00:00
}
2022-09-01 11:16:43 +00:00
} else if len ( product . SizeVariants ) > 0 {
weight , _ := strconv . ParseFloat ( product . Weight , 64 )
2022-09-22 14:52:27 +00:00
colorOption := gm . GetAttributeOption ( & tx , AttributesMap [ "color" ] . ID , product . Color )
2022-09-22 11:22:16 +00:00
for index , sizeVariant := range product . SizeVariants {
2022-09-22 13:43:01 +00:00
sizeSavePoint := "size" + strconv . Itoa ( index )
2022-09-22 11:22:16 +00:00
tx . SavePoint ( sizeSavePoint )
if sizeVariant . ItemNumber == 0 {
sizeVariant . ItemNumber = index
}
2022-08-27 06:43:56 +00:00
2022-09-22 14:30:44 +00:00
sku := fmt . Sprintf ( "%s-%s-%d-%d" , iproduct . Sku , product . ProductNumber , sizeVariant . ItemNumber , index )
2022-09-22 14:52:27 +00:00
sizeOption := gm . GetAttributeOption ( & tx , AttributesMap [ "size" ] . ID , sizeVariant . AttributeValue )
2022-09-01 11:16:43 +00:00
attributes := [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "source" ] . ID , TextValue : product . URLKey } ,
{ AttributeID : AttributesMap [ "favoritesCount" ] . ID , IntegerValue : product . FavoriteCount } ,
{ AttributeID : AttributesMap [ "sku" ] . ID , TextValue : sku } ,
{ AttributeID : AttributesMap [ "product_number" ] . ID , TextValue : fmt . Sprint ( sizeVariant . ItemNumber ) } ,
{ 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 [ "url_key" ] . ID , TextValue : sku } ,
{ AttributeID : AttributesMap [ "meta_keywords" ] . ID , TextValue : keywords , Channel : "default" , Locale : "tm" } ,
{ AttributeID : AttributesMap [ "size" ] . ID , IntegerValue : int ( sizeOption . ID ) } ,
{ AttributeID : AttributesMap [ "description" ] . ID , TextValue : mainProductFlat . Description , Channel : "default" , Locale : "tm" } ,
{ AttributeID : AttributesMap [ "color" ] . ID , IntegerValue : int ( colorOption . ID ) } ,
2022-09-26 08:46:10 +00:00
{ AttributeID : AttributesMap [ "cinsiyet" ] . ID , IntegerValue : int ( cinsiyetOption . ID ) } ,
2022-09-01 11:16:43 +00:00
}
flatVariant := gm . ProductFlat {
Status : true ,
VisibleIndividually : false ,
Name : product . Name ,
Sku : sku ,
ProductNumber : fmt . Sprint ( sizeVariant . ItemNumber ) ,
UrlKey : sku ,
Weight : weight ,
Size : int ( sizeOption . ID ) ,
SizeLabel : sizeOption . AdminName ,
MetaKeywords : keywords ,
Description : mainProductFlat . Description ,
2022-09-05 13:09:43 +00:00
Color : int ( colorOption . ID ) ,
ColorLabel : product . Color ,
FavoritesCount : uint ( product . FavoriteCount ) ,
ParentID : mainProductFlat . ID ,
2022-09-01 11:16:43 +00:00
}
if sizeVariant . Price . OriginalPrice . Value > sizeVariant . Price . DiscountedPrice . Value {
attributes = append ( attributes , [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "price" ] . ID , FloatValue : sizeVariant . Price . OriginalPrice . Value } ,
{ AttributeID : AttributesMap [ "special_price" ] . ID , FloatValue : sizeVariant . Price . DiscountedPrice . Value } ,
2022-09-01 11:16:43 +00:00
} ... )
flatVariant . Price = sizeVariant . Price . OriginalPrice . Value
flatVariant . SpecialPrice = sizeVariant . Price . DiscountedPrice . Value
flatVariant . MinPrice = sizeVariant . Price . DiscountedPrice . Value
flatVariant . MaxPrice = sizeVariant . Price . OriginalPrice . Value
} else {
2022-09-20 10:55:04 +00:00
attributes = append ( attributes , gm . ProductAttributeValue { AttributeID : AttributesMap [ "price" ] . ID , FloatValue : sizeVariant . Price . DiscountedPrice . Value } )
2022-09-01 11:16:43 +00:00
2022-09-20 10:55:04 +00:00
flatVariant . Price = sizeVariant . Price . DiscountedPrice . Value
2022-09-01 11:16:43 +00:00
flatVariant . MinPrice = sizeVariant . Price . DiscountedPrice . Value
2022-09-20 10:55:04 +00:00
flatVariant . MaxPrice = sizeVariant . Price . DiscountedPrice . Value
2022-09-01 11:16:43 +00:00
}
sizeVariantProduct := gm . Product {
ParentID : mainProductFlat . ProductID ,
Type : "simple" ,
Sku : sku ,
BrandID : mainProductFlat . BrandID ,
AttributeValues : attributes ,
AttributeFamilyID : iproduct . AttributeFamilyID ,
Categories : categories ,
2022-09-05 16:16:10 +00:00
}
for _ , element := range product . Images {
sizeVariantProduct . Images = append ( sizeVariantProduct . Images , gm . ProductImage { Type : "cdn" , Path : element } )
2022-09-01 11:16:43 +00:00
}
2022-09-22 11:22:16 +00:00
if errSizeVar := tx . Omit ( "Categories.*" ) . Create ( & sizeVariantProduct ) . Error ; errSizeVar != nil {
2022-09-22 13:02:26 +00:00
log . Println ( "ERR7" + errSizeVar . Error ( ) )
2022-09-22 14:09:19 +00:00
tx . RollbackTo ( sizeSavePoint )
2022-09-22 11:22:16 +00:00
continue
2022-09-01 11:16:43 +00:00
}
flatVariant . ProductID = sizeVariantProduct . ID
2022-09-22 11:44:01 +00:00
if errVariant := tx . Create ( & flatVariant ) . Error ; errVariant != nil {
2022-09-22 13:02:26 +00:00
log . Println ( "ERR8" + errVariant . Error ( ) )
2022-09-22 11:22:16 +00:00
tx . RollbackTo ( sizeSavePoint )
continue
2022-09-01 11:16:43 +00:00
}
2022-09-24 08:31:09 +00:00
if mainProductFlat . MinPrice > flatVariant . MinPrice || mainProductFlat . MinPrice == 0 {
mainProductFlat . MinPrice = flatVariant . MinPrice
}
if mainProductFlat . MaxPrice < flatVariant . MaxPrice {
mainProductFlat . MaxPrice = flatVariant . MaxPrice
}
2022-09-01 11:16:43 +00:00
mainProductFlat . Variants = append ( mainProductFlat . Variants , flatVariant )
2022-08-27 06:43:56 +00:00
}
2022-09-01 11:16:43 +00:00
2022-09-22 11:22:16 +00:00
if len ( mainProductFlat . Variants ) == 0 {
2022-09-24 08:31:09 +00:00
return tx . Rollback ( ) . Error
2022-09-22 11:22:16 +00:00
}
2022-09-01 11:16:43 +00:00
}
2022-09-21 09:47:58 +00:00
var errFlat error
if mainProductFlat . SpecialPrice != 0 {
2022-09-22 11:22:16 +00:00
errFlat = tx . Omit ( "ParentID" , "CreatedAt" , "Variants" ) . Save ( & mainProductFlat ) . Error
2022-09-21 09:47:58 +00:00
} else {
2022-09-22 11:22:16 +00:00
errFlat = tx . Omit ( "ParentID" , "CreatedAt" , "Variants" , "SpecialPrice" ) . Save ( & mainProductFlat ) . Error
2022-09-21 09:47:58 +00:00
}
2022-09-01 11:16:43 +00:00
if errFlat != nil {
2022-09-22 13:02:26 +00:00
log . Println ( "ERR9" + errFlat . Error ( ) )
2022-09-22 11:22:16 +00:00
tx . Rollback ( )
return errFlat
2022-08-27 06:43:56 +00:00
}
sProduct := createSellerProduct ( & mainProductFlat , product . Vendor )
2022-09-05 15:10:39 +00:00
2022-09-22 11:22:16 +00:00
errSProduct := tx . Create ( & sProduct ) . Error
2022-08-27 06:43:56 +00:00
if errSProduct != nil {
2022-09-22 13:02:26 +00:00
log . Println ( "ERR10" + errSProduct . Error ( ) )
2022-09-24 08:31:09 +00:00
tx . Rollback ( )
2022-09-22 11:22:16 +00:00
return errSProduct
2022-09-24 08:14:56 +00:00
} else {
return tx . Commit ( ) . Error
2022-08-27 06:43:56 +00:00
}
2022-09-07 12:55:55 +00:00
2022-08-27 06:43:56 +00:00
}
func createSellerProduct ( flat * gm . ProductFlat , sellerURL string ) gm . MarketplaceProduct {
2022-09-15 12:32:15 +00:00
sellerID := sellers [ sellerURL ] . ID
if sellerID == 0 {
sellerID = 1
}
2022-08-27 06:43:56 +00:00
sellerProduct := gm . MarketplaceProduct {
2022-09-15 12:32:15 +00:00
MarketplaceSellerID : sellerID ,
IsApproved : true ,
Condition : "new" ,
Description : "scraped" ,
IsOwner : true ,
ProductID : flat . ProductID ,
2022-08-27 06:43:56 +00:00
}
for _ , variant := range flat . Variants {
sellerProduct . Variants = append ( sellerProduct . Variants , gm . MarketplaceProduct {
2022-09-21 06:33:40 +00:00
ProductID : variant . ProductID ,
IsOwner : true ,
IsApproved : true ,
MarketplaceSellerID : sellerID ,
Condition : "new" ,
2022-08-27 06:43:56 +00:00
} )
}
return sellerProduct
}
func collectAttributes ( variant * models . Product , option * gm . AttributeOption ) ( [ ] gm . ProductAttributeValue , gm . ProductFlat ) {
sku := fmt . Sprintf ( "%s-%s" , variant . ProductGroupID , variant . ProductNumber )
weight , _ := strconv . ParseFloat ( variant . Weight , 64 )
2022-09-01 07:49:31 +00:00
var description string
2022-08-27 06:43:56 +00:00
2022-09-01 07:49:31 +00:00
for _ , desc := range variant . Descriptions {
description += "<p>" + desc . Description + "</p>"
}
2022-08-27 06:43:56 +00:00
2022-09-20 10:55:04 +00:00
flat := gm . ProductFlat {
2022-09-21 06:25:02 +00:00
Status : true ,
//VisibleIndividually: true,
Name : variant . Name ,
Sku : sku ,
ProductNumber : variant . ProductNumber ,
UrlKey : sku ,
Weight : weight ,
FavoritesCount : uint ( variant . FavoriteCount ) ,
Description : description ,
MinPrice : 0 ,
MaxPrice : 0 ,
2022-08-27 06:43:56 +00:00
}
attributes := [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "favoritesCount" ] . ID , IntegerValue : variant . FavoriteCount } ,
{ AttributeID : AttributesMap [ "source" ] . ID , TextValue : variant . URLKey } ,
{ AttributeID : AttributesMap [ "sku" ] . ID , TextValue : sku } ,
{ AttributeID : AttributesMap [ "product_number" ] . ID , TextValue : variant . ProductNumber } ,
{ AttributeID : AttributesMap [ "name" ] . ID , TextValue : variant . Name , Channel : "default" , Locale : "tm" } ,
{ AttributeID : AttributesMap [ "weight" ] . ID , TextValue : variant . Weight } ,
{ AttributeID : AttributesMap [ "status" ] . ID , BooleanValue : true } ,
2022-09-21 06:25:02 +00:00
//{AttributeID: AttributesMap["visible_individually"].ID, BooleanValue: true},
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "url_key" ] . ID , TextValue : sku } ,
2022-08-27 06:43:56 +00:00
{ AttributeID : option . AttributeID , IntegerValue : int ( option . ID ) } ,
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "description" ] . ID , TextValue : description , Channel : "default" , Locale : "tm" } ,
2022-08-27 06:43:56 +00:00
}
if variant . Price . OriginalPrice . Value > variant . Price . DiscountedPrice . Value {
attributes = append ( attributes , [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "price" ] . ID , FloatValue : variant . Price . OriginalPrice . Value } ,
{ AttributeID : AttributesMap [ "special_price" ] . ID , FloatValue : variant . Price . DiscountedPrice . Value } ,
2022-08-27 06:43:56 +00:00
} ... )
flat . Price = variant . Price . OriginalPrice . Value
flat . SpecialPrice = variant . Price . DiscountedPrice . Value
flat . MinPrice = variant . Price . DiscountedPrice . Value
flat . MaxPrice = variant . Price . OriginalPrice . Value
} else {
2022-09-20 10:55:04 +00:00
attributes = append ( attributes , gm . ProductAttributeValue { AttributeID : AttributesMap [ "price" ] . ID , FloatValue : variant . Price . DiscountedPrice . Value } )
flat . Price = variant . Price . DiscountedPrice . Value
2022-08-27 06:43:56 +00:00
flat . MinPrice = variant . Price . DiscountedPrice . Value
2022-09-20 10:55:04 +00:00
flat . MaxPrice = variant . Price . DiscountedPrice . Value
2022-08-27 06:43:56 +00:00
}
return attributes , flat
}
func prepearAttributesWithFlat ( data * models . Product ) ( [ ] gm . ProductAttributeValue , gm . ProductFlat ) {
2022-09-26 08:46:10 +00:00
weight , wError := strconv . ParseFloat ( data . Weight , 64 )
2022-09-10 09:32:19 +00:00
2022-09-26 08:46:10 +00:00
if wError != nil || weight == 0 {
2022-09-10 09:32:19 +00:00
weight = 0.5
}
2022-08-27 06:43:56 +00:00
var description string
for _ , desc := range data . Descriptions {
description += "<p>" + desc . Description + "</p>"
}
//$desc = implode(array_map(fn($value): string => '<p>' . $value['description'] . '</p>', $data['descriptions']));
var productAttributeValues = [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "source" ] . ID , TextValue : data . URLKey } ,
{ AttributeID : AttributesMap [ "favoritesCount" ] . ID , IntegerValue : data . FavoriteCount } ,
{ AttributeID : AttributesMap [ "sku" ] . ID , TextValue : data . ProductGroupID } ,
{ 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 [ "url_key" ] . ID , TextValue : data . ProductGroupID } ,
{ AttributeID : AttributesMap [ "description" ] . ID , TextValue : description , Channel : "default" , Locale : "tm" } ,
2022-08-27 06:43:56 +00:00
}
flat := gm . ProductFlat {
Status : true ,
VisibleIndividually : true ,
Name : data . Name ,
Sku : data . ProductGroupID ,
ProductNumber : data . ProductNumber ,
Description : description ,
UrlKey : data . ProductGroupID ,
Weight : weight ,
2022-09-01 07:49:31 +00:00
//Source: data.URLKey,
2022-08-27 06:43:56 +00:00
FavoritesCount : uint ( data . FavoriteCount ) ,
2022-09-20 10:55:04 +00:00
MaxPrice : 0 ,
MinPrice : 0 ,
Price : 0 ,
2022-08-27 06:43:56 +00:00
}
2022-09-21 06:22:50 +00:00
if len ( data . ColorVariants ) == 0 && len ( data . SizeVariants ) == 0 {
2022-09-20 10:55:04 +00:00
2022-09-06 10:19:37 +00:00
if data . Price . OriginalPrice . Value > data . Price . DiscountedPrice . Value {
productAttributeValues = append ( productAttributeValues , [ ] gm . ProductAttributeValue {
2022-09-15 12:24:06 +00:00
{ AttributeID : AttributesMap [ "price" ] . ID , FloatValue : data . Price . OriginalPrice . Value } ,
{ AttributeID : AttributesMap [ "special_price" ] . ID , FloatValue : data . Price . DiscountedPrice . Value } ,
2022-09-06 10:19:37 +00:00
} ... )
flat . Price = data . Price . OriginalPrice . Value
flat . SpecialPrice = data . Price . DiscountedPrice . Value
flat . MinPrice = data . Price . DiscountedPrice . Value
flat . MaxPrice = data . Price . OriginalPrice . Value
} else {
2022-09-20 10:55:04 +00:00
productAttributeValues = append ( productAttributeValues , gm . ProductAttributeValue { AttributeID : AttributesMap [ "price" ] . ID , FloatValue : data . Price . DiscountedPrice . Value } )
flat . Price = data . Price . DiscountedPrice . Value
flat . MinPrice = data . Price . DiscountedPrice . Value
flat . MaxPrice = data . Price . DiscountedPrice . Value
2022-09-06 10:19:37 +00:00
}
2022-08-27 06:43:56 +00:00
}
return productAttributeValues , flat
}
2022-09-07 12:55:55 +00:00
2022-09-09 09:43:25 +00:00
func UpdateProduct ( product models . Product , db * gorm . DB , productFlat gm . ProductFlat ) error {
productFlat . Status = true
2022-09-20 13:41:12 +00:00
minPrice := 0.0
maxPrice := 0.0
2022-09-09 09:43:25 +00:00
if len ( product . ColorVariants ) == 0 && len ( product . SizeVariants ) == 0 {
if product . Price . OriginalPrice . Value > product . Price . DiscountedPrice . Value {
//productAttributeValues = append(productAttributeValues, []gm.ProductAttributeValue{
2022-09-15 12:24:06 +00:00
// {AttributeID: AttributesMap["price"].ID, FloatValue: data.Price.OriginalPrice.Value},
// {AttributeID: AttributesMap["special_price"].ID, FloatValue: data.Price.DiscountedPrice.Value},
2022-09-09 09:43:25 +00:00
//}...)
if productFlat . Price != product . Price . OriginalPrice . Value {
productFlat . Price = product . Price . OriginalPrice . Value
productFlat . MaxPrice = product . Price . OriginalPrice . Value
//db.Model(&User{}).Where("active = ?", true).Update("name", "hello")
err := db . Model ( gm . ProductAttributeValue { } ) .
Where ( "attribute_id = ? AND product_id = ?" , 11 , productFlat . ProductID ) .
Update ( "float_value" , product . Price . OriginalPrice . Value ) . Error
if err != nil {
return err
}
}
if productFlat . SpecialPrice != product . Price . DiscountedPrice . Value {
productFlat . SpecialPrice = product . Price . DiscountedPrice . Value
productFlat . MinPrice = product . Price . DiscountedPrice . Value
err := db . Model ( gm . ProductAttributeValue { } ) .
Where ( "attribute_id = ? AND product_id = ?" , 13 , productFlat . ProductID ) .
Update ( "float_value" , product . Price . OriginalPrice . Value ) . Error
if err != nil {
return err
}
}
} else {
if productFlat . Price != product . Price . OriginalPrice . Value {
productFlat . Price = product . Price . OriginalPrice . Value
productFlat . MaxPrice = product . Price . OriginalPrice . Value
productFlat . MinPrice = product . Price . OriginalPrice . Value
err := db . Model ( gm . ProductAttributeValue { } ) .
Where ( "attribute_id = ? AND product_id = ?" , 11 , productFlat . ProductID ) .
Update ( "float_value" , product . Price . OriginalPrice . Value ) . Error
if err != nil {
return err
}
}
}
2022-09-20 13:41:12 +00:00
} else if len ( product . ColorVariants ) > 0 {
2022-09-20 10:55:04 +00:00
//todo update configurable product
2022-09-20 13:41:12 +00:00
product . ColorVariants = append ( product . ColorVariants , product )
2022-09-22 11:44:01 +00:00
//tx := db.Begin()
2022-09-20 13:41:12 +00:00
for _ , colorVariant := range product . ColorVariants {
if len ( colorVariant . SizeVariants ) > 0 {
for _ , sizeVariant := range colorVariant . SizeVariants {
sku := fmt . Sprintf ( "%s-%s-%d-col-size" , colorVariant . ProductGroupID , colorVariant . ProductNumber , sizeVariant . ItemNumber )
var variantFlat gm . ProductFlat
err := db . Where ( "sku=?" , sku ) . First ( & variantFlat ) . Error
if err != nil && errors . Is ( err , gorm . ErrRecordNotFound ) {
//todo insert variant
continue
}
if ! sizeVariant . Sellable {
variantFlat . Status = false
errVar := db . Save ( & variantFlat ) . Error
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
continue
}
prices := sizeVariant . Price
if prices . OriginalPrice . Value > prices . DiscountedPrice . Value {
variantFlat . Price = prices . OriginalPrice . Value
variantFlat . SpecialPrice = prices . DiscountedPrice . Value
variantFlat . MinPrice = prices . DiscountedPrice . Value
variantFlat . MaxPrice = prices . OriginalPrice . Value
2022-09-21 13:25:28 +00:00
errVar := db . Omit ( "CreatedAt" ) . Save ( & variantFlat ) . Error
2022-09-20 13:41:12 +00:00
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
} else {
variantFlat . MinPrice = prices . DiscountedPrice . Value
variantFlat . MaxPrice = prices . DiscountedPrice . Value
2022-09-21 13:29:09 +00:00
errVar := db . Exec ( "Update product_flat set price = ?,min_price=?, max_price=?, special_price = NULL where id = ?" , prices . DiscountedPrice . Value , variantFlat . MinPrice , variantFlat . MaxPrice , variantFlat . ID ) . Error
2022-09-20 13:41:12 +00:00
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
}
if minPrice == 0 || minPrice > variantFlat . MinPrice {
minPrice = variantFlat . MinPrice
}
if maxPrice == 0 || maxPrice < variantFlat . MaxPrice {
maxPrice = variantFlat . MaxPrice
}
}
} else {
sku := fmt . Sprintf ( "%s-%s-%s-%s" , product . ProductGroupID , colorVariant . ProductNumber , colorVariant . ProductCode , colorVariant . Color )
var variantFlat gm . ProductFlat
err := db . Where ( "sku=?" , sku ) . First ( & variantFlat ) . Error
if err != nil && errors . Is ( err , gorm . ErrRecordNotFound ) {
//todo insert variant
continue
}
if ! colorVariant . IsSellable {
variantFlat . Status = false
errVar := db . Save ( & variantFlat ) . Error
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
continue
}
prices := colorVariant . Price
if prices . OriginalPrice . Value > prices . DiscountedPrice . Value {
variantFlat . Price = prices . OriginalPrice . Value
variantFlat . SpecialPrice = prices . DiscountedPrice . Value
variantFlat . MinPrice = prices . DiscountedPrice . Value
variantFlat . MaxPrice = prices . OriginalPrice . Value
2022-09-09 09:43:25 +00:00
2022-09-21 13:25:28 +00:00
errVar := db . Omit ( "CreatedAt" ) . Save ( & variantFlat ) . Error
2022-09-20 13:41:12 +00:00
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
} else {
2022-09-21 13:25:28 +00:00
variantFlat . MinPrice = prices . DiscountedPrice . Value
variantFlat . MaxPrice = prices . DiscountedPrice . Value
2022-09-21 13:29:09 +00:00
errVar := db . Exec ( "Update product_flat set price = ?,min_price=?, max_price=?, special_price = NULL where id = ?" , prices . DiscountedPrice . Value , variantFlat . MinPrice , variantFlat . MaxPrice , variantFlat . ID ) . Error
2022-09-20 13:41:12 +00:00
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
}
if minPrice == 0 || minPrice > variantFlat . MinPrice {
minPrice = variantFlat . MinPrice
}
if maxPrice == 0 || maxPrice < variantFlat . MaxPrice {
maxPrice = variantFlat . MaxPrice
}
}
}
productFlat . MinPrice = minPrice
productFlat . MaxPrice = maxPrice
} else if len ( product . SizeVariants ) > 0 {
for _ , sizeVariant := range product . SizeVariants {
sku := fmt . Sprintf ( "%s-%s-%d-size" , product . ProductGroupID , product . ProductNumber , sizeVariant . ItemNumber )
var variantFlat gm . ProductFlat
err := db . Where ( "sku=?" , sku ) . First ( & variantFlat ) . Error
if err != nil && errors . Is ( err , gorm . ErrRecordNotFound ) {
//todo insert variant
continue
}
if ! sizeVariant . Sellable {
variantFlat . Status = false
errVar := db . Save ( & variantFlat ) . Error
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
continue
}
prices := sizeVariant . Price
if prices . OriginalPrice . Value > prices . DiscountedPrice . Value {
variantFlat . Price = prices . OriginalPrice . Value
variantFlat . SpecialPrice = prices . DiscountedPrice . Value
variantFlat . MinPrice = prices . DiscountedPrice . Value
variantFlat . MaxPrice = prices . OriginalPrice . Value
2022-09-21 13:25:28 +00:00
errVar := db . Omit ( "CreatedAt" ) . Save ( & variantFlat ) . Error
2022-09-20 13:41:12 +00:00
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
} else {
2022-09-21 13:25:28 +00:00
variantFlat . MinPrice = prices . DiscountedPrice . Value
variantFlat . MaxPrice = prices . DiscountedPrice . Value
2022-09-20 13:41:12 +00:00
2022-09-21 13:29:09 +00:00
errVar := db . Exec ( "Update product_flat set price = ?,min_price=?, max_price=?, special_price = NULL where id = ?" , prices . DiscountedPrice . Value , variantFlat . MinPrice , variantFlat . MaxPrice , variantFlat . ID ) . Error
2022-09-20 13:41:12 +00:00
if errVar != nil {
log . Println ( errVar . Error ( ) )
}
}
if minPrice == 0 || minPrice > variantFlat . MinPrice {
minPrice = variantFlat . MinPrice
}
if maxPrice == 0 || maxPrice < variantFlat . MaxPrice {
maxPrice = variantFlat . MaxPrice
}
}
productFlat . MinPrice = minPrice
productFlat . MaxPrice = maxPrice
2022-09-09 09:43:25 +00:00
}
2022-09-21 13:03:09 +00:00
var errFlat error
if productFlat . SpecialPrice != 0 {
errFlat = db . Omit ( "Product" , "ParentID" , "CreatedAt" ) . Save ( & productFlat ) . Error
} else {
errFlat = db . Omit ( "Product" , "ParentID" , "CreatedAt" , "SpecialPrice" ) . Save ( & productFlat ) . Error
}
2022-09-09 09:43:25 +00:00
return errFlat
}
2022-09-07 12:55:55 +00:00
//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{
2022-09-15 12:24:06 +00:00
// {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"},
2022-09-07 12:55:55 +00:00
// },flat
//}