session.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package basket
  2. import (
  3. "encoding/json"
  4. "html"
  5. "strings"
  6. "golang-fave/engine/sqlw"
  7. "golang-fave/utils"
  8. )
  9. type session struct {
  10. listCurrencies map[int]*currency
  11. totalSum float64
  12. Products map[int]*product `json:"products"`
  13. Currency *currency `json:"currency"`
  14. TotalSum string `json:"total_sum"`
  15. TotalCount int `json:"total_count"`
  16. }
  17. func (this *session) makePrice(product_price float64, product_currency_id int) float64 {
  18. if this.Currency == nil {
  19. return product_price
  20. }
  21. if this.Currency.Id == product_currency_id {
  22. return product_price
  23. }
  24. if product_currency_id == 1 {
  25. return product_price * this.Currency.Coefficient
  26. } else {
  27. if c, ok := this.listCurrencies[product_currency_id]; ok == true {
  28. return product_price / c.Coefficient
  29. } else {
  30. return product_price
  31. }
  32. }
  33. }
  34. func (this *session) updateProducts(db *sqlw.DB) {
  35. products_ids := []int{}
  36. for _, product := range this.Products {
  37. products_ids = append(products_ids, product.Id)
  38. }
  39. if len(products_ids) > 0 {
  40. if rows, err := db.Query(
  41. `SELECT
  42. shop_products.id,
  43. shop_products.name,
  44. shop_products.price,
  45. shop_products.alias,
  46. shop_currencies.id,
  47. shop_currencies.name,
  48. shop_currencies.coefficient,
  49. shop_currencies.code,
  50. shop_currencies.symbol,
  51. IF(image_this.filename IS NULL, IFNULL(shop_products.parent_id, shop_products.id), shop_products.id) as imgid,
  52. IFNULL(IFNULL(image_this.filename, image_parent.filename), '') as filename
  53. FROM
  54. shop_products
  55. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  56. LEFT JOIN (
  57. SELECT
  58. m.product_id,
  59. m.filename
  60. FROM
  61. shop_product_images as m
  62. LEFT JOIN (
  63. SELECT
  64. t.product_id,
  65. MIN(t.ord) as ordmin
  66. FROM
  67. shop_product_images as t
  68. GROUP BY
  69. t.product_id
  70. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  71. WHERE
  72. u.product_id IS NOT NULL
  73. ) as image_this ON image_this.product_id = shop_products.id
  74. LEFT JOIN (
  75. SELECT
  76. m.product_id,
  77. m.filename
  78. FROM
  79. shop_product_images as m
  80. LEFT JOIN (
  81. SELECT
  82. t.product_id,
  83. MIN(t.ord) as ordmin
  84. FROM
  85. shop_product_images as t
  86. GROUP BY
  87. t.product_id
  88. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  89. WHERE
  90. u.product_id IS NOT NULL
  91. ) as image_parent ON image_parent.product_id = shop_products.parent_id
  92. WHERE
  93. shop_products.active = 1 AND
  94. shop_products.id IN (` + strings.Join(utils.ArrayOfIntToArrayOfString(products_ids), ",") + `)
  95. ;`,
  96. ); err == nil {
  97. defer rows.Close()
  98. for rows.Next() {
  99. row := &utils.MySql_shop_product{}
  100. roc := &utils.MySql_shop_currency{}
  101. var img_product_id string
  102. var img_filename string
  103. if err = rows.Scan(
  104. &row.A_id,
  105. &row.A_name,
  106. &row.A_price,
  107. &row.A_alias,
  108. &roc.A_id,
  109. &roc.A_name,
  110. &roc.A_coefficient,
  111. &roc.A_code,
  112. &roc.A_symbol,
  113. &img_product_id,
  114. &img_filename,
  115. ); err == nil {
  116. if p, ok := this.Products[row.A_id]; ok == true {
  117. // Load product image here
  118. var product_image string
  119. if img_filename == "" {
  120. product_image = utils.GetImagePlaceholderSrc()
  121. } else {
  122. product_image = "/products/images/" + img_product_id + "/thumb-0-" + img_filename
  123. }
  124. p.Name = html.EscapeString(row.A_name)
  125. p.Image = product_image
  126. p.Link = "/shop/" + row.A_alias + "/"
  127. p.price = row.A_price
  128. p.currency.Id = roc.A_id
  129. p.currency.Name = html.EscapeString(roc.A_name)
  130. p.currency.Coefficient = roc.A_coefficient
  131. p.currency.Code = html.EscapeString(roc.A_code)
  132. p.currency.Symbol = html.EscapeString(roc.A_symbol)
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. func (this *session) updateTotals(p *SBParam) {
  140. this.totalSum = 0
  141. this.TotalCount = 0
  142. for _, product := range this.Products {
  143. product.Price = utils.FormatProductPrice(this.makePrice(product.price, product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
  144. product.Sum = utils.FormatProductPrice(this.makePrice(product.price*float64(product.Quantity), product.currency.Id), (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
  145. this.totalSum += this.makePrice(product.price, product.currency.Id) * float64(product.Quantity)
  146. this.TotalCount += product.Quantity
  147. }
  148. this.TotalSum = utils.FormatProductPrice(this.totalSum, (*p.Config).Shop.Price.Format, (*p.Config).Shop.Price.Round)
  149. }
  150. // Info, Plus, Minus
  151. func (this *session) Preload(p *SBParam) {
  152. user_currency := 1
  153. if cookie, err := p.R.Cookie("currency"); err == nil {
  154. user_currency = utils.StrToInt(cookie.Value)
  155. }
  156. // Clear list of currencies
  157. this.listCurrencies = map[int]*currency{}
  158. // Load currencies from database
  159. if rows, err := p.DB.Query(
  160. `SELECT
  161. id,
  162. name,
  163. coefficient,
  164. code,
  165. symbol
  166. FROM
  167. shop_currencies
  168. ORDER BY
  169. id ASC
  170. ;`,
  171. ); err == nil {
  172. defer rows.Close()
  173. for rows.Next() {
  174. roc := &utils.MySql_shop_currency{}
  175. if err = rows.Scan(
  176. &roc.A_id,
  177. &roc.A_name,
  178. &roc.A_coefficient,
  179. &roc.A_code,
  180. &roc.A_symbol,
  181. ); err == nil {
  182. this.listCurrencies[roc.A_id] = &currency{
  183. Id: roc.A_id,
  184. Name: html.EscapeString(roc.A_name),
  185. Coefficient: roc.A_coefficient,
  186. Code: html.EscapeString(roc.A_code),
  187. Symbol: html.EscapeString(roc.A_symbol),
  188. }
  189. }
  190. }
  191. }
  192. // Check if selected currency is exists
  193. if _, ok := this.listCurrencies[user_currency]; ok != true {
  194. user_currency = 1
  195. }
  196. // Save selected currency
  197. if c, ok := this.listCurrencies[user_currency]; ok == true {
  198. this.Currency = &currency{
  199. Id: c.Id,
  200. Name: c.Name,
  201. Coefficient: c.Coefficient,
  202. Code: c.Code,
  203. Symbol: c.Symbol,
  204. }
  205. }
  206. }
  207. func (this *session) String(p *SBParam) string {
  208. this.updateProducts(p.DB)
  209. this.updateTotals(p)
  210. json, err := json.Marshal(this)
  211. if err != nil {
  212. return `{"msg":"basket_engine_error","message":"` + err.Error() + `"}`
  213. }
  214. return string(json)
  215. }
  216. func (this *session) Plus(p *SBParam, product_id int) {
  217. if prod, ok := this.Products[product_id]; ok == true {
  218. prod.Quantity++
  219. this.updateProducts(p.DB)
  220. this.updateTotals(p)
  221. return
  222. }
  223. row := &utils.MySql_shop_product{}
  224. roc := &utils.MySql_shop_currency{}
  225. var img_product_id string
  226. var img_filename string
  227. if err := p.DB.QueryRow(`
  228. SELECT
  229. shop_products.id,
  230. shop_products.name,
  231. shop_products.price,
  232. shop_products.alias,
  233. shop_currencies.id,
  234. shop_currencies.name,
  235. shop_currencies.coefficient,
  236. shop_currencies.code,
  237. shop_currencies.symbol,
  238. IF(image_this.filename IS NULL, IFNULL(shop_products.parent_id, shop_products.id), shop_products.id) as imgid,
  239. IFNULL(IFNULL(image_this.filename, image_parent.filename), '') as filename
  240. FROM
  241. shop_products
  242. LEFT JOIN shop_currencies ON shop_currencies.id = shop_products.currency
  243. LEFT JOIN (
  244. SELECT
  245. m.product_id,
  246. m.filename
  247. FROM
  248. shop_product_images as m
  249. LEFT JOIN (
  250. SELECT
  251. t.product_id,
  252. MIN(t.ord) as ordmin
  253. FROM
  254. shop_product_images as t
  255. GROUP BY
  256. t.product_id
  257. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  258. WHERE
  259. u.product_id IS NOT NULL
  260. ) as image_this ON image_this.product_id = shop_products.id
  261. LEFT JOIN (
  262. SELECT
  263. m.product_id,
  264. m.filename
  265. FROM
  266. shop_product_images as m
  267. LEFT JOIN (
  268. SELECT
  269. t.product_id,
  270. MIN(t.ord) as ordmin
  271. FROM
  272. shop_product_images as t
  273. GROUP BY
  274. t.product_id
  275. ) as u ON u.product_id = m.product_id AND u.ordmin = m.ord
  276. WHERE
  277. u.product_id IS NOT NULL
  278. ) as image_parent ON image_parent.product_id = shop_products.parent_id
  279. WHERE
  280. shop_products.active = 1 AND
  281. shop_products.id = ?
  282. LIMIT 1;`,
  283. product_id,
  284. ).Scan(
  285. &row.A_id,
  286. &row.A_name,
  287. &row.A_price,
  288. &row.A_alias,
  289. &roc.A_id,
  290. &roc.A_name,
  291. &roc.A_coefficient,
  292. &roc.A_code,
  293. &roc.A_symbol,
  294. &img_product_id,
  295. &img_filename,
  296. ); err == nil {
  297. // Load product image here
  298. var product_image string
  299. if img_filename == "" {
  300. product_image = utils.GetImagePlaceholderSrc()
  301. } else {
  302. product_image = "/products/images/" + img_product_id + "/thumb-0-" + img_filename
  303. }
  304. this.Products[product_id] = &product{
  305. currency: &currency{Id: roc.A_id, Name: roc.A_name, Coefficient: roc.A_coefficient, Code: roc.A_code, Symbol: roc.A_symbol},
  306. Id: row.A_id,
  307. Name: html.EscapeString(row.A_name),
  308. Image: product_image,
  309. Link: "/shop/" + row.A_alias + "/",
  310. price: row.A_price,
  311. Quantity: 1,
  312. }
  313. this.updateProducts(p.DB)
  314. this.updateTotals(p)
  315. }
  316. }
  317. func (this *session) Minus(p *SBParam, product_id int) {
  318. if prod, ok := this.Products[product_id]; ok == true {
  319. if prod.Quantity > 1 {
  320. prod.Quantity--
  321. } else {
  322. delete(this.Products, product_id)
  323. }
  324. this.updateProducts(p.DB)
  325. this.updateTotals(p)
  326. }
  327. }
  328. func (this *session) Remove(p *SBParam, product_id int) {
  329. if _, ok := this.Products[product_id]; ok == true {
  330. delete(this.Products, product_id)
  331. this.updateProducts(p.DB)
  332. this.updateTotals(p)
  333. }
  334. }
  335. func (this *session) ProductsCount() int {
  336. return this.TotalCount
  337. }