151 lines
6.1 KiB
Go
151 lines
6.1 KiB
Go
package gorm_models
|
|
|
|
import (
|
|
"context"
|
|
helper "db_service/pkg"
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
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
|
|
Categories []Category `gorm:"many2many:product_categories;"`
|
|
AttributeValues []ProductAttributeValue
|
|
SuperAttributes []Attribute `gorm:"many2many:product_super_attributes;"`
|
|
//Variants []*Product `gorm:"many2many:product_relations;foreignKey:child_id;primaryKey:parent_id;"`
|
|
}
|
|
|
|
type ProductRelation struct {
|
|
ParentID uint
|
|
ChildID uint
|
|
}
|
|
|
|
type ProductFlat struct {
|
|
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"`
|
|
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)"`
|
|
SpecialPrice float64 `sql:"DEFAULT:NULL" gorm:"type:decimal(12,4);default:null"`
|
|
UrlKey string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
ShortDescription string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
Description string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
FavoritesCount uint `sql:"DEFAULT:NULL" gorm:"default:null;column:favoritesCount"`
|
|
CreatedAt time.Time `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
UpdatedAt time.Time `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
ProductID uint
|
|
Product Product
|
|
Channel string `sql:"DEFAULT:default" gorm:"default:default"`
|
|
Locale string `sql:"DEFAULT:tm" gorm:"default:tm"`
|
|
ParentID uint `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
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"`
|
|
Boyut int `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
BoyutLabel string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
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
|
|
Cinsiyet int `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
CinsiyetLabel string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
}
|
|
|
|
type ProductImage struct {
|
|
ID uint `gorm:"primary_key"`
|
|
Type string
|
|
Path string
|
|
ProductID uint
|
|
}
|
|
|
|
type ProductAttributeValue struct {
|
|
ID uint `gorm:"primary_key"`
|
|
Locale string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
Channel string `sql:"DEFAULT:NULL" gorm:"default:null"`
|
|
ProductID uint
|
|
AttributeID uint
|
|
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"`
|
|
}
|
|
|
|
type ProductSuperAttribute struct {
|
|
ProductID uint
|
|
AttributeID uint
|
|
}
|
|
|
|
type Tabler interface {
|
|
TableName() string
|
|
}
|
|
|
|
// TableName overrides the table name used by User to `profiles`
|
|
func (ProductFlat) TableName() string {
|
|
return "product_flat"
|
|
}
|
|
|
|
func DeleteProducts(db *gorm.DB) error {
|
|
//todo delete from elastico
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
|
defer cancel()
|
|
//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);"
|
|
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 " +
|
|
"LEFT JOIN wishlist wp ON p.id = wp.product_id " +
|
|
"JOIN marketplace_products mp ON p.id = mp.product_id "+
|
|
"WHERE oi.id IS NULL AND op.id IS NULL AND wp.id IS NULL AND mp.marketplace_seller_id=1;"
|
|
db.WithContext(ctx).Exec(qb)
|
|
db.WithContext(ctx).Exec("UPDATE product_flat set sku=concat(id,\"-ordered\"), status=0 where status=1 AND product_id IN (SELECT product_id from marketplace_products where marketplace_seller_id=1)" )
|
|
db.WithContext(ctx).Exec("UPDATE products set sku=concat(id,\"-ordered\") WHERE id IN (SELECT product_id from marketplace_products where marketplace_seller_id=1)")
|
|
return db.Error
|
|
}
|
|
|
|
func GetWishlistProducts(db *gorm.DB) ([]Product, error){
|
|
var products [] Product
|
|
err := db.Joins("JOIN wishlist wp ON products.id = wp.product_id").Joins("JOIN marketplace_products mp ON products.id = mp.product_id where marketplace_seller_id=1").Find(&products).Error
|
|
|
|
if err != nil {
|
|
log.Println(err.Error())
|
|
return nil, err
|
|
}
|
|
|
|
return products, nil
|
|
//qb := "SELECT p.id,p.sku FROM products p " +
|
|
// "JOIN wishlist wp ON p.id = wp.product_id " +
|
|
// "JOIN marketplace_products mp ON p.id = mp.product_id where marketplace_seller_id=1);"
|
|
}
|
|
|
|
//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
|
|
//}
|
|
|
|
func Flush() error {
|
|
_, err := helper.SendRequest("GET", os.Getenv("scout_flash"), nil, "")
|
|
|
|
return err
|
|
}
|