96 lines
2.4 KiB
Go
96 lines
2.4 KiB
Go
package productService
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"git.ma-al.com/goc_daniel/b2b/app/db"
|
|
"git.ma-al.com/goc_daniel/b2b/app/model"
|
|
"git.ma-al.com/goc_daniel/b2b/app/repos/productsRepo"
|
|
constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
|
|
"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"
|
|
)
|
|
|
|
type ProductService struct {
|
|
productsRepo productsRepo.UIProductsRepo
|
|
}
|
|
|
|
func New() *ProductService {
|
|
return &ProductService{
|
|
productsRepo: productsRepo.New(),
|
|
}
|
|
}
|
|
|
|
func (s *ProductService) GetJSON(p_id_product, p_id_lang, p_id_customer, b2b_id_country, p_quantity int) (*json.RawMessage, error) {
|
|
products, err := s.productsRepo.GetJSON(p_id_product, constdata.SHOP_ID, p_id_lang, p_id_customer, b2b_id_country, p_quantity)
|
|
if err != nil {
|
|
return products, err
|
|
}
|
|
|
|
return products, nil
|
|
}
|
|
|
|
func (s *ProductService) Find(id_lang uint, p find.Paging, filters *filters.FiltersList, customer *model.Customer, quantity int, shopID uint) (*find.Found[model.ProductInList], error) {
|
|
if customer == nil || customer.Country == nil {
|
|
return nil, errors.New("customer is nil or is missing fields")
|
|
}
|
|
// customer.ID, customer.CountryID, uint(customer.Country.CurrencyID), quantity, shopID
|
|
|
|
found, err := s.productsRepo.Find(id_lang, p, filters)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range found.Items {
|
|
if found.Items[i].VariantsNumber <= 0 {
|
|
err := s.PopulateProductPrice(&found.Items[i], customer, quantity, shopID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
|
|
return found, err
|
|
}
|
|
|
|
func (s *ProductService) 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 (s *ProductService) GetProductAttributes(
|
|
langID uint,
|
|
productID uint,
|
|
shopID uint,
|
|
customerID uint,
|
|
countryID uint,
|
|
quantity uint,
|
|
) ([]view.ProductAttribute, error) {
|
|
return s.productsRepo.GetProductVariants(langID, productID, shopID, customerID, countryID, quantity)
|
|
}
|