Merge branch 'product-procedures' of ssh://git.ma-al.com:8822/goc_daniel/b2b into update_categories
This commit is contained in:
@@ -2,7 +2,6 @@ package productsRepo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/config"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/db"
|
||||
@@ -10,13 +9,18 @@ import (
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model/dbmodel"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/filters"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/utils/query/find"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/view"
|
||||
"git.ma-al.com/goc_marek/gormcol"
|
||||
"github.com/WinterYukky/gorm-extra-clause-plugin/exclause"
|
||||
)
|
||||
|
||||
type UIProductsRepo interface {
|
||||
GetJSON(p_id_product, p_id_shop, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error)
|
||||
Find(id_lang, userID uint, p find.Paging, filt *filters.FiltersList) (find.Found[model.ProductInList], error)
|
||||
// GetJSON(p_id_product, p_id_shop, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error)
|
||||
Find(id_lang uint, userID uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.ProductInList], error)
|
||||
GetProductVariants(langID uint, productID uint, shopID uint, customerID uint, countryID uint, quantity uint) ([]view.ProductAttribute, error)
|
||||
GetBase(p_id_product, p_id_shop, p_id_lang uint) (view.Product, error)
|
||||
GetPrice(p_id_product uint, productAttributeID *uint, p_id_shop uint, p_id_customer uint, p_id_country uint, p_quantity uint) (view.Price, error)
|
||||
GetVariants(p_id_product, p_id_shop, p_id_lang, p_id_customer, p_id_country, p_quantity uint) ([]view.ProductAttribute, error)
|
||||
AddToFavorites(userID uint, productID uint) error
|
||||
RemoveFromFavorites(userID uint, productID uint) error
|
||||
ExistsInFavorites(userID uint, productID uint) (bool, error)
|
||||
@@ -29,30 +33,78 @@ func New() UIProductsRepo {
|
||||
return &ProductsRepo{}
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) GetJSON(p_id_product, p_id_shop, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error) {
|
||||
var productStr string // ← Scan as string first
|
||||
func (repo *ProductsRepo) GetBase(p_id_product, p_id_shop, p_id_lang uint) (view.Product, error) {
|
||||
var result view.Product
|
||||
|
||||
err := db.DB.Raw(`CALL get_full_product(?,?,?,?,?,?)`,
|
||||
p_id_product, p_id_shop, p_id_lang, p_id_customer, b2b_id_country, p_quantity).
|
||||
Scan(&productStr).
|
||||
Error
|
||||
err := db.DB.Raw(`CALL get_product_base(?,?,?)`,
|
||||
p_id_product, p_id_shop, p_id_lang).
|
||||
Scan(&result).Error
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !json.Valid([]byte(productStr)) {
|
||||
return nil, fmt.Errorf("invalid json returned from stored procedure")
|
||||
}
|
||||
|
||||
raw := json.RawMessage(productStr)
|
||||
return &raw, nil
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) Find(id_lang, userID uint, p find.Paging, filt *filters.FiltersList) (find.Found[model.ProductInList], error) {
|
||||
var list []model.ProductInList
|
||||
var total int64
|
||||
func (repo *ProductsRepo) GetPrice(
|
||||
p_id_product uint, productAttributeID *uint, p_id_shop uint, p_id_customer uint, p_id_country uint, p_quantity uint,
|
||||
) (view.Price, error) {
|
||||
|
||||
type row struct {
|
||||
Price json.RawMessage `gorm:"column:price"`
|
||||
}
|
||||
|
||||
var r row
|
||||
err := db.DB.Raw(`
|
||||
SELECT fn_product_price(?,?,?,?,?,?) AS price`,
|
||||
p_id_product, p_id_shop, p_id_customer, p_id_country, p_quantity, productAttributeID).
|
||||
Scan(&r).Error
|
||||
|
||||
if err != nil {
|
||||
return view.Price{}, err
|
||||
}
|
||||
|
||||
var temp struct {
|
||||
Base json.Number `json:"base"`
|
||||
FinalTaxExcl json.Number `json:"final_tax_excl"`
|
||||
FinalTaxIncl json.Number `json:"final_tax_incl"`
|
||||
TaxRate json.Number `json:"tax_rate"`
|
||||
Priority json.Number `json:"priority"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(r.Price, &temp); err != nil {
|
||||
return view.Price{}, err
|
||||
}
|
||||
|
||||
price := view.Price{
|
||||
Base: mustParseFloat(temp.Base),
|
||||
FinalTaxExcl: mustParseFloat(temp.FinalTaxExcl),
|
||||
FinalTaxIncl: mustParseFloat(temp.FinalTaxIncl),
|
||||
TaxRate: mustParseFloat(temp.TaxRate),
|
||||
Priority: mustParseInt(temp.Priority),
|
||||
}
|
||||
|
||||
return price, nil
|
||||
}
|
||||
func mustParseFloat(n json.Number) float64 {
|
||||
f, _ := n.Float64()
|
||||
return f
|
||||
}
|
||||
|
||||
func mustParseInt(n json.Number) int {
|
||||
i, _ := n.Int64()
|
||||
return int(i)
|
||||
}
|
||||
func (repo *ProductsRepo) GetVariants(p_id_product, p_id_shop, p_id_lang, p_id_customer, p_id_country, p_quantity uint) ([]view.ProductAttribute, error) {
|
||||
|
||||
var results []view.ProductAttribute
|
||||
|
||||
err := db.DB.Raw(`
|
||||
CALL get_product_variants(?,?,?,?,?,?)`,
|
||||
p_id_product, p_id_shop, p_id_lang, p_id_customer, p_id_country, p_quantity).
|
||||
Scan(&results).Error
|
||||
|
||||
return results, err
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) Find(langID uint, userID uint, p find.Paging, filt *filters.FiltersList) (*find.Found[model.ProductInList], error) {
|
||||
query := db.Get().
|
||||
Table(gormcol.Field.Tab(dbmodel.PsProductShopCols.Active)+" AS ps").
|
||||
Select(`
|
||||
@@ -67,9 +119,9 @@ func (repo *ProductsRepo) Find(id_lang, userID uint, p find.Paging, filt *filter
|
||||
COALESCE(f.is_favorite, 0) AS is_favorite
|
||||
`, config.Get().Image.ImagePrefix).
|
||||
Joins("JOIN "+dbmodel.PsProductCols.IDProduct.Tab()+" p ON p.id_product = ps.id_product").
|
||||
Joins("JOIN ps_product_lang pl ON pl.id_product = ps.id_product AND pl.id_lang = ?", id_lang).
|
||||
Joins("JOIN ps_product_lang pl ON pl.id_product = ps.id_product AND pl.id_lang = ?", langID).
|
||||
Joins("JOIN ps_image_shop ims ON ims.id_product = ps.id_product AND ims.cover = 1").
|
||||
Joins("JOIN ps_category_lang cl ON cl.id_category = ps.id_category_default AND cl.id_lang = ?", id_lang).
|
||||
Joins("JOIN ps_category_lang cl ON cl.id_category = ps.id_category_default AND cl.id_lang = ?", langID).
|
||||
Joins("JOIN ps_category_product cp ON cp.id_product = ps.id_product").
|
||||
Joins("LEFT JOIN variants v ON v.id_product = ps.id_product").
|
||||
Joins("LEFT JOIN favorites f ON f.id_product = ps.id_product").
|
||||
@@ -105,29 +157,64 @@ func (repo *ProductsRepo) Find(id_lang, userID uint, p find.Paging, filt *filter
|
||||
}).
|
||||
Order("ps.id_product DESC")
|
||||
|
||||
// Apply all filters
|
||||
if filt != nil {
|
||||
filt.ApplyAll(query)
|
||||
}
|
||||
query = query.Scopes(filt.All()...)
|
||||
|
||||
// run counter first as query is without limit and offset
|
||||
err := query.Count(&total).Error
|
||||
list, err := find.Paginate[model.ProductInList](langID, p, query)
|
||||
if err != nil {
|
||||
return find.Found[model.ProductInList]{}, err
|
||||
return nil, err
|
||||
}
|
||||
return &list, nil
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) GetProductVariants(langID uint, productID uint, shopID uint, customerID uint, countryID uint, quantity uint) ([]view.ProductAttribute, error) {
|
||||
var result []view.ProductAttribute
|
||||
err := db.DB.
|
||||
Raw(`
|
||||
CALL get_product_attributes_with_price(?, ?, ?, ?, ?, ?)
|
||||
`,
|
||||
langID,
|
||||
productID,
|
||||
shopID,
|
||||
customerID,
|
||||
countryID,
|
||||
quantity,
|
||||
).
|
||||
Scan(&result).Error
|
||||
|
||||
err = query.
|
||||
Limit(p.Limit()).
|
||||
Offset(p.Offset()).
|
||||
Find(&list).Error
|
||||
if err != nil {
|
||||
return find.Found[model.ProductInList]{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return find.Found[model.ProductInList]{
|
||||
Items: list,
|
||||
Count: uint(total),
|
||||
}, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) PopulateProductPrice(product *model.ProductInList, targetCustomer *model.Customer, quantity int, shopID uint) error {
|
||||
row := db.Get().Raw(
|
||||
"CALL get_product_price(?, ?, ?, ?, ?)",
|
||||
product.ProductID,
|
||||
shopID,
|
||||
targetCustomer.ID,
|
||||
targetCustomer.CountryID,
|
||||
quantity,
|
||||
).Row()
|
||||
|
||||
var (
|
||||
id uint
|
||||
base float64
|
||||
excl float64
|
||||
incl float64
|
||||
tax float64
|
||||
)
|
||||
|
||||
err := row.Scan(&id, &base, &excl, &incl, &tax)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
product.PriceTaxExcl = excl
|
||||
product.PriceTaxIncl = incl
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (repo *ProductsRepo) AddToFavorites(userID uint, productID uint) error {
|
||||
|
||||
247
app/repos/specificPriceRepo/specificPriceRepo.go
Normal file
247
app/repos/specificPriceRepo/specificPriceRepo.go
Normal file
@@ -0,0 +1,247 @@
|
||||
package specificPriceRepo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.ma-al.com/goc_daniel/b2b/app/db"
|
||||
"git.ma-al.com/goc_daniel/b2b/app/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UISpecificPriceRepo interface {
|
||||
Create(ctx context.Context, pr *model.SpecificPrice) error
|
||||
Update(ctx context.Context, pr *model.SpecificPrice) error
|
||||
Delete(ctx context.Context, id uint64) error
|
||||
GetByID(ctx context.Context, id uint64) (*model.SpecificPrice, error)
|
||||
List(ctx context.Context) ([]*model.SpecificPrice, error)
|
||||
SetActive(ctx context.Context, id uint64, active bool) error
|
||||
}
|
||||
|
||||
type SpecificPriceRepo struct{}
|
||||
|
||||
func New() UISpecificPriceRepo {
|
||||
return &SpecificPriceRepo{}
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) Create(ctx context.Context, pr *model.SpecificPrice) error {
|
||||
return db.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
now := time.Now()
|
||||
pr.CreatedAt = &now
|
||||
|
||||
if err := tx.Create(pr).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return repo.insertRelations(tx, pr)
|
||||
})
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) Update(ctx context.Context, pr *model.SpecificPrice) error {
|
||||
return db.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
now := time.Now()
|
||||
pr.UpdatedAt = &now
|
||||
|
||||
if err := tx.Save(pr).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := repo.clearRelations(tx, pr.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return repo.insertRelations(tx, pr)
|
||||
})
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) GetByID(ctx context.Context, id uint64) (*model.SpecificPrice, error) {
|
||||
var pr model.SpecificPrice
|
||||
err := db.DB.WithContext(ctx).Where("id = ?", id).First(&pr).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := repo.loadRelations(ctx, &pr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pr, nil
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) List(ctx context.Context) ([]*model.SpecificPrice, error) {
|
||||
var specificPrices []*model.SpecificPrice
|
||||
err := db.DB.WithContext(ctx).Find(&specificPrices).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range specificPrices {
|
||||
if err := repo.loadRelations(ctx, specificPrices[i]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return specificPrices, nil
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) SetActive(ctx context.Context, id uint64, active bool) error {
|
||||
return db.DB.WithContext(ctx).Model(&model.SpecificPrice{}).Where("id = ?", id).Update("is_active", active).Error
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) Delete(ctx context.Context, id uint64) error {
|
||||
return db.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_product WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_category WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_product_attribute WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_country WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_customer WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Delete(&model.SpecificPrice{}, "id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) insertRelations(tx *gorm.DB, pr *model.SpecificPrice) error {
|
||||
if len(pr.ProductIDs) > 0 {
|
||||
for _, productID := range pr.ProductIDs {
|
||||
if err := tx.Exec(`
|
||||
INSERT INTO b2b_specific_price_product (b2b_specific_price_id, id_product) VALUES (?, ?)
|
||||
`, pr.ID, productID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(pr.CategoryIDs) > 0 {
|
||||
for _, categoryID := range pr.CategoryIDs {
|
||||
if err := tx.Exec(`
|
||||
INSERT INTO b2b_specific_price_category (b2b_specific_price_id, id_category) VALUES (?, ?)
|
||||
`, pr.ID, categoryID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(pr.ProductAttributeIDs) > 0 {
|
||||
for _, attrID := range pr.ProductAttributeIDs {
|
||||
if err := tx.Exec(`
|
||||
INSERT INTO b2b_specific_price_product_attribute (b2b_specific_price_id, id_product_attribute) VALUES (?, ?)
|
||||
`, pr.ID, attrID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(pr.CountryIDs) > 0 {
|
||||
for _, countryID := range pr.CountryIDs {
|
||||
if err := tx.Exec(`
|
||||
INSERT INTO b2b_specific_price_country (b2b_specific_price_id, b2b_id_country) VALUES (?, ?)
|
||||
`, pr.ID, countryID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(pr.CustomerIDs) > 0 {
|
||||
for _, customerID := range pr.CustomerIDs {
|
||||
if err := tx.Exec(`
|
||||
INSERT INTO b2b_specific_price_customer (b2b_specific_price_id, b2b_id_customer) VALUES (?, ?)
|
||||
`, pr.ID, customerID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) clearRelations(tx *gorm.DB, id uint64) error {
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_product WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_category WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_product_attribute WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_country WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM b2b_specific_price_customer WHERE b2b_specific_price_id = ?", id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *SpecificPriceRepo) loadRelations(ctx context.Context, pr *model.SpecificPrice) error {
|
||||
var err error
|
||||
|
||||
var productIDs []struct {
|
||||
IDProduct uint64 `gorm:"column:id_product"`
|
||||
}
|
||||
if err = db.DB.WithContext(ctx).Table("b2b_specific_price_product").Where("b2b_specific_price_id = ?", pr.ID).Select("id_product").Scan(&productIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range productIDs {
|
||||
pr.ProductIDs = append(pr.ProductIDs, p.IDProduct)
|
||||
}
|
||||
|
||||
var categoryIDs []struct {
|
||||
IDCategory uint64 `gorm:"column:id_category"`
|
||||
}
|
||||
if err = db.DB.WithContext(ctx).Table("b2b_specific_price_category").Where("b2b_specific_price_id = ?", pr.ID).Select("id_category").Scan(&categoryIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range categoryIDs {
|
||||
pr.CategoryIDs = append(pr.CategoryIDs, c.IDCategory)
|
||||
}
|
||||
|
||||
var attrIDs []struct {
|
||||
IDAttr uint64 `gorm:"column:id_product_attribute"`
|
||||
}
|
||||
if err = db.DB.WithContext(ctx).Table("b2b_specific_price_product_attribute").Where("b2b_specific_price_id = ?", pr.ID).Select("id_product_attribute").Scan(&attrIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, a := range attrIDs {
|
||||
pr.ProductAttributeIDs = append(pr.ProductAttributeIDs, a.IDAttr)
|
||||
}
|
||||
|
||||
var countryIDs []struct {
|
||||
IDCountry uint64 `gorm:"column:b2b_id_country"`
|
||||
}
|
||||
if err = db.DB.WithContext(ctx).Table("b2b_specific_price_country").Where("b2b_specific_price_id = ?", pr.ID).Select("b2b_id_country").Scan(&countryIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range countryIDs {
|
||||
pr.CountryIDs = append(pr.CountryIDs, c.IDCountry)
|
||||
}
|
||||
|
||||
var customerIDs []struct {
|
||||
IDCustomer uint64 `gorm:"column:b2b_id_customer"`
|
||||
}
|
||||
if err = db.DB.WithContext(ctx).Table("b2b_specific_price_customer").Where("b2b_specific_price_id = ?", pr.ID).Select("b2b_id_customer").Scan(&customerIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, c := range customerIDs {
|
||||
pr.CustomerIDs = append(pr.CustomerIDs, c.IDCustomer)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user