session.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package basket
  2. import (
  3. "encoding/json"
  4. "html"
  5. "net/http"
  6. "strings"
  7. "golang-fave/engine/sqlw"
  8. "golang-fave/utils"
  9. )
  10. type session struct {
  11. listCurrencies map[int]*currency
  12. totalSum float64
  13. Products map[int]*product `json:"products"`
  14. Currency *currency `json:"currency"`
  15. TotalSum string `json:"total_sum"`
  16. TotalCount int `json:"total_count"`
  17. }
  18. func (this *session) makePrice(product_price float64, product_currency_id int) float64 {
  19. if this.Currency == nil {
  20. return product_price
  21. }
  22. if this.Currency.Id == product_currency_id {
  23. return product_price
  24. }
  25. if product_currency_id == 1 {
  26. return product_price * this.Currency.Coefficient
  27. } else {
  28. if c, ok := this.listCurrencies[product_currency_id]; ok == true {
  29. return product_price / c.Coefficient
  30. } else {
  31. return product_price
  32. }
  33. }
  34. }
  35. func (this *session) updateProducts(db *sqlw.DB) {
  36. products_ids := []int{}
  37. for _, product := range this.Products {
  38. products_ids = append(products_ids, product.Id)
  39. }
  40. if len(products_ids) > 0 {
  41. if rows, err := db.Query(
  42. `SELECT
  43. shop_products.id,
  44. shop_products.name,
  45. shop_products.price,
  46. shop_products.alias,
  47. shop_currencies.id,
  48. shop_currencies.name,
  49. shop_currencies.coefficient,
  50. shop_currencies.code,
  51. shop_currencies.symbol
  52. FROM
  53. shop_products
  54. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  55. WHERE
  56. shop_products.active = 1 AND
  57. shop_products.id IN (` + strings.Join(utils.ArrayOfIntToArrayOfString(products_ids), ",") + `)
  58. LIMIT 1;`,
  59. ); err == nil {
  60. defer rows.Close()
  61. for rows.Next() {
  62. row := &utils.MySql_shop_product{}
  63. roc := &utils.MySql_shop_currency{}
  64. if err = rows.Scan(
  65. &row.A_id,
  66. &row.A_name,
  67. &row.A_price,
  68. &row.A_alias,
  69. &roc.A_id,
  70. &roc.A_name,
  71. &roc.A_coefficient,
  72. &roc.A_code,
  73. &roc.A_symbol,
  74. ); err == nil {
  75. if p, ok := this.Products[row.A_id]; ok == true {
  76. p.Name = html.EscapeString(row.A_name)
  77. p.Image = "/products/images/1/thumb-0-1570673803.jpg"
  78. p.Link = "/shop/" + row.A_alias + "/"
  79. p.price = row.A_price
  80. p.currency.Id = roc.A_id
  81. p.currency.Name = html.EscapeString(roc.A_name)
  82. p.currency.Coefficient = roc.A_coefficient
  83. p.currency.Code = html.EscapeString(roc.A_code)
  84. p.currency.Symbol = html.EscapeString(roc.A_symbol)
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. func (this *session) updateTotals() {
  92. this.totalSum = 0
  93. this.TotalCount = 0
  94. for _, product := range this.Products {
  95. product.Price = utils.Float64ToStrF(this.makePrice(product.price, product.currency.Id), "%.2f")
  96. product.Sum = utils.Float64ToStrF(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), "%.2f")
  97. this.totalSum += this.makePrice(product.price, product.currency.Id) * float64(product.Quantity)
  98. this.TotalCount += product.Quantity
  99. }
  100. this.TotalSum = utils.Float64ToStrF(this.totalSum, "%.2f")
  101. }
  102. // Info, Plus, Minus
  103. func (this *session) Preload(r *http.Request, db *sqlw.DB) {
  104. user_currency := 1
  105. if cookie, err := r.Cookie("currency"); err == nil {
  106. user_currency = utils.StrToInt(cookie.Value)
  107. }
  108. // Clear list of currencies
  109. this.listCurrencies = map[int]*currency{}
  110. // Load currencies from database
  111. if rows, err := db.Query(
  112. `SELECT
  113. id,
  114. name,
  115. coefficient,
  116. code,
  117. symbol
  118. FROM
  119. shop_currencies
  120. ORDER BY
  121. id ASC
  122. ;`,
  123. ); err == nil {
  124. defer rows.Close()
  125. for rows.Next() {
  126. roc := &utils.MySql_shop_currency{}
  127. if err = rows.Scan(
  128. &roc.A_id,
  129. &roc.A_name,
  130. &roc.A_coefficient,
  131. &roc.A_code,
  132. &roc.A_symbol,
  133. ); err == nil {
  134. this.listCurrencies[roc.A_id] = &currency{
  135. Id: roc.A_id,
  136. Name: html.EscapeString(roc.A_name),
  137. Coefficient: roc.A_coefficient,
  138. Code: html.EscapeString(roc.A_code),
  139. Symbol: html.EscapeString(roc.A_symbol),
  140. }
  141. }
  142. }
  143. }
  144. // Check if selected currency is exists
  145. if _, ok := this.listCurrencies[user_currency]; ok != true {
  146. user_currency = 1
  147. }
  148. // Save selected currency
  149. if c, ok := this.listCurrencies[user_currency]; ok == true {
  150. this.Currency = &currency{
  151. Id: c.Id,
  152. Name: c.Name,
  153. Coefficient: c.Coefficient,
  154. Code: c.Code,
  155. Symbol: c.Symbol,
  156. }
  157. }
  158. }
  159. func (this *session) String(db *sqlw.DB) string {
  160. this.updateProducts(db)
  161. this.updateTotals()
  162. json, err := json.Marshal(this)
  163. if err != nil {
  164. return `{"msg":"basket_engine_error","message":"` + err.Error() + `"}`
  165. }
  166. return string(json)
  167. }
  168. func (this *session) Plus(db *sqlw.DB, product_id int) {
  169. if p, ok := this.Products[product_id]; ok == true {
  170. p.Quantity++
  171. this.updateProducts(db)
  172. this.updateTotals()
  173. return
  174. }
  175. row := &utils.MySql_shop_product{}
  176. roc := &utils.MySql_shop_currency{}
  177. if err := db.QueryRow(`
  178. SELECT
  179. shop_products.id,
  180. shop_products.name,
  181. shop_products.price,
  182. shop_products.alias,
  183. shop_currencies.id,
  184. shop_currencies.name,
  185. shop_currencies.coefficient,
  186. shop_currencies.code,
  187. shop_currencies.symbol
  188. FROM
  189. shop_products
  190. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  191. WHERE
  192. shop_products.active = 1 AND
  193. shop_products.id = ?
  194. LIMIT 1;`,
  195. product_id,
  196. ).Scan(
  197. &row.A_id,
  198. &row.A_name,
  199. &row.A_price,
  200. &row.A_alias,
  201. &roc.A_id,
  202. &roc.A_name,
  203. &roc.A_coefficient,
  204. &roc.A_code,
  205. &roc.A_symbol,
  206. ); err == nil {
  207. // Load product image here
  208. this.Products[product_id] = &product{
  209. currency: &currency{Id: roc.A_id, Name: roc.A_name, Coefficient: roc.A_coefficient, Code: roc.A_code, Symbol: roc.A_symbol},
  210. Id: row.A_id,
  211. Name: html.EscapeString(row.A_name),
  212. Image: "/products/images/1/thumb-0-1570673803.jpg",
  213. Link: "/shop/" + row.A_alias + "/",
  214. price: row.A_price,
  215. Quantity: 1,
  216. }
  217. this.updateProducts(db)
  218. this.updateTotals()
  219. }
  220. }
  221. func (this *session) Minus(db *sqlw.DB, product_id int) {
  222. if p, ok := this.Products[product_id]; ok == true {
  223. if p.Quantity > 1 {
  224. p.Quantity--
  225. } else {
  226. delete(this.Products, product_id)
  227. }
  228. this.updateProducts(db)
  229. this.updateTotals()
  230. }
  231. }
  232. func (this *session) Remove(db *sqlw.DB, product_id int) {
  233. if _, ok := this.Products[product_id]; ok == true {
  234. delete(this.Products, product_id)
  235. this.updateProducts(db)
  236. this.updateTotals()
  237. }
  238. }
  239. func (this *session) ProductsCount() int {
  240. return this.TotalCount
  241. }