Browse Source

Optimization

Vova Tkach 5 years ago
parent
commit
ac16e967fa
3 changed files with 31 additions and 44 deletions
  1. 3 25
      engine/basket/session.go
  2. 5 19
      engine/fetdata/shop_product.go
  3. 23 0
      utils/utils.go

+ 3 - 25
engine/basket/session.go

@@ -3,7 +3,6 @@ package basket
 import (
 	"encoding/json"
 	"html"
-	"math"
 	"strings"
 
 	"golang-fave/engine/sqlw"
@@ -20,27 +19,6 @@ type session struct {
 	TotalCount int              `json:"total_count"`
 }
 
-func (this *session) priceFormat(product_price float64, format, round int) string {
-	price := product_price
-	if round == 1 {
-		price = math.Ceil(price)
-	} else if round == 2 {
-		price = math.Floor(price)
-	}
-
-	if format == 1 {
-		return utils.Float64ToStrF(price, "%.1f")
-	} else if format == 2 {
-		return utils.Float64ToStrF(price, "%.2f")
-	} else if format == 3 {
-		return utils.Float64ToStrF(price, "%.3f")
-	} else if format == 4 {
-		return utils.Float64ToStrF(price, "%.4f")
-	}
-
-	return utils.Float64ToStrF(price, "%.0f")
-}
-
 func (this *session) makePrice(product_price float64, product_currency_id int) float64 {
 	if this.Currency == nil {
 		return product_price
@@ -169,12 +147,12 @@ func (this *session) updateTotals(p *SBParam) {
 	this.totalSum = 0
 	this.TotalCount = 0
 	for _, product := range this.Products {
-		product.Price = this.priceFormat(this.makePrice(product.price, product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
-		product.Sum = this.priceFormat(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
+		product.Price = utils.FormatProductPrice(this.makePrice(product.price, product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
+		product.Sum = utils.FormatProductPrice(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 		this.totalSum += this.makePrice(product.price, product.currency.Id) * float64(product.Quantity)
 		this.TotalCount += product.Quantity
 	}
-	this.TotalSum = this.priceFormat(this.totalSum, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
+	this.TotalSum = utils.FormatProductPrice(this.totalSum, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
 }
 
 // Info, Plus, Minus

+ 5 - 19
engine/fetdata/shop_product.go

@@ -2,7 +2,6 @@ package fetdata
 
 import (
 	"html/template"
-	"math"
 	"strings"
 	"time"
 
@@ -239,24 +238,11 @@ func (this *ShopProduct) Price() float64 {
 }
 
 func (this *ShopProduct) PriceNice() string {
-	price := this.Price()
-	if (*this.wrap.Config).Shop.Price.Round == 1 {
-		price = math.Ceil(price)
-	} else if (*this.wrap.Config).Shop.Price.Round == 2 {
-		price = math.Floor(price)
-	}
-
-	if (*this.wrap.Config).Shop.Price.Format == 1 {
-		return utils.Float64ToStrF(price, "%.1f")
-	} else if (*this.wrap.Config).Shop.Price.Format == 2 {
-		return utils.Float64ToStrF(price, "%.2f")
-	} else if (*this.wrap.Config).Shop.Price.Format == 3 {
-		return utils.Float64ToStrF(price, "%.3f")
-	} else if (*this.wrap.Config).Shop.Price.Format == 4 {
-		return utils.Float64ToStrF(price, "%.4f")
-	}
-
-	return utils.Float64ToStrF(price, "%.0f")
+	return utils.FormatProductPrice(
+		this.Price(),
+		(*this.wrap.Config).Shop.Price.Format,
+		(*this.wrap.Config).Shop.Price.Round,
+	)
 }
 
 func (this *ShopProduct) PriceFormat(format string) string {

+ 23 - 0
utils/utils.go

@@ -6,6 +6,7 @@ import (
 	"encoding/hex"
 	"fmt"
 	"html/template"
+	"math"
 	"net/http"
 	"os"
 	"regexp"
@@ -459,3 +460,25 @@ func SqlNullStringToString(arr *[]sql.NullString) *[]string {
 func GetImagePlaceholderSrc() string {
 	return "/" + consts.AssetsSysPlaceholderPng
 }
+
+func FormatProductPrice(price float64, format, round int) string {
+	p := price
+
+	if round == 1 {
+		p = math.Ceil(p)
+	} else if round == 2 {
+		p = math.Floor(p)
+	}
+
+	if format == 1 {
+		return Float64ToStrF(p, "%.1f")
+	} else if format == 2 {
+		return Float64ToStrF(p, "%.2f")
+	} else if format == 3 {
+		return Float64ToStrF(p, "%.3f")
+	} else if format == 4 {
+		return Float64ToStrF(p, "%.4f")
+	}
+
+	return Float64ToStrF(p, "%.0f")
+}