95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
package calculate
|
|
|
|
import (
|
|
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/internal/core"
|
|
"penahub.gitlab.yandexcloud.net/pena-services/accruals-service/internal/models"
|
|
)
|
|
|
|
func calculateProducts(products []core.Product) float64 {
|
|
productsPrice := float64(0)
|
|
|
|
for _, product := range products {
|
|
productsPrice = productsPrice + product.Price
|
|
}
|
|
|
|
return productsPrice
|
|
}
|
|
|
|
func calculateProductsMap(products map[string]core.Product) float64 {
|
|
productsPrice := float64(0)
|
|
|
|
for _, product := range products {
|
|
productsPrice = productsPrice + product.Price
|
|
}
|
|
|
|
return productsPrice
|
|
}
|
|
|
|
func calculatePricesGroup(pricesGroup map[string]float64) float64 {
|
|
finalPrice := float64(0)
|
|
|
|
for _, price := range pricesGroup {
|
|
finalPrice = finalPrice + price
|
|
}
|
|
|
|
return finalPrice
|
|
}
|
|
|
|
func calculateEachProduct(products map[string]core.Product, target models.DiscountCalculationTarget) map[string]core.Product {
|
|
if target.Products == nil && target.Factor == 0 {
|
|
return products
|
|
}
|
|
|
|
if target.Products == nil {
|
|
for key, product := range products {
|
|
products[key] = core.Product{
|
|
ID: key,
|
|
Price: product.Price * target.Factor,
|
|
}
|
|
}
|
|
|
|
return products
|
|
}
|
|
|
|
for _, productTarget := range target.Products {
|
|
product, ok := products[productTarget.ID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
if productTarget.Factor != 0 {
|
|
products[product.ID] = core.Product{
|
|
ID: product.ID,
|
|
Price: product.Price * productTarget.Factor,
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
products[product.ID] = core.Product{
|
|
ID: product.ID,
|
|
Price: product.Price * target.Factor,
|
|
}
|
|
}
|
|
|
|
return products
|
|
}
|
|
|
|
func calculateGroupProducts(productsGroup map[string][]core.Product, target models.DiscountCalculationTarget) map[string]float64 {
|
|
pricesGroup := make(map[string]float64)
|
|
|
|
if target.TargetGroup == "" && target.Factor == 0 {
|
|
for group, products := range productsGroup {
|
|
pricesGroup[group] = calculateProducts(products)
|
|
}
|
|
|
|
return pricesGroup
|
|
}
|
|
|
|
for group, products := range productsGroup {
|
|
pricesGroup[group] = calculateProducts(products) * target.Factor
|
|
}
|
|
|
|
return pricesGroup
|
|
}
|