2022-08-12 08:59:03 +00:00
package gorm_models
2022-09-01 11:53:11 +00:00
import (
2022-09-06 11:39:29 +00:00
"context"
2022-09-05 12:35:26 +00:00
helper "db_service/pkg"
2022-09-01 11:53:11 +00:00
"gorm.io/gorm"
2022-09-05 12:35:26 +00:00
"os"
2022-09-01 11:53:11 +00:00
"time"
)
2022-08-12 08:59:03 +00:00
type Product struct {
ID uint ` gorm:"primary_key" `
CreatedAt time . Time
UpdatedAt time . Time
Sku string
Type string
ParentID uint ` sql:"DEFAULT:NULL" `
AttributeFamilyID uint
AttributeFamily AttributeFamily
BrandID uint ` sql:"DEFAULT:NULL" `
Brand Brand
Images [ ] ProductImage
2022-09-25 17:24:33 +00:00
Categories [ ] Category ` gorm:"many2many:product_categories;" `
2022-08-12 08:59:03 +00:00
AttributeValues [ ] ProductAttributeValue
2022-08-15 05:15:49 +00:00
SuperAttributes [ ] Attribute ` gorm:"many2many:product_super_attributes;" `
2022-10-08 10:57:03 +00:00
//Variants []*Product `gorm:"many2many:product_relations;foreignKey:child_id;primaryKey:parent_id;"`
}
type ProductRelation struct {
ParentID uint
ChildID uint
2022-08-12 08:59:03 +00:00
}
type ProductFlat struct {
2022-11-01 06:35:43 +00:00
ID uint ` gorm:"primary_key" `
Sku string
ProductNumber string ` sql:"DEFAULT:NULL" gorm:"default:null" `
Name string ` sql:"DEFAULT:''" gorm:"default:''" `
Weight float64 ` sql:"DEFAULT:NULL" gorm:"type:decimal(12,4);default:null" `
Status bool ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-25 05:39:59 +00:00
VisibleIndividually bool ` sql:"DEFAULT:NULL" gorm:"default:null" `
Price float64 ` gorm:"type:decimal(12,4)" `
MinPrice float64 ` gorm:"type:decimal(12,4)" `
MaxPrice float64 ` gorm:"type:decimal(12,4)" `
2022-09-21 08:04:46 +00:00
SpecialPrice float64 ` sql:"DEFAULT:NULL" gorm:"type:decimal(12,4);default:null" `
2022-08-25 05:39:59 +00:00
UrlKey string ` sql:"DEFAULT:NULL" gorm:"default:null" `
ShortDescription string ` sql:"DEFAULT:NULL" gorm:"default:null" `
Description string ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-25 06:33:20 +00:00
FavoritesCount uint ` sql:"DEFAULT:NULL" gorm:"default:null;column:favoritesCount" `
2022-08-25 05:39:59 +00:00
CreatedAt time . Time ` sql:"DEFAULT:NULL" gorm:"default:null" `
UpdatedAt time . Time ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-15 05:15:49 +00:00
ProductID uint
2022-08-12 08:59:03 +00:00
Product Product
2022-08-25 05:39:59 +00:00
Channel string ` sql:"DEFAULT:default" gorm:"default:default" `
Locale string ` sql:"DEFAULT:tm" gorm:"default:tm" `
2022-09-01 07:49:31 +00:00
ParentID uint ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-25 05:39:59 +00:00
Variants [ ] ProductFlat ` gorm:"foreignKey:ParentID" `
Color int ` sql:"DEFAULT:NULL" gorm:"default:null" `
ColorLabel string ` sql:"DEFAULT:NULL" gorm:"default:null" `
Size int ` sql:"DEFAULT:NULL" gorm:"default:null" `
SizeLabel string ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-11-01 06:35:43 +00:00
Boyut int ` sql:"DEFAULT:NULL" gorm:"default:null" `
BoyutLabel string ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-25 05:39:59 +00:00
MetaTitle string ` sql:"DEFAULT:NULL" gorm:"default:null" `
MetaKeywords string ` sql:"DEFAULT:NULL" gorm:"default:null" `
BrandID uint ` sql:"DEFAULT:NULL" gorm:"default:null" `
Brand Brand
2022-12-17 07:07:49 +00:00
Cinsiyet int ` sql:"DEFAULT:NULL" gorm:"default:null" `
CinsiyetLabel string ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-12 08:59:03 +00:00
}
type ProductImage struct {
ID uint ` gorm:"primary_key" `
Type string
Path string
ProductID uint
}
type ProductAttributeValue struct {
2022-08-25 05:39:59 +00:00
ID uint ` gorm:"primary_key" `
Locale string ` sql:"DEFAULT:NULL" gorm:"default:null" `
Channel string ` sql:"DEFAULT:NULL" gorm:"default:null" `
2022-08-12 08:59:03 +00:00
ProductID uint
AttributeID uint
2022-08-25 05:39:59 +00:00
TextValue string ` sql:"DEFAULT:NULL" gorm:"default:null" `
BooleanValue bool ` sql:"DEFAULT:NULL" gorm:"default:null" `
IntegerValue int ` sql:"DEFAULT:NULL" gorm:"default:null" `
FloatValue float64 ` sql:"DEFAULT:NULL" gorm:"type:decimal(12,4);default:null" `
2022-08-12 08:59:03 +00:00
}
type ProductSuperAttribute struct {
ProductID uint
AttributeID uint
}
2022-08-25 05:39:59 +00:00
type Tabler interface {
TableName ( ) string
}
// TableName overrides the table name used by User to `profiles`
func ( ProductFlat ) TableName ( ) string {
return "product_flat"
}
2022-09-01 11:53:11 +00:00
func DeleteProducts ( db * gorm . DB ) error {
//todo delete from elastico
2022-09-20 13:41:12 +00:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 10 * time . Minute )
2022-09-06 11:39:29 +00:00
defer cancel ( )
2022-09-16 15:22:57 +00:00
//qb := "DELETE FROM products WHERE id NOT IN (select product_id as id from wishlist) AND id NOT IN (select product_id as id from order_items) AND id NOT IN (select parent_id as idfrom order_items);"
2022-09-21 09:47:58 +00:00
qb := "DELETE p FROM products p LEFT JOIN order_items oi ON p.id = oi.product_id LEFT JOIN order_items op ON p.id = op.parent_id WHERE oi.id IS NULL AND op.id IS NULL;"
2022-11-02 07:45:32 +00:00
db . WithContext ( ctx ) . Exec ( qb )
2022-11-02 07:47:46 +00:00
db . WithContext ( ctx ) . Exec ( "UPDATE product_flat set sku=concat(id,\"-ordered\"), status=0 where status=1" )
db . WithContext ( ctx ) . Exec ( "UPDATE products set sku=concat(id,\"-ordered\")" )
2022-11-02 07:45:32 +00:00
return db . Error
2022-09-01 11:53:11 +00:00
}
2022-09-05 12:35:26 +00:00
2022-11-02 07:45:32 +00:00
//func DisableProducts (db *gorm.DB) error {
// ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
// defer cancel()
// db.WithContext(ctx).Exec("UPDATE product_flat set sku=concat(sku,\"-ordered\"), status=0")
// db.WithContext(ctx).Exec("UPDATE products set sku=concat(sku,\"-ordered\")")
// return db.Error
//}
2022-09-05 12:35:26 +00:00
func Flush ( ) error {
2022-09-07 13:03:50 +00:00
_ , err := helper . SendRequest ( "GET" , os . Getenv ( "scout_flash" ) , nil , "" )
2022-09-05 12:35:26 +00:00
return err
}